$(document).ready (
	function ()
	{				
		$(".menu li").hover(
			function()
			{
				$(this).addClass("sfhover");
			},
			function()
			{
				$(this).removeClass("sfhover");
			}
		);
		
		$(".short").hover(
			function()
			{
				$(this).addClass("sfhover");
			},
			function()
			{
				$(this).removeClass("sfhover");
			}
		);
		
		$("#center .article:last,#right dl .article:last").css("background","none");		
		$("#right dl:last").css('min-height',"160px");
		
		var hgt = $("#right dl:last").height();		
		if (hgt < 160) $("#right dl:last").css('height','160px');
		
		var hgt1 = $("#center").height();		
		var hgt2 = $("#right").height();				
		if (hgt1 < hgt2) $("#center").height(hgt2);

		$(".main ul:first>li:first").addClass('first');
  		$(".main ul:last>li:last").addClass('last');

		// nabindovani vsech obrazku na gallery-highslide
		var imgId = 1000;
		$('img.zoomimg').each(function() {
			$(this).wrap('<a rel="highslide" onclick="return hs.expand(this, {captionId: \'caption'+imgId+'\'} );" href="'+$(this).attr('src')+'"></a>');			
			$(this).parent().after('<div id="caption'+imgId+'" class="highslide-caption">'+$(this).attr('alt')+'</div>');
			imgId++;
		});
		
		
		$('.form-5 .formcontent .line:lt(8)').wrapAll('<div class="fl">');
		$('.form-5 .formcontent .line:gt(7)').wrapAll('<div class="fr">');
		
		$('input[type="file"]').each(function() {
			$(this).hide();
			$(this).after('<input type="text" class="spec-file-input"><button class="spec-file-button">prochazet</button>');
			$('.spec-file-button').click(function(e) { e.preventDefault(); $(this).prev().prev().click(); });
			$('.spec-file-input').click(function() { $(this).prev().click(); });
			$(this).change(function() {
				$(this).next().val($(this).val());
			})
		});
		
		$('.form-5 .formcontent .line:eq(0)').addClass('mb-20');
		$('.form-5 .formcontent .line:eq(4)').addClass('mb-20');
		$('.form-5 .formcontent .line:eq(7)').addClass('mb-20');
		$('.form-5 .formcontent .line:eq(9)').addClass('mb-20');
		$('.form-5 .formcontent .line:eq(10)').addClass('mb-20');
		$('.form-5 .formcontent .line:eq(11)').addClass('mb-20');
		
		$('.form-4 .type-info:eq(0)').addClass('form-info1');
		$('.form-4 .type-info:eq(1)').addClass('form-info2');
		
		if ($('.main-left-sortiment').length > 0) {
			if ($('.katalog-pdf.first').length > 0) {
				$('.katalog-pdf.first a').addClass('ktlg-pdf');
			}
		}
		
		// eshop
		$(".e-button").hover(
			function()
			{
				$(this).addClass("e-button-hover");
			},
			function()
			{
				$(this).removeClass("e-button-hover");
			}
		);
        
        
        // detail produktu - manual - urceni delky textu v zavislosti na sirce obrazku
        var i = 0;
        $('#man .manual').each(function(i){
            if (i%3 == 2) $('<div class="cb"></div>').insertAfter($(this));
        });

		/* tabs - product full */
		if ($('.product.full').length > 0) {
			// odstrani tlacitka, ke kterym neni tab
			if ($('.tab-gallery a.image').length < 1) $('.open-e-gallery').remove();
			if ($('.tab-pdfs div.manual').length < 1) $('.open-e-pdfs').remove();
			if ($('.tab-related div.related-item').length < 1) $('.open-e-related').remove();
			
			// neprve zavre vsechny taby
			$('.eshop-tab-card').hide();
			if (location.hash != "") $('.' + location.hash.substring(1)).show();
			if (location.hash == "#tab-gallery") $('.open-e-gallery').addClass('e-button-active');
			if (location.hash == "#tab-specification") $('.open-e-specification').addClass('e-button-active');
			if (location.hash == "#tab-pdfs") $('.open-e-pdfs').addClass('e-button-active');
			if (location.hash == "#tab-related") $('.open-e-related').addClass('e-button-active');
			// rozkliknuti tabu
			$('.e-button').click(function(e) {
				if ($(this).hasClass('open-e-maptobuy')) return true;
				if ($(this).hasClass('related-button-link')) return;
				e.preventDefault();
				$('.e-button').removeClass('e-button-active');
				$(this).addClass('e-button-active');
				$('.eshop-tab-card').hide();
				var tabTarget = $(this).attr('target');
				$('.' + tabTarget).show();
				resize_rightpanel();
			});
		}
		
		// skryje buttony pro pridani do kosiku, pravy panel a odkaz do objednavky
		if ($('.button-logout').length < 1 && $('.index-sortiment').length > 0) {
			$('.basket-img-onlylog').remove();
			$('#panel-right').remove();
			$('.cart').remove();
			$('.wrap').css('width', '1000px');
		}
	}
);


/*
 * jQuery Cycle a nastaveni poc. hodnot
 */
$(document).ready(
    function ()
    {   
	    if ($('.slides').length > 0) {
        	$('.slides').cycle({ 
	           fx: 'fade',
	           cssBefore: {  
	             opacity: 1, 
	             display: 'block' 
	           }, 
	           animOut: {  
	             opacity: 0  
	           }, 
	           animIn: {  
	             left: 0,  
	             top: 0
	           }, 
	           cssAfter: {  
	             zIndex: 0 
	           }, 
	           speed:  'slow',
	           fit: 1,
	           containerResize: 1,
	           height: 'auto',
	           timeout: 5000, 
	           width: '100%',
	           pager:  '.nav',
	           pagerAnchorBuilder: function(index, el) {
	             return '<a href="#" class="dis'+(index+1)+'"></a>';
	           }
	       });
		}
        
        $('#top .top ul li a span').each(function() {
            $(this).html($(this).html().replace(/[-]/g,"<br />"));
        });
        
        $('tr').hover(
            function () {
                $(this).children().addClass("hover");
            },
            function () {
                $(this).children().removeClass("hover");
            }
        );
        
        $('.grey input').focus();
        $('.grey input').blur();
        $('#search input').focus();
        $('#search input').blur();
        
        $('.mark hr').first().css("display","none");
        $('.article hr').first().css("display","none");
        
        $('.gallery img').mouseover(function () { imgplus($(this)); });
        $('.gallery img').mouseout(function () { imgminus($(this)); });
        
		// rozklikavani leveho menu (pro eshopove nefunkcni)
        $('#main-left:not(".main-left-sortiment") .sub a').click(function () { return open_close_menu($(this).parent().children('ul')); });
        $('#main-left:not(".main-left-sortiment") .active a').click(function () { return false; });
        $('#main-left:not(".main-left-sortiment") .active ul li a').unbind('click');
        
        $('.img-bg').each(function() {
            $(this).attr('src',$(this).attr("src").replace(/[.]([a-zA-Z]+)$/g,"-bg.png"));
        });
        
        resize_rightpanel();
        
        bind_basket();
        
        $(".infomess").hide();
        $(".info").hover(function()
        {
            var elem = $(this).parent().parent().children(".infomess");
            elem.show();
        },function()
        {
            var elem = $(this).parent().parent().children(".infomess");
            elem.hide();   
        });
		
        $(".plus").click(function(){
            var val = $(this).parent().children(".quantity").val();
            if (val >= 0) 
            {
				var mTr = $(this).parent().parent().children();
			
				$.each(mTr, function() {
					$(this).css('background', '#c6c6c6');
				});
            }
        });
		
		$(".minus").click(function(){
			var val = $(this).parent().children(".quantity").val();

            if (val <= 1) 
            {
				var mTr = $(this).parent().parent().children();
				$(this).prev().prev().val("0");
				$.each(mTr, function() {
					$(this).css('background', '#ececec');
				});
            }
        });

		$('.quantity').change(function() {
			var val = parseInt($(this).val());
            if (val > 0) 
            {
				var mTr = $(this).parent().parent().children();
				$.each(mTr, function() {
					$(this).css('background', '#c6c6c6');
				});
            } else
			{
				var mTr = $(this).parent().parent().children();
				$(this).prev().prev().val("0");
				$.each(mTr, function() {
					$(this).css('background', '#ececec');
				});
			}
		});
		 
    }
);

function bind_basket_refresh (elems)
{
	/*
    $.get(window.location,function(data){
        var html=$(data);
        $('a.cart:first').html($('a.cart:first',html).html());
        $('#order .ordersum').html($('#order .ordersum',html).html());
        $('.smallcart').html($('.smallcart',html).html());
	$.each(elems, function() {
		if ((parseInt($(this).attr("value")) == 0) && ($('input[name=\''+$(this).attr("name")+'\']').parent().parent().parent().parent().parent().attr("action")) == "kosik.html")
		{
			$('input[name=\''+$(this).attr("name")+'\']').parent().parent().empty();
			$('input[name=\''+$(this).attr("name")+'\']').parent().parent().remove();
		} else {
			$('input[name=\''+$(this).attr("name")+'\']').val($(this).attr("value"));
		}
		var key = $(this).attr('name').replace('key[','').replace(']','');
		$('input[value=\''+key+'\']').prev().val($(this).attr("value"));
		$('input[rel=\''+key+'\']').val($(this).attr("value"));
        });
    });
	*/
    $.get(window.location,function(data){
        var html=$(data);
        $('a.cart:first').html($('a.cart:first',html).html());
        $('#order .ordersum').html($('#order .ordersum',html).html());
        $('.smallcart').html($('.smallcart',html).html());
	$.each(elems, function() {
		if ((parseInt($(this).attr("value")) == 0) && ($('input[name=\''+$(this).attr("name")+'\']').parent().parent().parent().parent().parent().attr("action")) == "kosik.html")
		{
			$('input[name=\''+$(this).attr("name")+'\']').parent().parent().empty();
			$('input[name=\''+$(this).attr("name")+'\']').parent().parent().remove();
		} else if ($('input[name=\''+$(this).attr("name")+'\']').parent().parent().parent().parent().parent().attr("action") == "kosik.html") {
			$('input[name=\''+$(this).attr("name")+'\']').val($(this).attr("value"));
		}
		var key = $(this).attr('name').replace('key[','').replace(']','');
		$('input[value=\''+key+'\']').prev().val($(this).attr("value"));
		$('input[rel=\''+key+'\']').val($(this).attr("value"));
        });
    });
}


function bind_basket()
{
    $(".infomess").hide();
	
    $('.cartitem .info').live('mouseover mouseout', function(event) {
      if (event.type == 'mouseover') {
           var elem = $(this).parent().parent().children(".infomess");
              elem.show();
      } else {
          var elem = $(this).parent().parent().children(".infomess");
          elem.hide();
      }
    });
    
    $(".plus").live("click",function()
    {
        var elem = $(this).parent().children(".quantity");
        var val = parseInt(elem.val()); 
        elem.val(val+1);
        var $form = $(this).closest('form');
        $.get($form.attr('action')+'?layout=empty',$('select,input,textarea',$form).serializeArray(),function(data){
            var elems = $('select,input,textarea',$form).serializeArray();
            bind_basket_refresh (elems);
        });
    });
    
    $(".quantity").live("change",function()
    {
        var $form = $(this).closest('form');
        $.get($form.attr('action')+'?layout=empty',$('select,input,textarea',$form).serializeArray(),function(data){
            var elems = $('select,input,textarea',$form).serializeArray();
            bind_basket_refresh (elems);
        });
    });

    $(".minus").live("click",function()
    {
        var elem = $(this).parent().children(".quantity");
        var val = parseInt(elem.val()); 
        if (val > 0) elem.val(val-1);
        var $form = $(this).closest('form');
        $.get($form.attr('action')+'?layout=empty',$('select,input,textarea',$form).serializeArray(),function(data){
			var elems = $('select,input,textarea',$form).serializeArray();
            bind_basket_refresh (elems);
        });
    });
}

$(window).load(function(){
    resize_rightpanel();
});

/* zmena velikosti praveho panelu v zavislosti na velikosti obsahu okna */
function resize_rightpanel()
{    
    if ($("#panel-right div").length > 0)
    {
        var konst = 26; // konstanta vyrovnavajici rozdil velikosti
        var mainleft = parseInt($("#main-left").css("height"));
        var maincenter = parseInt($("#main-centerright").css("height"));
        var mainheight = (maincenter > mainleft) ? maincenter : mainleft;
        
        var panelheight = parseInt($("#panel-right div").css("height"));
        if (mainheight < panelheight)
        {
            $("#main").css("height",(panelheight+konst));
            $("#panel-right").css("height",(panelheight));
        }
        if (mainheight > panelheight)
        {
            $("#main").css("height",(mainheight+konst));
            $("#panel-right").css("height",(mainheight));   
        }               
    }
}


function open_cart(id)
{
    $("#blackscreen").css("height",$(document).height());
    $("#blackscreen").show();
    $(id).show();
	
	$.each($('.quantity'), function() { 
		if ($(this).val() > 0 )
		{
			var myTr = $(this).parent().parent().children();
			$.each(myTr, function() {
				$(this).css('background', '#c6c6c6');
			});
		} else
		{
			var myTr = $(this).parent().parent().children();
			$.each(myTr, function() {
				$(this).css('background', '#ececec');
			});
		}
	});
}

function close_cart(id)
{
    $(id).hide();
    $("#blackscreen").hide();
}

/*
 * Nastaveni titulku hledani
 */
function set_search(label)
{
  $('.grey input').val(label);
}

/*
 * Priblizeni obrazku v galerii pri najeti mysi
 */
function imgplus(el)
{
    var h=$(el).height();
    var w=$(el).width();
    $(el).height(h+6);$(el).width(w+6);
}

function imgminus(el)
{
    var h=$(el).height();
    var w=$(el).width();
    $(el).height(h-6);$(el).width(w-6);
}


/*
 * Meni listy hledani pro fokusu
 */
function search_focus(label)
{
    $('.grey').css('background','url(\'/img/lista-hledat-aktivni.png\')');
    if($('.grey input').val() == label) $('.grey input').val('');
}

function search_blur(label)
{
    $('.grey').css('background','url(\'/img/lista-hledat.png\')');
    if($('.grey input').val() == '') $('.grey input').val(label);
}

function search_focus2(label)
{
    $('#search').css('background','url(\'/img/lista-nove-hledani-tmava.png\')');
    $('#search input').css('color','#fff');
    if($('#search input').val() == label) $('#search input').val('');
}

function search_blur2(label)
{
    $('#search').css('background','url(\'/img/lista-nove-hledani-svetla.png\')');
    $('#search input').css('color','#000');
    if($('#search input').val() == '') $('#search input').val(label);
}


/*
 * Zobrazi/schova aktualitu
 */
function open_close(elem)
{
    var text = $(elem).parent().parent().parent().children('.excerpt');
    if ($(text).css('display') == 'none')
    {
        $(text).css('display','block');
        $(elem).children('small').html('&lt; méně');
        resize_rightpanel();
    }
    else
    {
        $(elem).children('small').html('&gt; více');
        $(text).css('display','none');
        resize_rightpanel();
    }
}


/*
 * Zobrazi/schova aktualitu
 */
function open_close_menu(elem)
{
    if ($(elem).css('display') == 'none')
    {
        $(elem).css('display','block');
        return false;
    }
    else if ($(elem).css('display') == 'block')
    {
        $(elem).css('display','none');
        return false;
    }
    return true;
}


/*
 * Nastavuje min. vysku u hlavni stranky
 */
$(document).ready(hp);
$(window).resize(hp);
function hp() 
{
    if ($(window).height() < 670)
    {
        $(".img .label").css("top","290px");
        $(".img img").css("height","670px");
        $("#foot-home").css("top","470px");
    }
    else
    {
        $(".img .label").css("top","auto");
        $(".img img").css("height","100%");
        $("#foot-home").css("top","auto");
    }
}



/*
 * Aktuality popisek dnes, vcera, pred x dny
 */
$(document).ready(
    function ()
    { 
        $.each($(".article2"), function(){
            var date = $(this).children(".panel").children("span").children(".publish").html().split(".");
            date = new Date(date[2],date[1]-1,date[0]).getTime();
            var now = new Date().getTime();
            
            var diference = Math.floor((now-date)/(1000*60*60*24)); // dny
            var out = "";
            
            if (diference == 0) out = "dnes";
            if (diference == 1) out = "včera";
            if (diference > 1) out = "před "+diference+" dny";
            if (diference >= 7) out = "před týdnem";
            if (diference >= 14) out = "před "+Math.floor(diference/7)+" týdny";
            if (diference >= 30) out = "před měsícem";
            if (diference >= 61) out = "před "+Math.floor(diference/30)+" měsíci";
            if (diference >= 365) out = "před rokem";
            if (diference >= 730) out = "před "+Math.floor(diference/365)+" lety";
            
            $(this).children(".panel").children("span").children(".when").html(out);
        });
    }
);       

