$(document).ready(function() {
	
    //MENU
    var $a = $('#header .menu li:not(.active) a');
    var default_min_padding = 35;
    var default_max_padding = 50;
    var submenu_width = 200;

    var getPadding = function(obj, type) {
        var nested = obj.parents('ul').length;
        if (type == 'max') return (nested == 1) ? default_max_padding : default_max_padding-10;
        else return (nested == 1) ? default_min_padding : default_min_padding-10;
    }

    var hoverIn = function() {
        $this = $(this);
        $this.animate({
            paddingLeft: getPadding($this, 'max')+'px'
        }, {duration:200});
        $this.animate({
                paddingLeft: getPadding($this, 'min')+'px'
            }, { duration:100});
    };

    var hoverOut = function () {
        $(this).animate({
            paddingLeft: '0px'
        }, 100);
    };

    $('#header .menu li:not(.active) ul').hide().css('width', '0px');

    $a.unbind('mouseenter mouseleave');
    $a.hover(hoverIn, hoverOut);

    $('#header .menu li a').click(function() {
        var $this = $(this);
        var $li = $this.parent('li');
        
        if (!$li.is('.active')) {
            //schovani neaktivniho submenu
            $li.parent('ul').find('li.active ul').animate({width: '0px'}, 200, function() {
                $(this).hide();
            });
            //vysunuti aktivniho submenu
            $li.children('ul').show().animate({width: submenu_width+'px'}, 200);
            
            $li.parent().find('li.active a').animate({paddingLeft: '0px'}, 100);
            $li.parent().find('li.active').removeClass('active');
            $li.addClass('active');
            $this.css('paddingLeft', getPadding($this, 'min')+'px').unbind('mouseenter mouseleave');
            $('#header .menu li:not(.active) a').unbind('mouseenter mouseleave').hover(hoverIn, hoverOut);
        }
        if ($li.children('ul').length > 0) {
            return false;
        }
    });


    //Jazykove mutace
    $('.lng li.active a').animate({marginTop: '0px'}, 300);
    $('.lng li:not(.active) a').hover(function(e) {
       $(this).animate({marginTop: '0px'}, 300);
    }, function() {
       $(this).animate({marginTop: '5px'}, 300);
    });

    
    //Objednavka
    if (!$('#schipping_address_check input:checkbox').is(':checked')) {
        $('#shipping_address input').attr('disabled', 'disabled').css('backgroundColor', '#ececec');
    }
    
    $('#schipping_address_check').click(function(){
        var $checkbox = $(this).find(':checkbox');
        if ($checkbox.is(':checked')) {
            $('#shipping_address input').attr('disabled', '').css('backgroundColor', '');
        } else {
            $('#shipping_address input').attr('disabled', 'disabled').css('backgroundColor', '#ececec');
        }
    });


    //Mini kosik
    if ($('#shoppingCart').length) {
        var msie6 = $.browser == 'msie' && $.browser.version < 7;

        if (!msie6) {
            var top = $('#shoppingCart').offset().top - parseFloat($('#shoppingCart').css('margin-top').replace(/auto/, 0));
        
            $(window).scroll(function () {
                // what the y position of the scroll is
                var y = $(this).scrollTop();

                // whether that's below the form
                if (y >= top) {
                    // if so, ad the fixed class
                    $('#shoppingCart').addClass('fixed');
                } else {
                    // otherwise remove it
                    $('#shoppingCart').removeClass('fixed');
                }
            });
        }
    }






    //FOTOGELRIE
    $("a.thickbox").fancybox({
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'titlePosition' 	: 'over',
        'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
            return (currentArray.length > 1) ? '<span id="fancybox-title-over">Obrázek ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>' : '';
        }


    });



});
