function saveSessionInfo(type,id)
{
	$.ajax({
		url: "/saveoshsessions",
		data: "id="+id+"&type="+type,
		type: 'post',
		success: function(data) {
			
			//$("#wrap-malli").html(data);
			location.reload(true);
		}
	});
	
	
}
function unifyProductTableHeights()
{
	// size the product grid cells per row
	$(".product_grid > tbody > tr").each(function(x){
		if(!$(this).data("calculated"))
		{
			var ic = ".product_image"; // image cells
			var c = $(this).find(ic);
			var mH = 0;
			var p = 10;
			
			if(c.length > 0)
			{
				var h = new Array();
				for(var i=0; i < c.length; i++) { h[i] = $(c[i]).height(); }
				mH = Math.max.apply(null, h);
			}
			
			if(mH > 0)
			{
				var csstxt = "height: " + (mH+p) + "px; padding: 0px !important;";
				c.css("cssText", csstxt);
			}
			
			$(this).data("calculated", true);
		}
	});	
}
$(document).ready(function() {
	
	$("li.has_submenu").hover(
		function(){
			if(!$(this).data("timer"))
			{
				var z=$(this);
				var timerId=setTimeout(function(){
					z.data("timer", null).find(".submenu").fadeIn("fast");
				}, 333);
				$(this).data("timer", timerId);
			}
		},
		function(){
			var t;
			t=$(this).data("timer");
			if(t) clearTimeout(t);
			$(this).data("timer", null).find(".submenu").hide();
			
		}
	);
	
	// insert expand-buttons to toplevel categories
	// and also add proper class if it's opened (change to minus sign)
	$(".catkids").each(function(){
		$(this).prepend(($(this).hasClass("item-active"))? "<span class=\"expand_icon opened\"></span>":"<span class=\"expand_icon\"></span>");
	});
	
	
	$(".item-active ul").css("display","none");	
	$(".catkids ul").css("display","none");
	$(".categs li").each(function(){
		if($(this).hasClass("item-active"))
		{
			$(this).children("ul:eq(0)").css("display","block");			
		}
	});
	
	
	$(".expand_icon").click(
		function()
		{
			$(this).parent().children("ul:eq(0)").slideToggle("medium");
			$(this).parent().removeClass("item-active");
			$(this).toggleClass("opened");
		}
	);
	
	// more clickable
	$("table.product_grid td.product_image").click(
		function()
		{
			var url = $(this).find("a").attr("href");
			if(url) window.location = url;
		}
	);
	
	$("table.product_grid td.product_image").css("cursor","pointer");
	
	$(window).load(unifyProductTableHeights);
	
	
	if($(".selector-container").length > 0)
	{
		$(".selector-container").live("click",function(){
			
			var icon = $(this).find(".start-icon:eq(0)");			
			
			var elem = $(this).find(".selector-rowcontainer");			
			
			if(elem.css("display") == "block") {				
				elem.hide();
	 		}
			else{ 
				elem.show();							
			}			
			
			if(icon.attr("title") == "down")
			{
				icon.attr("title","up");
				icon.attr("src", "/static/style/img/icons/dropup_red.png");
			}
			else
			{
				icon.attr("title","down");
				icon.attr("src", "/static/style/img/icons/dropdown_red.png");
			}
	
		});
	
	
		$(".selector-row").live("click", function(){
			carBrand = $(this).attr("id").split("-");
			saveSessionInfo(carBrand[0],carBrand[1]);			
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	if($("#osh-automerkki-selector").length > 0)
	{
		activateCarBrandSelector();
	}
	
});
