$(document).ready(function() {

	// poptavka bez ceny
	$("#demandButton, #demandButton2").fancybox({
	   'width'             : '80',
            'height'            : '46',
            'autoScale'         : false,
            'transitionIn'      : 'none',
            'transitionOut'     : 'none',
            'type'              : 'iframe',
	    'padding'	:	"2px",
	    'centerOnScroll' : true
	});

	$("#currency a").click(function(){

		var
			$link = $(this);
			$class = $link.attr("class"),
			$currency = $("#selectedCurrency").val();

		if (($class == "sk" && $currency == "EUR") || ($class == "cz" && $currency == "CZK")) {

			$link.parents("form").submit();

		}
	})


	$(".pravavyhoda").corner("tr br");
	$(".infoboxy").corner();
	$(".pravavyhoda").click(function() {

		var	$thisObject = $(this),
			$id = $thisObject.attr("id");
			$backgroundColor = $(this).css("background-color");
			$child = $("div."+$id);
			$child.css({"background-color":$backgroundColor,"top":$thisObject.css('top')});


		$child.fadeIn();

	});
	$(".infoboxy").click(function() {
		$(this).fadeOut();
	})

});

function checkNumber() {

	var qty = document.getElementById('qty');

	reg = /^[1-9]{1}[0-9]*$/;

	if (reg.test(qty.value)) {

		return true

	} else {

		alert("Množství zadávejte pouze jako celé kladné číslo!");
		qty.value = 1;
		qty.focus();

		return false

	}

}
function checkBonus(code)
{
	var url = "/ajax/get_bonus_discount.php";
	if (code != "") {
	    $.ajax({
		    type: "GET",
		    url: url,
		    data: "bonusCode=" + code + "&orderTotal=" + $("#VoucherOrderTotal").val() + "&voucherHash=" + $("#VoucherHash").val() + "&voucherInCart=" + $("#voucherInCart").val() + "&VoucherCurrency=" + $("#VoucherCurrency").val()+ "&VoucherUserType=" + $("#VoucherUserType").val(),
		    dataType: "xml",
		    success: function(xml){
				    var msg = xml.firstChild.firstChild.firstChild.nodeValue;
				    alert(msg);
				    if(msg != "Neplatný slevový kupón, nebo nemáte dostatečně vysokou cenu objednávky pro uplatnění slevy!" && msg != 'Slevový kupón je již použit v objednávce!'){
					$('#frm_bonus').submit();
				    }
		    }
	    });
	} else {
		alert("Vložte prosím slevový kupón.");
	}
}


function changeStamp(sStamp, sPrice, sHref) {

	//var productStamp 	= document.getElementById('productStamp');
	//var price			= document.getElementById('priceToChange');
    //var rezervace		= document.getElementById('rezervace');

    //rezervace.href = sHref;
	//price.innerHTML = sPrice;
}

function changeStars(star, numero) {
	var ul = document.getElementById("ratings");
	var rattingsDB = document.getElementById("rating");


	if (ul.className == star+" rating") {

		ul.className = "rating";
		rattingsDB.value = 0;

	} else {

		ul.className = star+" rating";
		rattingsDB.value = numero;

	}
}

function manageSearchFieldWidth(action)
{
  var searchField = document.getElementById('search');
  var searchButton = document.getElementById('btn_search');
  switch(action)
  {
    case 'expand':
      searchField.value = '';
      searchButton.style.left = '600px';
      searchField.style.width = '272px';
    break;
    case 'reduce':
      if(searchField.value == '')
      {
        searchField.value = 'Hledej ...';
        searchButton.style.left = '450px';
        searchField.style.width = '122px';
      }
    break;
  }
}

function pd_switchImageLarge(imageFilename, imageAlt) {

	var htmlImageLarge = document.getElementById("imageLarge");
	var htmlLinkLarge = document.getElementById("linkLarge");

	htmlImageLarge.src = "/picts/thumb-large/" + imageFilename;
	htmlImageLarge.alt = imageAlt;
	htmlLinkLarge.title = imageAlt;
	htmlLinkLarge.href = "/picts/" + imageFilename;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function setPayment(choice){
	var element = document.getElementById('payment');
	if(choice == 4 && element.value == 1){
	  element.value = 2;
	  window.alert("Pro tento způsob dopravy je možnost platby pouze hotově. Způsob platby byl automaticky nastaven.");
	}else if (choice != 4 && element.value == 2){
	  element.value = 1;
	  window.alert("Pro tento způsob dopravy je možnost platby pouze dobírkou. Způsob platby byl automaticky nastaven.");
	}
}

function setShipping(choice){
	var element = document.getElementById('shipping');
	if(choice == 1 && element.value == 4){
	  element.value = 1;
	  window.alert('Způsob platby dobírkou není možný v případě osobního odběru. Způsob dopravy byl automaticky změněn na "Česká pošta - obyčejný balík".');
	}else if (choice == 2 && element.value != 4){
	  element.value = 4;
	  window.alert("V hotovosti lze platit pouze při osobním odběru. Způsob dopravy byl automaticky nastaven.");
	}
}

function cartadd(frm, session_id, currency, user_type){

	if(user_type == '') user_type = '1';
	var url = "../ajax/cart_add.php?currency="+currency+"&user_type="+user_type+"&session_id="+session_id+"&productStamp="+frm.productStamp.value+"&qty="+frm.qty.value;
	//window.location=url;
	var ajax = new net.ContentLoader(url, cartloading);

}

function zmenaHledani() {

	var element = document.getElementById('hledani');

	if (element.value == 'hledat...') {
		element.value = '';
	}

}

function cartloading(){
	var elmnt = document.getElementById('cart-ajax');
	var data = this.req.responseXML.documentElement;
	var ks = Number(data.lastChild.firstChild.nodeValue);
	var cena = data.firstChild.firstChild.nodeValue;
	if(!isNaN(ks)){
		elmnt.innerHTML = ks + "<br />"+cena;
		alert("Vybrané zboží bylo přidáno do košíku. Děkujeme.");
	}
}


function watchDog(e, productId){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.documentElement.scrollLeft;
		posy = e.clientY + document.documentElement.scrollTop;
	}


	var frm = document.getElementById('frm_watchDog');
	frm.wdProductId.value = productId;
	var wdBox = document.getElementById('watchDog');

	wdBox.style.top = (posy + 5) + "px";
	wdBox.style.left = (posx +5) + "px";
	wdBox.style.display = 'block';
}

function addItem(box, id) {
   var sel = document.getElementById(box);
   var opt = document.createElement("OPTION");
   opt.value = id;

	var txt = document.createTextNode(id);
	opt.appendChild(txt);
   sel.appendChild(opt);
}


function ValidateEmail( email){
	var regStr = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if( !regStr.test(email)){
		alert("Vámi zadaná e-mailová adresa '"+email+"' nemá platný formát.");
		return false;
	}
	return true;
}

function ValidateEmailNotStrict( email){
	if(email == '') return true;
	var regStr = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if( !regStr.test(email)){
		alert("Vámi zadaná e-mailová adresa nemá platný formát.");
		return false;
	}
	return true;
}

function ValidateNotEmpty( values){
	for(var i=0; i<values.length; i++){

		if(values[i] == ''){
			alert("Vyplňte prosím požadované údaje.");
			return false;
		}
	}
	return true;
}

function ValidateAreNumbers( values  ){
	var regStr = /^(\d)+$/;
	for(var i=0; i<values.length; i++){
		if(!regStr.test(values[i])){
			alert("Do číselných polí prosím vložte nezáporné celé číslo.");
			return false;
		}
	}
	return true;
}

function ShowTooltip(e, tip)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.documentElement.scrollLeft;
		posy = e.clientY + document.documentElement.scrollTop;
	}

	var tooltipBox = document.getElementById('tooltip');
	tooltipBox.innerHTML = tip;

	tooltipBox.style.top = (posy + 5) + "px";
	tooltipBox.style.left = (posx +5) + "px";
	tooltipBox.style.display = 'block';

}

function HideTooltip(){

	var tooltipBox = document.getElementById('tooltip');

	tooltipBox.style.display = 'none';
}


$(document).ready(
	function(){
		$('#slideshow').innerfade({
			speed: 'slow',
			timeout: 4300,
			type: 'sequence',
			containerheight: '180px'
		});
	});

