﻿//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
    ProductDetails.historyTracker.SetHandler(ProductDetails, ProductDetails.onPageNavigate);
}); 

//Used on the detail.aspx page for handling the deep linking 
//and ajax calls to load in the product grid items.
var ProductDetails = {

    currentLocation: -1,
    currentTag: -1,

    originalLocation: -1,
    originalTag: -1,

    //constructor function, This MUST be called
    //needs to know the original location id and the original tag id of the page when it is loaded.
    //origTag could be -1 if the pages is loaded only by location.
    Init: function(origLocation, origTag) {
        this.currentLocation = origLocation;
        this.currentTag = origTag;
        this.originalLocation = origLocation;
        this.originalTag = origTag;
    },

    historyTracker: new HistoryTracker(),

    //when a deep link is detected or changed
    onPageNavigate: function(sender, e) {

        var history = this.historyTracker.GetHistoryByName("s");

        if (history == null) {
            //we're at the beginning, reload the original category id
            this.LoadLocationCategories(this.originalLocation);
        }
        else if (history.key == "ids" && history.values.length == 2) {
            //we are at a sub category
            this.LoadSubCategoriesByLocation(history.values[0], history.values[1]);
        }
    },

    //Wire up the click event of the product images if we having navigated
    //to a product category, not a product category + tag.
    //If we've navigated to a tag, then we need to append a deep link address to it's url link
    //so that the product detail page knows how to initialize the left accordian menu.
    setupGridItems: function() {

        var _this = this;
        if (this.currentTag == -1) {
            $(".productThumbnail a").click(function() {
                _this.LoadSubCategoriesByLocation($(this).next(".HiddenCategory").val(), _this.currentLocation);
                return false;
            });
        }
        else {

            //append the deep link to the url
            var hash = location.hash;
            if (hash == null || hash == "") {
                //create the hash from the original location and tag
                hash = this.historyTracker.GetDeepLinkAddress("s", "ids", [(this.originalTag == -1 ? this.originalLocation : this.originalTag), this.originalLocation]);
            }

            $(".productThumbnail a").each(function() {
                $(this).attr("href", $(this).attr("href") + hash);
            });
        }
    },

    LoadLocationCategories: function(locationId) {
        //reset the current tag
        this.currentTag = -1;

        try {
            this.currentLocation = locationId;

            //create delegate
            var _this = this;
            var renderViewDelegate = function(result) { _this.RenderView.call(_this, result); };

            Sunbeam.Web.Pages.Browse.ViewService.GetLocationCategories(locationId, renderViewDelegate);
        }
        catch (ex) {
            alert("Web service call error: \n\n" + ex);
        }
    },

    LoadSubCategoriesByLocation: function(subCategoryId, locationId) {

        //store the current tag
        this.currentTag = subCategoryId;

        this.historyTracker.AddHistory("s", "ids", [subCategoryId, locationId]);

        try {
            this.currentLocation = locationId;

            //create delegate
            var _this = this;
            var renderViewDelegate = function(result) { _this.RenderView.call(_this, result); };

            Sunbeam.Web.Pages.Browse.ViewService.GetSubCategoriesByLocation(subCategoryId, locationId, renderViewDelegate);
        }
        catch (ex) {
            alert("Web service call error: \n\n" + ex);
        }
    },

    RenderView: function(result) {
        $(".BrowseTitle").text(result.Title);
        $(".BrowseTagline").text(result.Tagline);
        $(".BrowseResults").html(result.Content).hide().fadeIn();

//        // Make the scrollbar go! - removed custom scroller
//        $('.productDisplay').jScrollPane();
//        if ($('.productDisplay')[0].scrollTo) {
//            $('.productDisplay')[0].scrollTo(0);
//        }

        this.setupGridItems();
    }

};