eksFunc.prototype.site = new eksSite();
function eksSite(){
	
}
//rocounting each value in krepselis
eksSite.prototype.recountBasket = function(product_id, new_count, form_id){
	form = $("#" + form_id + "_" + product_id);
	form.submit();
}

eksSite.prototype.switchDivs = function(divs_object){
	divs = $(divs_object);
	divs.each(function(i){
		div = $(this);
		if(div.css('display') == "none"){
			div.show();
		}
		else{
			div.hide();
		}
	});
}

eksSite.prototype.updateSubCategoryList = function(identifier, parent_id){
	element = $(identifier);
	$.post('search/categorylist/', 
	{
		'parent_id' : parent_id
	}
	,function(response){
		element.empty();
		if(response.status == "ok"){
			for(i = 0; i < response.data.length; i++){
				element.append("<option value='" + response.data[i].id + "'>" + response.data[i].pavadinimas + "</option>");
			}
		}
		else{
			element.append("<option value=''>Visi</option>");
		}
	},"json");
	
}

eksSite.prototype.geogoerDropdown = function(identifier){
	element = $(identifier);
	
	button = element.find("span.button");
	block = element.find("div.block");
	
	$(button).bind('mouseover', function(e){
		$(this).addClass("button_hover");	
	});
	$(button).bind('mouseout', function(e){
		$(this).removeClass("button_hover");	
	});
	$(button).bind('click', function(e){
		$(this).parent(identifier).addClass("geogoer_dropdown_active");
	});
}

eksSite.prototype.menubar = function(identifier){
	element = $(identifier);
	

	
	//horizontal span click
	$('span.menubar_hor').bind('click', function(e){
		if($(this).next('div.menubar_hor_childs').css('display') == "none"){
			$(this).next('div.menubar_hor_childs').show();	
		}
		else{
			$(this).next('div.menubar_hor_childs').hide();
		}
	});
	
	//horizontal span mouseover
	$('span.menubar_hor').bind('mouseover', function(e){
		if(eks().site.menubarIsOpened(false)){
			$('div.menubar_hor_childs').each(function(i){
				$(this).hide();
			});
			$(this).next("div.menubar_hor_childs").show();		
		}
		$("span.menubar_hor").removeClass("menubar_hor_hover");
		$(this).addClass("menubar_hor_hover");
	});
	
	//horizontal span mouseout
	$('span.menubar_hor').bind('mouseout', function(e){
		if(!eks().site.menubarIsOpened(false)){
			$(this).removeClass("menubar_hor_hover");
		}
	});
	
	//horizontal chhilds mouseover
	$('div.menubar_hor_childs').bind('mouseover', function(e){
		$(this).prev("span.menubar_hor").addClass("menubar_hor_hover");
	});
	
	//vertical span mouseover
	$('span.menubar_ver').bind('mouseover', function(e){
		$("span.menubar_ver").removeClass("menubar_ver_hover");
		$(this).addClass("menubar_ver_hover");
		content_div = $(this).next("div.menubar_ver_childs");
		content_div.show();
	});
	
	//vertical span click
	$('span.menubar_ver').bind('click', function(e){
		content_div = $(this).next("div.menubar_ver_childs");
		if(content_div[0]){
			if(content_div.css('display') == "block"){
				content_div.hide();
			}
			else{
				content_div.show();
			}
		}
		else{
			$("div.menubar_ver_childs").hide();
			$("div.menubar_hor_childs").hide();
		}
	});
	
	//vertical span mouseout
	$('span.menubar_ver').bind('mouseout', function(e){
		content_div = $(this).next("div.menubar_ver_childs");
		content_div.hide();
	});
	
	//vertical childs mouseover
	$('div.menubar_ver_childs').bind('mouseover', function(e){
		$(this).show();
	});
	
	//vertical childs mouseout
	$('div.menubar_ver_childs').bind('mouseout', function(e){
		$(this).hide();
	});
}
eksSite.prototype.menubarIsOpened = function(element){
	opened = false;
	if(!element){
		top_level_childs = $('div.menubar_hor_childs');
		for(i = 0; i < top_level_childs.length; i++){
			if($(top_level_childs[i]).css("display") == "block"){
				opened = true;
			}	
		}
	}
	else{
		if(element.next("div.menubar_hor_childs").css('display') == "block"){
			opened = true;
		}
	}
	return opened;
}

$(document).ready(function(){

	$("#login")
			.focus
			(
				function() 
				{
					if( this.value == "Prisijungimo vardas" ) 
					{
						this.value = "";
					}
				}
			)
			.blur
			(
				function() 
				{
					if( !this.value.length ) 
					{
						this.value = "Prisijungimo vardas";
					}
				}
			);
				
	$("#pass")
			.focus
			(
				function()
				{
					this.value = "";
				}
			);
	
	$("#check_all_vendors")
			.click
			(
				function()
				{
				
					$("#fGamintojas").prepend("<img id=\"loaderis\" src=\"images/loading.gif\" alt=\"\" align=\"left\" />");
					
					var gamintojai = $(".vendor_checkbox");
					
					if ( $(this).html() == "Nužymėti visus" )
					{						
						gamintojai.removeAttr("checked");
						$(this).html("Pažymėti visus");
						start_filter();
					}
					else
					{						
						gamintojai.attr("checked", "checked");
						$(this).html("Nužymėti visus");
						start_filter();
					}
					
					
						var filtrai = "";
						  					
					if ( $(".gamintojas[@checked]") )
					{
						$(".gamintojas[@checked]").each
						(
							function(i)
							{
						 		if ( i > 0 ) filtrai = filtrai + "&";
   								filtrai = filtrai + $(this).attr("name") + "=";
   							}
   						);
   					}
					 	$.post("<?=Conf::SITE_URL?>Prekes/index/<?=$this->tevine?>/<?=$this->dukterine?>/", "filtruoti=1&" + filtrai, function(data){ 
					 	$("#produktai").html(data); $("#prekiu_kiekis").text($("#vid_kiekis").attr("value")); 
					 	
					 	if ( $(".gamintojas").length == $(".gamintojas[@checked]").length || $(".gamintojas[@checked]").length == 0 )
					 	{
					 		$("#filtro_pav").text("visi");
					 	}
					 	else if ( $(".gamintojas[@checked]").length == 1 )
					 	{
					 		$("#filtro_pav").text( $(".gamintojas[@checked]").attr("name").replace("_", " ") );
					 	}
					 	else
					 	{
					 		$("#filtro_pav").text("keli");
					 	}
					 	
					 	$("#loaderis").remove();
					 	
					 	}, "html");	
					
				}			
			);
	$("#paieska")
			.focus
			(
				function() 
				{
					if( this.value == "Paieška" ) 
					{
						this.value = "";
					}
				}
			)
			.blur
			(
				function() 
				{
					if( !this.value.length ) 
					{
						this.value = "Paieška";
					}
				}
			);
	$(".price")
			.focus
			(
				function() 
				{
					if( this.value == "0" ) 
					{
						this.value = "";
					}
				}
			)
			.blur
			(
				function() 
				{
					if( !this.value.length ) 
					{
						this.value = "0";
					}
				}
			);
							
});
