jQuery.fn.styleSwitcher = function(){
	$(this).click(function(){
		loadStyleSheet(this);
		return false;
	});
	// Set cookie with path of zone
	function setCookie(name, value, days, zone) {
		var expireDate = new Date()
		//set "expstring" to either future or past date, to set or delete cookie, respectively
		var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
		if ( zone ) 
			document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"+zone+"/;"
		else
			document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/;"	
	}	
	function loadStyleSheet(obj) {
		$('body').append('<div id="overlay" />');
		var pathArray = obj.href.split( '/' );
		$('body').css({height:'100%'});
		$('#overlay')
			.css({
				display: 'none',
				position: 'absolute',
				top:0,
				left: 0,
				width: '100%',
				height: '100%',
				zIndex: 1000,
				background: 'black url(/wp-content/themes/zonecsl/images/loading.gif) no-repeat fixed center'
			})
			.fadeIn(500,function(){
				$.get( obj.href+'&js',function(data){
					$('#stylesheet').attr('href','/wp-content/themes/zonecsl/css/' + data + '.css');
					cssDummy.check(function(){
						$('#overlay').fadeOut(500,function(){
							$(this).remove();
						});	
					});
					setCookie('style', data, 365, pathArray[3]);
					window.parent.Shadowbox.close();
				});
			});
	}
	var cssDummy = {
		init: function(){
			$('<div id="dummy-element" style="display:none" />').appendTo('body');
		},
		check: function(callback) {
			if ($('#dummy-element').width()==2) callback();
			else setTimeout(function(){cssDummy.check(callback)}, 200);
		}
	}
	cssDummy.init();
}