function setCookieMinutes(key, value, minutes) {
    value = encodeURIComponent(value); // hodnota value by měla být minimálně escapována pomocí funkce escape()
    value+='; path=/';
    // expirace
    var date = new Date();
    date.setTime(date.getTime() + minutes * 60 * 1000);
    value+='; expires=' + date.toGMTString();
    // secure
    document.cookie = key + '=' + value;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**** hlavicka.tpl ****/
function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
$(function() {
    setInterval( "slideSwitch()", 3000 );
});


$('document').ready(function(){

/**** registrace.tpl ****/	 
	$(".register").validate({
		rules: {
			jmeno: "required",
			prijmeni: "required",
			heslo: {
				required: true,
				minlength: 5
			},
			heslo2: {
				required: true,
				minlength: 5,
				equalTo: "#heslo"
			},
			hesloch: {
				minlength: 5
			},
			heslo2ch: {
				minlength: 5,
				equalTo: "#heslo"
			},
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			jmeno: "Vyplňte prosím své křestní jméno",
			prijmeni: "Vyplňte prosím své příjmení",
			heslo: {
				required: "Vyplňte prosím heslo",
				minlength: "Vaše heslo musí mít alespoň 5 znaků"
			},
			heslo2: {
				required: "Vyplňte prosím heslo",
				minlength: "Vaše heslo musí mít alespoň 5 znaků",
				equalTo: "Heslo není stejné"
			},
			email: "Vložte prosím Váš email ve spravném formátu"
		}
	});


	/**** vypis.tpl ****/
	$('select[name=vyrobce]').change(function(){
	   $('.filtr').submit();
	   return false;
	});
        /*
	$('input.checkbox_varianty').click(function(){
	   $('.filtr').submit();
	   return false;
	}); */
	
	$('select[name=sortovani]').change(function(){
	    var asort = $('.asort').attr('href');
	    var dsort = $('.dsort').attr('href');
	
	    if(asort.search("cena")>0) {
	        asort = asort.replace("cena", "nazev");
	        dsort = dsort.replace("cena", "nazev");
	    } else {
	        asort = asort.replace("nazev", "cena");
	        dsort = dsort.replace("nazev", "cena");
	    }
	    $('.asort').attr('href', asort);
	    $('.dsort').attr('href', dsort);
	});


/**** paticka.tpl ****/

	$('.produktovy').click(function(){
		$('.mdarek').hide();
        $('h2.darkovy').removeClass("darkovy_active");
        $('h2.darkovy').addClass("darkovy_neactive");
        $('h2.produktovy').removeClass("produktovy_neactive");
        $('h2.produktovy').addClass("produktovy_active");
		$('.mprodukt').show();
        setCookieMinutes("mod", "produktovy", 5, "/");	
        return false;
	});
	$('.darkovy').click(function(){
		$('.mprodukt').hide();
        $('h2.darkovy').removeClass("darkovy_neactive");
        $('h2.darkovy').addClass("darkovy_active");
        $('h2.produktovy').removeClass("produktovy_active");
        $('h2.produktovy').addClass("produktovy_neactive");
		$('.mdarek').show();
        setCookieMinutes("mod", "darkovy", 5, "/");
        return false;	
	});
	
});
