// JavaScript Document
var WhispererTimer = 0;
var SwitcherTimer  = 0;
var SwitcherCount  = 5;
var SwitcherAct    = 1;
var SwitchTime     = 4000;
var ajaxRunning    = false;
var FB             = null;
	/*
function googleTranslateElementInit() {
	new google.translate.TranslateElement({
		pageLanguage: 'cs'
	}, 'googleTranslateElement');
}
*/
$(function()
{
	$('a.lightbox').lightBox();
});
$(document).ready(function(){

	//smerovani do noveho okna
	targetBlank();
	switchSwitcherImagesStart();

	// TopTen (Top10)
	tt_DOM = $('#boxTop10');
	tt_setHeightInner();
	// Obsluha Navigace arrow left
	$('#boxTop10LabelNaviLeft', tt_DOM).click(function(event){
		clearTimeout(tt_saverTimer);
		tt_stepToLeft();
		tt_initSaver();
		event.preventDefault();
	});
	// Obsluha Navigace arrow right
	$('#boxTop10LabelNaviRight', tt_DOM).click(function(event){
		clearTimeout(tt_saverTimer);
		tt_stepToRight();
		tt_initSaver();
		event.preventDefault();
	});
	// Inicializace spořiče
	tt_initSaver();


	$('input').attr('autocomplete','off');

	// obsluha tlačítka Přihlásit
	$('#topMenuBox5top').click(function(event){
		$('#topMenuBox5Buttons').fadeOut(0, function(){
			$('#topMenuBox5Login').fadeIn();
		});
		event.preventDefault();
	});
	$('#topMenuBox5LoginBtClose').click(function(event){
		$('#topMenuBox5Login').fadeOut(0, function(){
			$('#topMenuBox5Buttons').fadeIn();
		});
		event.preventDefault();
	});

	// obsluha boxu Doporučit
	/*
	$('#middleLeftRecomendLabel').toggle(
		function(){
			$('#choices').fadeIn(500, function(){
				$('#middleLeftRecomendLabel').css('backgroundImage',"url('/images/layout/rec_arrow_open.jpg')");
			});
		},
		function(){
			$('#choices').fadeOut(500, function(){
				$('#middleLeftRecomendLabel').css('backgroundImage',"url('/images/layout/rec_arrow_close.jpg')");
			});
		}
	);
*/

	$('#topMenuBox5Login input[type=password]')
	.blur(function()
	{
			if ($(this).val()=='')
			{
			$(this).hide().next().show();
			}
	})
	.hide().after('<input type=\"text\" class=\"inputText\" value=\"'+textLoginPass+'\" />')
	.next().focus(function()
	{
		$(this).hide().prev().show().val('').focus();
	});

	$('.shareLinksLabel').click(function(event){
		event.preventDefault();
		$(this).next().toggle();
	});

	$(".middleLeftMenuMain").hover(
		function()
		{
			$(this).children("ul").show();
		},
		function()
		{
			$(this).children("ul").hide();
		}
	);
	$('#googleBt').parent().hover(
		function()
		{
			$('#googleTranslateElement').fadeIn(500);
			$('.goog-te-banner-frame').show();
			$('.goog-te-gadget').html($('.goog-te-gadget').html().replace(strUseTechnology,''));
		},
		function()
		{
			//$('#googleTranslateElement').fadeOut(500)
		}
	);
	// obsluha vstupu Hledat restaurace
	$('#boxFrmSearch input[name=searchtext]').focus(function()
	{
		if ($(this).val() == textSearchDefault)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefault);
		}
	}).keyup(function(){
		//Ajaxove se dodazovat
		if($(this).val().length >= 3)
			fillWhisperer($(this).val());
		else
		{
			$('#boxFrmSearchWhisperer').html('');
			$('#boxFrmSearchWhisperer').hide();
		}
	});
	$('#rozcestnikContainerSearch input[name=searchtext]').focus(function()
	{
		if ($(this).val() == textSearchDefault)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefault);
		}
	}).keyup(function(){
		//Ajaxove se dodazovat
		if($(this).val().length >= 3)
			fillWhispererSW($(this).val());
		else
		{
			$('#rozcestnikContainerSearchWhisperer').html('');
			$('#rozcestnikContainerSearchWhisperer').hide();
		}
	});

	$('#boxFrmSearch input[name=searchtextfrom]').focus(function()
	{
		if ($(this).val() == textSearchDefaultFrom)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefaultFrom);
		}
	});
	$('#boxFrmSearch input[name=searchtextto]').focus(function()
	{
		if ($(this).val() == textSearchDefaultTo)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefaultTo);
		}
	});

	$('#track input[name=searchtextfrom]').focus(function()
	{
		if ($(this).val() == textSearchDefaultFrom)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefaultFrom);
		}
	});
	$('#track input[name=searchtextto]').focus(function()
	{
		if ($(this).val() == textSearchDefaultTo)
		{
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(textSearchDefaultTo);
		}
	});

	$("#searchSwitchTrack").click(function(event){
		event.preventDefault();
		$("#searchSwitchTrack").hide();
		$("#searchSwitchFull").show();
		$("#boxFrmSearchFullText").hide();
		$("#boxFrmSearchTrack").show();
		$("#boxFrmSearch input[name=searchtext]").val(textSearchDefault);
	});
	$("#searchSwitchFull").click(function(event){
		event.preventDefault();
		$("#searchSwitchTrack").show();
		$("#searchSwitchFull").hide();
		$("#boxFrmSearchFullText").show();
		$("#boxFrmSearchTrack").hide();
		$("#boxFrmSearch input[name=searchtextfrom]").val(textSearchDefaultFrom);
		$("#boxFrmSearch input[name=searchtextto]").val(textSearchDefaultTo);
	});

	//podrobne vyhledavani
	$("#searchSwitchToTrack").click(function(event){
		event.preventDefault();
		$("#searchSwitchToTrack").hide();
		$("#searchSwitchToFull").show();
		$("#fulltext").hide();
		$("#track").show();
		$("#fulltext input[name=searchtext]").val(textSearchDefault);
	});
	$("#searchSwitchToFull").click(function(event){
		event.preventDefault();
		$("#searchSwitchToTrack").show();
		$("#searchSwitchToFull").hide();
		$("#fulltext").show();
		$("#track").hide();
		$("#track input[name=searchtextfrom]").val(textSearchDefaultFrom);
		$("#track input[name=searchtextto]").val(textSearchDefaultTo);
	});

	/*
	var textLoginDefault = 'jméno';
	$('#boxFrmLogin input[name=login]').focus(function()
	{
		if ($(this).val() == textLoginDefault)
		{
			$(this).val('');
		}
	}).blur(function()
	{
		if ($(this).val() == '')
		{
			$(this).val(textLoginDefault);
		}
	});
*/
	$('.mustBeLogged').click(function(event){
		event.preventDefault();
		var link = $("#topMenuBox5LoginHrefPass").attr('href').split('?')[1];
		tb_show(textLogin,'/ajax_login?'+link+'&height=278&width=400',false);
		//alert('Musíte být registrován a příhlášen');
	});
	// obsluha vstupu Prihlasit

	$('#boxFrmLogin input[name=login]').focus(function(){
		if ($(this).val() == textLoginDefault) {
			$(this).val('');
		};
	}).blur(function(){
		if ($(this).val() == '') {
			$(this).val(textLoginDefault);
		};
	});

	$('#googleTranslateElement select[name=googleTranslate]').change(function(){
		translate($(this).val());
	});

	$("#fbPublish").click(function(event)
	{
		event.preventDefault();
		FBPublish();
	});

	$("#footerSwitcher").click(function(event){
		$("#footerSwitcher").hide();
		$("#footerSwitcherDown").show();
		$("#footer").css("bottom",null);
		$("#footer").css("top",0);
		$("#containter").slideUp(1000);
		$("#rozcestnikContainer").slideDown(1000,function(){
			$('body').css('background-image', 'url("/images/rozcestnik/switcher_bg.jpg")');
		});
		event.preventDefault();
	});
	$("#footerSwitcherDown").click(function(event){
		$("#footerSwitcherDown").hide();
		$("#footerSwitcher").show();
		$("#footer").css("bottom",0);
		$("#footer").css("top",null);
		$("#rozcestnikContainer").slideUp(1000);
		$("#containter").slideDown(1000);
 		$('body').css('background-image', 'none')
		event.preventDefault();
	});

	$(".containerTile").hover(
		function()
		{
			if(SwitcherTimer)
				clearInterval(SwitcherTimer);
			var id = $(this).attr('id').split('_',2)[1];
			SwitcherAct = id;
			$("#rozcestnikContainerImage").attr('src', '/images/rozcestnik/top_image'+SwitcherAct+".jpg");
		},
		function()
		{
			switchSwitcherImagesStart();
		}
	);

	$(".vocabularyString").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 250,
		opacity: 1,
		fixPNG: true,
		showBody: " - ",
		extraClass: "pretty"
	});

	if(FB)
	{
		FB.init("f7c051f934deb24617dd7115b233e3bc");

		$("#FaceBookFanHolder").html('<fb:fan height="290" width="188" logobar="1" connections="6" stream="0" profile_id="143943738973858" class=" FB_fan FB_ElementReady"></fb:fan>');
	}
});

function fillWhisperer(val)
{
	if(WhispererTimer)
		clearInterval(WhispererTimer);
	if(!ajaxRunning)
	{
		WhispererTimer = setTimeout("fillWhispererAJAX('"+val+"')",1000);
	}
}

function fillWhispererSW(val)
{
	if(WhispererTimer)
		clearInterval(WhispererTimer);
	if(!ajaxRunning)
	{
		WhispererTimer = setTimeout("fillWhispererSWAJAX('"+val+"')",1000);
	}
}

function fillWhispererAJAX(val)
{
	ajaxRunning = true;
	var time = new Date();
	$.ajax({
		url: '/whisperer',
		data: 'search='+val+'&r='+Number(time),
		success: function(data) {
			$('#boxFrmSearchWhisperer').html(data);
			$('#boxFrmSearchWhisperer').show();
			$('#boxFrmSearchWhisperer a').click(function(event){
				$('#boxFrmSearch input[name=searchtext]').val( $(this).text() ).focus();
				$('#boxFrmSearchWhisperer').hide();
				event.preventDefault();
			});

			ajaxRunning = false;
		}
	});

}

function fillWhispererSWAJAX(val)
{
	ajaxRunning = true;
	var time = new Date();
	$.ajax({
		url: '/whisperer',
		data: 'search='+val+'&r='+Number(time),
		success: function(data) {
			$('#rozcestnikContainerSearchWhisperer').html(data);
			$('#rozcestnikContainerSearchWhisperer').show();

			$('#rozcestnikContainerSearchWhisperer a').click(function(event){
				$('#rozcestnikContainerSearch input[name=searchtext]').val( $(this).text() ).focus();
				$('#rozcestnikContainerSearchWhisperer').hide();
				event.preventDefault();
			});

			ajaxRunning = false;
		}
	});
}

function targetBlank()
{
	// vybereme všechny odkazy
	$("a.targetBlank").click(function(event)
	{
			window.open($(this).attr("href"), "");
			event.preventDefault();
	});
}


function switchSwitcherImages()
{
	SwitcherAct++;
	if(SwitcherAct > SwitcherCount)
		SwitcherAct = 1;
	$("#rozcestnikContainerImage").fadeOut(250,function(){
		$(this).attr('src', '/images/rozcestnik/top_image'+SwitcherAct+".jpg").fadeIn(250);
	});
}

function switchSwitcherImagesStart()
{
	SwitcherTimer = setInterval("switchSwitcherImages()",SwitchTime);
}

function translate(targetLanguage)
{
	if(targetLanguage=="orig")
	{
		return;
	}
	else if(targetLanguage!=''){
		window.top.location='http://translate.google.com/translate'+'?client=tmpg&hl=cs&'+targetLanguage+'&u='+unescape(window.top.location);
	}
}


function FBPublish()
{
	var url = "http://www.facebook.com/sharer.php"
	url += "?u=" + window.location
	url += "&t=" + document.title

	window.open(url, "fbshare", "width=520,height=250");
}

/**
 * Spořič pro TopTen (Top10)
 */
var tt_DOM = null; //DOM object top10
var tt_saverTimer = null; //timer pro sporic
var tt_saverNextStep = 5000; //milisekund do dalsiho kroku
function tt_initSaver() {
	if (tt_DOM) {
		tt_saverTimer = setTimeout("tt_doNextStep()", tt_saverNextStep);
	}
}
function tt_doNextStep() {
	tt_stepToRight();
	tt_saverTimer = setTimeout("tt_doNextStep()", tt_saverNextStep);
}
function tt_stepToRight() {
	var nextItem = $('#boxTop10LabelNaviItems a:visible', tt_DOM).eq(0).next();
	if (!nextItem.text()) {
		nextItem = $('#boxTop10LabelNaviItems a', tt_DOM).first();
	}
	$('#boxTop10LabelNaviItems a', tt_DOM).hide();
	$(nextItem).show();
	tt_changeInner( $(nextItem).attr('class'), 'right' );
}
function tt_stepToLeft() {
	var prevItem = $('#boxTop10LabelNaviItems a:visible', tt_DOM).eq(0).prev();
	if (!prevItem.text()) {
		prevItem = $('#boxTop10LabelNaviItems a', tt_DOM).last();
	}
	$('#boxTop10LabelNaviItems a', tt_DOM).hide();
	$(prevItem).fadeIn();
	tt_changeInner( $(prevItem).attr('class'), 'left' );
}
function tt_changeInner(visibleIdentifier, direction) {
	if (direction=='right') {
	    var items = $('#boxTop10InnerItems');
	    $(items)
			.children().eq(1).show().end().end()
			.animate(
				{"left":"-186px"},"slow",
				function(){
					$(this)
						.append( $(this).children().eq(0).clone(true) )
						.children().last().hide().end()
						.eq(0).remove().end().end()
						.css("left","0px");
     				tt_setHeightInner();
				});
	}
	else if (direction=='left') {
	    var items = $('#boxTop10InnerItems');
	    $(items).css("left","-186px");
	    $(items).prepend( $(items).children().last().clone(true) );
		$(items).children().eq(0).show();
		$(items).animate({"left":"0px"}, "slow", function(){
			$(items).children().last().remove();
			$(items).children().eq(1).hide();
   			tt_setHeightInner();
		});

	}
}
function tt_setHeightInner() {
	var h = $('#boxTop10InnerItems', tt_DOM).children(':visible').outerHeight();
	$('#boxTop10InnerItems', tt_DOM).parent().height( h );
}

function strpos (haystack, needle, offset) {
	var i = (haystack+'').indexOf(needle, (offset || 0));
	return i === -1 ? false : i;
}



// Funkce pro kontrolu jestli byly vstupni pole ve formulari pro prihlaseni vyplneni,
// a pripadne povoleni/zakazani prihlasovaciho tlacitka

$(document).ready(function(){
	
	$('#boxFrmLogin input[name=login]').bind('keyup',function(){
		checkLoginFormOnBlur();
	});
	
	$('#boxFrmLogin input[name=pass]').bind('keyup',function(){
		checkLoginFormOnBlur();
	});

	checkLoginFormOnBlur();
});

function checkLoginFormOnBlur()
{
	var jmenoInput			= $('#boxFrmLogin input[name=login]');
	var passInput			= $('#boxFrmLogin input[name=pass]');
	var btnSubmit			= $('#boxFrmLogin input[type=submit]');

	var isNotDefaultForm	= true;


	if ( $(jmenoInput).length > 0 && ($(jmenoInput).val().length==0 || $(jmenoInput).val()==textLoginDefault ) )
	{
		isNotDefaultForm	= false;
	}

	if ( $(passInput).length > 0 && ($(passInput).val().length==0 || $(passInput).val()==textLoginPass ) )
	{
		isNotDefaultForm	= false;
	}

	if ( isNotDefaultForm )
	{
		// Pokud je zmeneny anebo neni prazdny formular, povolime prihlasovaci tlacitko

		if ( $(btnSubmit).length > 0 )
		{
			$(btnSubmit).attr('disabled', false);
			$(btnSubmit).removeClass('disabled');
		}
	}
	else
	{
		// Pokud je nezmeneny anebo prazdny formular, zakazeme prihlasovaci tlacitko

		if ( $(btnSubmit).length > 0 )
		{
			$(btnSubmit).attr('disabled', true);
			$(btnSubmit).addClass('disabled');
		}
	}
}
