﻿var Page = function() {
    
};

Page.prototype = {
    init: function() {
        this._isAjax = true;
        this._selectedPicture = 0;
        this._picturesLoaded = false;
        this._flashProxyTop = null;
        this._flashProxyLeft = null;
        this._pageLoadedFirstTime = 1;
        this._selectedMenuLeftPosition = 1;
        this._currentTopFlashFile = null;
        this._currentTopFlashContext = null;

        this._mainContainer = null;
        this._menuTopContainer = null;
        this._headerContainer = null;

        if (location.href.search("ajax=false") > 0) {
            this._isAjax = false;
        }
        //this._isAjax = false;

        this._mainContainer = $("#main_content");
        this._menuTopContainer = $("#menu_top");
        this._headerContainer = $("#top_content");

        this.loadLeftMenu();

        if (this.isAjax()) {
            $.history.init($.proxy(page._loadPage, this), { unescape: "/" });
        }
        else {
            this._mainContainer.css('display', 'block');
            $("#header-container").show();
        }

    },
    isAjax: function() {
        return this._isAjax;
    },
    _loadPage: function(pageUrl) {
        if (pageUrl == "") {
            pageUrl = location.pathname;
        }

        pageUrl = pageUrl.replace(/\//g, "---");
        pageId = 0;
        var url = '/Base/Ajax/GetPageByUrl/' + pageUrl + '.aspx';
        if (pageId > 0) {
            url = '/Base/Ajax/GetPageById/' + pageId + '.aspx';
        }

        $('html, body').animate({ scrollTop: 0 }, 'slow');
        this._showLoading();

        $.ajax({
            url: url,
            dataType: 'json',
            dataFilter: function(data, type) {
                return data.substr(7, data.length - 15);
            },
            success: $.proxy(this._loadPageSuccess, this),
            error: $.proxy(this._loadPageError, this)
        });
    },
    _showLoading: function() {
        this._mainContainer.show().html($("<div id='loading' />"));
    },
    loadPage: function(pageUrl, pageId) {
        if ($.browser.msie) {
            pageUrl = pageUrl.replace("http://" + location.host, "");
        }

        if (!this.isAjax() || pageUrl.substr(0, 4) == "http" || pageUrl.substr(0, 3) == "www") {
            location.href = pageUrl;
            return;
        }
        if (typeof (_gaq) != 'undefined') {
            _gaq.push(['_trackPageview', '/' + pageUrl + '/']);
        }
        $.history.load(pageUrl);
    },
    _onLinkClick: function(e, el) {
        page.loadPage($(this).attr('href').substr(1), null);
        return false;
    },
    _loadPageSuccess: function(data, textStatus, XMLHttpRequest) {

        // load content
        this._mainContainer.show().html(data.contentMain);

        // left menu position
        if (this._selectedMenuLeftPosition != data.menuLeftPosition) {
            this._flashProxyLeft.call('SetMenuFromJs', data.menuLeftPosition);
            this._selectedMenuLeftPosition = data.menuLeftPosition;
        }

        // top menu
        if (this._menuTopContainer.html() != data.menuTop) {
            this._menuTopContainer.html(data.menuTop);
            this._menuTopContainer.css('diplay', (data.menuTop.length == 0) ? 'none' : 'block');
            this._menuTopContainer.css('height', (data.menuTop.length == 0) ? '0px' : '25px');
        }

        // header
        var regexTopFlash = new RegExp(/loadTopFlash\('([^']+)',\s'([^']+)',\s([0-9]+),\s'([^)]+)'\)/ig);
        var flashInfo = regexTopFlash.exec(data.header);
        if (flashInfo != null && flashInfo.length == 5) {
            var flashFile = $.trim(flashInfo[1]);
            var flashContext = $.trim(flashInfo[2]);
            var flashPosition = flashInfo[3];
            var flashLanguage = flashInfo[4];
            //console.info(flashFile + " - " + flashContext + " - " + flashPosition + " - " + flashLanguage);
            if (this._currentTopFlashContext != flashContext) {
                this.loadTopFlash(flashFile, flashContext, flashPosition, flashLanguage);
            }
            else {
                //console.info('move to position ' + flashPosition);
                this._flashProxyTop.call('SetItemFromJs', flashPosition);
            }

            this._currentTopFlashFile = flashFile;
            this._currentTopFlashContext = flashContext;
        }
        else {
            if (this._headerContainer.html() != data.header) {
                this._headerContainer.html(data.header);
            }
            this._currentTopFlashFile = "";
            this._currentTopFlashContext = "";
        }
        $("#header-container").show();

        this._processLinks(this._mainContainer);
        this._processLinks(this._menuTopContainer);

        if (this._pageLoadedFirstTime) {
            this._mainContainer.css('display', 'block');
            this._pageLoadedFirstTime = 0;
        }
    },
    _processLinks: function(container) {
        $("a", container).each(function(index) {
            var href = $(this).attr('href');
            $(this).attr('href', "#" + href);
        }).click(this._onLinkClick);
    },
    _loadPageError: function(XMLHttpRequest, errorType, Exception) {
        // TO DO: send error home
        //alert('Error: ' + XMLHttpRequest.responseText);
        location.href = "/404-not-found";

        if (this._pageLoadedFirstTime) {
            this._mainContainer.css('display', 'block');
            this._pageLoadedFirstTime = 0;
        }
    },
    setPictureSelected: function(value) {
        this._selectedPicture = value;
    },
    setPictureNotSelected: function() {
        this._selectedPicture = 0;
    },
    getPictureSelected: function() {
        return this._selectedPicture;
    },
    markPicturesAsLoaded: function() {
        this._picturesLoaded = true;
    },
    markPicturesAsNotLoaded: function() {
        this._picturesLoaded = false;
    },
    getPicturesLoaded: function() {
        return this._picturesLoaded;
    },
    loadTopFlash: function(swfPath, contentType, selectedPicture, language) {
        //for picture viewer to comunicate whith js
        var uid1 = new Date().getTime();
        this._flashProxyTop = new FlashProxy(uid1, '/images/JavaScriptFlashGateway.swf');
        //if (!$.browser.msie) {
        var tag1 = new FlashTag(swfPath, '503', '293');
        tag1.setFlashvars('selectedPicture=' + selectedPicture + "&itemContentType=" + contentType + "&lcId=" + uid1 + "&languageCode=" + language + "&tLocation=" + location.hostname + "&tPort=" + location.port);
        //}

        var divTop = document.getElementById("top_content");
        divTop.innerHTML = "";
        /*
        if ($.browser.msie) {
        var iFrame = document.getElementById("iframePictureViewer");
        divTop.style.display = "none";
        var iframe = $("#iframePictureViewer")[0].contentWindow.location.reload();
        iFrame.style.display = "inline";

        }
        else {
        } */
        var div = document.createElement("DIV");
        div.id = "divPictureViewer";
        div.innerHTML = tag1;
        div.style.display = "inline";
        divTop.innerHTML = "";
        divTop.appendChild(div);
    },
    loadLeftMenu: function() {
        var uid = new Date().getTime();
        this._flashProxyLeft = new FlashProxy(uid, '/images/JavaScriptFlashGateway.swf');
        var tag = new FlashTag('/images/menu.swf', '364', '251');
        tag.setFlashvars('lcId=' + uid + "&murls=" + leftMenuUrls + "&mlabels=" + leftMenuLabels + "&selectedPage=" + selectedPage + "&mids=" + leftMenuIds);
        $("#main_menu").get(0).innerHTML = tag;
    }
};

var page = new Page();
$(document).ready(function() {    
    page.init();
})
