/**********************************************************************
* cheslers.objects.js - File with object definitions. Should be 
* included before any othert cheslers scripts.
*
* Copyright (c) 2009, White Horse Technology Consulting Inc.
* Copyright (c) 2009, Cheslers Shoes Inc.
***********************************************************************/ 

// BrandData - Object to store and access brand data.
function BrandData() { 
  // INDICES TO OUR BRAND PROPERTIES
  var BRAND_ARRAY_DIRECTORY = 0
  var BRAND_ARRAY_SIZE_CHART = 1;
  var BRAND_ARRAY_HIDE_FITTING_NOTES = 2;
  var BRAND_ARRAY_BANNER = 3;
  var brandArray = new Object();

  var brandCount = 0;

  // addBrand - Adds a brand to our "associative array." We need to maintain a 
  // separate object because of issues with iterating through object properties
  // brandInfo is an array as documented in cheslers.conf.js
  function addBrand (brandName, brandInfo) {
      brandArray [brandName] = brandInfo;
      ++brandCount;
    } //END: addBrand

  // getBrandSizeDirectory - Object method to return the main brand directory
  function getBrandDirectory (brandName) {
    var directory = brandArray[brandName][BRAND_ARRAY_DIRECTORY];
    if (typeof(directory) == "undefined") {
    return "";
    }
    else {
    if (directory.lastIndexOf("/") != (directory.length -1)) {
        directory = directory + "/";
        }
      return directory;
      }
    } //END: getBrandDirectory
  
  // getBrandSizeChart - Object method to return the size chart file
  function getBrandSizeChart (brandName) {
    var sizeChart = brandArray[brandName][BRAND_ARRAY_SIZE_CHART];
    if (typeof(sizeChart) == "undefined") {
      return "";
      }
    else {
      return sizeChart;
      }
    } //END: getBrandSizeChart
  
  
  // getBrandFittingNotesStatus - Object method to whether fitting notes 
  // should be hidden or not
  function areBrandFittingNotesHidden (brandName) {
    var fittingNotesStatus = brandArray[brandName][BRAND_ARRAY_HIDE_FITTING_NOTES];
    if (typeof(fittingNotesStatus) == "undefined") {
      return false;
      }
    else {
      return Boolean(fittingNotesStatus);
      }
    } //END: areBrandFittingNotesHidden

  // getBrandBanner - Object method to return a brand's banner (file name only)
  function getBrandBanner (brandName) {
    var bannerUrl = brandArray[brandName][BRAND_ARRAY_BANNER];
    if (typeof(bannerUrl) == "undefined") {
      return "";
      }
    else {
      return bannerUrl;
      }
    } //END: getBrandBanner

  // getRandomBrandBanner - Object method to return thre file name of a randomly chosen brand banner
  function getRandomBrandBanner () {
    var bannerCount=0;
    var bannerIndex=Math.floor(Math.random()*(brandCount +1));

    for (brand in brandArray) {
      if (brandArray.hasOwnProperty(brand)) {
        if (bannerCount == bannerIndex) {
          return brandArray[brand][BRAND_ARRAY_BANNER];
          }
        else {
          ++bannerCount;
          }
        }
      }
        
    //Failsafe - should never execute
    return "";
    } //END: getRandomBrandBanner

  this.directory = getBrandDirectory;
  this.sizeChart = getBrandSizeChart;
  this.hideFittingNotes = areBrandFittingNotesHidden;
  this.banner = getBrandBanner;  
  this.randomBanner = getRandomBrandBanner;  
  this.brandCount = brandCount;
  this.add = addBrand;
  } //END: BrandData 



/**********************************************************************
 PageContext - Stores information about the current page
  
 We use this to hold information such as the type of page, the 
 current brand if it si a brand page, etc.

 Arguments:
   * breadcrumbContainer - jQuery object containing an array of anchor
                           tags for each element in the breadcrumb trail

**********************************************************************/
function PageContext (breadcrumbContainer) {
  // INDICES TO OUR MENU PROPERTIES
  var PAGE_CONTEXT_MENU_HOME = 0;
  var PAGE_CONTEXT_MENU_MAIN_CATEGORY = 1;
  var PAGE_CONTEXT_MENU_BRAND = 2;

  var menuStack = new Array(); 
  // Each menuStack entry has two properties - display name and URL
  var menuData = new Object();

  // Constructor
  menuStack=parseBreadcrumbs(breadcrumbContainer);

  // getCustomerName - Returns user name or "" if not logged in 
  function getCustomerName() {
    if (typeof(window["customerUserName"]) == "undefined") {
      return "";
      }
    else {
      return customerUserName;
      }
    } //END: getCustomerName

  // getPageType - Object method to return a page type. Depends on pageType variable 
  // being defined in key page templates
  function getPageType () {
    if (typeof(window["pageType"]) == "undefined") {
      return "";
      }
    else {
      // A brand page is just a regular category page within the Brands category
      if (pageType.toLowerCase() == "category") {
        if (this.topLevelMenuName().toLowerCase() == "brands") {
          return "brand";
          }
        else {
          return "category";
          }
        }
      else {
        return pageType;
        }
      }
    } //END: getPageType

  // Get the top level category of our menu (one below Home)
  function getTopLevelMenuName() {
    if (menuStack.length > PAGE_CONTEXT_MENU_MAIN_CATEGORY) {
      return menuStack[PAGE_CONTEXT_MENU_MAIN_CATEGORY].menuName;
      }
    else {
      return "";
      }
    } //END: getTopLevelMenuName

  // Get the top level link of our menu (one below Home)
  function getTopLevelMenuLink() {
    if (menuStack.length > PAGE_CONTEXT_MENU_MAIN_CATEGORY) {
      return menuStack[PAGE_CONTEXT_MENU_MAIN_CATEGORY].menuUrl;
      }
    else {
      return "";
      }
    } //END: getTopLevelMenuLink

  // Get a particular menu name from our list
  function getMenuName(menuIndex) {
    if (menuIndex < menuStack.length) {
      return menuStack[menuIndex].menuName;
      }
    else {
      return "";
      }
    } //END: getMenuName

  // Get a particular menu link from our list
  function getMenuLink(menuIndex) {
    if (menuIndex < menuStack.length) {
      return menuStack[menuIndex].menuUrl;
      }
    else {
      return "";
      }
    } //END: getMenuLink

  // Complete menu structure - text separated by > - goes down depth levels
  // or full depth if depth is -1.
  function getMenuLine(depth) {
    var menuLine = "";

      if ((depth == -1) || (depth > menuStack.length)) {
        depth = menuStack.length;
        }
  
      for (menuIndex=0; menuIndex < depth; menuIndex++ ) {
        menuLine += menuStack[menuIndex].menuName;
        if (menuIndex < (depth - 1)) {
          menuLine += ">"
          }
        }
        return menuLine;
    } //END: getMenuLine

  // Number of items in our menu structure
  function getMenuDepth() {
    try {
      return menuStack.length;
      }
    catch (err) {
      return 0;
      }
    } //END: getMenuDepth


  // getPageBrand - Return the brand name for a brand directory, or empty if
  //            not a brand direcotry
  function getPageBrand () {
    if (this.pageType() != "brand") {
      return "";
      }

    try {    
      return menuStack[PAGE_CONTEXT_MENU_BRAND].menuName;
      }
    catch (err) {
      return "";
      }  
    } //END: getBrand

  /**********************************************************************
  parseBreadcrumbs - Parses breadcrumbs into data
    
  3dCart insists on just dropping in its breadcrumbs, already in a table
  and everything. But we use the breadcrumbs to enable our multi-level
  menus, so we parse them to figure out just where we are. 
  
  The resulting data is placed in the array menuStack, an where each 
  element contains the menu item's name. menuStack is arranged so that 
  element 0 is the leftmost item in the breadcrumb trail, etc.
  
  Arguments:
    * breadcrumbContainer - jQuery object containing an array of anchor
                            tags for each element in the breadcrumb trail
  
  Returns:
    * menuStack - An array with the menuStack. See PageContext for 
                  details.
  **********************************************************************/
  function parseBreadcrumbs(breadcrumbContainer, menuStack) {
    var menuStack = new Array();
  
    $jq(breadcrumbContainer).each(function (breadcrumbIterator) {
      menuData = new Object();
      menuData.menuName = this.innerHTML;
      menuData.menuUrl = this.href;

      menuStack.push(menuData);
      });
  
    return menuStack;
    } //END: parseBreadcrumbs
  
  //this.menuStack = menuStack;
  this.customerName = getCustomerName;
  this.pageType = getPageType;
  this.pageBrand = getPageBrand;
  this.topLevelMenuName = getTopLevelMenuName;
  this.topLevelMenuLink = getTopLevelMenuLink;
  this.menuName = getMenuName;
  this.menuLink = getMenuLink;
  this.menuDepth = getMenuDepth;
  this.menuLine = getMenuLine;
  } //END: PageContext




  
