/**
 * Zmena zobrazovane sady v jedne nabidce.
 * @param my_lock Zamek - jen jenvyssi hodnota bude spustena.
 * @param selected Rucne vybrany uzel, muze byt null, pokud je automaticka zmena.
 */
function changePage(my_lock, selected) {
	// Tento proces jiz nevlastni zamek nad funkci, funkce muze bezet jen jednou v jednom case.
	if (my_lock != lock) return;
	// Pokud je deaktivovano automaticke prepinani (jen jedna stranka, nebo rucne vybrano).
	if (stop) return;
	var speed = 600;

	// Prodleva
	var delay = 2400;

	// Prodleva nulova pokud rucne vybrano.
	if (selected != null) {
		delay = 0;
	}

	// Aktualne zobrazovane cislo stranky.
	var activePage = $("#switcher .active");
	// Odaktivni.
	activePage.delay(delay).fadeOut(speed, function() {activePage.removeClass("active")}).fadeIn(speed);

	// Zaktivni nove cislo stranky.
	// Pokud byla vybrana stranka, zaktivni jeji cislo, v opacnem pripade zaktivni dalsi/prvni, pokud dalsi neni.
	if (selected != null) {
		$("#switcher").removeClass("active");
		selected.delay(delay).fadeOut(speed, function() {selected.addClass("active")}).fadeIn(speed);
	} else if( activePage.next().length != 0 && !activePage.next().hasClass("invisible")) {
		activePage.next().delay(delay).fadeOut(speed, function() {
				activePage.next().addClass("active");
				if (my_lock != lock) {
					activePage.next().removeClass("active");
				}
			}).fadeIn(speed);
	} else {
		$("#switcher span").first().delay(delay).fadeOut(speed, function() {
				$("#switcher span").first().addClass("active");
				if (my_lock != lock) {
					activePage.next().removeClass("active");
				}
			}).fadeIn(speed);
	}

	var activePageContent;
	activePageContent = $("#produkty-promo > div:not(.invisible) > div:not(.switch-off)");
	activePageContent.delay(delay).fadeOut(speed * 2, function() {activePageContent.addClass("switch-off")});

	if (selected != null) {
		$("#produkty-promo > div:not(.invisible) > div").addClass("switch-off");
		var sel = $("#produkty-promo > div:not(.invisible) > .p-" + (selected.children("a").html()));
		sel.first().delay(delay).fadeIn(speed * 2, function() {sel.first().removeClass("switch-off"); clicked = false;});
	} else if( activePageContent.next("div").length != 0) {
		activePageContent.next().first().delay(delay).fadeIn(speed * 2, function() {
				activePageContent.next("div").first().removeClass("switch-off");
				if (my_lock != lock) {
					activePageContent.next("div").first().addClass("switch-off");
					activePageContent.next("div").first().css("display", "none");
				}
			});
	} else {
		$("#produkty-promo > div:not(.invisible) > div").first().delay(delay).fadeIn(speed * 2, function() {
				$("#produkty-promo > div:not(.invisible) > div").first().removeClass("switch-off");
				if (my_lock != lock) {
					activePageContent.next().first().addClass("switch-off");
					activePageContent.next().first().css("display", "none");
				}
			});
	}
	setTimeout("changePage(" + (my_lock) + ")", 10000);
}

stop = false;
lock = 1;
clicked = false;
$(document).ready(function(){

	var linesCount = $("#produkty-promo > div:not(.invisible) > div").length;

	if (linesCount == 1) {
		stop = true;
	} else if (stop) {
		stop = false;
		setTimeout("changePage(" + (++lock) + ")", 2000);
	}

	$("#switcher .switch:gt(" + (linesCount - 1 )+ ")").css("display", "none");
	$("#switcher .switch:gt(" + (linesCount - 1 )+ ")").addClass("invisible");

	$("#prodejny > div").addClass("invisible");
	$("#prodejny div:first").removeClass("invisible");

	$("#produkty-promo div div.switch-off").css("display", "none");
	setTimeout("changePage(" + (++lock) + ")", 7000);

	$("#prodejny-tabs a").click(function(){
		$("#prodejny > div").addClass("invisible");
		$("div#" + this.hash).removeClass("invisible");

		$("#prodejny-tabs li").removeClass("active");
		$(this).parent().addClass("active");
		return false;
			
	});
   
	$("#kontakty-tabs a").click(function(){
		$("#kontakty > div").addClass("invisible");
		$("div#" + this.hash).removeClass("invisible");

		$("#kontakty-tabs li").removeClass("active");
		$(this).parent().addClass("active");
		return false;
			
	});
   
	$("#produkty-tabs a").click(function(){
		$("#produkty-promo > div.akce").addClass("invisible");
		$("div#" + this.hash).removeClass("invisible");

		$("#produkty-tabs li").removeClass("active");
		$(this).parent().addClass("active");

		$("#switcher .switch").removeClass("active");
		$("#switcher .switch").first().addClass("active");
		$("#switcher .switch").stop(true, false);
		$("#switcher .switch").css("opacity", "1");

		$("#produkty-promo > div > div").addClass("switch-off");
		$("#produkty-promo > div:not(.invisible) > div").first().removeClass("switch-off");
		$("#produkty-promo > div > div.switch-off").css("display", "none");
		$("#produkty-promo > div:not(#switcher) > div:not(.switch-off)").css("display", "block");

		var linesCount = $("#produkty-promo > div:not(.invisible) > div").length;
		if (linesCount == 1) {
			stop = true;
		} else if (stop) {
			stop = false;
			setTimeout("changePage(" + (++lock) + ")", 2000);
		}

		$("#switcher .switch:gt(" + (linesCount - 1 )+ ")").css("display", "none");
		$("#switcher .switch:gt(" + (linesCount - 1 )+ ")").addClass("invisible");

		$("#switcher .switch:lt(" + (linesCount)+ ")").removeClass("invisible");
		$("#switcher .switch:lt(" + (linesCount)+ ")").css("display", "block");

		return false;
	});

	// Akce pro zmenu stranek nabidky.
	$("#switcher .switch").click(function(){
		if (clicked) {return false;}

		if ($(this).hasClass("active")) {return false;}

		clicked = true;
		stop = false;
		changePage(++lock, $(this));
		stop = true;
		return false;
	});
});
