// $Id: annotate.js,v 1.8 2005/09/09 09:47:33 bas Exp $



// which user (id) is the lucky one
//
auth_person_id = '';
auth_user_id   = '-1';

//


//<!--@labelfile common/labels.txt -->



/*
 *
 * total annotations per image (initially)
 *
 */


var build_anntt_image_posts_total = function(args) 
{
	if (!okForXMLHTTPREQUEST()) return;
	var elem = args[0];
	elem.any = new Annotate_Posts_Total_Container(args[0], args[1]);
	return elem;
}

Annotate_Posts_Total_Container = function(el, img_id) 
{
	var elem	= this.elem = el; var any = this;
	any.img_id	= img_id;

	this.annotate_imagesgetEltInfo_total_onLoad = function(success, responseXML, responseText, params)
	{
		if (success)
		{
			if (responseXML.getElementsByTagName('total')[0]) 
			{
				var total = responseXML.getElementsByTagName('total')[0].getAttribute('count');

				if (total > 0)
				{
					var html = '';
					var html = total;
		
					elem.innerHTML	   		= html;
					elem.style.display 		= 'block';
					elem.style.paddingTop 	= '2px';
				}
			}
		}
		else
		{
			alert('getInfo - foutje:'+responseText);
		}
	}
	var params = {
		image_id:any.img_id,
		total:true
	}
	anymeta_rest ('anymeta.annotates.getInfo', params, any.annotate_imagesgetEltInfo_total_onLoad, any)
}







/*
 *
 * 
 *
 */


var build_image_butt = function(args) 
{
	var elem = args[0];
	elem.any = new Annotate_butt(args[0], args[1], args[2], args[3]);
	return elem;
}

Annotate_butt = function(el, img_id, isOff, base_name_off) 
{
	var elem = this.elem = el; var any = this;
	var base_name = any.base_name = el.id.split(el.className+'_').join(''); 

	any.lock_down = 0;
	any.img_id = img_id;

	if (base_name=='add_post' && !okForXMLHTTPREQUEST()) 
	{
		elem.style.display = 'none';
		return;
	}

	any.isOver = any.isDown = 0;
	any.isOff = (isOff) ? 1 : 0;

	elem.style.cursor = (document.all) ? 'hand' : 'pointer';

	elem.onmouseover = function()
	{
		if (window.ignore_editpost_action || window.ignore_editdiv_action) return;
		this.any.isOver = 1;
	}
	
	elem.onmouseout = function() 
	{
		this.any.isOver = 0;
		if (!this.any.lock_down) this.any.isDown = 0;
	}
	
	elem.onmousedown = function() 
	{
		if (window.ignore_editpost_action || window.ignore_editdiv_action) return;
		if (this.any.disabled) 
		{
			this.any.enable_others();
			this.any.enable_self();
		}
		this.any.isDown = 1;
		this.any.isOver = 1;
		var anntt_image_posts = getEl('anntt_image_posts');
	}
	elem.onmouseup = function() 
	{
		if (window.ignore_editpost_action || window.ignore_editdiv_action) return;
	
		if (this.any.disabled) return;
		var what_to_do = this.any[this.any.base_name+'_action']();
	
		this.any.isOver = 1;
		if (what_to_do==1) 
		{
			this.any.isDown = 1;
			this.any.lock_down = 1;
		}
		else
		{
			this.any.isDown = 0;
		}
	}
	this.get_src_file = function() 
	{
		return elem.src.split('/').pop();
	}
	this.disable_others = function() 
	{
		var all = document.getElementsByClass('image_butt', 'IMG');
		for (var i=0; i<all.length; i++)
		{
			if (all[i].any.base_name != any.base_name) all[i].any.disable_self();
		}
		var anntt_image_posts = getEl('anntt_image_posts');
		if (anntt_image_posts && anntt_image_posts.any) anntt_image_posts.any.take_her_away('disable_others');
		window.ignore_butt_action = 1;
	}
	this.enable_others = function() 
	{
		var all = document.getElementsByClass('image_butt', 'IMG');
		for (var i=0; i<all.length; i++)
		{
			if (all[i].any.base_name != any.base_name) all[i].any.enable_self();
		}
		window.ignore_butt_action = 0;
	}
	this.disable_self = function() 
	{
		any.disabled = 1;
		elem.style.cursor = 'default';
	}
	this.enable_self = function() 
	{
		any.disabled = 0;
		elem.style.cursor = (document.all) ? 'hand' : 'pointer';
	}
	this.add_post_action = function()
	{
		var anntt_image_posts = getEl('anntt_image_posts');
		if (anntt_image_posts && anntt_image_posts.any) anntt_image_posts.any.start_adding_post();
		return 0;
	}
}





/*
 *
 * 
 *
 */


var build_anntt_image_posts = function(args) 
{
	if (!okForXMLHTTPREQUEST()) return;
	var elem = args[0];
	elem.any = new Annotate_Posts_Container(args[0], args[1], args[2], args[3]);
	return elem;
}

Annotate_Posts_Container = function(el, image_div, img_id, object_url) 
{
	var elem = this.elem = el; var any = this;
//	any.hex_colors = {yellow:'#ffffd3', green:'#d0fdaf'};
//	any.hex_colors = {yellow:'#fe8'};
	any.isOver = any.editing = any.comming = any.dragging = 0;
	any.hide_while_dragging = 0;
	any.which_post = null;
	any.image_div = image_div;
	any.image_div.onmouseover = function() { elem.onmouseover(); }
	any.image_div.onmouseout = function(e) 
	{
		var s = getEventSrc(e);
		if (window.ignore_butt_action || window.ignore_editdiv_action || any.comming) return;
		any.hide_tim = setTimeout("getEl('anntt_image_posts').any.take_her_away('any.image_div.onmouseout');", 200);
	}
	any.img_id = img_id;

	any.object_url  = object_url;

	any.posts_text_div = getEl('posts_text_div');
	var html = '';
	var html = '<table cellpadding="0" cellspacing="0" border="0" style="padding:0px;" id="anntt_posts_text_table">';
	html+= '<tr>';
	html+= '<td class="anntt_td_post_style" style="padding:3px">';
	html+= '<span id="posts_text_span"><\/span>';
	html+= '<form id="posts_text_form">';
	html+= '<textarea onkeydown="global_textarea_limiter(this, 300)" onkeyup="place_posts_text_div(); adjust_textarea_height(this); global_textarea_limiter(this, 300)" id="posts_text_area" rows='+1+' ><\/textarea>';
	html+= '<\/form>';
	html+= '<\/td><\/tr>';
	html+= '<\/table>'; 
	html+= '<form id="posts_text_butts_form">';
	//
	html+= '<input type="button" class="anntt_save_but" value="Bewaar" onclick="getEl(\''+elem.id+'\').any.save_editing(); this.blur();" />';
	html+= '<input type="button" class="anntt_cancel_but" value="Annuleer" onclick="getEl(\''+elem.id+'\').any.cancel_editing(); this.blur();" />';
	html+= '<input type="button" class="anntt_remove_but" value="Verwijder" id="delete_post_butt" onclick="getEl(\''+elem.id+'\').any.delete_post(); this.blur();" />';
	//
	html+= '<\/form><div id="temp_div" style="visibility:hidden;"></div>';
	html+= '<\/form><div id="temp_div" style="visibility:hidden;"></div>';
	any.posts_text_div.innerHTML	  = html;
	any.anntt_posts_text_table		  = getEl('anntt_posts_text_table');
	any.posts_text_span				  = getEl('posts_text_span');
	any.posts_text_area				  = getEl('posts_text_area');
	if (!document.all || navigator.userAgent.indexOf('Opera') != -1) any.posts_text_area.style.height = '58px';
	any.posts_text_form				  = getEl('posts_text_form');
	any.posts_text_butts_form		  = getEl('posts_text_butts_form');
	any.delete_post_butt			  = getEl('delete_post_butt');
	any.posts_text_div.style.zIndex   = '1000';
	any.posts_text_div.style.display  = 'none';
	any.posts_text_div.style.width	  = '240px';
	any.posts_text_div.style.position = 'relative';

	if (navigator.userAgent.indexOf("Mozilla/5.0") != -1 && navigator.userAgent.indexOf("KHTML")==-1) 
	{ 
		any.posts_text_area.style.overflow = '-moz-scrollbars-none';
	}

	window.temp_div = getEl('temp_div');
	temp_div.style.width = any.posts_text_area.style.width;

	any.anntt_posts_text_table.onmouseover = function()
	{
		if (any.editing) return;
		if (any.which_post) clearTimeout(any.which_post.any.hide_timmy);
	}
	any.anntt_posts_text_table.onmouseout = function()
	{
		if (any.editing) return;
		if (any.which_post) any.which_post.any.box_div.onmouseout();
	}
	any.anntt_posts_text_table.onmouseup = function(e)
	{
		if (any.editing) return;
		var s = getEventSrc(e);
		if (s && el_is_in_a_link(s)) return;
		if (any.which_post && any.which_post.any.can_edit) {
			any.which_post.any.editing = 1;
			any.which_post.any.change_display();
			any.start_editing_one(any.which_post);
		}
	}

	any.anntt_cont_div = getEl('anntt_cont_div');
	var html = '';
	html+= '<table id="anntt_cont_table" cellpadding="0" cellspacing="0" border="1" style="padding:0px;">';
	html+= '<tr>';
	html+= '<td class="anntt_td_post_working" style="padding:3px 0 3px 10px;" id="anntt_cont_td">';
	html+= '<\/td><td class="anntt_td_post_working" style="padding: 6px 0 0 0;"  valign="top"><img id="cont_pulser_img" src="ima/annotate/pulser.gif" width="32" height="15" border="0"><\/td><td class="anntt_td_post_working"><\/td><\/tr>';
	html+= '<\/table>';
	any.anntt_cont_div.innerHTML	 = html;
	any.cont_pulser_img				 = getEl('cont_pulser_img');
	any.anntt_cont_table			 = getEl('anntt_cont_table');
	any.anntt_cont_td				 = getEl('anntt_cont_td');
	any.anntt_cont_div.style.zIndex  = '1002';
	any.anntt_cont_div.style.display = 'none';

	for (var i=0;i<elem.childNodes.length;i++) if (elem.childNodes[i].className=='anntt_image_post') elem.childNodes[i].any.papa = elem;

	elem.onmouseover = function() 
	{
		clearTimeout(this.any.hide_tim);
		if (!this.any.shown_initially) 
		{
			this.any.tried_to_show_before_shown_initially = 1;
			return;
		}
		if (window.ignore_butt_action || window.ignore_editdiv_action || this.any.comming) return;
		this.any.isOver = 1;
		this.any.change_display();
	}

	this.go_away_in_a_bit = function() 
	{
		any.hide_tim = setTimeout("getEl('anntt_image_posts').any.take_her_away('go_away_in_a_bit');", 3000);
	}
	this.take_her_away = function(from_where) 
	{
		clearTimeout(any.hide_tim);
		if (any.editing || any.comming || any.dragging) return;
		any.isOver = 0;
		any.stop_showing_one('take_her_away '+ from_where);
		any.change_display();
	}
/*
	this.make_post_this_color = function(color) 
	{	
		var last_color = (any.last_post_color) ? any.last_post_color : 'yellow';
		if (last_color==color) return;
	
		any.posts_text_area.style.backgroundColor = any.hex_colors[color];
	
		var tds = elem.getElementsByTagName('TD');
		for (var i=0; i<tds.length; i++) if (tds[i].className=='anntt_td_post_style') tds[i].style.backgroundColor = any.hex_colors[color];
	
		var imgs = elem.getElementsByTagName('IMG');
		for (var i=0; i<imgs.length; i++) if (imgs[i].src) imgs[i].src = imgs[i].src.replace(last_color, color);
	
		any.last_post_color = color;
	}
*/
	this.start_showing_one = function(which_post) 
	{
		clearTimeout(any.hide_tim);
		if (any.which_post) clearTimeout(any.which_post.any.hide_timmy);
		if (any.which_post != which_post) any.stop_showing_one();
//		any.make_post_this_color(which_post.any.use_color);
	
		var txt = which_post.any.saved_txt;
		if (which_post.any.author_uri == '#' && which_post.any.author_name != null) txt += '  - <span class="anntt_author">'+which_post.any.author_name+'<\/span>';
		else if (which_post.any.author_name != null) txt += '  - <a href="'+which_post.any.author_uri+'" class="anntt_author">'+which_post.any.author_name+'<\/a>';
		any.posts_text_span.innerHTML = txt;
	
		any.which_post = which_post;
	
		if (any.editing) 
		{
			any.posts_text_span.style.display = 'none';
			any.posts_text_form.style.display = 'block';
			any.posts_text_butts_form.style.display = 'block';
			if (any.which_post.id=='anntt_image_post_temp') 
			{
				any.delete_post_butt.style.display = 'none';
			}
			else 
			{
				any.delete_post_butt.style.display = 'inline';
			}
			if (which_post != any.which_post || any.posts_text_area.value=='') 
			{
				any.posts_text_area.value = which_post.any.saved_txt;
				adjust_textarea_height(any.posts_text_area)
			}
		}
		else
		{
			any.posts_text_span.style.display = 'inline';
			any.posts_text_form.style.display = 'none';
			any.posts_text_butts_form.style.display = 'none';
		}
		any.posts_text_div.style.display = 'block';
		any.place_posts_text_div();
	
		if (any.editing) 
		{
			any.posts_text_area.focus();
			any.posts_text_area.select();
		}
	}
	this.place_posts_text_div = function(post_left, post_top)
	{
		if (post_left && post_top)
		{
			var nx = post_left;
			var ny = post_top + any.which_post.any.get_post_height() + 5;
		}
		else
		{
			if (any.editing)
			{
				var allowed_under = 0;
				var allowed_over = 200;
			}
			else
			{
				var allowed_under = 100;
				var allowed_over = 200;
			}
	
			var nx = pInt(any.which_post.style.left);
			var ny = pInt(any.which_post.style.top) + any.which_post.any.get_post_height() + 5;
	
			if (ny + any.posts_text_div.offsetHeight > any.get_image_h() + allowed_under)
			{
				ny = pInt(any.which_post.style.top) - any.posts_text_div.offsetHeight - 10;
				if (document.all && navigator.userAgent.indexOf('Opera')==-1) ny+= 15;
			}
	
			if (ny < -allowed_over) {
				ny = pInt(any.which_post.style.top) + any.which_post.any.get_post_height() + 5;
			}
		}
	
		any.posts_text_div.style.left = nx+'px';
		any.posts_text_div.style.top = ny+'px';
	}
	this.stop_showing_one = function(from_w) 
	{
		any.posts_text_div.style.display = 'none';
		if (any.which_post) {
			any.which_post.any.isOver = 0;
			any.which_post.any.change_display();
		}
	}
	this.start_dragging_one = function()
	{
		any.dragging = 1;
		window.ignore_editpost_action = 1;
	}
	this.start_editing_one = function(which_post) 
	{
		any.editing = 1;
		any.dragging = 0;
		window.ignore_editpost_action = 1;
		any.start_showing_one(which_post);
	}
	this.hide_posts_text_div = function() 
	{
		any.posts_text_div.style.display = 'none';
	}
	this.refresh_posts = function()
	{
		if (okForXMLHTTPREQUESTandResponseXML())
		{
			var params = {
				image_id:any.img_id
			}
			anymeta_rest ('anymeta.annotates.getInfo', params, any.annotate_imagesgetEltInfo_onLoad, any)

			var params = {
				image_id:any.img_id,
				total:true
			}
			anymeta_rest ('anymeta.annotates.getInfo', params, any.annotate_imagesgetEltInfo_total_onLoad, any)
		}
//		else
//		{
//			document.location = current_url + '?changedposts=1&cb='+new Date().getTime();
//		}
	}
	
	this.annotate_imagesgetEltInfo_onLoad = function(success, responseXML, responseText, params)
	{
		if (success)
		{
			var temp = {};

			if (responseXML.getElementsByTagName('owner')[0]) 
			{
				var owner_id = responseXML.getElementsByTagName('owner')[0].getAttribute('object_owner_id');
				var posts = responseXML.getElementsByTagName('post');
				for (var t=0;t<posts.length;t++) {
					var post_text			= posts[t].getElementsByTagName('text')[0].firstChild.nodeValue;
					var post_left			= posts[t].getAttribute('x')*1;
					var post_top			= posts[t].getAttribute('y')*1;
					var post_width			= posts[t].getAttribute('w')*1;
					var post_height			= posts[t].getAttribute('h')*1;
					var post_id				= posts[t].getAttribute('id');
					var post_author			= posts[t].getAttribute('author');
					var post_author_name	= posts[t].getAttribute('authorname');
					var post_author_uri		= posts[t].getAttribute('authoruri');
					var post_author_usa_id	= posts[t].getAttribute('authorusaid');

					temp[post_id] = post_id;
		
					var which_post = getEl('anntt_image_post_'+post_id)

					if (which_post)
					{
						which_post.any.refresh_info(post_id, post_left, post_top, post_width, post_height, post_text);
					}
					else
					{
						var can_edit = (post_author_usa_id == auth_user_id || (post_author && post_author == auth_person_id)) ? true : false;
						var from_owner = (owner_id==post_author);

						any.create_post(post_id, post_left, post_top, post_width, post_height, post_text, can_edit, from_owner, post_author_name, post_author, post_author_uri, post_author_usa_id);
					}
				}
			}
			for (var i=elem.childNodes.length-1;i>=0;i--) 
			{
				if (elem.childNodes[i].className=='anntt_image_post')
				{
					if (!temp[elem.childNodes[i].any.post_id])
					{
						elem.removeChild(elem.childNodes[i]);
					}
				}
			}
		}
		else
		{
			alert('getInfo - foutje:'+responseText);
		}
		if (any.comming) any.stop_comming();
	}
	
	this.annotate_imagesgetEltInfo_total_onLoad = function(success, responseXML, responseText, params)
	{
		if (success)
		{
			var totalDiv = getEl('anntt_image_posts_total_'+any.img_id);

			if (responseXML.getElementsByTagName('total')[0]) 
			{
				var total = responseXML.getElementsByTagName('total')[0].getAttribute('count');

				if (total > 0)
				{
					var html = '';
					var html = total;
		
					totalDiv.innerHTML	      = html;
					totalDiv.style.display    = 'block';
					totalDiv.style.paddingTop = '2px';
				}
				else
				{
					totalDiv.style.display = 'none';
				}
			}
		}
		else
		{
			alert('getInfo - foutje:'+responseText);
		}
	}

	this.start_comming = function(msg)
	{
		any.comming = 1;
		window.ignore_editpost_action = 1;
		//
		var txt = (!msg) ? 'In bewerking' : msg;
		//
		any.anntt_cont_td.innerHTML = txt;
		any.show_anntt_cont_div();
	}
	
	this.place_y_anntt_cont_div = function(y)
	{
		if (y == undefined) {
			y = pInt((any.get_image_h()-any.anntt_cont_table.offsetHeight)/2);
			if (y < 10) y = 10;
		}
		any.anntt_cont_div.style.marginTop = y+'px';
	}
	
	this.show_anntt_cont_div = function()
	{
		any.anntt_cont_div.style.display = 'block';

		var x = pInt((any.get_image_w()-pInt(any.anntt_cont_table.offsetWidth))/2);
		if (x < 10) x = 10;
		any.anntt_cont_div.style.marginLeft = x+'px';

		var x = pInt(any.get_image_w()/2);
		if (x < 100) x = 100;
		any.anntt_cont_div.style.width = x+'px';
		
		any.cont_pulser_img.style.display = 'inline';
		elem.style.visibility = 'hidden';
		
		this.place_y_anntt_cont_div();
	}

	this.stop_comming = function(dont_show_posts)
	{
		any.comming = 0;
		window.ignore_editpost_action = 0;
		any.anntt_cont_div.style.display = 'none';

		if (!dont_show_posts)
		{
			elem.style.visibility = 'visible';
		}
	}
	
	this.start_adding_post = function()
	{
		if (any.editing)
		{
		//
			alert('Er kan slechts &eacute;&eacute;n tegelijk worden toegevoegd');
		//
			return;
		}
		any.empty_posts_text_area();
		elem.onmouseover();
		//
		var post_div = any.create_post('temp', 10, 10, 14, 14, 'Voeg hier de notitie toe', 1, true, '', '', '');
		//
		post_div.any.start_editing();
		any.start_editing_one(post_div);
	}
	
	this.create_post = function(post_id, post_left, post_top, post_width, post_height, post_text, can_edit, from_owner, author_name, author, author_uri, author_usa_id)
	{
		var post_div = document.createElement('DIV');
		post_div.className = 'anntt_image_post';
		post_div.id = 'anntt_image_post_'+post_id;

		post_div.style.left = Math.round(image_width * (post_left / 100) + 1)+'px';
		post_div.style.top  = Math.round(image_height * (post_top / 100) + 1)+'px';
		
		var html = '<div class="anntt_image_post_box_div"><div class="anntt_image_post_box_inner_div">';
		html+= '<div class="anntt_image_post_box_inner_inner_div"><div style="width:'+Math.round(image_width * (post_width / 100) - 5)+'px; height:'+Math.round(image_height * (post_height / 100) - 4)+'px;">';
		html+= '<\/div><\/div><\/div><\/div><div class="anntt_image_post_text_div"><\/div>';
		post_div.innerHTML = html;
		post_div.innerHTML = html;

		elem.appendChild(post_div);

		build(post_div, post_id, can_edit, from_owner, author_name, author, post_text, author_uri, author_usa_id);
		post_div.any.papa = elem;
		return post_div;
	}
	
	this.delete_post = function() 
	{
		any.which_post.any.stop_editing(0);
		any.editing = 0;
		any.stop_showing_one();
		//
		any.start_comming('Notitie wordt verwijderd');
		//
	
		var params = {image_id:any.img_id,post_id:any.which_post.any.post_id}
		anymeta_rest ('anymeta.annotates.remove', params, any.annotate_images_posts_delete_onLoad, any)
	}
	
	this.annotate_images_posts_delete_onLoad = function(success, responseXML, responseText) 
	{
		if (!success) 
		{
			alert('delete - foutje:'+responseText);
			any.stop_comming();
		}
		any.refresh_posts();
	}
	
	this.cancel_editing = function()
	{
		any.which_post.any.stop_editing(1);
		any.editing = 0;
		window.ignore_editpost_action = 0;
		any.stop_showing_one();
		any.empty_posts_text_area();
		if (any.which_post.id=='anntt_image_post_temp') elem.removeChild(any.which_post);
	}
	
	this.empty_posts_text_area= function() 
	{
		any.posts_text_area.value = '';
	}
	
	this.save_editing = function()
	{
		if (any.posts_text_area.value=='')
		{
			any.posts_text_area.focus();
			any.posts_text_area.select();
			return;
		}

		any.which_post.any.stop_editing(0);
		any.editing = 0;
	
		any.stop_showing_one();

		if (any.which_post.id=='anntt_image_post_temp')
		{
			//
			any.start_comming('Bewaar de nieuwe notitie');
			//
			var params = {
				image_id:any.img_id,
				post_left:any.which_post.any.get_post_left_perc(image_width),
				post_top:any.which_post.any.get_post_top_perc(image_height),
				post_width:any.which_post.any.get_post_width_perc(image_width),
				post_height:any.which_post.any.get_post_height_perc(image_height),
				post_text:any.posts_text_area.value
			}
			anymeta_rest ('anymeta.annotates.add', params, any.annotate_images_posts_add_onLoad, any)
			elem.removeChild(any.which_post);
		}
		else
		{
			//
			any.start_comming('Bewaar de notitie');
			//
			var params = {
				image_id:any.img_id,
				post_id:any.which_post.any.post_id,
				post_left:any.which_post.any.get_post_left_perc(image_width),
				post_top:any.which_post.any.get_post_top_perc(image_height),
				post_width:any.which_post.any.get_post_width_perc(image_width),
				post_height:any.which_post.any.get_post_height_perc(image_height),
				post_text:any.posts_text_area.value
			}
			anymeta_rest ('anymeta.annotates.edit', params, any.annotate_images_posts_edit_onLoad, any)
		}
		any.empty_posts_text_area();
	}
	
	this.annotate_images_posts_add_onLoad = function(success, responseXML, responseText)
	{
		if (success) 
		{
			any.refresh_posts();
		}
		else
		{
			alert('add - foutje:'+responseText);
			any.stop_comming();
		}
	}
	
	this.annotate_images_posts_edit_onLoad = function(success, responseXML, responseText)
	{
		if (success)
		{
			any.refresh_posts();
		}
		else
		{
			alert('edit - foutje:'+responseText);
			any.stop_comming();
		}
	}
	
	this.get_image_w = function()
	{
		return any.image_div.childNodes[0].width;
	}
	
	this.get_image_h = function()
	{
		return any.image_div.childNodes[0].height;
	}
	
	this.change_display = function()
	{
		if (any.isOver) 
		{
			elem.style.visibility = 'visible';
		}
		else
		{
			elem.style.visibility = 'hidden';
		}
	}

	var params = {
		image_id:any.img_id
	}
	anymeta_rest ('anymeta.annotates.getInfo', params, any.annotate_imagesgetEltInfo_onLoad, any)
}




/*
 *
 * 
 *
 */


var build_anntt_image_post = function(args)
{
	if (!okForXMLHTTPREQUEST()) return;
	var elem = args[0];
	elem.any = new Annotate_Post_any(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
	return elem;
}

Annotate_Post_any = function(el, post_id, can_edit, from_owner, author_name, author, post_text, author_uri, author_usa_id)
{
	var elem = this.elem = el; var any = this;
	any.post_id			= post_id;
	any.can_edit		= can_edit;
	any.author_name		= author_name;
	any.author			= author;
	any.author_uri		= author_uri;
	any.author_usa_id	= author_usa_id;
	any.from_owner		= from_owner;
//	any.use_color		= (any.from_owner) ? 'yellow' : 'green';
	any.isOver			= 0;
	any.editing			= 0;
	any.isDragging		= 0;
	any.resize_box_size	= 8;

	any.box_div			= elem.childNodes[0];
	any.innermost_div	= any.box_div.childNodes[0].childNodes[0].childNodes[0];

	if (document.all && !window.opera)
	{ 
		any.innermost_div.style.backgroundColor = "#fff";
		any.innermost_div.style.filter = "alpha(opacity:0)"; 
	}

	any.text_div		= elem.childNodes[1];
	any.saved_txt		= post_text;
	any.saved_x			= pInt(elem.style.left);
	any.saved_y			= pInt(elem.style.top);
	any.saved_w			= pInt(any.innermost_div.style.width);
	any.saved_h			= pInt(any.innermost_div.style.height);

	elem.appendChild(any.make_resize_div('nw'));
	elem.appendChild(any.make_resize_div('ne'));
	elem.appendChild(any.make_resize_div('sw'));
	elem.appendChild(any.make_resize_div('se'));

	any.box_div.onmouseover = any.onmouseover;
	any.box_div.onmouseout  = any.onmouseout;
	if (any.can_edit)
	{
		any.box_div.onmousedown = any.onmousedown;
		any.box_div.style.cursor = (document.all && !window.opera) ? 'hand' : 'pointer';
	}
}


/*
 *
 * prototypes
 *
 */

Annotate_Post_any.prototype.make_resize_div = function(which)
{
	var elem = this.elem; var any = this;
	var ref = 'resize_'+which+'_div';

	any[ref]  = document.createElement('DIV');
	any[ref].style.position = 'absolute';
	any[ref].style.display	= 'none';
	any[ref].style.cursor	= which+'-resize';
	any[ref].style.top		= (which=='ne' || which=='nw') ? '0px' : (any.get_post_height()-any.resize_box_size)+'px';
	any[ref].style.left		= (which=='sw' || which=='nw') ? '0px' : (any.get_post_width()-any.resize_box_size)+'px';
	any[ref].style.backgroundColor = '#fff';
	any[ref].innerHTML		= '<div style="border:1px solid black"><div style="width:'+(any.resize_box_size-2)+'px; height:'+(any.resize_box_size-2)+'px; background:#fff; font-size:1px"><\/div><\/div>';
	any[ref].onmousedown = function(e)
	{
		e || (e = window.event);
		any.start_dragging_resize(e, which);
	}
	any[ref].elem = elem;
	return any[ref];
}

Annotate_Post_any.prototype.start_dragging_resize = function(e, which)
{
	var elem = this.elem; var any = this;

	e || (e = window.event);
	var ref = 'resize_'+which+'_div';

	var corner = any[ref];

	any.papa.any.start_dragging_one();

	global_disable_select();

	corner.lastClientX = e.clientX;
	corner.lastClientY = e.clientY;

	if (which=='se')
	{
		var x = pInt(corner.style.left);
		var y = pInt(corner.style.top);

		var minX =  e.clientX - x + any.resize_box_size;
		var maxX = minX + any.papa.any.get_image_w() - pInt(elem.style.left) - (any.resize_box_size*2)+1;
		var minY = e.clientY - y + any.resize_box_size;
		var maxY = minY + any.papa.any.get_image_h() - pInt(elem.style.top) - (any.resize_box_size*2)+1;
	}
	else if (which=='sw')
	{
		var x = pInt(elem.style.left);
		var y = pInt(corner.style.top);

		var minX =  e.clientX - x + 1;
		var maxX = minX + x + any.get_post_width() - (any.resize_box_size*2)-1;
		var minY = e.clientY - y + any.resize_box_size;
		var maxY = minY + any.papa.any.get_image_h() - pInt(elem.style.top) - (any.resize_box_size*2)+1;
	}
	else if (which=='nw')
	{
		var x = pInt(elem.style.left);
		var y = pInt(elem.style.top);

		var minX =  e.clientX - x + 1;
		var maxX = minX + x + any.get_post_width() - (any.resize_box_size*2)-1;
		var minY = e.clientY - y + 1;
		var maxY = minY + y + any.get_post_height() - (any.resize_box_size*2)-1;

	}
	else if (which=='ne')
	{
		var x = pInt(corner.style.left);
		var y = pInt(elem.style.top);

		var minX =  e.clientX - x + any.resize_box_size;
		var maxX = minX + any.papa.any.get_image_w() - pInt(elem.style.left) - (any.resize_box_size*2)+1;
		var minY = e.clientY - y + 1;
		var maxY = minY + y + any.get_post_height() - (any.resize_box_size*2)-1;
	}

	any.isDragging = 1;
	any.change_display();

	document.dragee = corner;
	document.onmouseup = function(e)
	{
		if (document.dragee) document.dragee.elem.any.stop_dragging_resize();
		document.dragee = null;
	}

	document.onmousemove = function(e)
	{
		e || (e = window.event);

		if (any.papa.any.hide_while_dragging) any.papa.any.hide_posts_text_div();

		var ex = Math.min(Math.max(e.clientX, minX), maxX);
		var ey = Math.min(Math.max(e.clientY, minY), maxY);

		if (which=='se')
		{
			var x = pInt(corner.style.left);
			var y = pInt(corner.style.top);

			var nx = x + (ex - corner.lastClientX);
			var ny = y + (ey - corner.lastClientY);

			corner.style.left = nx + 'px';
			corner.style.top = ny + 'px';

			any.innermost_div.style.width = (pInt(corner.style.left) + any.resize_box_size - 4)+'px';
			any.innermost_div.style.height = (pInt(corner.style.top) + any.resize_box_size - 4)+'px';

			any['resize_ne_div'].style.left = corner.style.left;
			any['resize_sw_div'].style.top = corner.style.top;
		}
		else if (which=='sw')
		{
			var x = pInt(elem.style.left);
			var y = pInt(corner.style.top);

			var nx = x + (ex - corner.lastClientX);
			var ny = y + (ey - corner.lastClientY);

			elem.style.left		= nx + 'px';
			corner.style.top	= ny + 'px';

			any.innermost_div.style.width	= (pInt(any.innermost_div.style.width) + x - nx)+'px';
			any.innermost_div.style.height	= (pInt(corner.style.top) + any.resize_box_size - 4)+'px';

			any['resize_ne_div'].style.left = any['resize_se_div'].style.left = (pInt(any.innermost_div.style.width) - any.resize_box_size + 4)+'px';
			any['resize_se_div'].style.top	= corner.style.top;

		}
		else if (which=='nw')
		{
			var x = pInt(elem.style.left);
			var y = pInt(elem.style.top);

			var nx = x + (ex - corner.lastClientX);
			var ny = y + (ey - corner.lastClientY);

			elem.style.left = nx + 'px';
			elem.style.top = ny + 'px';

			any.innermost_div.style.width	= (pInt(any.innermost_div.style.width) + x - nx)+'px';
			any.innermost_div.style.height	= (pInt(any.innermost_div.style.height) + y - ny)+'px';

			any['resize_sw_div'].style.left = corner.style.left;
			any['resize_ne_div'].style.top	= corner.style.top;
			any['resize_se_div'].style.top	= any['resize_sw_div'].style.top = (pInt(any.innermost_div.style.height) - any.resize_box_size + 4)+'px';
			any['resize_se_div'].style.left = any['resize_ne_div'].style.left = (pInt(any.innermost_div.style.width) - any.resize_box_size + 4)+'px';

		}
		else if (which=='ne')
		{
			var x = pInt(corner.style.left);
			var y = pInt(elem.style.top);

			var nx = x + (ex - corner.lastClientX);
			var ny = y + (ey - corner.lastClientY);

			corner.style.left	= nx + 'px';
			elem.style.top		= ny + 'px';

			any.innermost_div.style.width	= (pInt(corner.style.left) + any.resize_box_size - 4)+'px';
			any.innermost_div.style.height	= (pInt(any.innermost_div.style.height) + y - ny)+'px';

			any['resize_se_div'].style.left = corner.style.left;
			any['resize_se_div'].style.top	= any['resize_sw_div'].style.top = (pInt(any.innermost_div.style.height) - any.resize_box_size + 4)+'px';
		}

		corner.lastClientX = ex;
		corner.lastClientY = ey;

		if (!any.papa.any.hide_while_dragging) any.papa.any.place_posts_text_div();
	}
}

Annotate_Post_any.prototype.stop_dragging_resize = function()
{
	var elem = this.elem; var any = this;
	global_enable_select();
	if (!any.isDragging) return;
	document.onmousemove = null;
	any.papa.any.start_editing_one(elem);
	any.isDragging = 0;
	any.change_display();
}

Annotate_Post_any.prototype.show_resize_divs = function() 
{
	var elem = this.elem; var any = this;
	any.resize_nw_div.style.display = any.resize_ne_div.style.display = any.resize_sw_div.style.display = any.resize_se_div.style.display = 'block';
}

Annotate_Post_any.prototype.hide_resize_divs = function() 
{
	var elem = this.elem; var any = this;
	any.resize_nw_div.style.display = any.resize_ne_div.style.display = any.resize_sw_div.style.display = any.resize_se_div.style.display = 'none';
}

Annotate_Post_any.prototype.get_post_width = function() 
{
	var elem = this.elem; var any = this;
	return pInt(any.innermost_div.style.width)+4;
}

Annotate_Post_any.prototype.get_post_width_perc = function(w) 
{
	var elem	= this.elem; var any = this;
	var ret = pInt(any.innermost_div.style.width)+4;
	ret		= Math.round(ret / w * 100);
	return ret;
}

Annotate_Post_any.prototype.get_post_height = function() {
	var elem = this.elem; var any = this;
	return pInt(any.innermost_div.style.height)+4;
}

Annotate_Post_any.prototype.get_post_height_perc = function(h) 
{
	var elem	= this.elem; var any = this;
	var ret = pInt(any.innermost_div.style.height)+4;
	ret		= Math.round(ret / h * 100);
	return ret;
}

Annotate_Post_any.prototype.get_post_left = function() 
{
	var elem = this.elem; var any = this;
	return pInt(elem.style.left)-1;
}

Annotate_Post_any.prototype.get_post_left_perc = function(w)
{
	var elem  = this.elem; var any = this;
	var ret = pInt(elem.style.left)-1;
	ret		= Math.round(ret / w * 100);
	return ret;
}

Annotate_Post_any.prototype.get_post_top = function() 
{
	var elem = this.elem; var any = this;
	return pInt(elem.style.top)-1;
}

Annotate_Post_any.prototype.get_post_top_perc = function(h) 
{
	var elem  = this.elem; var any = this;
	var ret = pInt(elem.style.top)-1;
	ret	    = Math.round(ret / h * 100);
	return ret;
}

Annotate_Post_any.prototype.onmouseover = function()
{
	var elem = this.parentNode; var any = elem.any;
	if (any.papa.any.editing || any.papa.any.comming || any.papa.any.dragging) return;
	any.isOver = 1;
	any.change_display();
	any.papa.any.start_showing_one(elem);
}

Annotate_Post_any.prototype.onmouseout = function() 
{
	var elem = this.parentNode; var any = elem.any;
	if (elem.id=='anntt_image_post_temp') return; 
	any.hide_timmy = setTimeout("getEl('"+elem.id+"').any.tAnother('Annotate_Post_any.prototype.onmouseout');", 200);
}

Annotate_Post_any.prototype.tAnother = function() 
{
	var elem = this.elem; var any = this;
	if (any.papa.any.editing || any.papa.any.comming || any.papa.any.dragging) return;
	any.isOver = 0;
	any.change_display();
	any.papa.any.stop_showing_one();
}

Annotate_Post_any.prototype.start_editing = function()
{
	var elem = this.elem; var any = this;
	if (any.editing) return; 
	any.isOver = 1;
	any.editing = 1;
	any.change_display();
}

Annotate_Post_any.prototype.stop_editing = function(revert)
{
	var elem = this.elem; var any = this;
	any.editing = 0;
	if (revert) 
	{
		elem.style.left = any.saved_x+'px';
		elem.style.top  = any.saved_y+'px';

		any.innermost_div.style.width	= any.saved_w+'px';
		any.innermost_div.style.height	= any.saved_h+'px';

		any.resize_nw_div.style.top		= any.resize_ne_div.style.top	= '0px';
		any.resize_sw_div.style.top		= any.resize_se_div.style.top	= (any.get_post_height()-any.resize_box_size)+'px';
		any.resize_nw_div.style.left	= any.resize_sw_div.style.left	= '0px';
		any.resize_ne_div.style.left	= any.resize_se_div.style.left	= (any.get_post_width()-any.resize_box_size)+'px';

		any.papa.any.start_showing_one(elem);
	}
	any.change_display();
}

Annotate_Post_any.prototype.onmousedown = function(e)
{
	var elem = this.parentNode; var any = elem.any;
	e || (e = window.event);
	if (!any.papa.any.editing && !any.papa.any.comming && !any.papa.any.dragging) any.start_editing();
	if (((!any.papa.any.editing && !any.papa.any.dragging) || any.papa.any.which_post==elem) && !any.papa.any.comming) any.start_dragging(e);
}

Annotate_Post_any.prototype.stop_dragging = function() 
{
	var elem = this.elem; var any = this;
	global_enable_select();
	if (!any.isDragging) return;
	document.onmousemove = null;
	any.papa.any.start_editing_one(elem);
	any.isDragging = 0;
	any.change_display();
}

Annotate_Post_any.prototype.start_dragging = function(e)
{
	var elem = this.elem; var any = this;

	e || (e = window.event);

	any.papa.any.start_dragging_one();

	global_disable_select();

	var x = pInt(elem.style.left);
	var y = pInt(elem.style.top);

	elem.lastClientX = e.clientX;
	elem.lastClientY = e.clientY;

	var minX =  e.clientX - x + 1;
	var maxX = minX + any.papa.any.get_image_w() - any.get_post_width();
	var minY = e.clientY - y + 1;
	var maxY = minY + any.papa.any.get_image_h() - any.get_post_height();

	any.isDragging = 1;
	any.change_display();

	document.dragee = elem;
	document.onmouseup = function(e)
	{
		if (document.dragee) document.dragee.any.stop_dragging();
		document.dragee = null;
	}

	document.onmousemove = function(e)
	{
		e || (e = window.event);

		if (any.papa.any.hide_while_dragging) any.papa.any.hide_posts_text_div();

		var x = pInt(elem.style.left);
		var y = pInt(elem.style.top);

		var ex = Math.min(Math.max(e.clientX, minX), maxX);
		var ey = Math.min(Math.max(e.clientY, minY), maxY);

		var nx = x + (ex - elem.lastClientX);
		var ny = y + (ey - elem.lastClientY);

		elem.style.left = nx + 'px';
		elem.style.top = ny + 'px';

		elem.lastClientX = ex;
		elem.lastClientY = ey;

		if (!any.papa.any.hide_while_dragging) any.papa.any.place_posts_text_div();
	}
}

Annotate_Post_any.prototype.refresh_info = function(post_id, post_left, post_top, post_width, post_height, post_text)
{
	var elem = this.elem; var any = this;
	any.post_id = post_id;
	elem.id = 'anntt_image_post_'+post_id;

	elem.style.left					= Math.round(image_width  * (post_left   / 100) + 1)+'px';
	elem.style.top					= Math.round(image_height * (post_top    / 100) + 1)+'px';
	any.innermost_div.style.width	= Math.round(image_width  * (post_width  / 100) - 5)+'px';
	any.innermost_div.style.height	= Math.round(image_height * (post_height / 100) - 5)+'px';

	// rememeber these for future cancels
	any.saved_txt = post_text;
	any.saved_x = pInt(elem.style.left);
	any.saved_y = pInt(elem.style.top);
	any.saved_w = pInt(any.innermost_div.style.width);
	any.saved_h = pInt(any.innermost_div.style.height);
}

Annotate_Post_any.prototype.change_display = function()
{
	var elem = this.elem; var any = this;
	any.box_div.childNodes[0].style.borderStyle = 'solid';
	any.box_div.childNodes[0].childNodes[0].style.borderStyle = 'solid';
	any.box_div.style.left = any.box_div.style.top = '0px';
	any.box_div.style.borderWidth = '0';

	any.hide_resize_divs();
	if (any.editing) 
	{
		any.show_resize_divs();
		any.box_div.childNodes[0].childNodes[0].style.borderStyle = 'dotted';
		any.box_div.childNodes[0].style.borderStyle = 'dotted';
	}
	else if (any.isOver)
	{
		any.box_div.style.left = any.box_div.style.top = '-1px';
		any.box_div.style.borderWidth = '1px';
	}
	else
	{
	}
}

var place_posts_text_div = function(t)
{
	if (!document.all || window.opera) return;
	var anntt_image_posts = getEl('anntt_image_posts');
	if (anntt_image_posts) 
	{
		anntt_image_posts.any.place_posts_text_div();
	}
	return;
}

var adjust_textarea_height = function(t)
{
	return;
	temp_div.innerHTML = t.value.replace('<', '&lt;').replace('\n', '<br>')+'g';
	t.style.height = (temp_div.offsetHeight)+'px';

	return;
	if (t.scrollHeight > t.clientHeight) t.style.height = t.scrollHeight;
}

function show_posts_init() 
{
	if (window.ignore_butt_action || window.ignore_editdiv_action) return;
	var anntt_image_posts = getEl('anntt_image_posts');
	if (anntt_image_posts && anntt_image_posts.any) 
	{
		anntt_image_posts.any.shown_initially = 1;
		anntt_image_posts.style.visibility = 'visible';
		if (!anntt_image_posts.any.tried_to_show_before_shown_initially) anntt_image_posts.any.go_away_in_a_bit();
	}
}

var undefined;
global_disable_select = function() 
{
	document.onselectstart = new Function ('return false');
}

global_enable_select = function()
{
	document.onselectstart = new Function ('return true');
}

function global_textarea_limiter(textarea, max) 
{
	if (textarea.value.length > max) textarea.value = textarea.value.substring(0, max);
}

pInt = function(str)
{
	return parseInt(str);
}

getEl = function(id)
{
	return document.getElementById(id);
}

String.prototype.nl2br = function() {
	return this.split('\n').join('<br \/>\n');
}

String.prototype.replace = function(find,replace) {
	return this.split(find).join(replace);
}

function getEventSrc(e)
{
	if (e){ return e.target; }
	if (window.event){ return window.event.srcElement; }
	return null;
}

function el_is_in_a_link(el)
{
	if (el.href) return 1;
	if (el.parentNode.href) return 1;
	if (el.parentNode.parentNode.href) return 1;
	if (el.parentNode.parentNode.parentNode.href) return 1;
	return 0;
}

function build() 
{
	var el = arguments[0];
	if (!el) return;
	arguments[0] = el;
	if (window['build_'+el.className]) return window['build_'+el.className](arguments);
}

 	 
document.getElementsByClass = function (needle, tagName) 
{
 	if (!tagName) tagName = '*';
	var my_array = document.getElementsByTagName(tagName);
	var retvalue = new Array();
	var i;	 
	var j;

	for (i = 0, j = 0; i < my_array.length; i++) 
	{
		var c = " " + my_array[i].className + " "; 	 
		if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i]; 	 
	}	
	return retvalue;
}

//