﻿// From: http: //blog.webjak.net/2008/09/08/control-silverlight-by-using-browser-back-and-foward-buttons/

function slLoad(sender) {
    
    var run = Function.createDelegate(historyInstance, historyInstance.init);
    //run(sender);
//    

    var s = document.getElementById("Xaml1");
    run(s);
}


historyManager = function() {
    this._silverlightControl = null;
    this._sHM = null;
}

historyManager.prototype = {
    init: function(sender) {
        //the sender variable contains a reference to the Silverlight object.
        //get it and store it in a local variable
        //this._silverlightControl = sender.get_element();
        this._silverlightControl = sender;
        //get and store a reference to the silverlightHistoryManager managed object
        //exposed from Silverligt using RegisterScriptableObject
        this._sHM = this._silverlightControl.Content.silverlightHistoryManager;

        // Added by ricardo.fiel at fullsix to support initial point
        var initialPointData = null;
        var idx = document.URL.toLowerCase().indexOf("#data=");
        if (idx != -1 && (idx + 6) < document.URL.length)
            initialPointData = document.URL.substr(idx + 6);

        //pass "this" object in to the Silverlight application so it may call back to
        //register history events.
        this._sHM.SetJSHistoryObject(this, initialPointData);
    },
    navigationEventHandler: function(sender, args) { //This method will be called by ASP.NET AJAX when the user uses the back and forward buttons.
        if (this._sHM != null) {
            this._sHM.LoadPoint(sender, args.get_state().data);
        }
    },
    addHistPoint: function(pointData, pageTitle) { //This method is called from Silverlight to add a new history point.
        var title = pageTitle;
        if (pageTitle == undefined)
            pageTitle = document.title;
        Sys.Application.addHistoryPoint({ data: pointData }, pageTitle);
    }
}

//Instantiate the historyManager. 
var historyInstance = new historyManager();

//Create a delegate to preserve scope when the navigation event handler fires.
var handler = Function.createDelegate(historyInstance, historyInstance.navigationEventHandler);

//Add the delegate tot he add_navigate event. This will cause the navigationEventHandler method of 
//historyManager to fire when the user uses the back and forward buttons in the browser.
Sys.Application.add_navigate(handler);