/* 
 * Style Switcher (c) Creative Commons 2006
 * License: http://creativecommons.org/licenses/by-sa/2.5/
 * Author: Steve Lounsbury | http://stephenminded.com
 * Reference: http://stephenminded.com/posts/javascript-stylesheet-switcher
 * Attribution: If you use or modify this, please provide a link to the
 *              reference URL above somewhere in your code.  Thanks!
 */

var StyleSwitcher = {
  /*
   *  Switcher to switch the id of the body element depending on the time
   *  of day the user is visiting your site.  The time is that of the 
   *  user's PC.  Feel free to modify the styles array to suit your own 
   *  preferences for what styles get set at what times.
   */
  timeSwitcher : {
    strategy : "id",
    defaultStyle : "night",
    // 0 = midnight, 23 = 11pm
    styles : { "morning" : { s:5, e:12 }, "afternoon" : { s:12, e:17 }, "evening" : { s:17, e:22 } },
    getStyle : function () {
      var h = new Date().getHours();
      for (var i in this.styles)
      {
        if (this.styles[i].s <= h && this.styles[i].e > h) return i;
      }
      return false;
    }
  },
  
  /*
   *  Switcher to switch the stylesheet based on the referring url.  for
   *  instance, you can select a custom stylesheet based on if the visitor
   *  is coming from google or digg.
   */
  referrerSwitcher : {
    strategy : "stylesheet",
    targetId : "style_target",
    defaultStyle : "referrer.css",
    // Feel free to change this as you wish, the key is a regular expression and
    // the value is the css file to use.
    styles : {'/digg.com/' : 'digg.css', 
              '/stephenminded.com/' : 'sm.css'},
    getStyle : function () {
      var ref = document.referrer;
      for (var i in this.styles)
      {
        var pattern = new RegExp(i);
        if (pattern.test(ref)) return this.styles[i];
      }
      return false;
    }
  },
  
  /*
   *  As the name suggests, this does the work of switching the style.  It
   *  takes a Switcher object which conatains the strategy by which you want
   *  to switch the style as well as the function to call which returns the
   *  value that the style should be.
   *
   *  Details about the switcher object:
   *    General structure:
   *      var switcher = {
   *        strategy      : String,
   *        defaultStyle  : String,
   *        target_id     : String, (only used when strategy=='stylesheet')
   *        getStyle      : Function
   *      }
   *
   *    Description of fields:
   *      switcher.strategy - a string containing either "id" or 
   *        "stylesheet".  When 'id' is specified, the script will change
   *        the id of the body element to the value returned by getStyle.
   *        When 'stylesheet' is specified, the script will grab the 
   *        element whos id is defined in target_id and set the href to 
   *        the value returned by getStyle.
   *      switcher.defaultStyle - a string which represents the style you
   *        want set if the getStyle function fails.
   *      switcher.targetId - used when strategy is 'stylesheet', this is
   *        the id of the stylesheet element you wish the script to change.
   *      switcher.getStyle - this is the function that decides which 
   *        style should be set.  it should return a string or false if 
   *        you want to use the value defined in defaultStyle.
   *
   *    For an example of the switcher object, look at referrerSwitcher and
   *    timeSwitcher above.
   *
   * @param switcher
   */
  doswitch : function (switcher) {
    if (!switcher.strategy || typeof(switcher.strategy) != "string") throw new Error("strategy must be defined as a string in switcher object");
    if (!switcher.getStyle || typeof(switcher.getStyle) != "function") throw new Error("getStyle must be defined as a function in switcher object");
    if (!switcher.defaultStyle || typeof(switcher.defaultStyle) != "string") throw new Error("defaultStyle must be defined as a string in switcher object"); 
    
    var newStyle = switcher.getStyle() || switcher.defaultStyle;
    switch (switcher.strategy) {
      case "id":
        document.body.id = newStyle;
        break;
      case "stylesheet":
        if (!switcher.targetId || typeof(switcher.targetId) != "string") throw new Error("stylesheet switchers must have targetId defined");
        var ss = document.getElementById(switcher.targetId);
        if (!ss) throw new Error("switcher targetId refers to non-existent element");
        if (!ss.rel || ss.rel != "stylesheet") throw new Error("switcher targetId does not refer to a stylesheet element");
        ss.setAttribute("href", newStyle);
        break;
      default:
        throw new Error("strategy must be one of 'id' or 'stylesheet' in switcher object");
        break;
    }
  }
};