$(document).ready(function(){

    function infoWindow(name, msg){
        var popID = "infoWindow"; //Get Popup Name
        var popURL = "#?w=500"; //Get Popup href to define size

        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
        $('#infoText').html(msg);
        
        //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2 + 50;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        
        //Apply Margin to Popup
        $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
//        $('#infoText').html(msg);
        $('#infoName').text(name);   
        return false;
    }  
    
    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });
    
 
    
    
    $('#newsletter').submit(function() {
        var email = $("#newsletterEmail").val();
        var isAdd = true;
                   
        $.ajax({
                type: "POST",
                url: "/newsletter/ajax/",
                data: "newsletter=1&email="+email,
                success: function(msg){
                    infoWindow("Příhlášení newsletteru", msg); 
                    $("#newsletterEmail").val("");  
                     
                }
        }); 
        
        if (isAdd)
            return false;
    });
    
    
    
    
    
    /************* DOPORUCENI STRANKY *******************/
    $(".doporucit").live("click", function() {
//        alert($(this).attr('title'));
        $.ajax({
//                'type': 'post',
                dataType: 'json',
                url: "/doporucit/",
                data: "url=1&ajax=1",
                success: function(msg){
                    infoWindow(msg.title, msg.text); 
                },
                error: function(mgs) {
                    alert('erro');
                }
        }); 
        return false;   
    });
    
    $("form[name=form-doporucit]").live("submit", function(){
        var ajax = $("input[name=ajax]", this).val();
       
        if ((ajax == 'undefined')  || (ajax != 1))
            return true;
            
        var jmeno = $("input[name=jmeno]", this).val();
        var url = $("input[name=url]", this).val();
        var email1 = $("input[name=email1]", this).val();
        var email2 = $("input[name=email2]", this).val();
        var email3 = $("input[name=email3]", this).val();
        
        $('#infoText').html("<center><img src='/js/ajax_load.gif' width='50px' />");
        
        $.ajax({
                dataType: 'json',
                url: "/doporucit/",
                data: "ajax=1&jmeno="+jmeno+"&email1="+email1+"&email2="+email2+"&email3="+email3+"&url="+url,
                success: function(msg){
                    infoWindow(msg.title, msg.text);
                },
                error: function(){
                    infoWindow("Error", "interni chyba");  
                }
        });      
         
        return false;
    });                    
    
    $('select[name=resort]').change(function () {
        var id = $(this).val(); 
        $.ajax({
            url: "/ajax.php",
            dataType: 'json',
            data: "akce=strediskoInfo&id="+id,
            success: function(msg){
                if (msg.stav == 1)
                    $("#form-infoareal").html(msg.text);
                else
                    $("#form-infoareal").html(msg.error);
        
            },
            error : function(mgs) {
                infoWindow("Error", "Nastala interni chyba systému,  omlouváme se.");    
            } 
        }); 
        
        return false;
    }); 
    
    $('input[name=arrive_type]').change(function () {
        var id = $(this).val();
//        alert(id); 
        
        if (id == "Bus") 
            $("input[name=transfer]").parents("p:first").hide("slow");
        else
            $("input[name=transfer]").parents("p:first").show("slow");
        
        /*$.ajax({
            url: "/ajax.php",
            dataType: 'json',
            data: "akce=strediskoInfo&id="+id,
            success: function(msg){
                if (msg.stav == 1)
                    $("#form-infoareal").html(msg.text);
                else
                    $("#form-infoareal").html(msg.error);
        
            },
            error : function(mgs) {
                infoWindow("Error", "Nastala interni chyba systému,  omlouváme se.");    
            } 
        }); 
        
        return false;*/
    }); 
    
    $('.stredisko').mouseenter(function () {
        var curl = $(this).attr("alt"); 
        
        
        $.ajax({
            url: "/ajax.php",

            dataType: 'json',
            data: "akce=strediskoInfo&curl="+curl,
            success: function(msg){
                
                if (msg.stav == 1)
                    $("#infoareal").html(msg.text);
                else
                    $("#infoareal").html(msg.error);
        
            },
            error : function(mgs) {
                infoWindow("Error", "Nastala interni chyba systému,  omlouváme se.");    
            } 
        }); 
        
        return false;
    });                      
    
    
    /** MANIPULACE S ANKETOU **/
    $('.anketa_hlasuj').live("click", function () {
        var id_odpovedi = $(this).attr("alt"); 
        var id_anketa = $(this).attr("title"); 
        
        $.ajax({
            url: "/ajax.php",
//            type: "get",
            dataType: 'json',
            data: "akce=anketa-hlasuj&id_ankety="+id_anketa+"&id_hlas="+id_odpovedi,
            success: function(msg){
                infoWindow("Anketa", msg.text); 
                if (msg.stav == 1)
                    $("#anketa-"+id_anketa).html(msg.anketa);
            },
            error : function(mgs) {
                infoWindow("Anketa", "Nastala interní chyba");
            } 
        }); 
        
        return false;
    });  
    
    
    
    /** KONTAKT FORM **/
    $("#kontaktovat").click(function() {
        
        var predmet = $(this).attr("title");
        
        $.ajax({
                type: "POST",
                url: "/kontakt/",
                data: "ajax=1&predmet="+predmet,
                success: function(msg){
                    infoWindow("Napište nám", msg);      
                }
        });    
        return false;
    });
    
       
    $("#form-kontaktovat").live("submit", function(){
        var jmeno  = $("input[name=jmeno]", this).val();
        var predmet = $("input[name=predmet]", this).val();
        var text = $("textarea[name=text]", this).val();
        var email = $("input[name=email]", this).val();
        var tel = $("input[name=tel]", this).val();

        $.ajax({
                type: "POST",
                url: "/kontakt/",
                data: "ajax=1&jmeno="+jmeno+"&predmet="+predmet+"&text="+text+"&email="+email+"&tel="+tel+'&odeslat=1',
                success: function(msg){
                    $('#infoText').html(msg); 
      
                }
        });       
        
        return false;
    }); 
    
    /** KONTAKT FORM **/
    $(".package-request-button").click(function() {
        
        var predmet = $(this).attr("title");
        
        $.ajax({
                type: "GET",
                url: "/kontakt/request/",
                data: "predmet="+predmet,
                success: function(msg){
                    infoWindow(predmet, msg);      
                }
        });    
        return false;
    });
    
       
    $("#request").live("submit", function(){
        
        var jmeno  = $("input[name=jmeno]", this).val();
        var predmet = $("input[name=predmet]", this).val();
        var text = $("textarea[name=text]", this).val();
        var email = $("input[name=email]", this).val();
        var tel = $("input[name=tel]", this).val();

        $.ajax({
                type: "GET",
                url: "/kontakt/request/",
                data: "jmeno="+jmeno+"&predmet="+predmet+"&text="+text+"&email="+email+"&tel="+tel+'&odeslat=1',
                success: function(msg){
                    $('#infoText').html(msg); 
      
                },
                error: function(msg) {
                    
                }
        });       
        
        return false;
    }); 
    
    
    /** CATALOG FORM **/
    $(".catalogue").click(function() {
        
        $.ajax({
                type: "GET",
                url: "/kontakt/catalogue/",
                data: "",
                success: function(msg){
                    infoWindow("Ask for catalogue", msg);      
                }
        });    
        return false;
    });

    $("#pocet_pyt").click(function() {
        
        if ($(this).val() == "PERSONS")
            $(this).val("");
    });
       
    $("#askcatalogue").live("submit", function(){
        
        var email = $("input[name=email]", this).val();
        
        $.ajax({
                type: "GET",
                url: "/kontakt/catalogue/",
                data: "email="+email+'&odeslat=1',
                success: function(msg){
                    $('#infoText').html(msg); 
      
                },
                error: function(msg) {
                    
                }
        });       
        
        return false;
    });
    
    
    
    /**   DEBUG **/
    $("#debuginfo").click(function() {
        
        $("#debuginfo").hide();
        $("#debug-text").show();
    
    });
    
    /**   DEBUG **/
    $("#debug-text").click(function() {
        $("#debug-text").hide();
        $("#debuginfo").show();
        
    
    });
    
    $("#date_od").datepicker({ minDate: +1, maxDate: "+2Y"});
    $("#date_do").datepicker({ minDate: +2, maxDate: "+2Y"}); 
    
    
    
    /*** MENU PRO F$E ***/
    
       //On Hover Over
    function megaHoverOver(){
        $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
        (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;    

        $(this).find(".row").each(function() {    //for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

//Set custom configurations
var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 50, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 100, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
    
                
        
        
}); 
 
    
