function sizing() {
	var container = document.getElementById("leftblock");
	var container2 = document.getElementById("rightblock");
	
	if (window.innerWidth) {	
		container.style.width = (window.innerWidth > 980)?(parseInt((window.innerWidth-980-15)/2+654)+'px'):'654px';	
		container2.style.width = (window.innerWidth > 980)?(window.innerWidth - parseInt(container.style.width) - 18 + 'px'):'324px'; 
	} else if (document.body.clientWidth) {
		container.style.width = (document.body.clientWidth > 980)?(parseInt((document.body.clientWidth-980)/2+654)+'px'):'654px';	
		container2.style.width = (document.body.clientWidth > 980)?(document.body.clientWidth - parseInt(container.style.width) - 1 + 'px'):'324px';
	}
}

function searchBoxReady() {
	var textBoxs = $('.stextinput');
	textBoxs.each(function() {
		var theValue = 'Vyhledávání';
		if($(this).attr('value') == ''){
			$(this).attr('value',theValue).removeClass('writingIt');
		}
		$(this).focus(function (){
			if($(this).attr('value') == theValue){
				$(this).attr('value','').addClass('writingIt');
			}
		}).blur(function () {
			if($(this).attr('value') == ''){
				$(this).attr('value',theValue).removeClass('writingIt');
			}
		});
	});
}

function hsnabidkaReady() {
	var hsnlinks = $('a.hsnlink');
	hsnlinks.each(function() {						
		$(this).click(function() {
			$("#scrollbarnab").html("<img class='loader' src='http://img.123shop.cz/img/css/123shop/newportal/ajax-loader.gif'/>");
			var selSekceId = $(this).attr('sekceid');
			$.ajax({
			   type: "GET",
			   url: "/hsnabidka/?umisteni=7&section=" + selSekceId,
			   context: document.body, 
			   dataType: "html",
			   success: function(data){
					$("#scrollbarnab").html(data);
					$(".hsnablink").removeClass('hsnablinksel');
					$("#hsnsel-" + selSekceId).addClass('hsnablinksel');
					$(".hsnablinkssipkydiv").html('&nbsp;');
					$("#hsnselsipka-" + selSekceId).html("<img class='sipka' src='http://img.123shop.cz/img/css/123shop/newportal/hsnselsekcesipka.gif'/>");
			   }
			});
		});
		var selVychozi = $(this).attr('vychozi');
		if (selVychozi == "t") {
			$(this).click();
		}
	});
}
					
function bigBannersReady() {
	/* velke bannery - tlacitka */
	$("#platbytxt").mouseenter(function() {
		if (($("#bigbanner_but1").attr('active') == "no") && ($("#bigbanner_but1").attr('over') == "no")) {
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-over.gif');
			$("#bigbanner_but1").attr('over', "yes");
		}
	}).mouseleave(function() {
		if (($("#bigbanner_but1").attr('active') == "no") && ($("#bigbanner_but1").attr('over') == "yes")) {
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-off.gif');
			$("#bigbanner_but1").attr('over', "no");
		}
	});

	$("#platbytxt").click(function() {
		if ($("#bigbanner_but1").attr('active') == "no") {
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-sel-on.gif'); 
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-off.gif');
			$("#bigbanner_but2").attr('active', "no");
			$("#bigbanner_but2").attr('over', "no");
			$("#bigbanner_but1").attr('active', "yes");
			
			/* nastavim nove pozadi banneru */
			if (!$("#leftblock").hasClass("leftblockplatby")) {
				$("#leftblock").addClass("leftblockplatby");
			}
			
			/* a nastavim kod banneru */
			$("#banner3Akce1Content").hide();
			$("#banner3Akce2Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").show(); /* "fast", "fast", "fast", "slow" */
		}
	});

	$("#triakcetxt").mouseenter(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "no")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-over.gif');
			$("#bigbanner_but2").attr('over', "yes");
		}
	}).mouseleave(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "yes")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-off.gif');
			$("#bigbanner_but2").attr('over', "no");
		}
	});

	$("#triakcetxt").click(function() {
		if ($("#bigbanner_but2").attr('active') == "no") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-off.gif');
			$("#bigbanner_but1").attr('active', "no");
			$("#bigbanner_but1").attr('over', "no");
			$("#bigbanner_but2").attr('active', "yes");
			
			/* nastavim puvodni pozadi banneru */
			if ($("#leftblock").hasClass("leftblockplatby")) {
				$("#leftblock").removeClass("leftblockplatby");
			}
			
		} else {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
		}
		/* a nastavim kod banneru */
		$("#banner3Akce2Content").hide();
		$("#banner3Akce3Content").hide();
		$("#bannerPlatbyContent").hide();
		$("#banner3Akce1Content").show();
	});
	
	$("#triakce1").mouseenter(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "no")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-over.gif');
			$("#bigbanner_but2").attr('over', "yes");
		} else if ($("#bigbanner_but2").attr('active') == "yes") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce1Content").show();
		}
	}).mouseleave(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "yes")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-off.gif');
			$("#bigbanner_but2").attr('over', "no");
		}
	});
	$("#triakce1").click(function() {
		if ($("#bigbanner_but2").attr('active') == "no") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-off.gif');
			$("#bigbanner_but1").attr('active', "no");
			$("#bigbanner_but1").attr('over', "no");
			$("#bigbanner_but2").attr('active', "yes");
			
			/* nastavim puvodni pozadi banneru */
			if ($("#leftblock").hasClass("leftblockplatby")) {
				$("#leftblock").removeClass("leftblockplatby");
			}
			
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce1Content").show();
		} else {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce1Content").show();
		}
	});

	$("#triakce2").mouseenter(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "no")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-over.gif');
			$("#bigbanner_but2").attr('over', "yes");
		} else if ($("#bigbanner_but2").attr('active') == "yes") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-2.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce1Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce2Content").show();
		}
	}).mouseleave(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "yes")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-off.gif');
			$("#bigbanner_but2").attr('over', "no");
		}
	});
	$("#triakce2").click(function() {
		if ($("#bigbanner_but2").attr('active') == "no") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-off.gif');
			$("#bigbanner_but1").attr('active', "no");
			$("#bigbanner_but1").attr('over', "no");
			$("#bigbanner_but2").attr('active', "yes");
			
			/* nastavim puvodni pozadi banneru */
			if ($("#leftblock").hasClass("leftblockplatby")) {
				$("#leftblock").removeClass("leftblockplatby");
			}
			
			/* a nastavim kod banneru */
			$("#banner3Akce1Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce2Content").show();
		} else {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-2.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce1Content").hide();
			$("#banner3Akce3Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce2Content").show();
		}
	});

	$("#triakce3").mouseenter(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "no")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-over.gif');
			$("#bigbanner_but2").attr('over', "yes");
		} else if ($("#bigbanner_but2").attr('active') == "yes") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-3.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce1Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce3Content").show();
		}
	}).mouseleave(function() {
		if ($("#bigbanner_but2").attr('active') == "no" && ($("#bigbanner_but2").attr('over') == "yes")) {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-nosel-off.gif');
			$("#bigbanner_but2").attr('over', "no");
		}
	});
	$("#triakce3").click(function() {
		if ($("#bigbanner_but2").attr('active') == "no") {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-1.gif');
			$("#bigbanner_but1_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-1-nosel-off.gif');
			$("#bigbanner_but1").attr('active', "no");
			$("#bigbanner_but1").attr('over', "no");
			$("#bigbanner_but2").attr('active', "yes");
			
			/* nastavim puvodni pozadi banneru */
			if ($("#leftblock").hasClass("leftblockplatby")) {
				$("#leftblock").removeClass("leftblockplatby");
			}
			
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce1Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce3Content").show();
		} else {
			$("#bigbanner_but2_img").attr('src', 'http://img.123shop.cz/img/css/123shop/newportal/banner-tlacitko-2-sel-3.gif');
			/* a nastavim kod banneru */
			$("#banner3Akce2Content").hide();
			$("#banner3Akce1Content").hide();
			$("#bannerPlatbyContent").hide();
			$("#banner3Akce3Content").show();
		}
	});
}

function recountGoods() {
	var $mans = "|";
	var $prokoho = "|";
	var $cena = "|";
	var $dar = "|";
	
	var $jenSkladem = "";
	$("input[name='skladem']").each(function() {
		$jenSkladem = $(this).val();
	});
	
	var $id = "";
	$("input[name='id']").each(function() {
		$id = $(this).val();
	});
	
	var $cat = "";
	$("input[name='cat']").each(function() {
		$cat = $(this).val();
	});
	
	var $man = "";
	$("input[name='man']").each(function() {
		$man = $(this).val();
	});
	
	var $place = "";
	$("input[name='place']").each(function() {
		$place = $(this).val();
	});
	
	var $section = "";
	$("input[name='section']").each(function() {
		$section = $(this).val();
	});
	
	$("input[name='prokoho'], input[name='cena'], input[name='dar'], input[name='fman']").each(function() {
		var $this = $(this);
		var which = $this.attr("name");
		var wval = $this.attr("value");
		
		if ($this.is(':checked')) {
			if (which == "fman") {
				$mans += wval + "|";
			} else if (which == "prokoho") {
				$prokoho += wval + "|";
			} else if (which == "cena") {
				$cena += wval + "|";
			} else if (which == "dar") {
				$dar += wval + "|";
			}
		} 
	});
	
	var $data = "";
	if ($jenSkladem != "") {
		$data += "skladem=" + $jenSkladem;
	}
	if ($id != "") {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "id=" + $id;
	}
	if ($cat != "") {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "cat=" + $cat;
	}
	if ($man != "") {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "man=" + $man;
	}
	if ($place != "") {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "place=" + $place;
	}
	if ($section != "") {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "section=" + $section;
	}
	if (($mans != "") && ($mans != "|") && ($mans != "||")) {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "mans=" + $mans;
	}
	if (($prokoho != "") && ($prokoho != "|") && ($prokoho != "||")) {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "prokoho=" + $prokoho;
	}
	if (($cena != "") && ($cena != "|") && ($cena != "||")) {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "cena=" + $cena;
	}
	if (($dar != "") && ($dar != "|") && ($dar != "||")) {
		if ($data.length > 0) {
			$data += "&";
		}
		$data += "dar=" + $dar;
	}
	
	$.ajax({
        url: '/vanocni-darky-count/', 
        cache: false,          
        data: $data, 
        dataType: 'xml',                  
        contentType: "text/xml charset=iso-8859-2",
        success: function(theXML) 
        {
            $('shopData',theXML).each(function(i) {
                var pocetpoomezeni = $(this).find("pocetvyrobkupoomezeni").text();
                if (pocetpoomezeni) {
                	//alert("nalezenyPocet: " + pocetpoomezeni);
                	if (pocetpoomezeni == 1) {
                		$("#zobraz").html("Zobrazit zboží&nbsp;(" + pocetpoomezeni + " výsledek)");
                	} else if ((pocetpoomezeni == 2) || (pocetpoomezeni == 3) || (pocetpoomezeni == 4)) {
                		$("#zobraz").html("Zobrazit zboží&nbsp;(" + pocetpoomezeni + " výsledky)");
                	} else {
                		$("#zobraz").html("Zobrazit zboží&nbsp;(" + pocetpoomezeni + " výsledků)");
                	}
                } else {
                	// nenasel jsem - necham jak bylo
                }
            });
        },
        error: function(x,e) {
      			if(x.status==0) {
      			   //alert('You are offline!!\n Please Check Your Network.');
      			} else if(x.status == 404) {
      			   //alert('Requested URL not found.');
      			} else if(x.status == 500) {
      			   //alert('Internal Server Error.');
      			} else if(e == 'parsererror') {
      			   //alert('Error.\nParsing XML Request failed.');
      			} else if(e == 'timeout') {
      			   //alert('Request Time out.');
      			} else {
      			   //alert('Unknow Error.\n' + x.responseText);
      			}
    		}
    });
}

function darkovnicekReady() {
	/* darkovnicek - formular na omezen*/
	$("input[name='allselect']").click(function() {
		var $this = $(this);
		var which = $this.val();
		
		if ($this.is(':checked')) {
			$("input[name='" + which + "']").attr("checked", true);
		} else {
			$("input[name='" + which + "']").attr("checked", false);
		}
		
		recountGoods();
	});
	
	$("input[name='prokoho'], input[name='cena'], input[name='dar']").click(function() {
		var $this = $(this);
		var which = $this.attr("name");
		
		if (!$this.is(':checked')) {
			$("input[id='allselect" + which + "']").attr("checked", false);
		} 
		
		recountGoods();
	});
	
	$("#zabal").click(function() {
		$("div.checkboxes").hide();
		$("#rozbal").show();
		$(this).hide();
		$("input[name='collapsed']").val("1");
	});
	
	$("#rozbal").click(function() {
		$("div.checkboxes").show();
		$("#zabal").show();
		$(this).hide();
		$("input[name='collapsed']").val("0");
	});
	
	$("div.tlacitko").click(function() {
		$("form[name='nabmanorder']").submit();
	});
}

					
function documentReadyTitulka() {
	sizing();
	$(window).resize(sizing);
	searchBoxReady();	
	hsnabidkaReady();
	bigBannersReady();
		
}

function documentReadyPodstranky() {
	searchBoxReady();
	darkovnicekReady();
}

function anchorHref(anchor) {
	if (anchor && anchor != "") {
		document.location.href = anchor; 
		return true;
	} else {
		return false;
	}
}
