
pm_current_suggestion_arr=new Array();


function createSuggestionItem(id,gallery_id,photo_id,isNew){	
	li=document.createElement('div');
	li.setAttribute('id', id);
	//if(val[1]=="1"){
		li_class='positive_annotation suggestion';		

		li.setAttribute('class',li_class);
		/*remove_button=document.createElement('span');
		remove_button.setAttribute('class','dismis_suggestion button');
		remove_button.innerHTML="x"
		$(remove_button).click(function(){
			dismissSuggestion(key,gallery_id,photo_id);
		});
		*/
				
		accept_button=document.createElement('span');
		

		accept_button.setAttribute('class','accept_suggestion button');
		accept_button.innerHTML="✓"
		$(accept_button).click(function(){
			addSuggestion(id,gallery_id,photo_id);
		});

		text=document.createElement('span');
		text.setAttribute('class','button');
//		text.setAttribute('href','/search/'+value[0].replace(/[_-]/g," "));
		text.innerHTML=tag_names[id];
		$(text).click(function(){
			dismissSuggestion(id,gallery_id,photo_id);
		});
				
	
		li.appendChild(accept_button);
		//li.appendChild(remove_button);
		li.appendChild(text);

		if(isNew){
			$(li).fadeIn("slow");
		}	

				
	//}
	return li ;
}


var MAX_SUGGESTIONS_ON_SCREEN=12;
/**
Funkce cte navrhy tagu ze serveru a vytvori k nim patricne UI
*/
function loadSuggestion(gallery_id,photo_id,first_run){
	current_tags_string="";
	count=pm_current_suggestion_arr.length;
	for(var i=0;i<count;i++){
		current_tags_string+=pm_current_suggestion_arr[i][0]+",";
	}
	
	
	output_size=MAX_SUGGESTIONS_ON_SCREEN-pm_current_suggestion_arr.length;
	if(output_size>0){
	$.getJSON("/gallery/"+gallery_id+"/"+  photo_id+"/get_suggestion/"+current_tags_string+"/"+output_size, function(data) {
		progress=document.getElementById('suggestion_progress');
		$(progress).hide();

		tag_root=document.getElementById('suggestion_list');

		//Odstraneni vsech potomku
		try{
			tag_root.removeChild(tag_root.firstChild);
		}catch(e){
		}
		ul=document.createElement('div');
		ul.setAttribute('class', "tags");
		p=document.createElement('p');

		//Vytvoreni html prvkuk jiz navrhnutym tagum
		count=pm_current_suggestion_arr.length;
		for(var i=0;i<count;i++){
			p.appendChild(createSuggestionItem(pm_current_suggestion_arr[i][0],pm_current_suggestion_arr[i][1],gallery_id,photo_id,false));
		}
		
		//Vytvoreni html prvkuk k nove navrhnutym tagum
		$.each(data, function(key, val) {
			p.appendChild(createSuggestionItem(val,gallery_id,photo_id,true));
			pm_current_suggestion_arr.push(new Array(key,val));			
  		});

		next_button=document.createElement('span');
		next_button.setAttribute('class','next_button suggestion');
		title=document.createElement('span');
		title.innerHTML="More suggestions";
		title.setAttribute('class','button');
		next_button.appendChild(title);
		$(next_button).click(function(){
			MAX_SUGGESTIONS_ON_SCREEN+=6;
			loadSuggestion(gallery_id,photo_id);
		});
		p.appendChild(document.createElement('p'));
		p.appendChild(next_button);

		if(first_run){
			$(ul).hide();
			$(ul).fadeIn("slow");
		}
		ul.appendChild(p);
		tag_root.appendChild(ul);

	});
	}
}



function addSuggestion(key,gallery_id,photo_id){
	
	//Remove item from suggestions array
	count=pm_current_suggestion_arr.length;
	for(var i=0;i<count;i++){
		if(pm_current_suggestion_arr[i][0]==key){
			pm_current_suggestion_arr.splice(i,1); 			
			break;
		}	
	}
	addTag(key,gallery_id,photo_id,1);
	li=document.getElementById(key);
	$(li).fadeOut("quick");
}

/**
* Funkce zrusi tag v seznamu navrhu tagu a prida ho do seznamu negativnich anotaci
**/
function dismissSuggestion(key,gallery_id,photo_id){	
	//Remove item from array
	count=pm_current_suggestion_arr.length;
	for(var i=0;i<count;i++){
		if(pm_current_suggestion_arr[i][0]==key){
			pm_current_suggestion_arr.splice(i,1); 
			break;
		}	
	}
	//add negative annotation 
	addTag(key,gallery_id,photo_id,-1);
	//remove html item
	li=document.getElementById(key);
	$(li).fadeOut("quick");
}

function createTagItem(id,gallery_id,photo_id){
	
	if(id>"0"){
		li=document.createElement('div');
		li.setAttribute('id', id);
		li.setAttribute('class','positive_annotation tag');
		remove_button=document.createElement('span');
		remove_button.setAttribute('class','remove_tag button');
		remove_button.innerHTML="x"
		$(remove_button).click(function(){
			remove_tag(id,gallery_id,photo_id);
		});

		text=document.createElement('a');
		text.setAttribute('class','text');
		text.setAttribute('href','/search/'+tag_names[id]);
		text.innerHTML=tag_names[id];
				
		/*if(val[1]=="-1"){
			n=document.createElement('span');
			n.innerHTML=" - ";
			li.appendChild(n);
		}*/
		
		li.appendChild(text);
		li.appendChild(remove_button)
		$(li).fadeIn("slow");
		
		return li;		
	}else return null;
	
}


/**
*load tags from curent photo and create UI
*/
function loadTags(gallery_id,photo_id,first_run){
	progress=document.getElementById('tags_progress');
	$(progress).hide();

	$.getJSON("/gallery/"+gallery_id+"/"+  photo_id+"/get_tags", function(data) {
		tag_root=document.getElementById('tags_list');
		try{
			tag_root.removeChild(tag_root.firstChild);
		}catch(e){
		}

		ul=document.createElement('div');
		ul.setAttribute('class', "tags");
		p=document.createElement('p');
		p.setAttribute('id','tags_list_p');
		
		

		$.each(data["sequence"]["image"]["tags"], function(key, val) {
			if(val>0)
			p.appendChild(createTagItem(val,gallery_id,photo_id));
  		});

		ul.appendChild(p);
		if(first_run){
				$(ul).css('display','none');
				$(ul).fadeIn("slow");
		}
		tag_root.appendChild(ul);
		progress=document.getElementById('tags_progress');
		$(progress).fadeOut("quick");

	});
}


function loadPhoto(gallery_id,photo_id){
	photo_container=document.getElementById('module_photo_img');
	photo_progress=document.getElementById('photo_progress');
	var img = new Image();
	// image onload
	$(img).load(function () {
		// hide first
		$(this).css('display','none'); // since .hide() failed in safari		
		$(photo_progress).css('display','none');
		photo_container.appendChild(this);
		$(this).fadeIn("slow");

		loadTags(gallery_id,photo_id,true);
		loadSuggestion(gallery_id,photo_id,true);
	}).error(function () {
		
	}).attr('src', "/getimagedata/"+ photo_id);
}

///////////////////////// REMOVE
function remove_tag(id,gallery_id,photo_id){
	$(".tag#"+id).fadeOut("slow");
	$.get('remove_tag/'+id, function(){
  		on_remove_tag_cb(id,gallery_id,photo_id);
	});

	tag_root=document.getElementById('tags_list_p');
	tag=$(tag_root).getElementById(id);
	$(tag).fadeOut("slow");
};

function on_remove_tag_cb(id,gallery_id,photo_id){
	/*loadAllTagsAsArray(function(tag_names,tags_dict){		
		all_tags=tags_dict.slice();
		$("#add_tag_text").autocomplete(tag_names);
	});*/
};

///////////////////////////// ADDD

//Callback funkce, ktera se zavola po provedeni pridani tagu
function on_send_tags_cb(gallery_id,photo_id){
	tag_root=document.getElementById('tags_list');
	
	request_processing=false;

	//Pokud je v bufferu neco "nabuffrovano"
	if(request_buffer.length!=0){
		sendTags(gallery_id,photo_id);
	}else{
		loadSuggestion(gallery_id,photo_id);
	}
}

function sendTags(gallery_id,photo_id){
	request_processing=true;
	tags=""

	//Vytvoreni seznamu tagu na pridani 
	for(var i=0 ;i<request_buffer.length;i++){
		tags=tags+request_buffer[i]+",";		
	}
	
	//erase buffer
	request_buffer.splice(0,request_buffer.length);

	$.get('add_tags/'+tags, function(){
		on_send_tags_cb(gallery_id,photo_id);
	});
}

/**
//Funkce na pridani noveho tagu
//Pozadavky se bufferuji a posilaji hromadne
*/
function addTag(tag_id,gallery_id,photo_id,type){
	var tag_root=document.getElementById('tags_list_p');
	var elements = tag_root.childNodes;
	var tag=tag_id+"";
	//Kontrola zda TAG uz neni pozitivne vybran
	for(var i=0;i<elements.length;i++){
		var elem=elements[i];
		//Pokud je tak se v seznamu zvyraznim a jinak se nestane nic
		if(elem.getAttribute("id")==tag){
			$(elem).hide();
			$(elem).fadeIn("slow");
			return;
		}
	}


	if(type==1){
		request_buffer.push(tag_id);

		key=tag_id;
		val=new Array(all_tags[key],"1");
		//alert(key+":"+val);
		
		tag_root.appendChild(createTagItem(key,val,gallery_id,photo_id));
	}else{
		request_buffer.push("-"+tag_id);
	}
	if(!request_processing){
		setTimeout(function(){sendTags(gallery_id,photo_id);},2000);

	}
	
}

function removeCurrentPostiveTagFromTagNames(tag_names){
	var tag_root=document.getElementById('tags_list_p');
	var elements = tag_root.childNodes;
	var tag=tag_id+"";
	//Kontrola zda TAG uz neni pozitivne vybran
	for(var i=0;i<elements.length;i++){
		var elem=elements[i].firstChild();
//		tag_names.remove(elem.getAttribute("id"));		
	}
}

/////////////////////////// NEW
function on_new_tag_cb(tag_name,gallery_id,photo_id){
	loadAllTagsAsArray(function(tag_names){
		$("#add_tag_text").autocomplete(tag_names);
		tag_id=all_tags.indexOf(tag_name);
		if(tag_id!=-1){
			addTag(tag_id,gallery_id,photo_id,1);
		}else{
			alert("chyba");
		}
	});
}

function newTag(tag_name,gallery_id,photo_id){
	$.get('/new_tag/'+tag_name.replace(" ","_"), function(){
  		on_new_tag_cb(tag_name,gallery_id,photo_id);
	});
}

/** Pole se vsemy TAGY v DB
*/
var all_tags=new Array();
var tag_names;


/**rika zda se zpracovava pozadavek na serveru*/
var request_processing=false;
var request_buffer=new Array();

function initPhotoModul(gallery_id,photo_id){
	request_processing=false;
	loadPhoto(gallery_id,photo_id);	
	

	text_box=document.getElementById('add_tag_text');
	$('#tag_add_button_positive').click(function(){
		id=0;
		tag_name=text_box.value;
		tag_id=all_tags.indexOf(tag_name);
		if(tag_id!=-1){
			addTag(tag_id,gallery_id,photo_id,1);	
		}else{
			newTag(tag_name,gallery_id,photo_id);
		}	   	
 	});
	
	loadAllTagsAsArray(function(tag_nms,tags_dict){		
		all_tags=tags_dict.slice();
		//removeCurrentPostiveTagFromTagNames(tag_names);
		$("#add_tag_text").autocomplete(tag_names);						
		tag_names=tag_nms;
		
	});

}

