﻿//This is MS version of document.ready... this however fires after jquery document.ready.
//This is required to bind the add_navigate event, if it is not bound in this method then navigate
//does not occur on page load!
//Sys.Application.add_init(function(sender, e) {
//recipeSearchHelper.historyTracker.SetHandler(YOUR HANDLER OBJECT, YOUR HANDLER OBJECT.YOUR HANDER METHOD);
//});

function HistoryTracker() {
    this.navigateHandler = null;
    this.navigateArgs = null;
};

//This must be called in order for this object to work.
//caller : the object setting the handler
//func : the handler method
HistoryTracker.prototype.SetHandler = function(caller, func) {
    //store the object setting the handler locally so we can use it to maintain scope in the delegate.
    var _caller = caller;
    var _this = this;
    //create a delegate so we retain the scope
    this.navigateHandler = function(sender, e) {
        _this.navigateArgs = e; //store the event args in this object
        func.call(_caller, sender, e); //call the actual handler with the correct scope
    };

    //initialize the handler
    Sys.Application.add_navigate(this.navigateHandler);
}

//Adds a browser history point
//Creates a deep link value for storing in the hash string for client navigation.
//type: the type of history to make (the key)
//values: an array of values to track (a value cannot contain a comma)
HistoryTracker.prototype.AddHistory = function(name, type, values) {

    //remove the handler so it doesn't fire when setting history
    Sys.Application.remove_navigate(this.navigateHandler);

    //create the value to store: the type first, the comma delimited values next
    var val = type + "|" + values.join(",");

    //create the object to store as history point
    var obj = {};
    obj[name] = val;
    Sys.Application.addHistoryPoint(obj, null); //the null is where you'd set the title of the page if needed

    //re-add the handler to fire when the browser changes
    Sys.Application.add_navigate(this.navigateHandler);
};

//Returns the string representation of a deep link based on the name key and values
HistoryTracker.prototype.GetDeepLinkAddress = function(name, key, values) {

    var str = "#" + name + "=" + key + "|" + values.join(",");
    return str;
}

//Returns a hash table of name value objects.
//Each value object is an object of key/values (see GetHistoryByName).
//Each item in the hash table is an object with a "name" and "value" property.
//EXAMPLE return:
/*
alert("GetHistory: " + GetHistory()[0].name);
alert("GetHistory: " + GetHistory()[0].value);
alert("GetHistory: " + GetHistory()[0].value.key);
alert("GetHistory: " + GetHistory()[0].value.values);
*/
HistoryTracker.prototype.GetHistory = function() {

    var e = this.navigateArgs;

    if (e == null)
        return null;

    var objs = e.get_state();
    var arr = new Array();
    for (i in objs) {
        var history = this.GetHistoryByName(i);
        arr.push({ name: i, value: history });
    }
    return arr;
}

//Checks the event arguments from the navigate event and tries to 
//get the key/values of the tracked history.
//e: the event args of the navigate event
//returns: an object with a key and values properties
HistoryTracker.prototype.GetHistoryByName = function(name) {

    var e = this.navigateArgs;

    //inpect the event args to ensure they are valid and created with this class
    if (e != null && e.get_state != null && e.get_state()[name] != null && e.get_state()[name].indexOf("|") > -1) {
        var deepLink = e.get_state()[name].split("|");

        //ensure it's valid
        if (deepLink.length != 2)
            return null;

        var type = deepLink[0];
        var val = deepLink[1].split(",");

        return { key: type, values: val };
    }
    else
        return null;
}

//Checks the event arguments from the navigate event and tries to 
//get the values of the tracked history by type
//type: the type of history tracked (the key)
//e: the event args of the navigate event
//returns: an array of values
HistoryTracker.prototype.GetHistoryValuesByName = function(name, type) {

    var e = this.navigateArgs;
    if (e == null)
        return null;

    var history = this.GetHistoryByName(name);
    if (history.key == type) {
        return history.values;
    }

    return null;
}
