//-----------------------------------------------------------------
	var glob_first_init_tag_holder = true;
	var glob_tags_counter = 0;
	var global_tags = [];
	var glob_first_tags_list;
	var glob_init_select_function;
	var glob_form_select;
	var glob_isTaggingActive = false;
	var glob_isMouseOverTagArea = false;
	var glob_photo_save = 'Suggest a title for this photo ...';
	var glob_photo_upd_ok = 'Sorry, the update failed.';
	var glob_photo_upd_no = 'Saving...';
	var glob_title_save = 'Click to add a description ...';
	var glob_tag_txt = 'Click to add tags to this photo (e.g., Tokyo, Fraises des Bois, Cathedral, etc ...)';
	
	var glob_ajax_url = window.location.href;
	
// ------------------------------------------------------------------
jQuery.noConflict();

jQuery(document).ready( function(){
	
	createTagsForm();
	
	//add inactive class to magnif glass
	//res = document.documentElement.clientWidth;
	
	initEscKey();
	
	jQuery('div.photo_main_photo').css('z-index','100');
	
	initTaggingButtClick();
	
	initReqestToVars();
	
	var curent_html = getCurrentHTML();

	jQuery(curent_html).imgAreaSelect({
		disable:true,
		hide:true
	});	
	
	jQuery('#pagebody').css('overflow','hidden');
	jQuery('#pagebody').click( function(){
		if ( glob_isTaggingActive === true ){
			taggingOff();
			jQuery('#photo-notes').css('display', 'none');
		}
	});
	
	initPhotoClick();
});
//
//block FUNCTION -----------------------------------------------------------------------
//
//
function getCurrentHTML(){
	if ( jQuery('div.photo_main_photo > a > img').length > 0 ){
		var _html = 'div.photo_main_photo > a > img';
	}
	if ( jQuery('div.photo_main_photo > img').length > 0 ){
		var _html = 'div.photo_main_photo > img';
	}
	return _html;
}
//
//
function createTagsForm(){
	var data = 'action=autocomplete_tags';
	jQuery.ajax({
		type: "POST",
		url: glob_ajax_url,
		data: data,
		success: function(tags){
			var vars = tags.match(/(.*?)\,\s?/ig);
			for (var i=0; i < vars.length; i++){
				vars[i] = vars[i].replace(',','');
			}
			
			glob_first_tags_list = '<ul class="select-tag">';
			for (var i=0; i < vars.length; i++){
				glob_first_tags_list += '<li class="">'+ vars[i] +'</li>';
			}
			global_tags = vars;
			glob_first_tags_list += '</ul>';
			glob_form_select = '<form action=\"#\" id=\"tag-area-form-select\" style=\"background-color: #000000; opacity: 0.65; -moz-opacity: 0.65; filter: alpha(opacity=65);\">' +
								'<fieldset>' +
									'<label for=\"tag-area-select\"></label>' +
									'<p>Type any name or tag:</p>'+
										'<input type=\"text\" class="tag-area-select" name=\"tag-area\" id=\"tag-area-select\" size=\"60\"/>' +
									'<p>or choose a person:</p>' +
									'<div class="select-list" style="background-color: #000000;" id="select-tag-list">' + glob_first_tags_list +
									'</div>' +
								'<div class="buttons"><input type=\"submit\" value=\"Tag\" class=\"submit\">' +
								'<input type="button" id="cancel" name="Cancel" value="Cancel"  /></div>' +
								'</fieldset>' +
							'</form>';
		},
		error: function(msg) { }
 	});
}
//
//
function initEscKey(){
	jQuery('#tagging_menu').parent('li').addClass('tag_inactive');
	jQuery(document.body).keyup(function(event){
		if (event.keyCode == '27'){
			taggingOff();
			jQuery('#photo-notes').css('display', 'none');
			jQuery('#tag-area-form-select').css({'display':'none'});
			
			var current_html = getCurrentHTML();
			
			jQuery(current_html).imgAreaSelect({
				disable:true,
				hide:true
			});	
			return false; 
		};
	});
}

//
//
function initTaggingButtClick(){
		jQuery('#tagging_menu').click(function(){
		// activate tagging mode
		if (glob_isTaggingActive == false){
			glob_isTaggingActive = true;
			//add magnif glass color change onn
			jQuery('#tagging_menu').parent('li').removeClass('tag_inactive');
			jQuery('#tagging_menu').parent('li').addClass('tag_active');
			
			jQuery('div.tag-rectangle').css('display','none');
			
			if ( jQuery('#photo-notes').length > 0 ){
				jQuery('#photo-notes').text('Select the area around an object to tag it.');
			}
			else{
					jQuery('div.photo_main_photo').prepend('<div id="photo-notes">Select the area around an object to tag it..</div>');
			}
			jQuery('#photo-notes').css({
				'overflow':'hidden',
				'display':'block',
				'color':'#AAAAAA',
				'font-size':'14px',
				'font-weight':'bold',
				'position':'absolute',
				'width':'730px',
				'text-decoration':'none',
				'top':'0px',
				'left':'0px',
				'height':'20px',
				'padding-top':'8px',
				'background':'rgba(0, 0, 0, 0.5)',
				'text-align':'center',
			});
			
			// disable link
			jQuery('div.photo_main_photo > a').bind('click', noClick);
			
			jQuery('div.photo_main_photo > a').css('cursor','Crosshair');
			jQuery('div.imgareaselect-outer').css('cursor','Crosshair');
			
			var current_html = getCurrentHTML();
					
			jQuery(current_html).imgAreaSelect({
				selectionColor: 'transparent',
				outerOpacity:0,
				borderColor1:'transparent',
				movable: false,
				resizable: false,
				enable:true,
				maxHeight:"492px",
				maxWidth:"740px",
				hide:false,
				onSelectStart: function(){
					jQuery('#photo-notes').text('Select things in the photo to tag them.');
					
					jQuery('div.photo_main_photo > a').css('cursor','Crosshair');
					jQuery('div.imgareaselect-outer').css('cursor','Crosshair');
					
					if (jQuery('#tag-area-form-select') ) jQuery('#tag-area-form-select').replaceWith('');
					//if (jQuery('#tag-area-form') ) jQuery('#tag-area-form').replaceWith(''); //test!!!!!!!!!!!!!!!!!!!!!
					if (jQuery('div.ac_results') )jQuery('div.ac_results').replaceWith('');
					
				}, 
				onSelectEnd: selectionEnd
			 });
			return false;
		}
		// deactivate tagging mode
		else{
			taggingOff();
			isTaggingActive = false;
			//add magnif glass color change off
			jQuery('#tagging_menu').parent('li').removeClass('tag_active');
			jQuery('#tagging_menu').parent('li').addClass('tag_inactive');
			//if ( jQuery('#photo-notes') ){
		//		jQuery('#photo-notes').text('Select things in the photo to tag them: Done Tagging...');
		//	}
			//jQuery('#photo-notes').text('').text('Done Tagging.');
			jQuery('#photo-notes').css('display', 'none');
			// enable link
			jQuery('div.photo_main_photo > a').unbind('click', noClick);
			
			var current_html = getCurrentHTML();
			
			jQuery(current_html).imgAreaSelect({
				disable:true,
				hide:true
			})
			// back cursor to hand
			jQuery('div.photo_main_photo > a').css('cursor','pointer');
			jQuery('div.imgareaselect-outer').css('cursor','pointer');
			
			return false;
		}
	})
}
//
//
function noClick(){
	return false;
}

//
//
function selectionEnd(img, selection){
	//div.imgareaselect-border2
	jQuery('#pagebody').after("<form action=\"#\"  class=\"tag-area-form\" id=\"tag-area-form-"+glob_tags_counter+"\">"
								+"<label for=\"tag-area\"></label>"
								+"<div class=\"tag-area-class\">"
									+"<input type=\"text\" name=\"tag-area-"+glob_tags_counter+"\" id=\"tag-area-"+glob_tags_counter+"\" size=\"60\"/>"
								+"</div>"
								+"<input type=\"submit\" value=\"submit\" class=\"submit\">"
							  +"</form>");
	if (jQuery('#tag-area-form-select').length==0){
		jQuery('#pagebody').after(glob_form_select);
	}
	
//	jQuery('#tag-area-form').css({'visibility':'hidden', 'display':'block', 'z-index':'1000'});
	jQuery('#tag-area-form-select').css({'visibility':'hidden', 'display':'block', 'z-index':'1000'});
	
	var cur_tag_width = jQuery('#tag-area-form-'+glob_tags_counter).width();
	var cur_select_width = selection.width;
	
	var current_html = getCurrentHTML();
	

	var allow_area_h = jQuery(current_html).height();
	var allow_area_w = jQuery(current_html).width();
	
	var offs = 0;
	var ofs_fix = 0;
 	centr =  ( jQuery(window).scrollLeft() + parseInt(jQuery(window).width() / 2) - 300 ) ;   		

	jQuery('#tag-area-form-select').css({'visibility':'visible', 'z-index':'9999'});
	
	if ( selection.x2 > ( allow_area_w /2 + cur_select_width-120) ) offs = - (320 + cur_select_width);
	if (cur_select_width > allow_area_w - 300) offs = -300;
	var offs_y = 0;
	if ( selection.y2 > (allow_area_h - 40) ) offs_y = -(selection.height+40) ;
	jQuery('#tag-area-form-select').css({'top':selection.y1+140, 'left':selection.x2+centr+offs, 'visibility':'visible', 'z-index':'9999'});
	
	jQuery('#tag-area-select').autocomplete(global_tags, {"scrollHeight":"220px",
		"width":"180px",
		"autoFill":true
		});

	jQuery('#tag-area-select').focus();
	initTagForm(selection);
}

//
//
function initTagForm(selection){
	jQuery('ul.select-tag li').click(function(){
		jQuery('#tag-area-select').attr('value', jQuery(this).text() );
		
		jQuery('#tag-area-form-select').submit();
		taggingOff();
		jQuery('#tagging_menu').click();
	});
	
	// hide basick tag list
	jQuery('#tag-area-select').keyup( function(e){
		if ( jQuery('div.ac_results ul li').length > 0 ){
			if ( jQuery('div.mail-form') ){
				jQuery('div.mail-form').replaceWith('');
			}
			
			jQuery('div.ac_results ul li').click(function() {
				jQuery('#tag-area-select').attr('value', jQuery(this).text() );
				jQuery('#tag-area-form-select').submit();
			});
		}
		// is no tag enter
		if ( jQuery(this).attr('value').length > 0 ){
			jQuery('div.select-list ul').css('display','none');
		}
		else{
			jQuery('div.select-list ul').css('display','block');
//			jQuery('div.select-list ul').css('position','absolute');
			
			var isActive = 0;
			var current = -1;
			
			// down key
			if (e.keyCode == 40 && ( jQuery('div.select-list ul').css('display')!='none' ) ){
				var listItems = jQuery('div.select-list ul li');
				
				for (var i=0; i < listItems.length; i++){
					var item = listItems[i];
					if ( jQuery(item).attr('class') == 'ac_over' ){
						isActive = 1;
						current = i;
						break;
					}
				}
				
				if (isActive != -1 && (current+1)<=(listItems.length-1) ){
					var next_active = listItems[current+1];
					var curr_active = listItems[current];
					if ( (current+1) >= 10){
						var offset = ( (current+1) - 9) * 12;
						document.getElementById('select-tag-list').scrollTop = offset;
					}
					jQuery(curr_active).removeClass('ac_over');
					jQuery(next_active).addClass('ac_over'); 
					jQuery('div.select-list ul').attr('title', jQuery(next_active).text() );
				}
				
			}
			// up key
			if (e.keyCode == 38 && ( jQuery('div.select-list ul').css('display')!='none' ) ){
				var listItems = jQuery('div.select-list ul li');
				
				for (var i=0; i < listItems.length; i++){
					var item = listItems[i];
					if ( jQuery(item).attr('class') == 'ac_over' ){
						isActive = 1;
						current = i;
						break;
					}
				}
				
				if (isActive != -1 && current >= 1 ){
					var prev_active = listItems[current-1];
					var curr_active = listItems[current];
					jQuery(curr_active).removeClass('ac_over');
					jQuery(prev_active).addClass('ac_over'); 
					jQuery('div.select-list ul').attr('title', jQuery(prev_active).text() );
				}
			}
		
		}
		
		// is new tag not in list present
		setTimeout('null', 100);
		if ( jQuery('div.ac_results > ul').html() == '' || ( jQuery('div.ac_results ul li').length < 0 && jQuery('div.select-list ul').css('display') == 'none' ) ){
			if ( jQuery('div.mail-form') ){
				jQuery('div.mail-form').replaceWith('');
			}
			var mail_form = '<div class="mail-form"><p>The tag "'+ jQuery('#tag-area-select').attr("value") +'" was not found.<br /><br />Tagging this object will add it to the system.<br /><br />Please enter the tag with proper casing, e.g., "Jones Smith" to keep things consistent.</p></div>';
			jQuery('#tag-area-form-select div.select-list').append(mail_form);
		}
	});
	
	// hover on <li> tag list
	jQuery('div.select-list ul.select-tag li').hover(
		function(){
			jQuery(this).addClass('ac_over');
		},
		function(){
			jQuery(this).removeClass('ac_over');
		}
	);
	
	// click on tag list item event
	jQuery('div.select-list ul.select-tag li').click(function(){
		jQuery('#tag-area-select').attr("value", jQuery(this).text() );
		return false;
	});
	
	// cancel button
	jQuery('#tag-area-form-select input#cancel').click( function(){
		jQuery('#tag-area-form-select').css({'display':'none'});
		
		var current_html = getCurrentHTML();
		
		jQuery(current_html).imgAreaSelect({
			disable:true,
			hide:true
		});
		taggingOff();
		jQuery('#tagging_menu').click();
		return false; 
	});
	
	// submit tag form
	jQuery('#tag-area-form-select').submit( function(){
		// if there are no holder for tags (first enter)
		jQuery('#photo-notes').text('Done Tagging.');
		
		var current_tag;
		if ( glob_first_init_tag_holder ){
			
			glob_first_init_tag_holder = false;
			var tags = jQuery('<div></div>');
			tags.addClass('tags-holder');
			
			if ( jQuery('#tag-area-select').val() != '' )
				current_tag = jQuery('#tag-area-select').val();
			else if ( jQuery('div.select-list ul').attr('title') != '' )
				current_tag = jQuery('div.select-list ul').attr('title');
			else
				return false;
			
			if (current_tag != ''){
			
				var match = '/("*)/gi';
				current_tag = current_tag.replace('"', "&quot;");
				current_tag = current_tag.replace('"', "&quot;");
				
											
				tag_holder = '<span>IN THIS PHOTO</span></p><ul>';
				tag_holder  = tag_holder + '<li><a class="link-tag" title="'+ current_tag +'" href="javascript:;" rel="x1:'
					+ selection.x1 + ',y1:' + selection.y1 + ',x2:' + selection.x2 + ',y2:'
					+ selection.y2 + '">' + current_tag + '</a><a class="delete" rel="'+glob_tags_counter+'" href="javascript:;">&nbsp;X&nbsp;&nbsp;</a></li>';
				tag_holder = tag_holder + '</ul>';
				tags.append(tag_holder);	
				jQuery('div.photoview_main').after(tags);
			};
			
		}
		// else
		else{
			var tag_holder = jQuery('div.tags-holder > ul');
			
			if ( jQuery('#tag-area-select').val() != '' )
				current_tag = jQuery('#tag-area-select').val();
			else if ( jQuery('div.select-list ul').attr('title') != '' )
				current_tag = jQuery('div.select-list ul').attr('title');
			else
				return false;
			
			if (current_tag != ''){
			
				var match = '/("*)/gi';
				current_tag = current_tag.replace('"', "&quot;");
				current_tag = current_tag.replace('"', "&quot;");
				
				tag_to_enter  = '<li><a class="link-tag" title="'+ current_tag +'" href="javascript:;" rel="x1:'
					+ selection.x1 + ',y1:' + selection.y1 + ',x2:' + selection.x2 + ',y2:'
					+ selection.y2 + '">' + current_tag + '</a><a class="delete" rel="'+glob_tags_counter+'" href="javascript:;">&nbsp;X&nbsp;&nbsp;</a></li>';
				tag_holder.append(tag_to_enter);	
			};
			
		};
		
		// save curent rectangle
		jQuery('div.photo_main_photo').append('<div class="tag-rectangle tag-rectangle-' + glob_tags_counter + '"><a title="'+current_tag+'" href="javascript:;">&nbsp;</a></div>');
		var top = jQuery('div.imgareaselect-border2').css('top');
		var left = jQuery('div.imgareaselect-border2').css('left');
		var width = jQuery('div.imgareaselect-border2').css('width');
		var height = jQuery('div.imgareaselect-border2').css('height');
		
//		left = parseInt(left);
//		top = parseInt(top);
		
		jQuery('div.tag-rectangle-' + glob_tags_counter).css({ 
			'display':'none',
			'position':'absolute',
			'width':width,
			'height':height,
			'top':selection.y1,
			'left':selection.x1
		});
		jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a').attr({
			'rel':'x1:'+ selection.x1 + ',y1:' + selection.y1 + ',x2:' + selection.x2 + ',y2:' + selection.y2
		});
		jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a').css('display','block');
		jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a').css('width','100%');
		jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a').css('height','100%');
		jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a').css('text-decoration','none');

		
		activateTagRectangle( glob_tags_counter++ );
		
		// activate tags link when list is generated
		jQuery('div.tags-holder').css({'display':'block'});
		activateTagsLink();
		// hide curent selection after form submit
		jQuery('#tag-area-form-select').css({'display':'none'});
		
		var current_html = getCurrentHTML();
			
		jQuery(current_html).imgAreaSelect({
			disable:true,
			hide:true
		});	
		var data;
		var image_id = jQuery(current_html).attr('src');
		
//		jQuery('div.imgareaselect-outer, div.imgareaselect-selection, div.imgareaselect-border1, div.imgareaselect-border2').replaceWith('');
		
		if (jQuery('#mail') ){
			var email = jQuery('#mail').attr('value');
			data = 'x1='+selection.x1+'&y1='+selection.y1+'&x2='+selection.x2+'&y2='+selection.y2+'&tag='+current_tag+'&email='+email+'&image='
				+image_id;
		}
		else
			data = 'x1='+selection.x1+'&y1='+selection.y1+'&x2='+selection.x2+'&y2='+selection.y2+'&tag='+current_tag+'&image='
				+image_id;

		jQuery.ajax({
	   type: "POST",
	   url: glob_ajax_url,
	   data: data,
	   success: function(msg){ },
		 error: function(msg) { }
	 	});
	 	
			taggingOff();
		jQuery('#tagging_menu').click();	
	return false;
	});
}

//
//
function activateTagRectangle(current_rectangle){

	jQuery('div.tag-rectangle-' + current_rectangle + ' > a').mouseover(
		function(){
			if(glob_isTaggingActive == false){
			if (glob_isMouseOverTagArea == false){
				glob_isMouseOverTagArea = true;
				var elem = jQuery(this);
				//x1:115,y1:69,x2:274,y2:210
				var rel = jQuery(elem).attr('rel');
				if ( !rel.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i ) ) {
					//error!!!!!!!!!!!!!!!!!!!!!!!!!!
					return;
				}
				var vars = rel.match(/x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i);
				var coord = {	x1:parseInt(vars[1]), y1:parseInt(vars[2]), x2:parseInt(vars[3]), y2:parseInt(vars[4]) };
				coord.tag = elem.attr('title');
					
				elem.css('border-width','1px')
					.css('border-color','#fff')
					.css('border-style','dashed');
					
					var curent_html = getCurrentHTML();
					
				jQuery(curent_html).imgAreaSelect({
					//show:'true',
					x1:coord.x1,
					y1:coord.y1,
					x2:coord.x2,
					y2:coord.y2,
					selectionOpacity:0,
					outerOpacity:0,
					borderWidth:1,
					enable:true,
					movable: false,
					hide: true,
					maxWidth:"740px",
					maxHeight:"492px",
					minWidth:1,
					minHeight:1,
					resizable: false
				});
				// show tag text
				jQuery('#tag-area-form-'+current_rectangle).css({'display':'block', 'z-index':'999', 'position':'absolute'}).show();
				jQuery('#tag-area-'+current_rectangle).attr('value',coord.tag);
				
				var cur_tag_width = jQuery('#tag-area-form-'+current_rectangle).width();
				var cur_select_width = parseInt(coord.x2) - parseInt(coord.x1);
				var cur_select_height = parseInt(coord.y2) - parseInt(coord.y1);
					
				if (cur_select_width > cur_tag_width){
					var offs = (cur_select_width - cur_tag_width) / 2; 
				}else{
					var offs = (cur_tag_width - cur_select_width) / -2;
				};
				
				var allow_area_h = jQuery(curent_html).height();
				var allow_area_w = jQuery(curent_html).width();
				
				if ( ( parseInt(coord.x2) > ( allow_area_w - (cur_tag_width - 130) ) ) && cur_select_width < cur_tag_width ) {
					offs = - cur_tag_width + 50;
				}
				if ( ( parseInt(coord.x2) < ( 0 + cur_tag_width ) ) && cur_select_width < cur_tag_width ){
					offs = 10;
				}
				
				var offs_y = 0;
				if ( parseInt(coord.y2) > (allow_area_h - 40) ){
					offs_y = -(cur_select_height+40) ;
				}
				
				var image = jQuery(curent_html).offset();
				
				var top_new = parseInt(coord.y2)+150+offs_y;
				var left_new = parseInt(coord.x1)+image.left+offs;
				
				jQuery('#tag-area-form-'+current_rectangle).css({'top':top_new, 'left':left_new})
															.css({'width':150, 'height':20});
				
				return false;
			}
		}
		});
			jQuery('div.tag-rectangle').mouseout(function(){
				glob_isMouseOverTagArea = false;
				jQuery(this).children().css('border','none');
				jQuery('#tag-area-form-'+current_rectangle).css({'display':'none'}).hide();
				
				var current_html = getCurrentHTML();
				
				jQuery(current_html).imgAreaSelect({
					disable:true,
					hide:true
				});	
		})
}

//
//
function activateTagsLink(){
	jQuery('div.tags-holder > ul > li > a.delete').click(function(){ //init DELETE CLICK
		if(	jQuery('div.tags-holder > ul > li').length < 2){
			jQuery('div.tags-holder').css({'display':'none'});
		}
		var del_block = jQuery(this).attr('rel');
		
		var rel = jQuery(this).parent().children('a.link-tag').attr('rel');
		
		if ( !rel.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i ) ) {
			//error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			return;
		}
		var vars = rel.match(/x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i);
		var coord = {	x1:parseInt(vars[1]), y1:parseInt(vars[2]), x2:parseInt(vars[3]), y2:parseInt(vars[4]) };
		jQuery(this).parent().replaceWith('');
		jQuery('div.tag-rectangle-'+del_block).css('display','none');
		
		var current_html = getCurrentHTML();
		
		var image = jQuery(current_html).attr('src');
		
		if ( !image.match( /(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i ) ){
			//erreor!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			return;
		}
		var vars = image.match(/([A-Za-z0-9\/\-.]*\.(gif|jpg|png|bmp))/i);
		
		var data = 	'x1='+coord.x1
					+'&y1='+coord.y1
					+'&x2='+coord.x2
					+'&y2='+coord.y2
					+'&tag='+jQuery(this).parent().children('a.link-tag').attr('title')
					+'&image='+ image
					+ '&action=delete';

		jQuery.ajax({
			type: "POST",
			url: glob_ajax_url,
			data: data,
			success: function(msg){ alert(msg) },
			error: function(msg) { }
	 	}); 
		
		return false;
	});

	jQuery('div.tags-holder > ul > li > a.link-tag').hover(
		function(){
			var rel = jQuery(this).attr('rel');
			
			if ( !rel.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i ) ) {
				//errore!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
				return;
			}
			var vars = rel.match(/x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+)/i);
			var coord = {	x1:parseInt(vars[1]), y1:parseInt(vars[2]), x2:parseInt(vars[3]), y2:parseInt(vars[4]) };
		
			coord.tag = jQuery(this).attr('title');
			
			var current_html = getCurrentHTML();
				 
			jQuery(current_html).imgAreaSelect({
				show:'true',
				x1:coord.x1,
				y1:coord.y1,
				x2:coord.x2,
				y2:coord.y2,
				selectionOpacity:0,
				outerOpacity:0,
				borderWidth:1,
				movable: false,
				resizable: false
			});
		// show tag text
		var num_of_form = jQuery(this).parent().find('a.delete').attr('rel');
		
		jQuery('#tag-area-form-'+num_of_form).css({'display':'block', 'z-index':'9999'});
		jQuery('#tag-area-'+num_of_form).attr('value',coord.tag);
		var cur_tag_width = jQuery('#tag-area-form-'+num_of_form).width();
		var cur_select_width = coord.x2 - coord.x1;
		var cur_select_height = coord.y2 - coord.y1;
		if (cur_select_width > cur_tag_width){
			var offs = (cur_select_width - cur_tag_width) / 2; 
		}
		else{
			var offs = (cur_tag_width - cur_select_width) / -2;
		};
		var allow_area_h = jQuery(current_html).height();
		var allow_area_w = jQuery(current_html).width();
		
		if ( (coord.x2 > ( allow_area_w - (cur_tag_width - 130) ) ) && cur_select_width < cur_tag_width ) offs = - cur_tag_width + 50;
		if ( (coord.x2 < ( 0 + cur_tag_width ) ) && cur_select_width < cur_tag_width ) offs = 10;
		
		var offs_y = 0;
		if ( coord.y2 > (allow_area_h - 40) ) offs_y = -(cur_select_height+40) ;
		
		var image = jQuery(current_html).offset();					
		
		jQuery('#tag-area-form-'+num_of_form).css({'top':coord.y2+150+offs_y, 'left':coord.x1+image.left+offs})
											.css({'width':150, 'height':20});
		},
		function(){
			var num_of_form = jQuery(this).parent().find('a.delete').attr('rel');
				jQuery('#tag-area-form-'+num_of_form).css({'display':'none'});
				
				var current_html = getCurrentHTML();
				
				jQuery(current_html).imgAreaSelect({
					disable:true,
					hide:true
				});	
			if (glob_isTaggingActive == false){
				jQuery('div.imgareaselect-outer, div.imgareaselect-selection, div.imgareaselect-border1, div.imgareaselect-border2').replaceWith('');
			}
		}
	);
}

// 
//read ajax response based on image path
function initReqestToVars(){
	if ( jQuery('div.photo_main_photo > a > img').length > 0  ){
		var data = jQuery('div.photo_main_photo > a > img').attr('src');
	} else {
		var data = jQuery('div.photo_main_photo > img').attr('src');
	}
	if ( !data.match( /(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i ) ){
		return; //!!!!!!!!!!!!error!!!!!!!!!!!!!!
	}
	var vars = data.match(/([A-Za-z0-9\/\-.]*\.(gif|jpg|png|bmp))/i);
	
	data = 'image=' + vars[1] + '&action=request_date';

	jQuery.ajax({					//ajax, create tag list under image
		type: "POST",
		url: glob_ajax_url,
		data: data,
		success: function(msg){
			var vars = [];
			if ( msg.match( /\((.*)\)/ig ) )
				var vars = msg.match( /\((.*)\)/ig );
			else if ( msg.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+),tag\:(.*)/i ) )
				var vars = msg.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+),tag\:(.*)/i )
			if (vars.length){
				for (var i=0; i < vars.length; i++){
					vars[i] = vars[i].replace('(','');
					vars[i] = vars[i].replace(')','');
					ajaxCreateTagsList(vars[i]);
				}
			}
		},
		error: function(msg) {
			alert('failure');
		}
	});
}
//
//
function ajaxCreateTagsList(data){
	var tags = jQuery('<div></div>');
	tags.addClass('tags-holder');
	
	if ( !data.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+),tag\:(.*)/i ) ) {
		return; //!!!!!!!!!!!!error!!!!!!!!!!!!!!
	}
	
	var vars = data.match( /x1\:([0-9]+)\,y1\:([0-9]+)\,x2\:([0-9]+)\,y2\:([0-9]+),tag\:(.*)/i );
	var request = {	x1:parseInt(vars[1]), y1:parseInt(vars[2]), x2:parseInt(vars[3]), y2:parseInt(vars[4]), tag:vars[5] };
	
	var current_tag = request.tag;
	
	if ( glob_first_init_tag_holder == true ){ //create TAG and HOLDER
		glob_first_init_tag_holder = false;
		var tags = jQuery('<div></div>');
		tags.addClass('tags-holder');
		if (current_tag != ''){
			var match = '/("*)/gi';
			
			current_tag = current_tag.replace('"', "&quot;");
			current_tag = current_tag.replace('"', "&quot;");
			
			tag_holder = '<span>IN THIS PHOTO</span></p><ul>';
			tag_holder  += '<li><a class="link-tag" title="'+ current_tag +'" href="javascript:;" rel="x1:'
							+ request.x1 + ',y1:' + request.y1 + ',x2:' + request.x2 + ',y2:'
							+ request.y2 + '">' + current_tag + '</a>'+
							'<a class="delete" rel="'+glob_tags_counter+'" href="javasctript:;">&nbsp;X&nbsp;&nbsp;</a>, </li>';
			tag_holder += '</ul>';
			tags.append(tag_holder);	
			jQuery('div.photoview_main').after(tags);
		}
		//incl 1---------
		jQuery('div.imgareaselect-border2').after(glob_form_select);
		//incl 1---------		
		
	}
	else{//HOLDER exist. Create TAG.
		var tag_holder = jQuery('div.tags-holder > ul'); 
	   
		if (current_tag != ''){
			var match = '/("*)/gi';
			
			current_tag = current_tag.replace('"', "&quot;");
			current_tag = current_tag.replace('"', "&quot;");
			
			tag_to_enter  = '<li><a style="float:left;" class="link-tag" title="'+ current_tag +'" href="javascript:;" rel="x1:'
							+ request.x1 + ',y1:' + request.y1 + ',x2:' + request.x2 + ',y2:'
							+ request.y2 + '">' + current_tag + '</a>'+
							'<a class="delete" rel="'+glob_tags_counter+'" href="javascript:;">&nbsp;X&nbsp;&nbsp;</a>, </li>';
			tag_holder.append(tag_to_enter);	
		};
	}
	// restore select rectangle area
	var current_html = getCurrentHTML();
  
	jQuery(current_html).imgAreaSelect({//create AREA SELECT
		show:true,
		hide:true,
		x1:request.x1,
		y1:request.y1,
		x2:request.x2,
		y2:request.y2,
		selectionOpacity:0,
		outerOpacity:0,
		borderWidth:1,
		movable: false,
		resizable: false
	});

	jQuery('div.photo_main_photo')
			.append('<div class="tag-rectangle tag-rectangle-'
					+ glob_tags_counter
					+ '"><a title="'
					+ current_tag
					+ '" href="javascript:;">&nbsp;</a></div>');
	jQuery('div.imgareaselect-border2')
		.after("<form action=\"#\" class=\"tag-area-form\" id=\"tag-area-form-"+(glob_tags_counter)+"\">"+
					"<label for=\"tag-area\"></label>"+
					"<div class=\"tag-area-class\">"+
						"<input type=\"text\" name=\"tag-area-"+glob_tags_counter+"\" class=\"tag-area\" id=\"tag-area-"+(glob_tags_counter)+"\" size=\"60\"/>"+
					"</div>"+
					"<input type=\"submit\" value=\"submit\" class=\"submit\">"+
				"</form>");
	//exec 1---------
	//exec 1---------
	
	var top = parseInt(request.y1);
	var left = parseInt(request.x1);
	var width = request.x2 - request.x1;
	var height = request.y2 - request.y1;
	
	jQuery('div.tag-rectangle-' + glob_tags_counter).css({ 
		'display':'block',
		'position':'absolute',
		'width':width,
		'height':height,
		'top':top,
		'left':left
	});
	jQuery('div.tag-rectangle-' + glob_tags_counter + ' > a')
		.attr({
			'rel':'x1:'+ request.x1 + ',y1:' + request.y1 + ',x2:' + request.x2 + ',y2:' + request.y2
		})
		.css('display','block')
		.css('width','100%')
		.css('height','100%')
		.css('text-decoration','none');

	activateTagRectangle( glob_tags_counter++ );
	
	// activate tags link when list is generated
	activateTagsLink();
}

//
//
function initPhotoClick(){
	jQuery("#photo_title").click(function () { 
		setTimeout('clearPhoto()',10);
		setTimeout('initTitle()',10);
	});

	jQuery("#photo_text").click(function () { 
		setTimeout("jQuery('#photo_text_edit').text('').focus()",10);
		setTimeout('initEditor()',10);
	});
	jQuery("#photo_tags").click(function () {
		setTimeout("jQuery('#photo_tags_edit').text('').focus()",10);
		setTimeout('initPhotoTagsEditor()',10);
	});
}
//
//
function initPhotoTagsEditor(){
	if(jQuery('#photo_tags_edit').val() == glob_tag_txt){
		jQuery('#photo_tags_edit').val('');
	}
	jQuery('#photo_tags_edit').blur(function(){
		if(jQuery('#photo_tags_edit').val()==""){
			jQuery('#photo_tags_cancel').trigger('click');
			jQuery('#photo_tags_edit').html(tag_text);
		}
		else{
			jQuery('#photo_tags_save').trigger('click');
		}
	});
}
//
//
function initTitle(){
	jQuery('#photo_title_edit').blur(function(){
		if(jQuery('#photo_title_edit').val()==""){
			jQuery('#photo_title_cancel').trigger('click');
			jQuery('#photo_title').html(glob_photo_save);
		}
		else{
			jQuery('#photo_glob_title_save').trigger('click');
		}
	});
	jQuery('#photo_title_edit').keypress(function (e) {
		if(e.which == 13){
			if(jQuery('#photo_title_edit').val()==""){
				jQuery('#photo_title_cancel').trigger('click');
				jQuery('#photo_title').html(glob_photo_save);
			}
			else{
				jQuery('#photo_glob_title_save').trigger('click');
			}
		}
	});
}

//
//
function initEditor(){
	jQuery('#photo_text_edit').keyup(function(){
		if ( jQuery('#photo_text_edit').val() != '')
			jQuery('#edit_tooltip_descript').css('display','none');
		else
			jQuery('#edit_tooltip_descript').css('display','block');
			
		return false;
	});
	
	jQuery('#photo_text_edit').blur(function(){
		if(jQuery('#photo_text_edit').val()==""){
			jQuery('#photo_text_cancel').trigger('click');
			jQuery('#photo_text').html(glob_title_save);
		}
		else{
			jQuery('#photo_text_save').trigger('click');
		}
	});
	jQuery('#photo_text_edit').keypress(function (e) {
		if(e.which == 13){
			if(jQuery('#photo_text_edit').val()==""){
				jQuery('#photo_text_cancel').trigger('click');
				jQuery('#photo_text').html(desc_save);
			}
			else{
				jQuery('#photo_text_save').trigger('click');
			}
		}
		else{
		}
	});
}

//
//
function clearPhoto(){
	if(jQuery('#photo_title').html() == glob_photo_save||jQuery('#photo_title').html() == glob_photo_upd_ok||jQuery('#photo_title').html() == glob_photo_upd_no){
		jQuery('#photo_title_edit').val('').focus();
	}
	else{
		jQuery('#photo_title_edit').val(jQuery('#photo_title').html()).focus();
	}
}

//
//
function clearTitle(){
	if(jQuery('#photo_text').html() == glob_photo_save||jQuery('#photo_text').html() == glob_photo_upd_ok||jQuery('#photo_text').html() == glob_photo_upd_no){
		jQuery('#photo_title_edit').val('').focus();
	}
	else{
		jQuery('#photo_title_edit').focus().val(jQuery('#photo_text').html());
	}
}

//
//
function taggingOff(){
	glob_isTaggingActive = false;
	jQuery('#photo_main > div.tag-rectangle').css({display:'block'});
	jQuery('#photo-notes').text('');
	jQuery('#tag-area-form-select').remove();
	jQuery('#tagging_menu').parent('li').removeClass('tag_active');
	jQuery('#tagging_menu').parent('li').addClass('tag_inactive');
	// enable link
	jQuery('div.photo_main_photo > img').unbind('click', noClick);
	jQuery('div.photo_main_photo > a').unbind('click', noClick);
	jQuery('div.photo_main_photo > img').imgAreaSelect({
		disable:true,
		hide:true
	});	
	jQuery('div.photo_main_photo > a').imgAreaSelect({
		disable:true,
		hide:true
	});
	jQuery('div.photo_main_photo > a > img').imgAreaSelect({
		disable:true,
		hide:true
	});	
	// back cursor to hand
	jQuery('div.photo_main_photo > a').css('cursor','pointer');
	jQuery('div.imgareaselect-outer').css('cursor','pointer');
}
