﻿var PreferredFontSizeCookieName = "PreferredFontSize";

var EmailRegExpPattern = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$");

var KeyCode_Enter = 13;
var KeyCode_PageUp = 33;
var KeyCode_PageDown = 34;
var KeyCode_End = 35;
var KeyCode_Home = 36;
var KeyCode_ArrowLeft = 37;
var KeyCode_ArrowUp = 38;
var KeyCode_ArrowRight = 39;
var KeyCode_ArrowDown = 40;

var SearchInputErrorMessage = "Zadejte alespoň 3 znaky.";

/// ********************************
/// SEARCH INIT
/// ********************************
function SearchInit(inputClass, buttonClass)
{
	$(document).ready
	(
		function()
		{
			$("." + inputClass).bind("keypress", function(e) { OnSearchKeyPress(e, buttonClass); }); 
			$("." + buttonClass).bind("click", function(e) { OnSearchInputValidate(e, inputClass); });
		}
	);
}

function OnSearchKeyPress(e, buttonClass)
{
	if (e.keyCode == KeyCode_Enter)
	{
		e.preventDefault();
		$("." + buttonClass).click();
	}
}

function OnSearchInputValidate(e, inputClass)
{
	var sinput = jQuery.trim($("." + inputClass).val());
	
	if (sinput.length >= 3)
	{
		return true;
	}
	else
	{
		e.preventDefault();
		window.alert(SearchInputErrorMessage);
		return false;
	}
}

/// ********************************
/// VALIDATION
/// ********************************
function OnComboBoxClientValidate(validator, args)
{
	var cmb = eval(validator.controltovalidate);
	args.IsValid = cmb.GetText() != cmb.Items[0].Text;
}

function OnCompanyDataClientValidate(validator, args)
{
	var cname = jQuery.trim($(".company-name").val()),
		cnumber = jQuery.trim($(".company-number").val()),
		cvatnumber = jQuery.trim($(".company-vat-number").val());
	
	if (cname.length > 0 || cnumber.length > 0 || cvatnumber.length > 0)
	{
		args.IsValid = cname.length > 0 && cnumber.length > 0 && cvatnumber.length > 0;
	}
	else args.IsValid = true;
}

function OnDeliveryAddressClientValidate(validator, args)
{
	var dstreet = jQuery.trim($(".delivery-street").val()),
		dcity = jQuery.trim($(".delivery-city").val()),
		dzip = jQuery.trim($(".delivery-zip").val());
	
	if (dstreet.length > 0 || dcity.length > 0 || dzip.length > 0)
	{
		args.IsValid = dstreet.length > 0 && dcity.length > 0 && dzip.length > 0;
	}
	else args.IsValid = true;
}

function OnAgreementClientValidate(validator, args)
{
	args.IsValid = $(".checkbox-agreement").attr("checked");
}

function OnPasswordClientValidate(validator, args)
{
	var pwd1 = $(".password-1").val(), pwd2 = $(".password-2").val();
	if (pwd1.length > 0 || pwd2.length > 0) args.IsValid = pwd1 == pwd2;
	else args.IsValid = true;
}

function OnBasketProductConfirm(title)
{
	var cmessage = "Opravdu si přejete odstranit tento produkt?\n\n" + title;
	
	return window.confirm(cmessage);
}

function OnBasketCleanConfirm()
{
	return window.confirm("Opravdu si přejete odstranit obsah košíku?");
}

/// ********************************
/// COLORBOX INIT
/// ********************************
$(document).ready(InitColorBox);

function InitColorBox()
{
	InitColorBoxGroup(".thickbox");
	InitColorBoxGroup(".colorbox");
}

function InitColorBoxGroup(groupSelector, rel)
{
	if (rel)
	{
		$(groupSelector).colorbox({ transition: "elastic", rel: true, speed: 700, title: false, opacity: 0.85, current: "Obrázek {current} z {total}", next: "Předchozí", previous: "Další", close: "Zavřít" });
	}
	else
	{
		$(groupSelector).colorbox({ rel: false, speed: 700, title: true, opacity: 0.85 });
	}
}

function ReinitColorBox()
{
	$(document).ready(InitColorBox);
}

/// ********************************
/// RELATED BROWSER INIT
/// ********************************
var RelatedBrowserPageSize = 4, RelatedBrowserItemWidth = 32;

function RelatedBrowserInit()
{
	$(document).ready
	(
		function()
		{
			$(".items .item").each
			(
				function(index)
				{
					var length = $(this).find(".item-photos div a").each
						(function (subIndex) { $(this).click(function(e){ e.preventDefault(); RelatedBrowserOnSelect(index, subIndex); }); })
						.length;
					
					if (length > RelatedBrowserPageSize)
					{
						$(this).find(".item-photos")
							.find(".next").show().click(function(e){ e.preventDefault(); RelatedBrowserMove(index, 1); }).end()
							.find(".prev").hide().click(function(e){ e.preventDefault(); RelatedBrowserMove(index, -1); });
					}
				}
			);
			
			RelatedBrowserPreloadImages();
		}
	);
}

function RelatedBrowserPreloadImages()
{
	var preloadImagesArray = new Array();
			
	$(".item-photos .data .image").each
	(
		function()
		{
			var preloadImage = new Image();
			preloadImage.src = $(this).text();
		
			preloadImagesArray.push(preloadImage);
		}
	);
}

function RelatedBrowserOnSelect(parentIndex, index)
{
	var pitem = $(".items .item:eq("+String(parentIndex)+")"),
		photos = pitem.find(".item-photos"),
		related = photos.find(".data:eq("+String(index)+")")
	
	var title = related.find(".title").text(),
		code = related.find(".code").text(),
		discount = related.find(".discount").text(),
		url = related.find(".url").text(),
		image = related.find(".image").text();
	
	pitem.find(".cat-number").html(code).end()
		.find("h3").css("filter", "").end()
		.find("h3 a").attr("href", url).attr("title", title).text(title).end()
		.find(".item-photo a").attr("href", url).attr("title", title)
			.find("img").animate({ opacity: 0.1 }, "fast",
				function()
				{
					$(this).attr("src", image).attr("alt", title).animate({ opacity: 1 }, "fast");
				});
	
	if (discount.length > 0)
	{
		pitem.find(".sale").hide().animate({ opacity: 0.1 }, "fast",
			function()
			{
				$(this).find(".discount").text(discount).end()
					.show().animate({ opacity: 1 }, "fast");
			});
	}
	else pitem.find(".sale").hide();
	
	photos.find("div a").removeClass("active").end()
		.find("div a:eq("+String(index)+")").addClass("active");
}

function RelatedBrowserMove(parentIndex, offsetToSet)
{
	var photos = $(".items .item:eq("+String(parentIndex)+") .item-photos"),
		length = photos.find("div").length,
		offset = parseInt(photos.find(".offset").text()),
		maxOffset = length - RelatedBrowserPageSize,
		movementValid = false;
	
	if (offsetToSet > 0) offset++;
	else if (offsetToSet < 0) offset--;
	
	var lower = offset, upper = offset + RelatedBrowserPageSize - 1;
	
	if (offsetToSet > 0 && offset <= maxOffset)
	{
		// posun vpravo
		movementValid = true;
		
		photos.find("div").hide().slice(lower, upper).show().end().end()
			.find("div:eq("+String(upper)+")").fadeIn("normal");
	}
	else if (offsetToSet < 0 && offset >= 0)
	{
		// posun vlevo
		movementValid = true;
		
		photos.find("div").hide().slice(lower + 1, upper + 1).show().end().end()
			.find("div:eq("+String(lower)+")").fadeIn("normal");
	}
	
	if (movementValid)
	{
		photos.find(".offset").text(offset);
		
		if (offset > 0) photos.find(".prev").show();
		else photos.find(".prev").hide();
		
		if (offset < maxOffset) photos.find(".next").show();
		else photos.find(".next").hide();
	}
}
