Type.registerNamespace("ASPCode.net");

/*
    Ported to Microsoft Ajax by Stefan Holmberg
    http://www.aspcode.net
    
    Modified for PJPA by Guillaume Boit 2007/04/20
    
 * jQuery history plugin
 *
 * Copyright (c) 2006 Taku Sano (Mikage Sawatari)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */



ASPCode.net._History = function() {
    ASPCode.net._History.initializeBase(this);    
}

ASPCode.net._History.prototype = {
    // OK to declare value types in the prototype
    _useFrame: function()
    {   
        if(Sys.Browser.agent == Sys.Browser.InternetExplorer)
            return true;
        return false;
    },
    _verifyIEFrame: function()
    {
        if(this._useFrame() )
        {             
            var history = document.getElementById("aspcodenethistory");
            var iframe = history.contentWindow.document;
                       
            if (iframe.location.hash.length < 2)
            {                
                iframe.open();
                iframe.close();
                iframe.location.hash = location.hash;
            }
        }
    },

    //functions
    setLocation: function(sNewHash, isBack)
    {        
		var newhash = '#' + sNewHash;	
		this._historyCurrentHash = sNewHash;
        var hidden = document.getElementById("divhiddenhistory").getElementsByTagName("input")[0];

		//re-init
		if(newhash != "#")
		    this._verifyIEFrame();
		if ( !window.historyInterval )
        {
            window.historyInterval = setInterval(Function.createDelegate(this, this._timerCallback), 100);
        }
             
        
        if (isBack == true)
        {
            var handler = this.get_events().getHandler("locationChanged");
            if (handler)
            {
                handler(this, sNewHash);
            }
            else
            {
                ASPCode.net.History.add_locationChanged(OnLocationChanged);
                var handler = this.get_events().getHandler("locationChanged");
                handler(this, sNewHash);
            }
            hidden.value = newhash;
        }
        else
        {
            if(newhash != "#")
            {
                hidden.value = newhash;
            }
            if(this._useFrame())
            {
                var histframe = $get("aspcodenethistory");
                var iframe = histframe.contentWindow.document;
                iframe.open();                
                iframe.close();
                iframe.location.hash = newhash;                
                //for bookmarking
                location.hash = location.hash;
            }
            else
            {
                location.hash = newhash;  
            }
        }
        
    },


    
    _timerCallback: function() 
    {
        //if the current hash is different than the last one stored, an history back
        //has occured, we must launch the event
        if(this._useFrame())
        {
            var histframe = $get("aspcodenethistory");
            var iframe = histframe.contentDocument || histframe.contentWindow.document;
			var current_hash = iframe.location.hash;
			var hidden = document.getElementById("divhiddenhistory").getElementsByTagName("input")[0];
			if(current_hash.replace(/^#/, '') != this._historyCurrentHash &&  
			    hidden.value != current_hash) 
			{
			    this._historyCurrentHash = current_hash.replace(/^#/, '');
                this.setLocation(this._historyCurrentHash, true);
            }
        }
        else 
        {
	        // otherwise, check for location.hash
	        var current_hash = location.hash;	        
			if(current_hash.replace(/^#/, '') != this._historyCurrentHash) 
	        {	        	        
		        this._historyCurrentHash= current_hash.replace(/^#/, '');
		        this.setLocation(this._historyCurrentHash, true);		     
	        }
	     }        
    
     },
    
    // events
    add_locationChanged: function(handler) {
        /// <summary>Adds a event handler for the LocationChanged event.</summary>
        /// <param name="handler" type="Function">The handler to add to the event.</param>
        this.get_events().addHandler("locationChanged", handler);
    },
    remove_locationChanged: function(handler) {
        /// <summary>Removes a event handler for the LocationChanged event.</summary>
        /// <param name="handler" type="Function">The handler to remove from the event.</param>
        this.get_events().removeHandler("locationChanged", handler);
    },

    dispose: function() {
        // call set_enabled so the property changed event fires, for potentially attached listeners.
        // be sure to call base.dispose()
        ASPCode.net._History.callBaseMethod(this, 'dispose');
    }


}

// JSON object that describes all properties, events, and methods of this component that should
// be addressable through the Sys.TypeDescriptor methods, and addressable via xml-script.
ASPCode.net._History.descriptor = {
    properties: [  ],
    events: [ {name: 'locationChanged'} ]
}

ASPCode.net._History.registerClass('ASPCode.net._History', Sys.Component);

ASPCode.net.History = new ASPCode.net._History();

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



