/**
 * Genworth Header Forms
 *
 * The GenworthStyleSwitcher.js is a JavaScript class used to dynamically 
 * change the active stylesheet for pages in the Gewnworth.com domain. The 
 * class also updates the style selector icons located on the page.<br>
 * <br>
 * This class requires the CookieFactory.js file to be imported as well in 
 * order to function properly.
 *
 * @author Genworth Financial, Inc.
 * @created 19 April 2007 
 * @since 19 April 2007
 * @version 1.0
 * @modified 19 April 2007 : Birth
 */

/* CONSTRUCTOR */
  /*GenworthStyleSwitcher*/ function GenworthStyleSwitcher ()
  {
    this._styleCookieName = 'gw_style';
    this._styleCookieLife = 365;
    this._defaultStyle = 'Medium Text';
	this._ImgPath = '/IMG/'; /* HDI Seguros  */
    this._protectedStyles = new Array();
    this._offIcons = new Array();
    this._onIcons = new Array();

    this._setIcons();
  }


/**
 * public addProtectedStyle method
 *
 * @param style The stylesheet to be added to the protected list
 */
  /*void*/ GenworthStyleSwitcher.prototype.addProtectedStyle = function (/*String*/ style)
  {
    this._protectedStyles[this._protectedStyles.length] = style;
  }

/**
 * public changeAndSaveStyle method
 *
 * @param style The stylesheet to be used 
 */
  /*void*/ GenworthStyleSwitcher.prototype.changeAndSaveStyle = function (/*String*/ style)
  { 
    if (!this._styleExists(style)) { return; }
    this.changeStyle(style);
    this.changeStyleIcon(style);
	CookieFactory.createCookie (this._styleCookieName, style, this._styleCookieLife);
  }

/**
 * public changeStyle method
 *
 * @param style The stylesheet to be used
 */
  /*void*/ GenworthStyleSwitcher.prototype.changeStyle = function (/*String*/ style)
  {
    if (!document.getElementsByTagName('')) { return; }
    if (!this._styleExists(style)) { return; }
    
    /*Object[]*/ var links = document.getElementsByTagName('link');
    for (/*int*/ var i=0; i<links.length; i++)
    {
      if (links[i].getAttribute('rel').indexOf('style') != -1 && 
      	links[i].getAttribute('title'))
      {
        if (links[i].getAttribute('title') == style)
        {
          links[i].disabled = false;
        }
        else
        {
          if (!this._isProtected(style)) { links[i].disabled = true; }
        }
      }
    }
  }

/**
 * public changeStyleIcon method
 *
 * @param style The stylesheet to be used
 */
  /*void*/GenworthStyleSwitcher.prototype.changeStyleIcon = function(/*String*/style) {
      if (!this._styleExists(style)) { return; }
      /*Image*/var styleIconSm = document.getElementById('style_icon_sm');
      /*Image*/var styleIconMd = document.getElementById('style_icon_md');
      /*Image*/var styleIconLg = document.getElementById('style_icon_lg');
      // Luis Orozco
      // Hack para usarse en las paginas con el nuevo diseņo que no tienen
      // botones "StyleSwitcher"
      if (!styleIconSm) {
          return false;
      }
      switch (style) {
          case 'Small Text':
              styleIconSm.src = this._onIcons['Small Text'];
              styleIconMd.src = this._offIcons['Medium Text'];
              styleIconLg.src = this._offIcons['Large Text'];
              break;
          case 'Large Text':
              styleIconSm.src = this._offIcons['Small Text'];
              styleIconMd.src = this._offIcons['Medium Text'];
              styleIconLg.src = this._onIcons['Large Text'];
              break;
          default:
              styleIconSm.src = this._offIcons['Small Text'];
              styleIconMd.src = this._onIcons['Medium Text'];
              styleIconLg.src = this._offIcons['Large Text'];
              break;
      }
  }

/**
 * public showDefaultStyle method
 */	
  /*void*/ GenworthStyleSwitcher.prototype.showDefaultStyle = function ()
  {
    /*String*/ var style = CookieFactory.readCookie(this._styleCookieName);
    if (!style) { style = this._defaultStyle; }
    this.changeStyle(style);
    this.changeStyleIcon(style);
  }


/**
 * private isProtected method
 *
 * @param style The style name to be checked
 * @return boolean If the style is protected
 */
  /*boolean*/ GenworthStyleSwitcher.prototype._isProtected = function (/*String*/ style)
  {
    if (!this._protectedStyles) { return false; }
    for (/*int*/ var i=0; i<this._protectedStyles.length; i++)
    {
      if (this._protectedStyles[i] == style) { return true; }
    }
    return false;
  }
 
/**
 * private setIcons method
 */
  /*void*/ GenworthStyleSwitcher.prototype._setIcons = function ()
  {
    this._offIcons['Small Text'] = this._ImgPath + "text_sizer-small.gif";
    this._offIcons['Medium Text'] = this._ImgPath + "text_sizer-medium.gif";
    this._offIcons['Large Text'] = this._ImgPath + "text_sizer-large.gif";
    
    this._onIcons['Small Text'] =  this._ImgPath + "text_sizer-small-on.gif";
    this._onIcons['Medium Text'] =  this._ImgPath + "text_sizer-medium-on.gif";
    this._onIcons['Large Text'] =  this._ImgPath + "text_sizer-large-on.gif";
  }

/**
 * private styleExists method
 *
 * @param style The style to be checked
 * @return boolean If the style is found included on the page
 */
  /*boolean*/ GenworthStyleSwitcher.prototype._styleExists = function (/*String*/ style)
  {
    if (!document.getElementsByTagName('')) { return false; }
    if (!document.getElementsByTagName('link')) { return false; }
    /*Object[]*/ var links = document.getElementsByTagName('link');
    for (/*int*/ var i=0; i<links.length; i++)
    {
      if (links[i].getAttribute('rel').indexOf('style') != -1 && 
      links[i].getAttribute('title'))
      {
        if (links[i].getAttribute('title') == style)
        {
          return true;
        }
      }
    }
    return false;
  }

/**
 *
 *Static method to change style
 *
 *@param style The stylesheet to be used 
 */
function changePageStyle(style) {
    var changer = new GenworthStyleSwitcher();
    changer.changeAndSaveStyle(style);
}
function changeStyle(/*String*/style){
	var changer = new GenworthStyleSwitcher();
	changer.changeAndSaveStyle(style);
}
