$(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;
    });
    
    
    
    $(".pm-button").click(function() {
        
        $.ajax({
            dataType: 'json',
            url: "/poptavky/pridat/",
            data: "ajax=1",
            success: function(msg){
                
                infoWindow(msg.title, msg.text); 
            },
            error: function(msg) {
                infoWindow(msg.title, "Omlouváme se ale nastala interní chyba systému");  
            }
        }); 
        return false;   
    });
    

    $(".galerie img").lazyload({ 
        placeholder : "/images/grey.gif",
        effect : "fadeIn"
    });
    

    
    
    /** KONTAKT FORM **/
    $(".ajax_link").click(function() {
        var predmet = $(this).attr("title");
        var link = $(this).attr("href");
        alert(link);
        $.ajax({
                type: "POST",
                url: link,
                data: "ajax=1&predmet="+predmet,
                success: function(msg){
                    infoWindow("Dotaz na produkt", 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();

        var actionUrl = $(this).attr('action');
        $.ajax({
                type: "POST",
                url: actionUrl,
                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 **/
    $("#kontaktovat").click(function() {
        var predmet = $(this).attr("title");
        $.ajax({
                type: "POST",
                url: "/kontakt/",
                data: "ajax=1&predmet="+predmet,
                success: function(msg){
                    infoWindow("Váš názor na nás", msg);      
                }
                
        });    
        
        return false;
    });
    
    
    $("form.ajax-submit").live("submit", function() {
        var data = "";
        var first = true;
        
        var actionUrl = $(this).attr('action');
        
        $(":input", this).each(function(index) {
            if (!first)
                data += "&";
            data = data + $(this).attr("name") + "=" + $(this).val();
            first = false;
        });
        data = data + "&ajax=1";
        
        $('#infoText').html("<center><img src='/js/ajax_load.gif' width='50px' />");

        $.ajax({
                dataType: 'json',
                url: actionUrl,
                data: data,
                success: function(msg){
                    alert(msg.debug);
                    $('#infoText').html(msg.text);
                },
                error: function(){
                    $('#infoText').html("interni chyba");  
                }
        });      
         
        
        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){
                    $('#infoText').html(msg.text);
                },
                error: function(){
                    infoWindow("Error", "interni chyba");  
                }
        });      
         
        return false;
    }); 
    
    /*********************************************/
   
    
    
    /**   DEBUG **/
    $("#debuginfo").click(function() {
        
        $("#debuginfo").hide();
        $("#debug-text").show();
    
    });
    
    /**   DEBUG **/
    $("#debug-text").click(function() {
        $("#debug-text").hide();
        $("#debuginfo").show();
        
    
    }); 
    
    
                
        
        
}); 

