// viostream.history.js 
// version: 1.0
// requires:
//      - viostream.js
//      - jquery.history.js

Viostream.History.PageClick = function(state) {
    /// <summary> Called on every click action </summary>
    var ParseNavString = function(navstring) {
        navstring = !navstring ? "" : navstring.replace("%2C", ",");
        var matches = navstring.split(",");
        matches.unshift(navstring);
        return matches;
    }
    if (state == "" && location.href.indexOf("#") < 0) {
        state = Viostream.History.QueryStringParameter("section") || "";
    }
    // if we're at the original page, then there isn't much point doing anything
    var initialSection = document.getElementById("initialSection");
    if (initialSection && initialSection.value == state) {
        initialSection.value = "SOMERANDOMTEXT"; // don't allow this check to work until we press refresh
    } else if (state == "") {
        // if there is no state then we need to load the ALL items tab  
        Viostream.LoadAllItems();
    } else {
        var matches = ParseNavString(state)
        if (matches != null) {
            var catID = matches[1];
            var pageIndex = matches[2];
            var sortOrder = matches[3];
            var mediaType = matches.length >= 4 ? matches[4] : "All";
            Viostream.LoadCategoryFromGuid(catID, pageIndex, sortOrder, mediaType);
        } else {
            alert("No match for state = " + state);
        }
    }
};
