/* Categories, Departments */

var CATEGORY = 'Category';
var DEPARTMENT = 'Department';
img_remove = '<img src="/media/images/remove.gif" class="img_remove"/>';

function selected(value, id, type){
	if (type == CATEGORY){
		$("#id_categories > option").each(function(i, obj){
			if ($(obj).attr('text') == value){
				if ($(obj).attr('selected') == false){
					$(obj).attr('selected', true);
					$('#results').append('<li class="result_item" id="result'+type+'_'+id+'">'+value+' ('+type+')<a href="javascript:unselected('+ "'" + value + "'" +', '+ "'" + id + "'"+', '+ "'" + type + "'"+');">'+img_remove+'</a></li>');
				}
			}
		});
	};
	if (type == DEPARTMENT){
		$("#id_departments > option").each(function(i, obj){
			if ($(obj).attr('text') == value){
				if ($(obj).attr('selected') == false){
					$(obj).attr('selected', true);
					$('#results').append('<li class="result_item" id="result'+type+'_'+id+'">'+value+' ('+type+')<a href="javascript:unselected('+ "'" + value + "'" +', '+ "'" + id + "'"+', '+ "'" + type + "'"+');">'+img_remove+'</a></li>');
				}
			}
		});
	};
	
}

function unselected(value, id, type){
	if (type == CATEGORY){
		$("#id_categories > option").each(function(i, obj){
			if ($(obj).attr('text') == value){
				if ($(obj).attr('selected') == true){
					$(obj).attr('selected', false);
					$("#result"+type+'_'+id).remove();
				}
			}
		});
	};
	if (type == DEPARTMENT){
		$("#id_departments > option").each(function(i, obj){
			if ($(obj).attr('text') == value){
				if ($(obj).attr('selected') == true){
					$(obj).attr('selected', false);
					$("#result"+type+'_'+id).remove();
				}
			}
		});
	};
	
}


/* Tags */

function select_tag(value, id){
	$("#id_tags > option").each(function(i, obj){
		if ($(obj).attr('text') == value){
			if ($(obj).attr('selected') == false){
				$(obj).attr('selected', true);
				$('#results_tags').append('<li class="result_item" id="result_tag_'+id+'">'+value+' <a href="javascript:unselect_tag('+ "'" + value + "'" +', '+ "'" + id + "'" +');">'+img_remove+'</a></li>');
			}
		}
	});
}

function unselect_tag(value, id){
	$("#id_tags > option").each(function(i, obj){
		if ($(obj).attr('text') == value){
			if ($(obj).attr('selected') == true){
				$(obj).attr('selected', false);
				$("#result_tag_"+id).remove();
			}
		}
	});	
}



/* Document Ready */

$(document).ready(function(){
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	var values = [];
	var values_departments = [];
	var values_categories = [];
	var values_tags = [];
	
	$("#id_categories option:selected").each(function(i,obj){
		if ($(obj).attr('text') == ''){
			$(obj).attr('selected', false);
		}else{
			$('#results').append('<li class="result_item" id="result'+CATEGORY+'_'+$(obj).attr('value')+'">'+$(obj).attr('text')+' ('+ CATEGORY +')<a href="javascript:unselected('+ "'" + $(obj).attr('text') + "'" +', '+ "'" + $(obj).attr('value') + "'"+', '+ "'" + CATEGORY + "'"+');">'+img_remove+'</a></li>');					
		}
	});
	
	$("#id_departments option:selected").each(function(i,obj){ 
		if ($(obj).attr('text') == ''){
			$(obj).attr('selected', false);
		}else{
			$('#results').append('<li class="result_item" id="result'+DEPARTMENT+'_'+$(obj).attr('value')+'">'+$(obj).attr('text')+ ' ('+ DEPARTMENT +')<a href="javascript:unselected('+ "'" + $(obj).attr('text') + "'" +', '+ "'" + $(obj).attr('value') + "'"+', '+ "'" + DEPARTMENT + "'"+');">'+img_remove+'</a></li>');					
		}
	});

	$("#id_tags option:selected").each(function(i,obj){ 
		if ($(obj).attr('text') == ''){
			$(obj).attr('selected', false);
		}else{
			$('#results_tags').append('<li class="result_item" id="result_tag_'+$(obj).attr('value')+'">'+$(obj).attr('text')+' <a href="javascript:unselect_tag('+ "'" + $(obj).attr('text') + "'" +', '+ "'" + $(obj).attr('value') + "'"+');">'+ img_remove +'</a></li>');
		}
	});

	$("#id_categories").attr('multiple', 'multiple');
	$("#id_departments").attr('multiple', 'multiple');
	$("#id_tags").attr('multiple', 'multiple');
	$("#id_categories > option").each(function(i, obj){ values_categories[i]= [$(obj).attr('text'), $(obj).attr('value'), CATEGORY] });
	$("#id_departments > option").each(function(i, obj){ values_departments[i]= [$(obj).attr('text'), $(obj).attr('value'), DEPARTMENT] });
	$("#id_tags > option").each(function(i, obj){ values_tags[i]= [$(obj).attr('text'), $(obj).attr('value')] });
	values = values_departments.concat(values_categories);
	values = values.sort();
	values_tags = values_tags.sort();

	// load categories and departments in windows
	for (i=0; i<values.length; i++){
		value = values[i];
		if (value[0]){
			$('#list_window_content').append("<a href='javascript:selected("+'"'+value[0]+'"'+","+'"'+value[1]+'"'+","+'"'+value[2]+'"'+");$.prettyPhoto.close();'><div class='item_list'>"+ value[0] +" ("+ value[2] +")"+"</div></a>");
		}	
	};

	// load tags
	for (i=0; i<values_tags.length; i++){
		value = values_tags[i];
		if (value[0]){
			$('#list_window_content_tags').append("<a href='javascript:select_tag("+'"'+value[0]+'"'+","+'"'+value[1]+'"'+");$.prettyPhoto.close();'><div class='item_list'>"+ value[0] +"</div></a>");
		}	
	};

	
	/* autocomplete departments/categories */

	$("#input_text").autocompletex(values, {
		minChars: 0,
		max: 50,
		autoFill: true,
		mustMatch: true,
		matchContains: true,
		scrollHeight: 220,
		formatItem: function(data) {
			return data[0] + " - " + data[2]; 
		}
	});

	function findValueCallback(event, data, formatted) {
		if (data){
			$("#input_text").attr('value', '');
			selected(data[0], data[1], data[2]);
		}
	}
	$("#input_text").result(findValueCallback).next().click(function() {
		$(this).prev().search();
	});
	
	/* autocomplete tags */

	$("#input_tags").autocompletex(values_tags, {
		minChars: 0,
		max: 50,
		autoFill: true,
		mustMatch: true,
		matchContains: true,
		scrollHeight: 220,
		formatItem: function(data) {
			return data[0]; 
		}
	});

	function findValueCallbackTag(event, data, formatted) {
		if (data){
			$("#input_tags").attr('value', '');
			select_tag(data[0], data[1]);
		}
	}
	$("#input_tags").result(findValueCallbackTag).next().click(function() {
		$(this).prev().search();
	});			
	
});


