/**
 * Styleswitch Stylesheet Switcher jQuery Plugin 2008.05.18 
 * A simple jQuery Plugin to switch the stylesheet.
 * 
 * This is based upon Kelvin Luck's ( http://www.kelvinluck.com/ )
 * "Styleswitch stylesheet switcher built on jQuery"
 * Under an Attribution, Share Alike License
 * Original by Kelvin Luck ( http://www.kelvinluck.com/ )
 * By Seamus P. H. Leahy ( http://moronicbajebus.com/ )
 *
 * 
 * This plugin gives you a simple way to turn on and off stylesheets. It will also store 
 * the current stylesheets state in a user cookie so they can be restored when they come 
 * to the page the next time. This is good if you want to switch the theme via stylesheet
 * or make the text bigger by turning on an additional stylesheet.
 *
 * To utilize the storing the user's settings in a cookie, you will need to also use the 
 * jQuery cookie plugin: http://plugins.jquery.com/project/cookie
 *
 * 
 * Interface:
 *
 * jQuery.styleswitch
 *  This is the namespace and object for Styleswitch.
 *
 * jQuery.styleswitch.disable(title, [cookie])
 *  Disable the stylesheets (via the link element) with the title attr matching the title param.
 *  @param title string - the title attr on the link elements you will be disabling
 *  @param cookie object optional - the cookie settings to save it (see Cookie Settings below)
 *  @return jQuery global object
 *
 * jQuery.styleswitch.enable(title, [cookie])
 *  Enable the stylesheets (via the link element) with the title attr matching the title param.
 *  @param title string - the title attr on the link elements you will be enabling
 *  @param cookie object optional - the cookie settings to save it (see Cookie Settings below)
 *  @return jQuery global object
 *
 * jQuery.styleswitch.toggle(title, [cookie])
 *  Toggles the stylesheets (via the link element) with the title attr matching the title param.
 *  If the stylesheets have different disabled states, then all of them will be set to match the
 *  toggle state of the first match.
 *  @param title string - the title attr on the link elements you will be toggling
 *  @param cookie object optional - the cookie settings to save it (see Cookie Settings below)
 *  @return jQuery global object
 *
 * jQuery.styleswitch.isDisabled(title)
 *  Checks if the stylesheet with the title attr matching the param title is disabled.
 *  If the stylesheets have different disabled states, then the disabled state of the first
 *  matched element is checked.
 *  @param title string - the title attr on the link elements you want to check
 *  @return boolean - true if is disabled, false if enabled
 *
 * jQuery.styleswitch.load()
 *  Loads the stylesheet disabled settings from the cookie (if set).
 *
 * jQuery.styleswitch.reset([cookie])
 *  Resets the stylesheet disabled state to their states before this plugin changed the
 *  disabled state for a stylesheet.
 *  
 *  If you want to reset the states without clearing out the cookie, pass false for the cookie.
 *  @param cookie object optional - the cookie settings to save it (see Cookie Settings below)
 *
 * 
 * jQuery.styleswitch.removeCookie()
 *  This will remove the cookie used to save the users settings. This is for advance use.
 *
 * jQuery.styleswitch.cookieName 
 *  This is a string for the name of the cookie. This is for advance use.
 * 
 * jQuery.styleswitch.defaultCookie
 *  This is for advance use.
 *  For more information, read the jQuery cookie plugin.
 *  The object for the default cookie settings.
 *    expires: 14, 
 *    path: '/', 
 *    domain:  window.location.hostname, 
 *    secure: window.location.protocol == 'https'}
 *
 * 
 * Cookie Settings
 * 1. If nothing is passed in, then the defaultCookie settings are used
 * 2. If false is passed for the cookie, then no cookie will be used
 * 3. If an object is passed with settings, then those settings will be 
 *    used and override the default settings
 *
 * Settings:
 *   expires number - the number of days until the cookie is expired
 *   path string - the path which the cookie will use for
 *   domain string - the domain the cookie will be valid for
 *   secure boolean - is this a secure cookie
 *
 *   For more information about the settings, visit http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
 **/



(function(){ // Keeping the namespace tidy
	
	jQuery.styleswitch = {
		disable: 		function(title, cookie){
								setStyleSheetState(title, true);
								getStyleSheets(title).attr('disabled', 'disabled');
								setCookie(cookie);
								return this;
							},
		enable:			function(title, cookie){
								setStyleSheetState(title, false);
								getStyleSheets(title).removeAttr('disabled');
								setCookie(cookie);
								return this;
							},
		toggle:			function(title, cookie){
								var s = getStyleSheets(title);
								
								// this is to sync all of them to be in the same state
								if(!s.get(0)){
									return this;
								}
								if(s.get(0).disabled === true) {
									jQuery.styleswitch.enable(title, cookie);
								} else {
									jQuery.styleswitch.disable(title, cookie);
								}
								return this;
							},
		load:				function(){
								// The 2nd param is false in ***ableStyleSheet because we are updating the
								// DOM state to match the user's cookie settings state instead of the usually
								// other way around.
								applyStylesheetStates(unserializeCookie(), false);
								return this;
							},
		reset:			function(cookie){
								applyStylesheetStates(originalState, cookie);
							},
		removeCookie:	function(){
								removeTheCookie();
							},
	/**
	 * isDisabled
	 *
	 * @param title string - the title attr for the link elements
	 * @return true if it is disabled, false if it is enabled
	 */
		isDisabled:		function(title){
								if(stylesheetState[title] !== undefined){
									return stylesheetState[title];
								} else if(!getStyleSheets(title).get(0)){
									return undefined;
								} else {
									stylesheetState[title] = getStyleSheets(title).get(0).disabled === true;
									return stylesheetState[title];
								}
							},
		defaultCookie: { expires: 1, path: '/', domain:  window.location.hostname , secure: window.location.protocol == 'https'},
		cookieName:		'StyleSwithStyleSheetSwitcher'
	};
	
	
	
	// Internal
	
	
	// 
	// Returns the jQuery set for the stylesheets with the given title
	// @title string = the title attr on the link elements
	var getStyleSheets = function(title){
		return jQuery('link[@rel*=style][@title='+title+']');
	};
	
	
	var setStyleSheetState = function(title, disabled){
		if(originalState[title] === undefined){
			originalState[title] = jQuery.styleswitch.isDisabled(title);
		}
		
		var prev = stylesheetState[title];
		stylesheetState[title] = disabled;
		// trigger event
		jQuery(document).trigger('styleswitch', [title, disabled, prev]);
	};
	
	// Cookie stuff
	
	// The values to save in the cookie
	var stylesheetState = {};
	
	// The original values
	var originalState = {};
	
	
	// Converts stylesheetState into a string form to save in the cookie
	// @returns string - the serialized version
	var serializeCookie = function(){
		var a = [];
		// create an array of the title=bool
		for(var b in stylesheetState){
			if(stylesheetState[b]){
				a.push(b+'=1');
			} else {
				a.push(b+'=0');
			}
		}
		
		// insert & between pairs and return
		return a.join('&');
	};
	
	
	// Converts the serialized cookie into the stylesheetState variable
	var unserializeCookie = function(){
		if(!jQuery.cookie){ // without jQuery.cookie we cannot load cookie
			return this;
		}
		var states = {};
		var v = jQuery.cookie(jQuery.styleswitch.cookieName);
		if(v){
			// split in title=bool
			var a = v.split('&');
			for(var i=0; i<a.length; i++){
				var b = a[i].split('=');
				states[b[0]] = b[1] == '1';
			}
		}
		return states;
	};
	
	// This sets the users cookie for the stylesheet information.
	var setCookie = function(cookie){
		if(cookie === false || !jQuery.cookie){
			return;
		}
		
		cookie = jQuery.extend(jQuery.styleswitch.defaultCookie, cookie);
		
		// Update the cookie state to match the DOM state
		jQuery.cookie(jQuery.styleswitch.cookieName, serializeCookie(), cookie);
	};
	
	
	var removeTheCookie = function(cookie){
		if(cookie === false || !jQuery.cookie){
			return;
		}
		cookie = jQuery.extend(jQuery.styleswitch.defaultCookie, cookie);
		jQuery.cookie('StyleSwithStyleSheetSwitcher', null, cookie);
	};
	
	var applyStylesheetStates = function(s, cookie){
		for(var c in s){
			if(s[c]){
				jQuery.styleswitch.disable(c, cookie);
			} else {
				jQuery.styleswitch.enable(c, cookie);
			}
		}
	};
	
})();