﻿//
// Huguenots_Webservices:
// This file is needed for talking to the Huguenots.Webservices.PageUtilities service
// The Initialize method has to be called on page load for the webservice to work!
// PLEASE MAKE SURE that ScriptRunner.JPG is in the same folder as this JS file
//

var contentConsumerName;
var categoryConsumerName;
var subCategoryConsumerName;
var contentChangeMethod;

var defaultError = " was undefined, please use Initialize method when document.onLoad occurs";

//
// The Initialize method acting as a constructor
// Parameter: The Content Consumer
// Parameter: The Category Consumer
// Parameter: The Sub Category Consumer
// Parameter: The Content Changed Delegate
//
function Initialize(contentConsumerName, categoryConsumerName, subCategoryConsumerName, contentChangeMethod)
{
    this.contentConsumerName = contentConsumerName;
    this.categoryConsumerName = categoryConsumerName;
    this.subCategoryConsumerName = subCategoryConsumerName;
    this.contentChangeMethod = contentChangeMethod;
}

function LoadModule(key)
{
    //debugger;
    var categoryKey = key.split('.')[0];
    var response = function(result) 
                        {
                          var elem = $get(subCategoryConsumerName);
                          elem.innerHTML = result;
                          LoadContent(key);   
                        }

    LocalService.GetSubCategoriesNoAutoload(dataSource, categoryKey, response);
}

//
// LoadDefaultContent - for default only
// Parameter: the key of the SubCategory to load
//
function LoadDefaultContent(key) 
{
     
     //
     // Fire the ContentChanged Delegate if the page needs to do custom styles
     //
     
     contentChangeMethod.call(this, key);
     if(contentConsumerName != undefined)
     {
        //
        // Create a response delegate
        //
        var response = function(result) 
                        {
                            var elem = $get(contentConsumerName);
                            elem.innerHTML = result;
                            elem.style.display = "";
                        }
        
        LocalService.ProvideContent(dataSource, key, response);
       
     }
     else
     {
        Error("ContentConsumer" + defaultError);
     }
}
//
// SelectLink
// Parameter: the key of the Link to highlight
  function SelectLink(key)
  {
    var elems = document.getElementsByTagName("img");
            
    for(i = 0; i < elems.length; i++)
    {
      var thisElem = elems[i];

      // If this image belongs to the selected item then make it visible
      if(thisElem.id == "img_"+key)
      {
        thisElem.style.visibility = "visible";
        var link = document.getElementById("ln_"+key);
        link.style.color="#333333";
        break;
      }
    }
  }

//
// LoadContent
// Parameter: the key of the SubCategory to load
//
function LoadContent(key) 
{
     //
     // Fire the ContentChanged Delegate if the page needs to do custom styles
     //
     contentChangeMethod.call(this, key);
     
     if(contentConsumerName != undefined)
     {
        //
        // Create a response delegate
        //
        var response = function(result) 
                        {
                            var elem = $get(contentConsumerName);
                            elem.innerHTML = result;
                        }
        LocalService.ProvideContent(dataSource, key, response);
     }
     else
     {
        Error("ContentConsumer" + defaultError);
     }
}

//
// LoadCategories
//
function LoadCategories()
{
    if(contentConsumerName != undefined)
    {
        //
        // Create a response delegate
        //
        var response = function(result) 
                        {
                          var elem = $get(categoryConsumerName);
                          elem.innerHTML = result;   
                        }
        LocalService.GetCategories(dataSource, response);
    }
    else
    {
        Error("CategoryConsumer" + defaultError);
    }
}

//
// LoadSubCategories
//
function LoadSubCategory(key, autoLoad)
{
    if(contentConsumerName != undefined)
    {
        //
        // Create a response delegate
        //
        var response = function(result) 
                        {
                          var elem = $get(subCategoryConsumerName);
                          elem.innerHTML = result;   
                        }
        if(autoLoad)
        {
          LocalService.GetSubCategoriesWithAutoload(dataSource, key, response);
        }
        else
        {
           LocalService.GetSubCategoriesNoAutoload(dataSource, key, response);
        }
    }
    else
    {
        Error("SubCategoryConsumer" + defaultError);
    }
}

//
// LoadClientWidget
// Parameter: The key of the widget
// Parameter: The Consumer of the widget
// Parameter: The parameters for the widget, this will be specified in the .vars file on the 
//            Webservice Widgets directory of the specified datasource
//
function LoadClientWidget(key, consumer, params)
{
    if(consumer != undefined)
    {
        var response = function(result) 
                        {
                          consumer.innerHTML = result;   
                        }
        LocalService.GetClientWidget(dataSource, key, params, response);
    }
    else
    {
        Error("Widget Consumer '"+key+"' was undefined, please make sure the consumer is defined in your html page");
    }
}

//
// LoadServerWidget
// Parameter: The key of the widget
// Parameter: The Consumer of the widget
//
function LoadServerWidget(key, consumer)
{
    if(consumer != undefined)
    {
        var response = function(result) 
                        {
                          consumer.innerHTML = result;   
                        }
        LocalService.GetServerWidget(dataSource, key, response);
    }
    else
    {
        Error("Widget Consumer '"+key+"' was undefined, please make sure the consumer is defined in your html page");
    }
}