// Class to enable alternate StyleSheets to be added or removed from the page.
// Links will be present in the page to allow the user to choose an alternate StyleSheet or choose
// the default (i.e. no extra stylesheets).
// If the user selects an alternate then that one is loaded into the page and enabled. If the
// user then selects the default, the extra StyleSheet(s) is/are then disabled.
// A cookie is created and holds the value of the chosen alternate StyleSheet (or blank for the default)
// so that all subsequent visits to a page with this functionality enabled will have that StyleSheet active.
//
SEAT.PG.AlternateCSS = function(cssPrefix) {
  this.cssPrefix = cssPrefix;
  
  // Get the correct domain to use when writing the cookie - get the last 2 elements of the full domain
  var c = document.domain.split(".");
  this.cookieDomain = (c.length >= 3) ? c[c.length - 2] + "." + c[c.length - 1] : document.domain;
  
  // Enable the appropriate stylesheet is a previous cookie value is set, or leave the default.
  this.setCSS(SEAT.COOKIE.getCookie(this.COOKIE_NAME) || "");
  
  // Bind the onclick event
  $.each(this.STYLES, function(i, v) {
                        $("#" + v.id).click(function() {
                                              SEAT.PG.altCSS.setCSS(v.title);
                                            });
                      });
};

SEAT.PG.AlternateCSS.prototype = {
  
  COOKIE_NAME:            "altcss",
  COOKIE_EXPIRY_DAYS:     1825,   // 5 years
  STYLES:                 [ {title: "", id: "altCSSP", cls: "selectedp"}, 
                            {title: "switch_font", id: "altCSSG", cls: "selectedg"}],
  
  setCSS: function(title) {
    var self = this;
    
    if (title === "") {
      // If the extra CSS exists (added by the server) make sure it is an "alternate stylesheet"
      $("link[rel*='style'][title]").attr({rel: "alternate stylesheet"});
      // Set the active stylesheet to a non esistant name so that all alternate stylesheets are disabled
      this.setActiveStyleSheet("Z");
    }
    else {
      // Add the extra CSS if it doesn't exist
      if ($("link[rel*='style'][title='" + title + "']").length === 0) {
        $("head").append('<link rel="alternate stylesheet" type="text/css" title="' + title + '" href="' + this.cssPrefix + title + '.css"/>');
      }
      this.setActiveStyleSheet(title);
    }
    
    // Activate the correct icon
    $.each(this.STYLES, function(i, v) {
                          if (v.title === title) {
                            $("#" + v.id).addClass(v.cls);
                          }
                          else {
                            $("#" + v.id).removeClass(v.cls);
                          }
                        });
    
    // Cookie
    var expireDate = new Date();
  	expireDate.setTime(expireDate.getTime() + (this.COOKIE_EXPIRY_DAYS * 24 * 60 * 60 * 1000));
    SEAT.COOKIE.setCookie(this.COOKIE_NAME, title, expireDate, this.cookieDomain, false);
     
      if($('#altCSSG').hasClass('selectedg')){
        $('#setHp').hide();
      } 
      else{  
        if($('#altCSSP').hasClass('selectedp') && $('#cSpeedDefault').hasClass('none') && window.external && document.all){
            $('#setHp').show();           
        }
      }    
        
  },
  
  setActiveStyleSheet: function(tit) {
    // Get all of the link tags that are a stylesheet and have the "title" attribute
    $("link[rel*='style']").each(function() {
      var t = $(this).attr("title");
      //console.log("title:" + t);
      if (t && (t !== "")) {
        // We need to initially set disabled to "true" - otherwise IE doesn't work properly
        this.disabled = true;
        // If the value of the title attribute matches the passed in value then "enable" the
        // stylesheet by setting its "disabled" attribute to "false".
        this.disabled = !($(this).attr("title") === tit);
      }
    });
  }
};

