$(document).ready(function()
{
    // funkce na nahodne prohazeni elementu -- thx: http://yelotofu.com/2008/08/jquery-shuffle-plugin/
    (function($)
    {
        $.fn.shuffle = function()
        {
            return this.each(function()
            {
                var items = $(this).children();
                return (items.length) ? $(this).html($.shuffle(items)) : this;
            });
        }
        
        $.shuffle = function(arr)
        {
            for(
                    var j, x, i = arr.length; i;
                    j = parseInt(Math.random() * i),
                    x = arr[--i], arr[i] = arr[j], arr[j] = x
            );
            return arr;
        }
    })(jQuery);
 

    
    
    // Vypis provozoven pro jednotlive znacky v pravem sloupecku
	$("#sloupecek_znacky a").each(
		function (i)
		{
		   $(this).mouseover(
			   function ()
			   {
				   var znacka = $(this).attr("rel"); // name znacky
				   var id = "vrakoviste_" + znacka; // id HTML prvku, ktery bude obsahovat seznam provozoven dane znacky
				   
				   // skryt vsechny elementy obsahujici seznam provozoven
                   $("div.provozovny_podle_znacky").each(
					    function ()
						{
							if ($(this).attr("id") != id)
							{
								$(this).slideUp("fast")
							}
						}
				   );
				   if ($("#" + id).length > 0) // pokud jiz element existuje
				   {
					   // zobrazime element
					   $("#" + id).show("slow");
				   }
				   else // jinak ho nahrajeme a zobrazime
				   {
				   	   $(this).after("<div id='" + id + "' class='provozovny_podle_znacky'></div>") //<img src='/images/loading.gif' alt='loading...' class='loading' />
					   $("#" + id).load("/modules/provozovny/ajax.seznam-provozoven.php?znacka=" + znacka, {}).slideDown("slow");
				   }
			   }
		   )
		}
	);
    
    // lightbox init
    $('.fotky a').lightBox();
    
    //skryvane boxy (dalsi sluzby, etc)
    $('.shuffle').shuffle();
    $('.skryvat').hide();
    $('.odkryvat a').each(
        function (i)
        {
            $(this).click(
                function ()
                {
                    //alert( $(this).attr('href').substring(1) );
                    $('.skryvat').each(
                        function ()
                        {
                            if ($(this).css('visibility') != 'hidden')
                            {
                                $(this).slideUp('slow');
                            }
                        }
                    );
                    
                    $($(this).attr('href')).slideDown('slow');
                }
            );
        }
    );
    
    // automaticke preklady
    $('#obsah').append("<div id='googleTranslate'></div>");
    $('#googleTranslate').load('/modules/googleTranslate/translateForm.php', {}, function()  //nacteni formulare
    {
        // animace
        /*
        $('#googleTranslate a').fadeTo('fast', 0.4);
        $('#googleTranslate a').each(function()
        {
            $(this).hover(
                function()
                {
                    $(this).not('.googleTranslate_selected').fadeTo('fast', 1);
                },
                function()
                {
                    $(this).not('.googleTranslate_selected').fadeTo('fast', 0.4);
                }
            ); 
        });
        */
        // zaveseni prekladacich funkci
        $('#googleTranslate a')
            .click(function()
            {
                var lng = $(this).attr('rel');
                
                $(this).addClass('googleTranslate_selected').fadeTo('fast', 1);
                $('#googleTranslate a')
                    .unbind('click')
                    .click(function()
                    {
                        $.cookie('GoogleTransalte', $(this).attr('rel'), { expires: 0, path: '/', secure: false });
                        window.location.reload();
                    });

                $("#content *").not("#mapa_provozoven, #mapa_provozoven *").sundayMorning({
                    source:'cs',
                    destination: lng
                });
                // ulozeni informace o prelozeni do cookie
                $.cookie('GoogleTransalte', lng, { expires: 0, path: '/', secure: false });
            });                                    
        // pokud je v cookie ulozena informace o prekladu do nejakeho jazyka, spustime preklad
        var targetLng = $.cookie('GoogleTransalte');
        if (targetLng && targetLng != 'cs')
        {
            $('#googleTranslateLng_' + targetLng).click();
        }
    });
});