﻿function InitializeShopSearch()
{
	$("#search-data-content > .button a").bind("click", function(event){
		event.preventDefault();
		document.location = "/vysledky-vyhledavani/default.aspx?shopstext=" + encodeURI($("#search-data-content > input").val());
	});
	$("#search-data-content > input").bind($.browser.mozilla ? 'keypress' : 'keydown', function(event){
		var code=event.charCode || event.keyCode;
		if(code && code == 13) {
  			event.preventDefault();
			document.location = "/vysledky-vyhledavani/default.aspx?shopstext=" + encodeURI($(this).val());
		};
	});
};

//****************************************
function InitializeShopTabs()
{
	var Containers 	= $(".tab-container");
	if(Containers.length>1)
	{
		Containers.eq(0).addClass("tab-container-left");
	}

	var MaxHeight = 0;
	if($("#shop-items-related").length>0)
	{
		MaxHeight = $("#shop-items-related").height();
		$("#shop-items-related").appendTo(".tab-container:eq(0) > .tab-container-data");
		$(".tab-container:eq(0) > .tab-container-header > .tab:last").removeClass("tab-last");
		$(".tab-container:eq(0) > .tab-container-header > .tab:last").after($("<div class='tab tab-last'>Doporučujeme" + (Containers.length>1 ? "" : " přikoupit") + "</div>"))
	}

	var Tabs 	= $(".tab-container:eq(0) > .tab-container-header > .tab");
	var Panels 	= $(".tab-container:eq(0) > .tab-container-data > .tab-data");

	Tabs.bind("click", function(){
		var Current = Tabs.index(this);
		Tabs.removeClass("tab-active");
		Tabs.eq(Current).addClass("tab-active");
		Panels.removeClass("tab-data-active");
		Panels.eq(Current).addClass("tab-data-active");

		var MHeight = $("#shop-items-related").height();
		var AllPanels = $(".tab-container > .tab-container-data > .tab-data");
		AllPanels.each(function(){
			$(this).css({"height": "auto"});
			MHeight = Math.max($(this).height(), MHeight);
		});
		AllPanels.each(function(){
			$(this).css({"height": MHeight});
		});
	});

	var AllPanels = $(".tab-container > .tab-container-data > .tab-data");

	AllPanels.each(function(){
		MaxHeight = Math.max($(this).height(), MaxHeight);
	});
	AllPanels.each(function(){
		$(this).css({"height": MaxHeight});
	});

	if($(".shop-item-clip-detail").length==0)
	{
		$(".tab-container-right").hide();
	}
};
//****************************************
function InitializeShopParamSearch()
{
	$(".form-search-item-type select > option[value='0']").text("nerozhoduje");
	$(".form-search-operator select > option[value='?']").text("nerozhoduje");
	$(".form-search-operator select > option[value='=']").text("je přesně");
	$(".form-search-operator select > option[value='<>']").text("je různo od");
	$(".form-search-param select > option[text='(neuveden)']").remove();
	$(".form-search-item-type select").bind("change", function(){
		ChangeSearchFormItemType();
	});
};

//****************************************
$(document).ready(function(){
			InitializeShopSearch();
			InitializeShopParamSearch();
			InitializeShopTabs();
});

$(window).load(function(){
	ResizeColumns();
});

function ResizeColumns()
{
	var MaxColHeight = Math.max($("#page-data-lcol").innerHeight(), $("#page-data-rcol").innerHeight()+20);
	$("#page-data-lcol").css({"height":MaxColHeight});
}


//****************************************
function ChangeSearchFormItemType(){
	var SearchParams 	= document.location.search.replace("?", "").split("&");
	var ReplaceParams 	= false;
	var ItemType		= $(".form-search-item-type select").val();
	for(var i = 0; i < SearchParams.length; i++)
	{
		if(SearchParams[i].indexOf("id_sitype=") == 0)
		{
			ReplaceParams 	= true;
			SearchParams[i] = "id_sitype=" + ItemType;
			break;
		}
	}

	var Query = "";
	for(var i = 0; i < SearchParams.length; i++)
	{
		if(Query.length == 0)
		{
			Query = SearchParams[i];
		}else{
			Query = Query + "&" + SearchParams[i];
		}
	}
	if(!ReplaceParams)
	{
		if (Query.length == 0){
			Query = "id_sitype=" + ItemType;
		}else{
			Query = Query + "&" + "id_sitype=" + ItemType;
		}
	}
	document.location = document.location.pathname + "?" + Query;
}

//****************************************
function ShowBuyInfo(id_shopitem, count, specdesc)
{
	//
}