$(function() {
    $('.form .text input, .form textarea').each(function() {
        $(this).attr('default', $(this).parent().parent().find('.label').text());
    });
    function inputFocus() {
        if($(this).val() == $(this).attr('default')) {
            $(this).val('');
        }
    }
    function inputBlur() {
        if($(this).val() == '') {
            $(this).val($(this).attr('default'))
        }
    }
    $('#header .search .text, form .text, form .text input, .form textarea').focus(inputFocus).blur(inputBlur).blur();
    $(document).ready(function(){
        $("a[rel^='prettyPhoto'], #gallery a").prettyPhoto({
            theme : 'dark_square'
        });
    });
    $('form').submit(function() {
        $(this).find('input[type=text], textarea').each(function() {
            if($(this).attr('value') == $(this).attr('default')) {
                $(this).attr('value');
            }
        })
    })
    var pddLeft = parseInt($('#header .menu > li > a').css('paddingLeft'));
    var width = $('#header .menu').width();
    var fullWidth = 0;
    $('#header .menu >li > a').each(function() {
        fullWidth += $(this).outerWidth() - (2*pddLeft);
    });
    pddLeft = Math.floor( (width-fullWidth)/2/$('#header .menu > li > a').get().length);
    $('#header .menu > li > a').css('padding', '0px ' + pddLeft + 'px');
    $('#header .menu > .first > a').css('paddingLeft', pddLeft + (width - fullWidth - (pddLeft *($('#header .menu > li > a').get().length * 2) ))+0 + 'px');
    $('#header .menu > li').hover(function() {
        $(this).find('> span').stop().show().fadeTo('normal',1);
        $(this).find('> a').addClass('open');
    }, function() {
        $(this).find('> span').stop().fadeTo('normal',0, function() {
            $(this).hide();
        });
        $(this).find('> a').removeClass( 'open');
    });
    
    $('#footer .log img').hide().fadeIn(2000);
    function animate_promo(obj) {
        $(obj).css({
            'position' : 'absolute',
            'left' :  -1 * $(obj).width() + 'px',
            'top' : '0px',
            'display' : 'block'
        }).animate({
            'left' : '0px'
        }, 1000);
        
    }
    $('.promoWrap .promo').each(function() {
        $(this).wrap('<div class="promoAjax"></div>').parent().width($(this).width()).height($(this).height()).css({
            position: 'relative',
            overflow: 'hidden'
        });
        animate_promo(this);
        setInterval(function() {
            $.get(baseurl + lang + '/home/promo/'+$('.promoWrap .promo').attr('offset'), {
                time : Date()
            }, function(data) {
                $('.promoWrap .promo').animate({
                    left: $('.promoWrap').width() + 'px'
                }, 1000, function() {
                    $(this).remove();
                });
                $('.promoAjax').append($(data).find('.promo').hide().addClass('new'));
                animate_promo($('.promoWrap .new').removeClass('new').get(0));
                
            }, 'text');
            
        }, 4000);
    });
    
    $('.logoWrap').each(function() {
        $(this).wrap('<div class="logoAjax"></div>').parent().css('width', '100%').height($(this).height()).css({
            position: 'relative',
            overflow: 'hidden'
        });
        //animate_promo(this);
        setInterval(function() {
            $.get(baseurl + lang + '/home/logos', {
                time : Date()
            }, function(data) {
                $('.logoWrap').remove();
                $('.logoAjax').append($(data).find('.logoWrap').hide().addClass('new'));
                $('.logoAjax .new').removeClass('new').hide().fadeIn(2000);
                
            }, 'text');
            
        }, 4500);
    });
    var last_loaded = null;
    $('#left .menu .hide').hide();
    $('#left .menu a').click(function() {

        var ul = $(this).parent().find('>ul');

        var el = $(this);
        if(!$(this).is('.act')) {
            $('#left ul').each(function() {
                
                if($(this).has(el.get(0)).get().length == 0) {
                    if($(this).parent().has(el.get(0)).get().length ==0) {
                        $(this).slideUp()
                    }
                    $(this).find('a').removeClass('act').removeClass('current_page_item');
                }
                $(this).find('a').removeClass('act').removeClass('current_page_item');
            });
            $(this).addClass('current_page_item').addClass('act');
            $(this).parent().parent('ul').prev().addClass('current_page_item')
            $(this).parent().parent('ul').parent('li').parent('ul').prev().addClass('current_page_item')
        }
        if(!ul.is(':visible')) {
            ul.slideDown('fast').addClass('opened');
        } 
      
        if($(this).attr('href') != last_loaded) {
            last_loaded = $(this).attr('href'); 
            
            $('#right').html('<div id="loader"></div>');
            $.get($(this).attr('href'), {
                'time' : new Date
            }, function(data) {
                var dd = $(data);
                window.History.pushState({}, dd.find('h1.title').text(), last_loaded);
                $('#right').html(dd.find('#right').html());
            }, 'text')
        }
        window.History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
			var State = History.getState(); // Note: We are using History.getState() instead of event.state
			var el = $('a[href="' + State.url + '"]');
			if(el.get().length == 0) {
			    window.location = State.url;
			} else {
			    el.click();
			}
		});
        return false;
    });
});

