var isVisible = false;
var lastDiv = "";
var imageList = [];

$(document).ready(function(){

	if(imageList['list'] != undefined && imageList['list'].length > 0){

		$.each(imageList['list'].split(','), function(k, v){

			if($("#"+v+"").length == 0) alert($.language("Missing header id")+" \""+v+"\"");
			$("#"+v+"").append('<a href="#" class="filter-img-button" id="filter-img-'+v+'" style="margin:0px;margin-left:5px;padding:0px;"><img src="/templates/'+_template.name+'/images/admin/filter.png" style="border:0px;height:10px;"/></a>');
		});
	}
	if(imageList['selected'] != undefined && imageList['selected'].length > 0){

		$.each(imageList['selected'].split(','), function(k, v){

			$("#"+v+"").append('<a href="#" class="filter-img-button" id="filter-img-'+v+'" style="margin:0px;margin-left:5px;padding:0px;"><img src="/templates/'+_template.name+'/images/admin/filter_selected.png" style="border:0px;height:10px;"/></a>');
		});
	}

	$(".filter-img-button").click(function(){

		var curElem = $(this).attr("id").replace("filter-img-", "");
		var elem = "."+$(this).attr("id").replace("filter-img-", "filter-div-");
		var toElem = "#"+$(this).attr("id");
		$(".filter-div-current").remove();

		if(lastDiv != curElem) isVisible = false;

		if( isVisible == false ){

			//var close = '<img src="/templates/project/images/admin/close_enabled.png" style="display:block;float:right;margin-right:-11px;margin-top:-11px;margin-left:15px;width:15px;cursor:pointer;" 				class="filter-img-close"/>';
			var close = "";
			var html = '<input type="submit" name="filter_search" value="'+$.language('set filter')+'" style="margin-left:5px;">';
			html += '<a href="#" style="display:block;margin-top:-3px;margin-left:4px;float:right;" class="filter-clear"><img src="/templates/project/images/admin/close_red.png"/></a>';

			$(toElem).after('<div class="filter-div-current" style="position:absolute;background:white;padding:10px;padding-bottom:5px;border:1px solid lightgrey;">'+close+$(elem).html()+html+'</div>');

			var pos = $(toElem).position();
			var pos2 = $(".filter-div-current").position();
			var margin = pos.left-pos2.left;
			$(".filter-div-current").css({"margin-left" : margin+"px", "margin-top" : "5px"});
			isVisible = true;
		}
		else{

			isVisible = false;
		}
		lastDiv = curElem;
		return false;
	});

	$(".filter-img-close").live("click", function(){$(".filter-div-current").remove();isVisible = false;});

	$(".filter-clear").live("click", function(){

		var list = "";
		$(".filter-input", ".filter-div-current").find("input,select,textarea").each(function(){

			var elem = this;
			name = $(elem).attr("name");
			list += "&"+name+"=";
		});
		window.location.href = $.linkJSAppend("?filter=true"+list);
		return false;
	});

	$("[name=filter_search]").live("click", function(){

		var list = "";
		var name = "";
		var value = "";
		var multi = false;

		$(".filter-input", ".filter-div-current").find("input,select,textarea").each(function(){

			var elem = this;
			name = $(elem).attr("name");
			if( list.indexOf("&"+name+"=") == -1 )
				list += "&"+name+"={"+name+"}";

			if( $(elem).attr("type") != undefined && ($(elem).attr("type").toLowerCase() == "checkbox" || $(elem).attr("type").toLowerCase() == "radio") ){

				if( $(elem).attr("checked") != undefined && $(elem).attr("checked").toLowerCase() == "checked" ){

					value += value ? ","+$(elem).val() : $(elem).val();
					multi = true;
				}
			}
			else if( elem.tagName.toUpperCase() == "SELECT" ){

				value += value ? ","+$(elem).val() : $(elem).val();
				multi = true;
			}
			else{

				value = $(elem).val();
				list = list.replace("{"+name+"}", value);
			}
		});
		if( multi == true )
			list = list.replace("{"+name+"}", "["+value+"]");

		window.location.href = $.linkJSAppend("?filter=true"+list);
		return false;
	});
});