﻿/* jQuery extensions */
jQuery.extend({
    site_loadPage: function (url, logState) {
        kp.RefreshNotifications();
        if (logState) {
            $(document.body).css("overflow", "auto");
            $("#overlay").hide();
            $("#lightbox_holder").hide();
            $(".bgtag").hide();
            $("#pagecontent").show();
            $("#lightbox").data("url", "page");
        }
        if (url.indexOf('/start') != -1) {
            //console.log($('#menu').find('a[rel=start]').find('img'));
            $('#menu').find('a[rel=start]').find('img').attr('src_active', '/Content/img/menu-home-active.jpg');
            $('#menu').find('a[rel=start]').find('img').removeAttr("src_active");
            $('#menu').find('a[rel=start]').find('img').attr("src", $('#menu').find('a[rel=start]').find('img').attr("src").replace('-active', ''));
            $('#menu').find('a[rel=start]').find('img').attr("src", $('#menu').find('a[rel=start]').find('img').attr("src").replace(/([^.]*)\.(.*)/, "$1-active.$2"));
            $('#menu').find('a[rel=start]').find('img').attr('src_active', $('#menu').find('a[rel=start]').find('img').attr("src"));
        }
        //if ($("#page").data("url") == url)
        //    return;      
        //console.log((!$.browser.msie || (($.browser.version == '9.0' || $.browser.version == '8.0') && $.browser.msie)));
        if (!$.browser.msie || (($.browser.version == '9.0' || $.browser.version == '8.0') && $.browser.msie)) {
            $("#page").data("url", url).html("<div class='loadingimage'><img src='/Content/img/loadingAnimation.gif' border='0' /></div>").load(url + ((url.indexOf("?") == -1) ? "?" : "&") + 'duration=' + new Date().getTime(), function (response) {
                //console.log($('#page').get(0).offsetHeight);
                var hgt = $('div#page').get(0).offsetHeight - 346; // - 346
                $('#menu').find('ul.menu').css('height', hgt + 'px');
                //alert(hgt);
                var temp = $('div#page').get(0).offsetHeight - 346;
                $('#menu').find('ul.menu').css('height', temp + 'px');
                //alert(temp);
            });
        }
        else {
            document.location = url + ((url.indexOf("?") == -1) ? "?" : "&") + 'duration=' + new Date().getTime();
        }
        //document.location = "#" + url;
        if (logState) {
            $(document).data("currentState", { source: "page", url: url });
            kp.history.pushState({ source: "page" }, null, url);
        }
    },
    site_loadPopup: function (url, logState) {
        var compUrl = url.replace("?layout=lightbox", "");
        compUrl = compUrl.replace("&layout=lightbox", "");
        if ($("#lightbox").data("url") == compUrl)
            return;
        $(document.body).css("overflow", "hidden");
        $(".bgtag").hide();
        $("#pagecontent").show();
        if (!$("#page").data("url")) {
            if (!$.browser.msie || (($.browser.version == '9.0' || $.browser.version == '8.0') && $.browser.msie)) {
                $.site_loadPage("/sv.html/start", false);
            }
            /*else {
                
            }*/
        }
        $("#overlay").show();
        $("#lightbox_holder").show();
        $("#lightbox").data("url", url);
        var pushUrl = url + "";
        pushUrl += ((pushUrl.indexOf("?") == -1) ? "?" : "&");
        pushUrl += "layout=lightbox";
        $("#lightbox .lightboxreload").html('<div class="loadingimage"><img src="/Content/img/loadingAnimation.gif" alt="" style=""></div>').load(pushUrl);
        //$("#page").data("url", "lightbox");
        if (logState) {
            $(document).data("currentState", { source: "popup", url: pushUrl });
            kp.history.pushState({ source: "popup" }, null, pushUrl);
        }
    },
    site_closePopup: function () {
        /*
        var currState = kp.history.getState();
        var prevState = null;
        $(kp.history.savedStates).each(function () {
        if (this.url == currState.url)
        return false;
        prevState = this;
        });
        if (prevState != null && prevState.data.source == "page")
        kp.history.back();
        else {*/
        var lightboxurl = $("#lightbox").data("url");
        $(document.body).css("overflow", "auto");
        $("#overlay").hide();
        $("#lightbox .lightboxreload").empty();
        $('#fancy_outer').remove();
        $("#lightbox_holder").hide();
        $("#lightbox").data("url", "closed");
        if (lightboxurl.indexOf('refresh=true') != -1)
            kp.history.back();
        //kp.history.back();
        //}
    },
    /*
    site_loadcontent: function (url) {
    $(".bgtag").hide();
    $("#pagecontent").show();
    $("#content").html("<div class='loadingimage'><img src='/Content/img/loadingAnimation.gif' border='0' /><div>").load(url);
    },
    */
    site_loadcategory: function (address) {
        $('#right').load("/sv/Article.mvc/ListCategory/?address=" + address);
    },
    site_ShowAgreementWindow: function (agreementurl, accepturl) {
        $.mvc_getJSON(agreementurl, {}, function (response) {
            //console.log(response.showwindow);
            if (response.showwindow != null && response.showwindow == true) {
                $.site_loadPopup(accepturl, false);
            }
        });
    }
});
$(function () {
    $("#lightbox #close").click(function () {
        $.site_closePopup();
    });
    /*$('.image-menu').hover(function () {
    $(this).attr("src", $(this).attr("src").replace(/([^.]*)\.(.*)/, "$1-active.$2"));
    }, function () {
    $(this).attr("src", $(this).attr("src").replace('-active', ''));
    });
    */
    $("img").live("click", function (e) {
        if ($(this).hasClass("image-menu")) {
            $(".image-menu").each(function () {
                $(this).removeAttr("src_active");
                $(this).attr("src", $(this).attr("src").replace('-active', ''));
            });
            $(this).attr("src", $(this).attr("src").replace(/([^.]*)\.(.*)/, "$1-active.$2"));
            $(this).attr('src_active', $(this).attr("src"));
        }
    });
   
    $("a").live("click", function (e) {
        var self = $(this);
        if (self.hasClass("page")) {
            e.preventDefault();
            $.site_loadPage(self.attr("href"), true);
            /*
            if ($(this).hasClass("display-category")) {
            $.site_loadcategory($(this).attr('address'));
            }
            */
        }
        /*
        if ($(this).hasClass("menu-content")) {
        e.preventDefault();
        if ($(this).attr('category') != null)
        $.site_loadcontent($(this).attr("href") + '/?category=' + $(this).attr('category'));
        else
        $.site_loadcontent($(this).attr("href"));
        if ($(this).hasClass("display-category")) {
        $.site_loadcategory($(this).attr('address'));
        }
        }
        */
        if (self.hasClass("popup")) {
            e.preventDefault();
            $.site_loadPopup(self.attr("href"), true);
        }
        if ($(this).hasClass("paging")) {
            var reload = self.closest(".pagingreload");
            var a = $("a#paging", reload);
            if (a.offset().top > $(window).scrollTop())
                e.preventDefault();
            reload.mvc_load(self.attr("action"));
        }
    });
});
/* history.js */
$(window).bind("statechange", function () {
    var state = History.getState();
    //kp.log(state);
    var beg = (window.location.protocol + "//" + window.location.host + "").length;
    var shortUrl = state.url.substring(beg);
    if ($(document).data("currentState") && $(document).data("currentState").source == state.data.source && $(document).data("currentState").url == shortUrl)
        return;
    if (state.data.source == "page") {
        $.site_loadPage(shortUrl, true);
    }
    else if (state.data.source == "popup") {
        $.site_loadPopup(shortUrl, false);
    }
    else {
        window.location = state.url;
    }
});
