$(document).ready(function() {

    //Hack for getting live submit in IE. 
    if ($.browser.msie) {
        $('.orderProcessForm input[type=submit]').live('click',function() {
            $(this).parents('form').trigger('submit');
            return false;
        })
    } 
    
    
    function trackCheckout(step) {
        var GAUrl  = "/checkout/"+step;
        var YWAUrl = "http://"+window.location.host+GAUrl;
        if(typeof YWAUrl != "undefined") YWATracker.setUrl(YWAUrl);
        if(step == "step3") YWATracker.setAction("01");
        YWATracker.submit();
        if(typeof GAUrl == "undefined") {    
            _gaq.push(['_trackPageview']);    
        } else {    
            _gaq.push(['_trackPageview', GAUrl]);    
        }
    
    }  


    $(".order_popup").click(function(){ 
        trackCheckout('step1');
        console.log('step1'); 
    });
    
    $('.orderProcessForm' ).live('submit', function(event){
        var $this = $(this);
        var $step = $('input[name="stage"]', $this).val();
        trackCheckout($step);
        console.log($step);
    });
        
       
    
    //====Order process
    var do_order = function(){
        $('#cboxLoadedContent .orderProcessForm' ).live('submit', function(event){
                var $this = $(this);
                var $step = $('input[name="stage"]', $this).val();
                $('input[name="sendingAjax"]', $this).val( 'true' );
                $this.parents('.container_process').empty();
                $.colorbox({
                    overlayClose:    false,
                    onLoad      :    function() {
                    $('#cboxLoadingOverlay').css({   
                                                'background-position' : '50% 50%',
                                                'display'             : 'block', 
                                                'z-index'             : '9999'
                                            });
                    
                    },
                    
                    onComplete: function() {    
                        $("form").validationEngine({openDebug: false });    
                    }  
                });//End colorbox loading
                   
                var url = $this.attr('action');
                var dataToSend = $this.serialize();        
                var callback = function(dataReceived){        
                    var res = $('#order_container',dataReceived).html();
                    $.colorbox({       
                        html:          res,
                        overlayClose:  false,     
                        onComplete:    function() { 
                            $("form").validationEngine({openDebug: false });
                            $(this).colorbox.resize(); 
                            $('#cboxLoadingOverlay').css({'background-position': '50% 50%', 'display': 'none', 'z-index':'-9999'});
                            //trackCheckout($step);
                        }
                        
                    });
                };
                
                var typeOfDataToReceive = 'html';
                $.post( url, dataToSend, callback, typeOfDataToReceive );
                return false;
        
        });
    
    }
    
    //====Order process :: Cancel
    $('a.cancel_pop').live('click', function(){
    var $this = $(this);
    var href = $this.attr('href');
    var container = $this.parents('.container_process');
    var ContentObjectID = $('input[name="ContentObjectID"]',container).val();
    var Epost = $('input[name="Epost"]',container).val();
    var ContentNodeID   = $('input[name="ContentNodeID"]',container).val();
    var $step = $('input[name="stage"]', container).val();
    container.empty();
    $.post(
        href, 
        { 'cancel' : "protein_plan", 'ContentNodeID' : ContentNodeID, 'ContentObjectID' : ContentObjectID, 'Epost' : Epost  },
        function(dataReceived){
            var res = $('#order_container',dataReceived).html();
            $.colorbox({
                html: res,
                overlayClose: false,
                onComplete: function() {                
                    $(this).colorbox.resize(); 
                   //trackCheckout($step);
                }
            });
            trackCheckout($step); 
            console.log($step);   
        },
        'html'   
    );
    return false;
    
    })
    
    
    //========Tabs
    $(".tab_content").hide();
    $("ol.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();
    $("ol.tabs li").click(function() {    
        $("ol.tabs li").removeClass("active");
        $(this).addClass("active"); 
        $(".tab_content").hide(); 
        
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    
    });
    
    
    
    //=======Lightbox--
    
    $("a.popup").colorbox({ 
        href: function(){return $(this).attr('data-link');},
        initialWidth:"0px",
        initialHeight:"0px",
        maxWidth: "800px",
        scrolling:  false,
        previous: "&laquo; forrige",
        next:     "neste &raquo;",
        current:  "",
        close:    "lukk",
        opacity:  "0.5",
        transition: "none",
        speed:  "0",
        onComplete: function() { $(this).colorbox.resize(); }       
    });
    
     
    $("a.order_popup").colorbox({
            href: function(){return $(this).attr('data-link');},
            maxWidth: "990px",
            initialWidth:"0px",
            initialHeight:"0px",
            scrolling:  true,
            opacity:  "0.5",
            overlayClose: false,
            close:    "lukk",
            transition: "none",
            speed:  "50",
            onComplete: function() {
                $(this).colorbox.resize();
                $("form").validationEngine({openDebug: false });
                //trackCheckout('step1'); 
                do_order();  
            }     
    });
    
    
                    
    
    $("a.social").colorbox({ 
        href: function(){return $(this).attr('data-link');},
        initialWidth:"0px",
        initialHeight:"0px",
        maxWidth: "900px",
        scrolling:  true,
        previous: "",
        next:     "",
        current:  "",
        close:    "lukk",
        opacity:  "0.5",
        transition: "none",
        speed:  "0",
        //inline:true,
        onComplete: function() {       
            $(this).colorbox.resize();       
        }
    
    });
    
    //=============Placeholders in forms
    $("input, textarea").labelify();
    
    
    //=====================Accordion FAQ----
    $('.article_faq .inside').hide();
    $('.article_faq h2').toggle(
    function() {
        $('.article_faq .inside').slideUp('slow'); $(this).parent().next().slideDown('slow');
    }, 
    function() {
        $('.article_faq .inside').slideUp('slow');
    }); 
    
    
    //=============Validation=
    $("form").validationEngine({openDebug: false });
    
    
    //=============Read more Quotes=
    $('.article_quote footer').hide();
    var les_mer = $('<span style="display:inline;">les mer</span>');
    $('span.read_more').prepend(les_mer);
    $('span.read_more').click(function(){
        $(this).siblings('footer').toggle().end().hide();
    });
    
    
    //=============Images on references=
    $(".article_quote figure a").click(function(){ 
        return false;
    });
    
    //=============Dirty fix - removing extra <p> tags from "Om oss"
    /*
    $("#employes_people p")
    .filter(function() {
        return $.trim($(this).text()) === ''
    })
    .remove()
    
    $("#employes_people2 p")
    .filter(function() {
        return $.trim($(this).text()) === ''
    })
    .remove()
    */
    

});

