  var isAnimated = false;
  var isRollDown = false;
  var isLoaded = false;
  var prevShortcut = "";
  //wspolczynniki
  var chkCoefficients = new Array();
  var txtCoefficients = new Array();
  var fldCoefficients = new Array();
  var defCoefficients = new Array();

function rollCriteria() {
	if ($("#result #t_criteria").hasClass("rollDown") == true) {
		$("#result #t_criteria").slideUp("fast");
		$("#result #t_criteria").removeClass("rollDown");
	} else {
		$("#result #t_criteria").slideDown("fast");
		$("#result #t_criteria").addClass("rollDown");
	}
}
  
function boxAnim(roller,cos) {	
if (!isAnimated) {	
	if ($(roller).hasClass("rollUp") == false) {

		$(roller).parent("div[class=navigation]").find("a[class=refresh]").css("display","none");
		$("#"+cos).attr("animated","yes");
		isAnimated = true;
		//$("#"+cos).parent("div").find("div[class=box]").not("#"+cos).hide("fast");
		$(roller).addClass("rollUp");
		$(roller).html('zwiń');
		var mseWidth = $("#mseContainer").width();
		$("#"+cos).css('position', 'absolute');
                $("#"+cos).css('z-index', '99');
                $("#"+cos).animate({
                        width: "97%",
			height: "150px",
			opacity: "1.0",
			marginLeft: "2px",
			fontSize: "11px",
			borderTopWidth: "1px",
			borderLeftWidth: "1px",
			borderRightWidth: "1px",
			borderBottomWidth: "3px",                        
                        left: "17px"
		}, "fast", function() {
			$("#"+cos+"Content").css("overflow","auto");
			isAnimated = false;	
			});

	} else {		
		isAnimated = true;
		$(roller).removeClass("rollUp");
		$(roller).html('więcej');		
		$("#"+cos).attr("animated","no");
		$("#"+cos+"Content").css("overflow","hidden");
                $("#"+cos).css('position', 'static');
                $("#"+cos).css('z-index', '0');
		$("#"+cos).animate({
			width: "190px",
			height: "150px",
			opacity: "1.0",
			marginLeft: "2px",
			fontSize: "11px",
			borderTopWidth: "1px",
			borderLeftWidth: "1px",
			borderRightWidth: "1px",
			borderBottomWidth: "3px"

		}, 1000, "swing", function() {						
			//$("#"+cos).parent("div").find("div[class=box]").not("#"+cos).show("fast");
			isAnimated = false;
			$(roller).parent("div[class=navigation]").find("a[class=refresh]").css("display","block");
			});
	}
} 	
}

function strip_tags(text){
 return text.replace(/<\/?[^>]+>/gi, ''); 
}

function fldModifier(sender) {
	var n = fldCoefficients.length;
	var i = 0;
	var isExists = false;
	var value = $(sender).val();
	
	if (sender.checked == true) {
		if (n == 0) {
			fldCoefficients.push(value);
		} else {
			while (i < n) {
			if (fldCoefficients[i] == value) {					
				isExists = true;
				break;
			}
			i++;
			}
			if (!isExists) {
				fldCoefficients.push(value);		
			}
		}				
	} else {
		while (i < n) {
		if (fldCoefficients[i] == value) {					
			fldCoefficients.splice(i,1);
			break;
		}
		i++;
		}					
	}	
}

function txtModifier(sender) {
	var n = txtCoefficients.length;
	var i = 0;
	var s = '!#/';
	var isFound = false;
	var value = strip_tags($(sender).val());
	
	var tn = $(sender).attr("tn");
	var fd = $(sender).attr("fd");	
	var newValue = tn+s+fd+s+value;
	
		if (n == 0) {
			txtCoefficients.push(newValue);
		} else {
			while (i < n) {
			var tmp = new Array();
			tmp = txtCoefficients[i].split(s);	
			if (tmp[0] == tn && tmp[1] == fd) {					
				if (value != "" && value != tmp[2]) {
					//alert(value);
					tmp[2] = value;
					txtCoefficients[i] = tmp.join(s);
					isFound = true;
					break;
				} else if (value == "") {
					//alert(value);
					txtCoefficients.splice(i,1);
					isFound = true;
					break;
				}				
			}
			i++;
			}
			if (!isFound) {
				txtCoefficients.push(newValue);		
			}
		}

		var tab2 = new Array();
		var tab3 = new Array();
		for (i=0; i<txtCoefficients.length; i++) tab2[txtCoefficients[i]]=true;
		for (element in tab2) tab3[tab3.length]=element;
		txtCoefficients = tab3;
}

function chkModifier(sender) {
	var n = chkCoefficients.length;
	var i = 0;
	var s = '!#/';
	var isFound = false;
	var value = $(sender).val();
	var tn = $(sender).attr("tn");
	var fd = $(sender).attr("fd");	
	var newValue = tn+s+fd+s+value;
	
	if (sender.checked == true) {
			chkCoefficients.push(newValue);
	} else {
		while (i < n) {
		if (chkCoefficients[i] == newValue) {					
			chkCoefficients.splice(i,1);
			break;
		}
		i++;
		}					
	}	
}

function defModifier(sender) {
	var n = defCoefficients.length;
	var i = 0;
	var s = '!#/';
	var isFound = false;
	var value = $(sender).val();
	var tn = $(sender).attr("tn");
	var fd = $(sender).attr("fd");	
	var newValue = tn+s+fd+s+value;
	
	if (sender.checked == true) {
			defCoefficients.push(newValue);
	} else {
		while (i < n) {
		if (defCoefficients[i] == newValue) {					
			defCoefficients.splice(i,1);
			break;
		}
		i++;
		}					
	}	
}

function parseURL()
    {
      var str =window.location.search;
      if(parseURL.arguments.length == 1)
        str = parseURL.arguments[0];
      if(str.length < 2)
        throw new Error('No aruments in URL');
      var arr = str.substring(1).split('&');
      var key = '';
      var val = '';
      for(var part in arr)
      {
        key = arr[part].split('=')[0];
        val = unescape(arr[part].substring(key.length + 1));
        this[key] = val;
      }
    }
    

function sendQuery(page) {	
	$("#mseLoader").addClass("loading");
	$("#searchEngine #result").html("");
	$("#searchEngine #result").addClass("load_result");
	//$("#searchEngine #result").animate({height: "50px"},1000);
	//$("#searchEngine #result").css("height","50px");
	//$("#searchEngine #result").slideDown("slow");
	var _GET = new parseURL();
	$.post("searchHandler.php", {	"action": "buildQuery",
									"menu_id": _GET['menu'],
									"page": page,
									"lang": _GET['lang'],
							"chkCoefficients[]": chkCoefficients, 
							"defCoefficients[]": defCoefficients, 
							"txtCoefficients[]": txtCoefficients, 
							"fldCoefficients[]": fldCoefficients
						},
  			function(data){ 
    			//$("#searchEngine #result").animate({height: "0px"},1000);
    			//$("#searchEngine #result").slideUp("slow");
    			$("#mseLoader").removeClass("loading");
    			$("#searchEngine #result").removeClass("load_result");
    			$("#searchEngine #result").html(data);
    			$("#content").find("div[class=prodMenu]").css("display","none");
    			$("#content").find("ul[class=list2]").css("display","none");
  	});
}

function onMouseAction() {
	var boxSize = {};
	
	$("#mseContainer").find("div[class=box]").mouseover(function() {	
		//$("#refresh").fadeOut("slow"); efekt migania
		var isAnimated = $(this).attr("animated");
		if (isAnimated != "yes") {
		prevShortcut = $(this).attr("id");
		var boxPos = $(this).position();
		boxSize.w = $(this).width();
		boxSize.h = $(this).height();
		var t = (boxPos.top + boxSize.w)-65;
		var l = (boxPos.left + boxSize.h)-10;
		$(this).find("a[class=refresh]").css("top",t+"px");
		$(this).find("a[class=refresh]").css("left",l+"px");		
		$(this).find("a[class=refresh]").css("display","block");
		} 
	});
	$("#mseContainer").find("div[class=box]").mouseout(function() {		
		$(this).find("a[class=refresh]").css("display","block");
		$("#"+prevShortcut).find("a[class=refresh]").css("display","none");
	});
}

function mseLoader() {	
	if (!isRollDown) {
		$("#mseClient").show("fast");									
		isRollDown = true;
		isAjax = false;
		if (!isLoaded) {
		isAjax = true;
		var _GET = new parseURL();
			$.ajax({ 
				method: "get", url: "searchHandler.php", data: "action=generateBoxes&menu_id="+_GET['menu']+'&lang='+_GET['lang'],
				beforeSend:	function() { 
				$("#mseLoader").addClass("loading");},
				complete: function() {  $("#mseLoader").removeClass("loading");},
				success: function(html) {
					//$("#searchEngine #mseClient").html(html);
					var elem = document.getElementById("mseClient").innerHTML = html;
					//$("#sidebar").next("h2").css("display","none");
					//$("#sidebar ul[class=list]").css("display","none");					
					//$("sidebar ul[class=list4]").css("display","none");
					$("#menu_fld").css("display","none");
					$("#folder_grp").css("display","none");
					$("#mseLoader a").addClass("windowHide");
					$("#mseClient").show("fast");
					isLoaded = true;
					onMouseAction();
					}					
			});
		}
		if (!isAjax) {
            $("#mseLoader a").addClass("windowHide");
		}
	} else {
		$("#mseClient").hide("fast");
		$("#mseLoader a").removeClass("windowHide");
		isRollDown = false;
	}
}

function enterSendQuery(event,sender) {
    if ((event.which && event.which == 13) ||
    (event.keyCode && event.keyCode == 13)) {
        txtModifier(sender);
		sendQuery();
    } else {
		return true;
    }
}
