var FG_HP_Slider_Listener_Enabled = true;

function initSliderHP() {
	var actualSlideIndex = 0;
	var buttonsCount = $(".promo-hp .nav-buttons>li").size();

	//nav arrows

	$(".promo-hp").append('<a href="#" class="slider-arrow slider-arrow-left" rel="-1">Předcházející</a>');
	$(".promo-hp").append('<a href="#" class="slider-arrow slider-arrow-right" rel="1">Další</a>');

	prepareNavArrows(actualSlideIndex);

	$(".promo-hp a.slider-arrow").bind("click", function(event) {
		event.preventDefault();

		if (!FG_HP_Slider_Listener_Enabled || $(this).hasClass("disabled")) {
			return false;
		}

		var offset = $(this).attr("rel");
		if (offset == "1") { actualSlideIndex++; }
		if (offset == "-1") { actualSlideIndex--; }
		doAnimate(actualSlideIndex);
	});

	//$(".promo-hp .items>ul>li:gt(0)").hide();
	$(".promo-hp .nav-buttons>li:first").addClass("active");

	// vypocet sirky
	var liWidth = $(".promo-hp .items>ul>li:first").width();
	var liCount = $(".promo-hp .items>ul>li").size();

	$(".promo-hp .items>ul").width(liWidth*liCount+10);


	$(".promo-hp .nav-buttons>li>a").bind("click", function(event) {
		event.preventDefault();

		// pokud bezi animace, click se ignoruje
		if (!FG_HP_Slider_Listener_Enabled) {
			return false;
		}

		actualSlideIndex = $(".promo-hp .nav-buttons>li>a").index(this);
		doAnimate(actualSlideIndex, this);
	});

	function prepareNavArrows(ix) {
		$(".promo-hp a.slider-arrow").removeClass("disabled");
		if (ix == 0) {
			$(".promo-hp .slider-arrow-left").addClass("disabled");
		}
		if (ix+1 == buttonsCount) {
			$(".promo-hp .slider-arrow-right").addClass("disabled");
		}
	};

	function doAnimate(ix) {
		$(".promo-hp .nav-buttons>li").removeClass("active");
		$(".promo-hp .nav-buttons>li:eq("+ix+")").addClass("active");

		FG_HP_Slider_Listener_Enabled = false;
		$(".promo-hp .items>ul").animate({
			marginLeft: -1*ix*liWidth
		}, 800, "easeOutBack", function() {
			FG_HP_Slider_Listener_Enabled = true;
			prepareNavArrows(ix);
		});
	}

	return true;
}

function initQuickNav() {
	var actualSlideIndex = 0;
	var lastSlideIndex = $(".quick-nav ul>li").size()-1;

	//debugItems();

	// vypocet sirky
	var liWidth = recountWidth();

	if ($(".quick-nav ul").width() > liWidth) {
		return false;
	}

	$(".quick-nav ul").width(liWidth+10);

	//nav arrows
	$(".quick-nav").append('<a href="#" class="slider-arrow slider-arrow-left" rel="-1">Předcházející</a>');
	$(".quick-nav").append('<a href="#" class="slider-arrow slider-arrow-right" rel="1">Další</a>');

	$(".quick-nav a.slider-arrow").bind("click", function(event) {
		event.preventDefault();

		if (!FG_HP_Slider_Listener_Enabled) {
			return false;
		}

		var offset = $(this).attr("rel");
		doAnimate(actualSlideIndex, offset);
	});



	// append and prepend
	var items = new Array();

	$(".quick-nav ul>li").each(function() {
		items[$(".quick-nav ul>li").index(this)] = $(this).html();
	});

	function duplicateItems(ix, direction) {
		var prevIx, nextIx;
		prevIx = (ix == 0) ? items.length-1 : ix-1;
		nextIx = (lastSlideIndex == (items.length-1)) ? 0 : lastSlideIndex+1;

		var animateOffsetValue = 0;

		if (direction == "1") {
			$(".quick-nav ul").append('<li>'+items[nextIx]+'</li>');
			animateOffsetValue = $(".quick-nav ul>li:first").width();
			lastSlideIndex = nextIx;
			actualSlideIndex = (actualSlideIndex != (items.length-1)) ? actualSlideIndex+1 : 0;
		}
		if (direction == "-1") {
			$(".quick-nav ul").prepend('<li>'+items[prevIx]+'</li>');
			animateOffsetValue = $(".quick-nav ul>li:first").width();
			$(".quick-nav ul").css( { "margin-left" : "-"+animateOffsetValue+"px" });
			actualSlideIndex = prevIx;
			lastSlideIndex = (lastSlideIndex != 0) ? lastSlideIndex-1 : (items.length-1);
		}
		$(".quick-nav ul").width(recountWidth());

		return animateOffsetValue;
	}

	function debugItems() {
		if (window.console && window.console.log) {
			$(".quick-nav ul>li").each(function() {
				console.log($("a", $(this)).text());
			});
			console.log("=====================================================================");
		}
	}

	function doAnimate(ix, direction) {
		FG_HP_Slider_Listener_Enabled = false;
		var liWidth = duplicateItems(ix, direction);

		if (direction == "1") {
			$(".quick-nav ul").animate({
				marginLeft: -1*liWidth
			}, 300, function() {
				FG_HP_Slider_Listener_Enabled = true;
				$(".quick-nav ul>li:first").remove();
				$(".quick-nav ul").css( { "margin-left" : "0px" });
				debugItems();
			});
		}
		if (direction == "-1") {
			$(".quick-nav ul").animate({
				marginLeft: 0
			}, 300, function() {
				FG_HP_Slider_Listener_Enabled = true;
				$(".quick-nav ul>li:last").remove();
				debugItems();
			});
		}
	}

	function recountWidth() {
		var liWidth = 0;
		$(".quick-nav ul>li").each(function() {
			liWidth += $(this).width();
		});
		return liWidth;
	}

	return true;
}

function initBannersHp() {
	$(".bannerhp-with-rightarrow").css({ cursor : "pointer"}).bind("click", function() {
		window.location.href = $("a:first", $(this)).attr("href");
	});
}

function validateForm(formName) {
	var questions = document.forms[formName]["recordCount"].value,
		answersSelected = $('input[name^="frm.answer"]:checked').size();

	if (answersSelected<questions)
		return window.confirm(locale["examPrompt"]);
	return true;
}


/**
 * FHL FG google map jquery plugin
 */
(function($){
	$.fn.fggm = function(options) {
		var opts = $.extend({}, $.fn.fggm.defaults, options);

		// hlavní cyklus
		this.each(function() {
			var $t = $(this);
			$t.css({
				height: opts.height,
				width: opts.width
			});
			var map = $.fn.fggm.buildMap(opts, $t);
			$.fn.fggm.placeMarkers(map, new google.maps.LatLng(opts.startingPosition.lat, opts.startingPosition.lng));
		});	// konec hlavního cyklu
	};

	$.fn.fggm.buildMap = function(o, $t) {
		var latLng = new google.maps.LatLng(o.startingPosition.lat, o.startingPosition.lng);
		map = new google.maps.Map(document.getElementById($t.attr("id")), {
			zoom: o.startingPosition.zoom,
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			streetViewControl: false
		});

		return map;

	};

	$.fn.fggm.placeMarkers = function(m, l) {

		// vytvoření markeru
		return marker = new google.maps.Marker({
			position: l,
			map: m,
			icon: new google.maps.MarkerImage('/img/u/gmaps/marker-v2.png', new google.maps.Size(81,38), new google.maps.Point(0,0), new google.maps.Point(40,38)),
			shadow: new google.maps.MarkerImage('/img/u/gmaps/shadow-marker-v2.png', new google.maps.Size(101,38), new google.maps.Point(0,0), new google.maps.Point(40,38)),
			draggable: false
		});
	};

	$.fn.fggm.defaults = {
		width						:	300,
		height						:	240,
		startingPosition			:	{
			lat							:	50.059716,
			lng							:	14.413806,
			zoom						:	11
		},
		markers						: []
	};

	$.fn.fggm.StringBuffer = function() {
		this.buffer = "";
		this.append = function(str) { this.buffer += str; return this; };
		this.getString = function() { return this.buffer; };
		this.reset = function() { this.buffer=""; };
	};

	$.fn.fggm.debug = function(str) {
		if (window.console && window.console.log) console.log(str);
	};
})(jQuery);

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

	// key and at least value given, set cookie...
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);

		if (value === null || value === undefined) {
			options.expires = -1;
		}

		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}

		value = String(value);

		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}

	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

$(document).ready(function() {
	initBannersHp();

	if ($.cookie('background') != null) {
		$("body").removeClass("bg-1").addClass("bg-"+$.cookie("background"));
		$("#backgroundSelect li").removeClass("active");
		$("#backgroundSelect li:eq("+($.cookie("background")-1)+")").addClass("active");
	}
	$("#backgroundSelect li").click(function() {
		if ($.cookie('background') != null) {
			$("body").removeClass("bg-"+$.cookie("background")).addClass("bg-"+($("#backgroundSelect li").index(this)+1));
		} else {
			$("body").removeClass("bg-1").addClass("bg-"+($("#backgroundSelect li").index(this)+1));
		}
		$("#backgroundSelect li").removeClass("active");
		$(this).addClass("active");
		$.cookie("background",($("#backgroundSelect li").index(this)+1), { path: '/' });
	});
});

