﻿//Dynamic pages manager object
//  dependecy on
//  callback.js,common.js,AjaxControlToolkit
//**********************************************
var pm = {
    browser: null,
    pageHolderId: null,
    loadingMsgId: null,
    pageId: null,
    isBusy: false,
    currentPageId: null,
    historyArgs: null,
    historyNoEvent: false,
    timerId: null,

    Init: function(pageHolderId, loadingMsgId)
    {
        this.pageHolderId = pageHolderId;
        this.loadingMsgId = loadingMsgId;
        browser = new cbo();
        browser.OnComplete = function(responseText, responseXml) { pm.LoadComplete(responseText); };

        this.HistoryTimer();
    },
    Show: function(pageId)
    {
        this.pageId = pageId;
        this.StartTransition();
    },
    Load: function(pageId, url)
    {
        if (!this.isBusy)
        {

            this.pageId = pageId;
            //           alert(browser);
            //get url's html
            browser.FetchUrl(url);
            //show loader
            this.WaitStart();

            this.isBusy = true;

        }
    },
    HistoryOnChange: function(args)
    {
        alert(args)
    },
    HistoryTimer: function()
    {
        var w = $("navWin");
        var doc = w.contentWindow.document;
        //var args=doc.body.innerHTML;

        //history changes in FF by changing hash only
        var args = (document.all) ? doc.location.search : document.location.hash;

        if (args != this.historyArgs)
        {
            if (this.historyArgs != null && !this.historyNoEvent)
                this.HistoryOnChange(args);

            this.historyNoEvent = false;
            this.historyArgs = args;
        }
        timerId = setTimeout("pm.HistoryTimer()", 100);
    },
    HistorySet: function(args)
    {
        var w = $("navWin");
        if (w)
        {
            //w.src="Navigator.htm?" + args;
            if (document.all)
                w.contentWindow.document.location = "Navigator.htm?" + args;

            //discard event changes
            this.historyNoEvent = true;

            //          var doc = w.contentWindow.document;
            //          //doc.open("javascript:'<html></html>'");
            //          doc.open("text/html");
            //          //doc.write("<html><body>" + args + "</body></html>");
            //          doc.write(args);
            //          doc.close();

            document.location = "#" + args;
        }
        else alert("Error recording history");
    },
    ClearTimer: function()
    {
        if (timerId != null)
        {
            clearTimeout(timerId);
            timerId = null;
        }
    },
    LoadComplete: function(responseText)
    {
        //fill page with new data
        this.SetPageContent(responseText);

        //hide loader
        this.WaitStop();

        //load content
        if (this.pageId != this.currentPageId)
            this.StartTransition();

        this.isBusy = false;

        this.OnLoadComplete(this.pageId);
    },
    OnLoadComplete: function(pageId)
    { },
    SetPageContent: function(responseText)
    {
        var page = $(this.pageId);
        if (!page)
        {//create new page
            var el = document.createElement("div");
            el.style.display = "none";
            el.id = this.pageId;
            $(this.pageHolderId).appendChild(el);
            page = el;
        }
        page.innerHTML = responseText;
    },
    IsExist: function(pageId)
    {
        var page = $(pageId);
        if (page)
            return true;
        else
            return false;
    },
    StartTransition: function()
    {
        if (typeof (AjaxControlToolkit) == "object")
        {
            //start transition
            var target = $(this.pageHolderId);

            var fadeOut = new AjaxControlToolkit.Animation.FadeOutAnimation(target, 0.15, 20, 0.5, 1, true);
            var fadeIn = new AjaxControlToolkit.Animation.FadeInAnimation(target, 0.2, 20, 0.5, 1, true);
            //var script=new AjaxControlToolkit.Animation.ScriptAction(target, 0.1, 0, "$('" + this.pageHolderId + "').innerHTML=pm.responseText;" );
            var script = new AjaxControlToolkit.Animation.ScriptAction(target, 0.1, 0, "pm.SwitchPage();");

            var seq = new AjaxControlToolkit.Animation.SequenceAnimation(target, 0, 20);
            seq.add(fadeOut);
            seq.add(script);
            seq.add(fadeIn);
            seq.play();
        }
        else
        {
            //don't use animation
            //just switch content
            this.SwitchPage();
        }
    },
    SwitchPage: function()
    {
        var oldPage = $(this.currentPageId);
        var newPage = $(this.pageId);

        $("loader").style.display = "none";

        if (oldPage && oldPage.id != newPage.id)
        {
            //clear page content to stop media objects from playing on background
            if (oldPage.id == "itemInfo")
                oldPage.innerHTML = "";

            //oldPage.style.display = "none";
            oldPage.style.position = "absolute";
            oldPage.style.visibility = "hidden";
            oldPage.style.top = "-10000px";
        }

        if (newPage)
        {
            newPage.style.display = "block";
            newPage.style.visibility = "visible";
            newPage.style.position = "";
            newPage.style.top = "";
            
            this.currentPageId = this.pageId;
        }
    },
    WaitStart: function()
    {
        $Show(this.loadingMsgId);
    },
    WaitStop: function()
    {
        $Hide(this.loadingMsgId);
    }
}

//**********************************************