﻿//Relies on:
// History.js

//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) {
    //setup the history tracker handler
    accordianMenuHelper.historyTracker.SetHandler(accordianMenuHelper, accordianMenuHelper.onPageNavigate);
});     

//a helper class for selecting the correct items in the menu based on the location and tag id
var accordianMenuHelper = {

    historyTracker: new HistoryTracker(),
    openHistoryElementOnLoad: null,

    //mainId = location id
    //subId = tag id
    //to select all location products, pass the location id to both params 
    doMenuSelection: function(mainId, subId) {

        //need to expand the category from the left nav
        var h4 = $("#" + mainId).parents("div.item").find("h4");
        if (!h4.hasClass("selected")) {
            $(".menuAccordian").accordion("activate", h4); //open the one we want
        }

        //need to unselect all categories
        $(".subcategorySelected").removeClass("subcategorySelected");

        //need to select the category from the left nav
        $("#" + subId).addClass("subcategorySelected");
    },

    //when a deep link is detected, update the menu selections
    onPageNavigate: function(sender, e) {

        var history = this.historyTracker.GetHistoryByName("s");
        if (history == null) {
            //no deep link
        }
        else if (history.key == "ids" && history.values.length == 2) {
            setTimeout(function() {
                accordianMenuHelper.doMenuSelection(history.values[1], history.values[0]);
            }, 20); // accordion sometimes will not open without using delay
        }

    }

};
