/**
 * 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
 * @modified 19 April 2007 : Birth
 * @modified 30 May 2007 : updated cookiePath attribute to make style cookies 
 *                         valid for the entire domain - reynolds
 */

/* CONSTRUCTOR */
  /*GenworthStyleSwitcher*/ function GenworthStyleSwitcher ()
  {
    this._styleCookieName = 'gw_style';
    this._styleCookieLife = 365;
    this._styleCookiePath = '/';
    this._defaultStyle = 'Medium Text';
    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, this._styleCookiePath);
	


  }

/**
 * 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');

	if (!styleIconSm || !styleIconMd || !styleIconLg) { return; }

    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;
	}

	//SAFARI SETUP
	// Setting up the link state
    /*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'))
      	{	
			links[i].disabled = true;
		}
	}
	// END SAFARI SETUP

	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 ()
  {  

	var urlLocation = location.href;
	

	if (urlLocation.match("https://") != null) {
		
		
		
		this._offIcons['Small Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-small.gif";
		this._offIcons['Medium Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-medium.gif";
		this._offIcons['Large Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-large.gif";

		this._onIcons['Small Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-small-on.gif";
		this._onIcons['Medium Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-medium-on.gif";
		this._onIcons['Large Text'] = "https://pro.genworth.com/content/apps/genworth/docroot/images/text_sizer-large-on.gif";

	} else {
		

		this._offIcons['Small Text'] = "http://www.genworth.com/shared/images/text_sizer-small.gif";
		this._offIcons['Medium Text'] = "http://www.genworth.com/shared/images/text_sizer-medium.gif";
		this._offIcons['Large Text'] = "http://www.genworth.com/shared/images/text_sizer-large.gif";

		this._onIcons['Small Text'] = "http://www.genworth.com/shared/images/text_sizer-small-on.gif";
		this._onIcons['Medium Text'] = "http://www.genworth.com/shared/images/text_sizer-medium-on.gif";
		this._onIcons['Large Text'] = "http://www.genworth.com/shared/images/text_sizer-large-on.gif";

}







	//this._offIcons['Small Text'] = "http://www.genworth.com/shared/images/text_sizer-small.gif";
    //this._offIcons['Medium Text'] = "http://www.genworth.com/shared/images/text_sizer-medium.gif";
    //this._offIcons['Large Text'] = "http://www.genworth.com/shared/images/text_sizer-large.gif";
    
	//this._onIcons['Small Text'] = "http://www.genworth.com/shared/images/text_sizer-small-on.gif";
    //this._onIcons['Medium Text'] = "http://www.genworth.com/shared/images/text_sizer-medium-on.gif";
    //this._onIcons['Large Text'] = "http://www.genworth.com/shared/images/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;
  }