﻿var BaseHref = "http://merkur.agora.cz/tmplimg/";

//*************************************************
// START TEMPLATE HADNLINGS
//*************************************************
$(document).ready(function(){
			if($.browser.msie)
			{ 
			    $('head').append('<link href="' + BaseHref + '_Core.Css/ie.css" rel="stylesheet" />');
			    if($("#DefaultShop").length>0)
			    {
				$('head').append('<link href="' + BaseHref + '_Core.Css/ie_defaultshop.css" rel="stylesheet" />');
			    }
			}
			else if($.browser.opera)
			{ 
			    $('head').append('<link href="' + BaseHref + '_Core.Css/opera.css" rel="stylesheet" />');
			}
			else if($.browser.safari)
			{ 
			    $('head').append('<link href="' + BaseHref + '_Core.Css/safari.css" rel="stylesheet" />');
			}
			else if($.browser.chrome)
			{ 
			    $('head').append('<link href="' + BaseHref + '_Core.Css/chrome.css" rel="stylesheet" />');
			}

			$('head').append('<link href="' + BaseHref + '_Core.Css/lightbox.css" rel="stylesheet" />');
			$("#pageskelet_middle").attr("align", "center");
			SetTemplate();
});
//*************************************************
function SetTemplate()
{
	$(".sti_chooser").hide();
	$(".stilist_stock > br").hide();
	$(".stilist_stock > span.bold").hide();
	$(".stilist_stock > input").hide();
	$(".chk_basket").hide();

	if(HPBasedOnQS())
	{
		//homepage
		if($("#mainbanner").length>0)
		{
			var oX = $("#mainbanner").html();
			$("#mainbanner").empty();
			$("#main > div:eq(0)").prepend("<div class=\"main-banner-clip\">" + oX + "</div>");
		}
	}


	if( $(".sti_info").length>0 )
	{
		//detail
		SetItemDetail();
		$(window).load(function(){
			ResizeImage();
		});

	}
	else if( $("#table_pict").length>0 )
	{
		//table gallery
		SetItemList(0);
	}
	else if( $("#table").length>0 )
	{
		if( $(".stilist_top").length>0 )
		{
			//table
			SetItemList(1);
		}
		
	}
	else if( $(".stilist_div").length>0 )
	{
		if( $(".sti_info").length>0 )
		{
			//detail
			SetItemDetail();
		}
		else
		{
			//gallery
			SetItemList(2);
		}
	}

	if( $("#order_search").length>0 )
	{
		$("<br />").insertAfter($("#order_search > nobr"));
	}
}

//*************************************************
// ITEM LIST
//*************************************************

// 0 - table gallery
// 1 - table
// 2 - gallery

//*************************************************
function SetItemList(ListType)
{
	SetItemListHeader();
	SetPager();
	ShowTab(".stilist_top", 0);
	$(".stilist_top").show();
};
//*************************************************
function SetItemListHeader()
{
	$("<div class=\"tab-control\"></div>").insertAfter(".stilist_top > h1");
	$(".stilist_top > p").each(
		function(i)
		{
			var TxNodes = $(this).GetTextNode();
			if(TxNodes!=null)
			{
				$(TxNodes[0]).wrap("<span class='noview'/>");
				$(".stilist_top > .tab-control").append(
					"<span class='tab'>" +
					"<span class='tab-content'>" + 
					$.trim(TxNodes[0].nodeValue).replace(":","") + 
					"</span>" + 
					"</span>"
				);
				$(this).addClass("tab-data");
			}
			$(this).addClass("noview");
		}
	);
	$(".stilist_top > .tab-control > .tab").each(
		function(i)
		{
			$(this).bind("click", function(){ShowTab(".stilist_top", i)});
		}
	);
};
//*************************************************
function SetPager()
{
	$(".stilist_top > .tab-control > .tab > .tab-content:contains('zobrazen')").parent().hide();
	$(".stilist_top > .tab-data > span:contains('zobrazen')").parent().attr("id", "display-view-selector");
	$("#main > div.mb img").each(
		function(i)
		{
			var oImg = $(this);
			oImg.attr("src", (BaseHref + "_Core.Images/Pager/mb_") + String(oImg.attr("src")).substring(oImg.attr("src").indexOf("img/mb_")+7) );
		}
	);
	
	$("#display-view-selector > a").each(
		function(i)
		{
			$("#main > div.mb").append(
				"<a class='display-view view-" + i + "' " +
				"href='" + $(this).attr("href") + "'>" +
				"</a>"
			);
		}
	);
}



//*************************************************
// ITEM DETAIL
//*************************************************

var Headers 	= new Array("Popis produktu", "Parametry", "Zařazení", "Související dokumenty", "Zaslat informaci o produktu známému", "Nalezli jste jinde na internetu nižší cenu?", "Dotaz k tomuto produktu", "Historie cen", "Související produkty");
var TabNames  	= new Array("Popis", "Parametry", "Zařazení", "Dokumenty", "Zaslat", "Garance", "Dotaz", "Historie", "Související");

//*************************************************
function SetItemDetail()
{
	$("#main").hide();
	$("#main > table.sti_params").wrap("<div class='tab-data'></div>");
	$("#main > div.sti_relation").wrap("<div class='tab-data'></div>");
	$("#main > form").wrap("<div class='tab-data'></div>");
	$("#main > div.tab-data").append("<div class='clr'></div>");
	$("#main .cleaner").hide();
	$("#main > a[href='#StiLowPrice']").hide();

	$("<div class=\"light-box-clip\"></div>").insertAfter("#main > br");
	$("<div class=\"set-list\"></div>").insertAfter("#main > .light-box-clip");
	$("<div class=\"tab-control\"></div>").insertAfter("#main > .set-list");
	$("#main > h1").css({"height":"auto", "marginBottom":"10px"});

	$("#table").appendTo(".set-list");
	
	$("#main > .tab-data > form > table.sti_params > tbody > tr.background_dark > th, #main > .tab-data > table.sti_params > tbody > tr.background_dark > th, #main > .tab-data > div.sti_relation > div.sti_relation_head").each(
		function(i)
		{
			$("#main > .tab-control").append(
				"<span class='tab'>" +
				"<span class='tab-content'>" + 
				GetTabName($(this).text()) + 
				"</span>" + 
				"</span>"
			);
		}
	);
	$("#main > .tab-data > form > table.sti_params > tbody > tr > th, #main > .tab-data > table.sti_params > tbody > tr > th, #main > .tab-data > div.sti_relation > div.sti_relation_head").hide();

	$("#main > .tab-control > .tab").each(
		function(i)
		{
			$(this).bind("click", function(){ShowTab("#main", i)});
		}
	);

	$("#sti_td_prodwww_1 > a").text("www");
	if($("#main > .tab-control > .tab").length>0)
	{
		ShowTab("#main", 1);
	}
	else
	{
		ShowTab("#main", 0);
	}
	BuildGallery();
	$("#main").show();
};
//*************************************************
function GetTabName(OrigText)
{
	for(var i=0; i<Headers.length; i++)
	{
		if(OrigText==Headers[i])
		{
			return TabNames[i];
		}
	}
	return OrigText;
}
//*************************************************
function ResizeImage()
{
	$(".sti_pict  .sti_detail_gallery > img").parent().hide();
	$("<div class='clr'></div>").insertAfter(".sti_pict");
	$(".sti_pict  td").css("text-align", "center");
	var DetailImg = $(".sti_pict  img:visible")
	if(DetailImg.length>0)
	{
		var ratio = {width:255, height:200, resize:1};
		var w = DetailImg.width();
		var h = DetailImg.height();
		if(w > ratio.width)
		{
			if(h > ratio.height)
			{
				if(ratio.width / w < ratio.height / h)
				{
					ratio.resize = 	ratio.width / w;
				}
				else
				{
					ratio.resize = 	ratio.height / h;
				}
			}
			else
			{
				ratio.resize = 	ratio.width / w;
			}
		}
		else
		{
			if(h > ratio.height)
			{
				ratio.resize = 	ratio.height / h;
			}
		}

		ratio.width = parseInt(w*ratio.resize);
		ratio.height = parseInt(h*ratio.resize);

		DetailImg.hide().css("width", ratio.width+"px").css("height", ratio.height+"px").attr("src", DetailImg.attr("src")).show();
	};
}
//*************************************************
function BuildGallery()
{
	$(".sti_pict > tbody > tr:gt(0) > td > div > img").each(
		function()
		{
			$(".light-box-clip").append("<a class='light-box' href='" + ParseGalleryLink(String($(this).attr("onclick"))) + "'><img src='" + $(this).attr("src") + "' alt='' border='0' /></a>");
		}
	);
	$(function(){
		$('.light-box').lightBox({
			imageLoading: 		BaseHref + '_Core.Images/ico-loading.gif',
			imageBtnClose: 		BaseHref + '_Core.Images/LightBox/btn-close.gif',
			imageBtnPrev: 		BaseHref + '_Core.Images/LightBox/btn-prev.gif',
			imageBtnNext: 		BaseHref + '_Core.Images/LightBox/btn-next.gif',
			containerResizeSpeed: 	200,
			txtImage: 		'Obrázek',
			txtOf: 			'z'
		});
	});
}
//*************************************************
function ParseGalleryLink(oTx)
{
	if(!oTx)
	{
		return "";
	}
	try
	{
		var oStart = oTx.indexOf("window.open(") + 13;
		var oEnd = oTx.indexOf(",");
		oTx = oTx.substring(oStart, oEnd);
		oTx = oTx.replace("'", "").replace('"', "").replace(" ", "");
		return oTx;
	}
	catch(e)
	{
		return "";
	}
}

//*************************************************
// TAB DATA
//*************************************************
function ShowTab(ClassName, Index)
{
	$(ClassName + " > .tab-control > .tab").removeClass("tab-active");
	$(ClassName + " > .tab-control > .tab:eq(" + Index + ")").addClass("tab-active");
	$(ClassName + " > .tab-data").hide();
	$(ClassName + " > .tab-data:eq(" + Index + ")").show();
};

//*************************************************
// JQUERY EXTENSIONS
//*************************************************
$.fn.GetTextNode = function()
{
	var ret = [];
	if(this.contents().length==0)
	{
		return null;
	}
	this.contents().each(
			function()
			{
				var fn = arguments.callee;
				if(this.nodeType == 3)
				{
					ret.push(this);
				}
			}
	);
	return $(ret);
}


//*************************************************
// QUERYSTRING PARSER
//*************************************************
function GetParsedQS()
{

	var searchString 	= document.location.search;
	var nvPairs 		= null;
	try
	{
		searchString.replace("?", "").split("&");
	}
	catch(e){}
	return nvPairs;
}

function HPBasedOnQS()
{
	var QS = GetParsedQS();
	return (!QS || QS==null || QS=="" || QS.length==0);
}