/**
 * jQuery blikací plugin
 *
 * @author Jan Marek
 */
jQuery.fn.extend({
	blikat: function (timeout, opacity) {
		if (!timeout) timeout = 1000;
		if (!opacity) opacity = 0.5;

		var _this = this;

		setInterval(function () {
			_this.fadeTo(timeout / 4, opacity, function () {
				_this.fadeTo(timeout / 4, 1);
			});
		}, timeout);
	}
});

// ----

function blik() {
	$("#obsah-button").fadeTo("slow", 0.5, function () {
		$("#obsah-button").fadeTo("slow", 1);
	});

	setTimeout(blik, 10000);
}

$(function () {

	$("a.colorbox").colorbox();

	$(".jsonly").show();

	// a.blank jako target blank
	$("a.blank").each(function () {
		$(this).click(function () {
			window.open(this.href);
			return false;
		});
	});

	// Tabulky
	$("table tr:odd").addClass("odd");
	$("table tr:even").addClass("even");

	// workflow - bubliny
	$(".workflow a.target").each(function () {
		var _this = $(this);
		_this.hover(function () {
			$(_this.attr("href")).show();
		}, function () {
			$(_this.attr("href")).hide();
		}).click(function () {
			return false;
		});
	});

	// homepage - bubliny
	$(".right-col a.target").each(function () {
		var _this = $(this);
		_this.hover(function () {
			$(_this.attr("href")).show();
		}, function () {
			$(_this.attr("href")).hide();
		}).click(function () {
			return false;
		});
	});

	// button
	function blik() {
		$("#obsah-button").fadeTo("slow", 0.5, function () {
			$("#obsah-button").fadeTo("slow", 1);
		});

		setTimeout(blik, 10000);
	}

	setTimeout(blik, 2000);

	// rychly blik
	function blikFast () {
		$("#scanner-button").fadeTo("slow", 0.5, function () {
			$("#scanner-button").fadeTo("slow", 1);
		});

		$("#server-button").fadeTo("slow", 0.5, function () {
			$("#server-button").fadeTo("slow", 1);
		});

		setTimeout(blikFast, 2000);
	}

	setTimeout(blikFast, 2000);


	$("#obsah-button").click(function () {
		if ($("#call-form").is(":visible")) {
			$("#call-form").slideUp("slow");
		} else {
			$("#call-form").slideDown("slow", function () {
				$("#button-bubble").hide();
			});
		}
		return false;
	}).hover(function () {
		$("#button-bubble").show();
	}, function () {
		$("#button-bubble").hide();
	});

	// hack pro zobrazeni call formu nahore (v IE)
	$("#call-form-wrapper").appendTo("#page").css({
		'top' : '365px',
		'right' : '30px'
	});
	$("#button-bubble").appendTo("#page").css({
		'top' : '110px',
		'right' : '30px'
	});

	$('#frmnewsletter-s').replaceWith('<a href="#" onclick="$(this).parents(\'form\').submit()" class="button">ok</a>');

	setScreen(0);

	$(".screenshots-tabs a").click(function () {
		window.screens = window[$(this).attr("rel")];
		setScreen(0);
		$(this).parent().find('a').removeClass('active');
		$(this).addClass('active');
		return false;
	});

	function moveScreen(direction) {
		var current = $(".screenshots .screen").data("current");
		if (direction == -1 && current == 0) {
			setScreen(screens.length - 1);
		} else if (direction == 1 && current == screens.length - 1) {
			setScreen(0);
		} else {
			setScreen(current + direction);
		}
	}

	$(".screenshots .prev").click(function () {
		moveScreen(-1);
		return false;
	});

	$(".screenshots .next").click(function () {
		moveScreen(1);
		return false;
	});

	/* tabmenu */
	$(".faze").slice(1).hide();

	// schovame vsechny vlastnosti Schvalovani faktur prijatych
	$(".sfp-vlastnost").hide();

	// hack pro ie, natvrdo nastavime z-index u call formu
	$("#call-form").each(function () {
		$(this).css("z-index", "999");
	});

	// prepinani mezi mapou a fotkou kancelare
	$("#showphoto").click(function() {
		$("#contact-map").hide();
		$("#contact-photo").show();
		return false;
	});
	$("#showmap").click(function() {
		$("#contact-map").show();
		$("#contact-photo").hide();
		return false;
	});

	// calback form hack (nejde kliknout do formularoveho policka a psat do nej)
	if ($.browser.msie) {
		$("input").mouseover(function () {
			$(this).trigger("focus");
		});
	}

	// .new-homepage
	$(".new-homepage .tab, .new-homepage .tab-box").hover(function () {
		var images = {
			first: "schvalovani-faktur",
			second: "dokumentovy-sklad",
			third: "uspesny-obchodnik",
			fourth: "reklamacni-rizeni",
			fifth: "posta-dosla",
                        seventh: "helpdesk",
                        eighth: "enterprise"
		};
//                		var images = {
//			first: "schvalovani-faktur",
//			second: "schvalovani-faktur",
//			third: "reklamacni-rizeni",
//			fourth: "dokumentovy-sklad",
//			fifth: "enterprise"
//		};
		$(".new-homepage .tab, .new-homepage .tab-box, .new-homepage .cont").removeClass("active");

		var cssClass = "";

		if ($(this).is(".first")) cssClass = "first";
		if ($(this).is(".second")) cssClass = "second";
		if ($(this).is(".third")) cssClass = "third";
		if ($(this).is(".fourth")) cssClass = "fourth";
		if ($(this).is(".fifth")) cssClass = "fifth";
                if ($(this).is(".seventh")) cssClass = "seventh";
                if ($(this).is(".eighth")) cssClass = "eighth";
			
		$(".new-homepage ." + cssClass).addClass("active");
		$(".box-photo").attr("class", "box-photo " + images[cssClass]);
	});
	
	$(".new-homepage .tab").click(function () {
		return false;
	});

	// šipky

	$(".new-homepage .arr-right").blikat(2000, 0.6);

	$(".new-homepage .arr-right").click(function () {
		var first = $(".new-homepage .tabs a.tab:first");
		first.hide().appendTo(".new-homepage .tabs");
		$(".new-homepage .tabs a:eq(2)").show();

		$(".new-homepage .tab-boxes .tab-box:first").hide().appendTo(".new-homepage .tab-boxes");
		$(".new-homepage .tab-boxes .tab-box:eq(2)").show();

		if (first.is(".active")) {
			$(".new-homepage .tabs a.tab:first").mouseover();
		}

		return false;
	});

	$(".new-homepage .arr-left").click(function () {
		$(".new-homepage .tabs a.tab:last").show().prependTo(".new-homepage .tabs");
		$(".new-homepage .tabs a:eq(3)").hide();

		$(".new-homepage .tab-boxes .tab-box:last").show().prependTo(".new-homepage .tab-boxes");
		$(".new-homepage .tab-boxes .tab-box:eq(3)").hide();

		if ($(".new-homepage .tabs a.tab:eq(3)").is(".active")) {
			$(".new-homepage .tabs a.tab:eq(2)").mouseover();
		}

		return false;
	});

	// referenční řeč
	var ref = ["images/cont/ref-rec-1.jpg", "images/cont/ref-rec-2.jpg", "images/cont/ref-rec-3.jpg"];
	$(".referencni-rec .links .next").click(function () {
		ref.push(ref.shift());
		$(".referencni-rec img").attr("src", ref[0]);
		return false;
	});

	$(".referencni-rec .links .prev").click(function () {
		ref.unshift(ref.pop());
		$(".referencni-rec img").attr("src", ref[0]);
		return false;
	});

        //$("#box_faktury div.vid").show();
});



// screeny
function setScreen(i) {
	if (typeof(screens) == "undefined" || typeof(screens[i]) == "undefined") return;

	var scr = $(".screenshots .screen");
	var popis = $(".screenshots .popis");
	var heading = $(".screenshots .heading");

	scr.fadeOut("slow", function () {
		scr.css("background-image", 'url("' + ROOT + "files/images/" + screens[i][0] + '")')
		.data("current", i).fadeIn("slow");
	});

	popis.fadeOut("slow", function () {
		popis.html("<strong>" + (i + 1) + "/" + screens.length + "</strong> " + screens[i][1]).fadeIn("slow");
	});

	heading.fadeOut("slow", function () {
		var headingText = $("<div>" + screens[i][1] + "</div>").find("strong").text();
		heading.text(headingText).fadeIn("slow");
	});
}

function changePageMenuItem(i) {
	$(".tab-menu a").removeClass("active").eq(i).addClass("active");
	$(".faze").hide("slow").eq(i).show("slow");
	return false;
};

/**
 * Reference - stalo se - skryje a zobrazi detaily jednotlivych pribehu
 */
function readOn(id) {
	var visible = $("#" + id).is(":visible");
	$(".sfp-vlastnost:visible").slideToggle("slow", function () {
		$(this).hide();
	});
	if (!visible) $("#" + id).slideToggle("slow");
	return false;
}

/**
 * Prohlednete si inovio - preklikavani mezi videi
 */ 
function showVid(id) {
  $(".vid").hide();
  $("#" + id + " div.vid").show();

  $(".videoBox_big").removeClass("videoBox_big").addClass("videoBox_small");

  $("#" + id).removeClass("videoBox_small").addClass("videoBox_big");
  return false;
}
