/**
 * Javascriptove funkce
 */

$(document).ready(function(){

   $(function () {
    $('#textLayout').countdown(
      {until: new Date(2012, 7-1, 03), layout: '{dn} {dl}/{hn} {hl}/{mn} {ml}/{sn} {sl}'});
    });
          
    // myší hover fade na něco
    $(".mousefade").fadeTo('fast',0.4);
    
    $(".mousefade").mouseenter(function(){
      $(this).fadeTo('fast',1);
    })  
                   .mouseleave(function(){
      $(this).fadeTo('fast',0.4);
    })  
    
    // fix na # anchory v textu stran BASE HREF
    $("a[href^='\#']").click(function(e){
      e.preventDefault();
      document.location.hash=this.href.substr(this.href.indexOf('#')+1);
    })
    
    // antispamové dobrůtky
    $("#protirobotum").val(Math.round(5.8));
    $("#spamprotirobotum").css({'display' : 'none'});
        
    
    
    // vybernik fotogalerie
    $("#photogallery_select").
        change(function () {
           location.href = serverUrl + $(this).val();
        });
        
    // jsem zde poprve
    /*
    $("#jsem_zde_poprve_button").
        click(function () {
            $(document).unbind("click");

            $("#jsem_zde_poprve_bg")
                .css({
                    'width'   : $(document).width(),
                    'height'  : $(document).height(),
                    'top'     : 0,
                    'left'    : 0,
                    'opacity' : 0.5,
                    'display' : 'none'
                })
               .fadeIn(500);

            $("#jsem_zde_poprve_popup")
                .css({
                    'left' : (($(document).width() - $("#jsem_zde_poprve_popup").width()) / 2)
                })
                .fadeIn(
                    500,
                    function () {
                        $(document)
                            .bind(
                                "click",
                                function(){
                                    $("#jsem_zde_poprve_popup")
                                        .fadeOut(500);

                                    $("#jsem_zde_poprve_bg")
                                        .fadeOut(500);
                                }
                            );
                    }
                );

        });
*/

    // karusel
    $("#carousel")
        .carousel();

    // photostream
    var small_number_handler_call = false;

    // skryva tooltip photostreamu
    function hideTooltip() {
        $(".photostream-over-bg").fadeOut(500);
        $(".photostream-tooltip").fadeOut(500);
    }

    $("#photostream_obrazky")
        .photostream({
            item : "a",

            small_number_handler : function () {
                if (!small_number_handler_call) {
                    small_number_handler_call = true;
                       
                    $.post(
                        serverUrl + "ajax/photostream_get_items.php",
                        {
                            number : 3
                        },
                        function (images) {
                            var imgLast = $("#photostream_obrazky a:last");

                            $.each(images, function (i) {
                                var src = this.src;
                                if (src.substr(0, 1) != '/') { src = '/' + src;}
                                var a =
                                    $('<a href="' + this.url + '" class="' + this.className + '" />').css({
                                        left : parseInt(imgLast.css("left")) + (i + 1) * imgLast.outerWidth(true)
                                    });

                                a.append(
                                    $('<img src="' + src + '" />')
                                );

                                $("#photostream_obrazky").append(a);
                            });

                            small_number_handler_call = false;
                        },
                        "json"
                    );
                }
            },

            item_over_handler : function (item, event) {
                $(item).data("timer",
                    setTimeout(function () {
                        var pathParts = $('img', item).attr("src").split('/');
                        var fileParts = pathParts[pathParts.length - 1].split('_');
                        fileParts.shift();

                        pathParts[pathParts.length - 1] = fileParts.join('_')

                        var src = pathParts.join('/');

                        var tooltip = $(".photostream-tooltip");

                        if (!tooltip.length) {
                            tooltip = $('<div class="photostream-tooltip" />');

                            $('body').append(tooltip);
                        }
                        
                        tooltip.html('<img src="' + src + '" alt="" class="photostream-photo" />');
                        tooltip.css({
                            left : event.pageX,
                            //left : ($(document).width() / 2) - ($(".photostream-photo").width() /2), 
                            top  : event.pageY
                        });
                        tooltip.show();
                        // alert ($(".photostream-photo").width());

                        // zasednuti pozadi
                        var overBg = $(".photostream-over-bg");

                        if (!overBg.length) {
                            overBg = $('<div class="photostream-over-bg" />').click(function () {
                                hideTooltip();
                            });

                            $('body').append(overBg);
                        }

                        overBg
                            .css({
                                'width'   : $(document).width(),
                                'height'  : $(document).height(),
                                'top'     : 0,
                                'left'    : 0,
                                'opacity' : 0.5,
                                'display' : 'none'
                            })
                           .fadeIn(500);

                        // za 2s opet skryjeme detail obrazku a zasednuti
                        /*
                        setTimeout(function () {
                            hideTooltip();
                        }, 2000)
                        */
                    }, 1000)
                );
            },

            item_out_handler : function (item) {
                clearTimeout($(item).data("timer"));

                //$(".photostream-tooltip").hide();
            }
        });

    // menu    
    $("#menu > li").mouseover(function(){      
      $(this).children("ul").slideDown(0).addClass("toclose");      
    })
    
    $("#menu > li > ul, #menu > li").mouseleave(function(){
      $(".toclose").stop(true, true).slideUp(0).removeClass("toclose");
    })
  	  
    // náhodné barvičky odstavců s třídou .otazka
    $('.otazka').each(function () {        
        var barvy = ["#7BBED7", "#C4007A", "#7BC523", "#B58F4F", "#EF7638"];
        var hue = barvy[Math.floor(Math.random() * barvy.length)];
        $(this).css("background-color", hue);
    });

    
    // kliknutí na vystoupení v harmonogramu uloží anebo smaže cookie
    // závislé na pluginu jQuery Cookie
    $("li[id^='vystoupeni_']").click(function(){
      
      if ($.cookie(this.id)==1)
        {$.cookie(this.id,null);$(this).removeClass('vybrane_vystoupeni');}
      else{$.cookie(this.id,"1",{expires: 360});$(this).addClass('vybrane_vystoupeni');}
      //return false;   
    })
    $("li[id^='vystoupeni_'] a").click(function(event){
      event.stopPropagation();    
    });      
    
    // fotogalerie

    $(".colorbox").colorbox({
      slideshow: true,
      slideshowSpeed	: 4000,
      slideshowAuto : false,
      next: 'Další',
      previous: 'Předchozí',
      close : 'Zavřít',
      slideshowStart : 'Promítat',
      slideshowStop : 'Nepromítat',
      current : '{current} z {total}',
      loop : true      
     });
          
     $(".iframecolorbox").colorbox({iframe:true, width:"80%", height:"80%"});
     
     
    // fb social pluginy
          window.fbAsyncInit = function() {
      	FB.init({
      		appId: '199358750083243',
      		status: true,
      		cookie: true,
      		xfbml: true
      	});
      };
      
      (function() {
      	var e = document.createElement('script');e.async = true;
      	e.src = document.location.protocol + '//connect.facebook.net/cs_CZ/all.js#xfbml=1';
      	document.getElementById('fb-root').appendChild(e);
      }());    
    
});




// nastavuje  vysku stranek na maximalni velikost

/*function nastav_velikost() {
    
    mujdiv = document.getElementById('mainleft');

    if(document.defaultView) { //MOZ
        vyskadivu = window.document.defaultView.getComputedStyle(mujdiv,null).getPropertyValue('height');
    }
    if(mujdiv.currentStyle) { // IE
        vyskadivu = eval("mujdiv.currentStyle.height")+150;
    }
    vyska = document.documentElement.clientHeight-75;
    if (vyskadivu.substring(0, vyskadivu.length - 2) < vyska || vyskadivu.substring(0, vyskadivu.length) == "auto"){
        document.getElementById('mainleft').style.height = vyska + 'px';
    }
    
}
*/
// kontrola formulare
function kontrola_formulare(formular, typ) {
    nevyplneno_vse = 0;
    prvni = "";
    if (typ == "prispevek") {
        text = "Příspěvek nemůže být vložen, protože nebyla vyplněna tato pole:\n";
    }

    for (var i = 2; i < kontrola_formulare.arguments.length; i++) {
        prvek = kontrola_formulare.arguments[i];
        i++;
        jmeno = kontrola_formulare.arguments[i];

        if (document.forms[formular].elements[prvek].value == "") {
            nevyplneno_vse = 1;
            if (prvni == "") {
              prvni = prvek;
            }
            text += jmeno+"\n";
        }
    }

    if (nevyplneno_vse) {
        alert(text);
        document.forms[formular].elements[prvni].focus();
        return false;
    }

    return true;
}

// filtrovani fora
/*
function filtruj_forum(url) {
    location.href = url + (($("#forum_typ").val() > 0) ? '?typ=' + $("#forum_typ").val() : '');
}
*/

