
$.fn.setChecked = function() {
    return this.each(function() {    
        var CHOSEN = 'the-chosen-one';
        $(this)
        .attr('checked', true)
        .parents('tr')
        .siblings()
        .removeClass(CHOSEN)
        .end()
        .addClass(CHOSEN);
    });
};

$.fn.enableRow = function() {
    return $(this).toggleRow(false);
};

$.fn.disableRow = function() {
    return $(this).toggleRow(true);
};

$.fn.toggleRow = function(bool) {
    return this.each(function() {        
        $(this)
        .attr('disabled', bool)
        .parents('tr').toggleClass('disabled', bool);
    });
};

$(document).ready(function(){

    /**
     * step 2 klikani na radky aktivuje inputy + kompatibilita delivery 2 pay
     */
    
    var $step2 = $('#step2');
    if ($step2.length) {
        $step2.find('td.label').click(function (e) {
            var $input = $(this).parent('tr').find('input');
            
            $input.setChecked();

            /**
             * pokud je input z tabulky table-pay, tak dalsi kod nevykonavej
             * nebo neni definovano pole s kompatibilnima prekladama (jsonPay2Del)
             */
            if ($input.parents('#table-pay').length == 1 || typeof jsonPay2Del == 'undefined') {
                return;
            }

            /**
             * najdu vsechny pay inputy
             */
            var $payInputs = $('#table-pay').find('input');
            
            /**
             * odebira tridu disabled a input na disabled = false;
             */
            $payInputs.enableRow();

            /**
             * ze vsech payinputu odebere ty, ktere nejsou s danou dopravou kompatibilni
             */
            $.each(jsonPay2Del[$input.val()], function(i, pay) {
                $payInputs = $payInputs.not('input[value="' + pay + '"]');
            });
            
            /**
             * pridava tridu disabled a input na disabled = true;
             */
            $payInputs.disableRow();
            
            /**
             * pokud je v disablovanych nejaky zaskrtnuty,
             * tak musim zaskrtnout jiny, ktery je aktivni
             */
            if ($payInputs.filter(':checked').length) {
                $('#table-pay').find('input:enabled:first').setChecked();
            }
        });
        
        $('#table-delivery').find('input:checked').click();
        
    }    
    

/**
 *  PRO HEZCI TABULKU V GUIDE CLIENT CENTER
 */

    $("table.order").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
        );

    $("#guide table tbody tr").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
        );

    $("#guide table tbody tr td").click(function(){
        var url = $(this).parent("tr").children("td.order-id").children("a").attr("href");
        if (url) {
            window.location = url;
        }
    });

    $(".order tr td").click(function(){
        var url = $(this).parent("tr").parent("tbody").prev("thead").children("tr").children("th.link").children("a").attr("href");
        if (url) {
            window.location = url;
        }
    });

    $(".order tr th").click(function(){
        var url = $(this).parent("tr").children("th.link").children("a").attr("href");
        if (url) {
            window.location = url;
        }
    });

    $("#branches table tr td").click(function(){
        var url = $(this).parent("tr").children("td.upravit").children("a").attr("href");
        if (url) {
            window.location = url;
        }
    });

    /********************************** KOSIK ***********************************************/


    // Pravy panel - moznost upravit dopravu a platbu

    $("#order-right-pane .item").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
        );

    
    /**
     *  Zalozkovy system 3. kroku objednavky
     */

     $('#step3-login h2.click2Show').click(function(e){
        var $t = $(e.target),
        toToggle = $t.closest('#step3-login h2.click2Show span#login-icons').length > 0 ? true : false;
        if (toToggle == false) {
            $(this).next('form').slideToggle();
            $(this).toggleClass('opened');
        }
     });

     $('#step3-login h2.click2Show .text-cover').hover(function(e){
        $(this).parent().toggleClass('hover');
     });

    /* zatim nebudeme otevirat v novem okne */
    $('#step3-login h2.click2Show #login-icons a').each(function() {
        $(this).popupWindow({ 
            height:500, 
            width:940,
            centerBrowser: 1,
            centerScreen: 1

        }); 
    });

    // scroll down na error hlasku 
    if ($('#basket.step3 .alert').length > 0) {
        var target = $('#basket.step3 .alert');
        var targetOffset = target.offset().top;
        $('html,body').animate({scrollTop: targetOffset}, 800);
    }

    // Rozklikavaci formular pro zapomenute heslo - forgotten password

    $("#forgottenPasswordLink").click(function(){
        $("#forgottenPassword").slideDown();
    });

    var loginLink = $("#basket.step3 .error a");
    if ( (loginLink != undefined) && (loginLink.attr('href') == "login.php") ){
        $(loginLink).click(function(e){
            e.preventDefault();
            $("#forgottenPassword").slideDown();
        });
    }


    // Rozklikavani formulare fakturacni adresy

    var fakturacniBox = $('#fakturacniToggle.checkbox:checked').val();
	
    if (fakturacniBox != "on"){
        $("#form-customer-invoicing-address").hide();
    }

    $('#fakturacni-toggle').click(function(e){
        
        if ($(e.target).is('input') == false) {
            return;
        }

        var checkboxChecked = $(this).find('input').attr("checked");
        
        if(checkboxChecked == 'checked'){
        
            if (($('input#name').val()) && ($('input#jmenoF').val().length == 1)) { // "1" protoze je jmeno, mezera, prijmeni
                $('input#jmenoF').val($('input#name').val());
            }
            if (($('input#firma').val()) && ($('input#firmaF').val().length == 0)) {
                $('input#firmaF').val($('input#firma').val());
            }
            if (($('input#ulice').val()) && ($('input#uliceF').val().length == 0)) {
                $('input#uliceF').val($('input#ulice').val());
            }
            if (($('input#mesto').val()) && ($('input#mestoF').val().length == 0)) {
                $('input#mestoF').val($('input#mesto').val());
            }
            if (($('input#psc').val()) && ($('input#pscF').val().length == 0)) {
                $('input#pscF').val($('input#psc').val());
            }
            if ($('select#stat').length){
                if (($('input#stat').val()) && ($('input#statF').val().length == 0)) {
                    $('input#statF').val($('input#stat').val());
                }    
            }
            else {
                if (($('div#stat').text()) && ($('div#statF').text().length == 0)) {
                    $('input#statF').text($('input#stat').text());
                } 
            }   
        }
            
        $("#form-customer-invoicing-address").slideToggle();
	    
    });
	
	
	
    // Rozklikavani formulare pro zmenu hesla

    var hesloBox = $('#registration #passwordToggle.checkbox:checked').val();
	
    if (hesloBox != "on"){
        $("fieldset#change-passwd").hide();
    }	
	
    $('#registration #passwordToggle.checkbox').click(function(){
        var checkboxChecked = $(this).attr("checked");
	    
		
        //	    $("#basket.step3.general legend").slideToggle(400, 'swing');
        $("fieldset#change-passwd").slideToggle(1200, 'swing').siblings('#password-toggle').toggleClass('active');


        var fakturacniBox = $('#registration #fakturacniToggle.checkbox:checked').val();
	    
    });
	
    /** je aby se nam nebily dva podtrzeni v registraci **/	
    $("#registration fieldset:last").css('border-bottom', '0px none').css('padding-bottom', '0px');
        
        


    /**
     * Prepocitavani kosiku
     */
    var recountAmount;
    // konstrukce pro moznost projektoveho vypnuti javascriptoveho prepocitavani
    if (recountAmount === undefined){
        recountAmount = true;
    }
        
    if ( (recountAmount === true) && $("#step1-form").length) {
            
        $('input.amount').keyup(function() {
               
            var $input = $(this);
               
            setTimeout(function(){
                $input.fadeOut('fast');
                $input.next("img").fadeIn('fast');
                setTimeout(function(){
                    $("#step1-form").submit();
                }, 500);
            }, 1300);
                
        });
    }


});


(function($){         
    $.fn.popupWindow = function(instanceSettings){
        
        return this.each(function(){
        
        $(this).click(function(){
        
        $.fn.popupWindow.defaultSettings = {
            centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
            centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
            height:500, // sets the height in pixels of the window.
            left:0, // left position when the window appears.
            location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
            menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
            resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
            scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
            status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
            width:500, // sets the width in pixels of the window.
            windowName:null, // name of window set from the name attribute of the element that invokes the click
            windowURL:null, // url used for the popup
            top:0, // top position when the window appears.
            toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
        };
        
        settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
        
        var windowFeatures =    'height=' + settings.height +
                                ',width=' + settings.width +
                                ',toolbar=' + settings.toolbar +
                                ',scrollbars=' + settings.scrollbars +
                                ',status=' + settings.status + 
                                ',resizable=' + settings.resizable +
                                ',location=' + settings.location +
                                ',menuBar=' + settings.menubar;

                settings.windowName = this.name || settings.windowName;
                settings.windowURL = this.href || settings.windowURL;
                var centeredY,centeredX;
            
                if(settings.centerBrowser){
                        
                    if ($.browser.msie) {//hacked together for IE browsers
                        centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
                        centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
                    }else{
                        centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
                        centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
                    }
                    window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
                }else if(settings.centerScreen){
                    centeredY = (screen.height - settings.height)/2;
                    centeredX = (screen.width - settings.width)/2;
                    window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
                }else{
                    window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();  
                }
                return false;
            });
            
        }); 
    };
})(jQuery);

