/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('WinDoH.Html');
    
WinDoH.Html.PageNavigation = function()
{
    //Call Base Method
    WinDoH.Html.PageNavigation.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._settings = {};

    //create delegates (MAKE SURE YOU CLEAN UP IN dispose!)
    this._updateSuccessDelegate = Function.createDelegate(this, this._updateSuccess);
    this._updateFailDelegate = Function.createDelegate(this, this._updateFail);
    this._loadPageDelegate = Function.createDelegate(this, this._loadPage);
}

WinDoH.Html.PageNavigation.prototype =
{
    //Properties
    get_ns: function() {return this.get_id() + '_';},
    get_HtmlControl: function() {return this._htmlControl;},
    set_HtmlControl: function(value) {this._htmlControl = value;},
    get_msgs: function() {return this._msgs;},
    set_msgs: function(value) {this._msgs = Sys.Serialization.JavaScriptSerializer.deserialize(value);},
    get_settings: function() {return this._settings;},
    set_settings: function(value) {this._settings = Sys.Serialization.JavaScriptSerializer.deserialize(value);},

    //Events
    initialize: function() 
    {
        //Call Base Method    
        WinDoH.Html.PageNavigation.callBaseMethod(this, 'initialize');
        //hookup event handlers
        var pages = $get(this.get_ns() + 'Pages');
        var links = pages.getElementsByTagName("a");
        for( var i = 0, link; link = links[i]; i++ ) {
            if(link.tagName=="A"&&link.hasAttribute("guid")) {
                $addHandlers(link, {"click": this._loadPageDelegate}, this);
            }
        }
        pages = null;
        links = null;
    },
    
    _loadPage: function(src, args)
    {
        this._displayWait(true);
        if(src.target.hasAttribute("guid")) {
            var v_htmlGuid = src.target.getAttribute("guid");
            dnn.xmlhttp.callControlMethod('WinDoH.Html.PageNavigation.' + this.get_id(), 
                'LoadPage', {htmlGuid:v_htmlGuid}, this._updateSuccessDelegate, this._updateFailDelegate);
        }
        
    },

    //Methods
    getMessage: function(key)
    {
        return this._msgs[key];
    },

    getSetting: function(key, def)
    {
        if (this._settings[key])
            return this._settings[key];
        return def;
    },

    //Private Methods

    _displayWait: function(show)
    {
        document.body.style.cursor=(show ? 'wait' : 'default');
        $get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },
    
    _updateSuccess: function(payload, ctx, req)
    {
        this._displayWait(false); 
        dnn.dom.getById(this.get_HtmlControl()).innerHTML = payload.Content;
    },

    _updateFail: function(payload, ctx, req)
    {
        this._displayWait(false);    
        alert('error: ' + payload);
    },

    dispose: function()
    {
        this._updateSuccessDelegate = null;
        this._updateFailDelegate = null;
        this._loadPageDelegate = null;
    }
}

//register class and inherit from Sys.Component
WinDoH.Html.PageNavigation.registerClass('WinDoH.Html.PageNavigation', Sys.Component);
