var freeze = false;
var thisUrl = '';
var $activeElement = $('#menu li:eq(1) a');
var pushState = (history && history.pushState) ? true : false;
var localState = pushState ? false : true;
var globalData = {'menu': '', 'main': ''};
var language = 'en';

$(document).ready(function() {
    // $(window).scroll(function(){
    //     if ($(".load_more").length >0) {
    //         if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    //             Load.more();
    //             $(".load_more").remove();
    //         }
    //     }
    // });

    $.address.init(function(event) {
        $('a.internal').address(function() {
            localState = true;
            freeze = true;
            $activeElement = $(this);
            thisUrl = $(this).attr('href').replace(/.*\/\/[^\/]*/, '');
            return thisUrl;
        });
    }).change(function(event) {

        if (localState && event.pathNames[0]) {
            currentIndex = $(this).parent().index();
            Loader.play();
            $('#menu a').each(function() {
                if ($(this).attr('href') == event.value) {
                    $(this).addClass('active');
                } else {
                    $(this).removeClass('active');
                }
            });
            $.ajax({
                url: event.value  + '&ajax=history',
                error: function(XMLHttpRequest, textStatus, errorThrown) {},
                success: function(response, textStatus, XMLHttpRequest) {
                    if (typeof response.lang == 'string') {
                        language = response.lang;
                    }
                    $.address.title(response.title);
                    $('#new_menu').html(response.menu);
                    Menu.calculations();
                    Menu.animations();
                    $('#loadconteiner').html(response.container);
                    $(document).waitForImages(function(){
                        Load.performance();
                        setTimeout(function() {
                            Layout.fixes();
                        }, 200);
                    });
                    $('#menu a').each(function() {
                        if ($(this).attr('href') == event.value) {
                            $(this).addClass('active');
                        } else {
                            $(this).removeClass('active');
                        }
                    });
                },
                dataType: 'json'
            });
        }
    }).externalChange(function() {
        localState = true;
    });


    Layout.init();
    Layout.fixes();
    Load.iframes();
});

