/**
* Change Style: a jQuery Plugin
* @author: Trevor Morris (trovster)
* @url: http://www.trovster.com/lab/code/plugins/jquery.change-style.js
* @documentation: http://www.trovster.com/lab/plugins/change-style/
* @published: 22/10/2008
* @updated: 22/10/2008
* @requires: jQuery v.1.2.6 or above, jquery cookie plugin (optional)
*
*/
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			changestyle: function(options) {
				var settings = $.extend({}, $.fn.changestyle.defaults, options);
			
				return this.each(
					function() {
						if($.fn.jquery < '1.2.6') {return;}
						var $t = $(this);
						var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
						
						/**
						* Get all the link elements
						* They must have a title
						* One of their rel values must be 'stylesheet'
						*/
						var $link_stylesheets = $('link[title][rel|=stylesheet]');
						
						$t.click(function(){
							var $a = $(this); $a.blur();
							
							/**
							* Parse the href attribute through the query function
							* Returns an object with key/value pairs based on GET data.
							* @returns object
							*/
							var query = $.fn.changestyle.query($a.attr('href'));
							
							/**
							* Check whether this stylesheet actually exists
							* And that it matches exactly one linked stylesheet
							*/
							stylesheet_exists = $link_stylesheets.filter('[href^='+query[o['query']]+']').length;
							if(stylesheet_exists==1) {
								/**
								* Filter the stylesheet which is not alternate, and thus currently active
								* Change the rel attribute, adding the alternate rel attribute, and also set to disabled
								* Then filter based on the clicked query string and set the rel attribute.
								*/
								$link_stylesheets.filter('rel=stylesheet').attr('rel','alternate stylesheet').attr('disabled',true)
								 .end().filter('[href^='+query[o['query']]+']').attr('rel','stylesheet').attr('disabled',false);
								
								/**
								* If the Cookie option is set, and the cookie plugin is available.
								* Use the Cookie option value as the cookie name
								*/
								if(o['cookie']!=false && typeof $.cookie != 'undefined') {
									$.cookie(o['cookie'], query[o['query']], { path: '/', expires: 10 });
								}
							 
								return false;
							}
						});
					}
				);
			}
		});
		
		/**
		* Plugin Defaults
		*/
		$.fn.changestyle.defaults = {
			'query' : 'style',
			'cookie' : 'change_stylesheet'
		};
		
		/**
		* Query Function
		* @url http://stilbuero.de/demo/jquery/query.html?bar=foor&number=0
		*/
		$.fn.changestyle.query = function(s) {
			var r = {};
			if(s) {
				var q = s.substring(s.indexOf('?') + 1);
				q = q.replace(/\&$/, '');
				$.each(q.split('&'), function() {
					var splitted = this.split('=');
					var key = splitted[0];
					var val = splitted[1];
					
					/**
					* Convert Numbers
					*/
					if(/^[0-9.]+$/.test(val)) val = parseFloat(val);
					
					/**
					* Convert Booleans
					*/
					if(val == 'true') val = true;
					if(val == 'false') val = false;
					
					/**
					* Ignore Empty Values
					*/
					if(typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
				});
			}
			return r;
		};
	});
}