$(document).ready(function() {
	
	var c = readCookie('limon_style');
	if (c) switchStylesheet(c);
	
	//var d = readCookie('limon_bg');
	//if (d) switchBg(d);
	
	 $('.selected_color').ColorPicker({ 
			color: '#eeeeee',
			onShow: function() {
				$(".color_picker").die();
			},
			onChange: function (hsb, hex, rgb) {
				
				var bg_image = $("body").css("background-image");
				
				var patt=/\"|\'|\)|\(|url/g;
				var bg_name = bg_image.replace(patt,'');
				
				$("body").css("background", "#"+hex+ " url('"+bg_name+"')");
				
				$('.selected_color div').css('backgroundColor', '#' + hex);
			},
			onHide: function() {
				$(".color_picker").live("mouseleave", function() {
					$(this).stop(true, true).fadeOut(200, function() { 
							$(".show_switcher").stop(true, true).fadeIn(200); 
					});
				});
			}
		});
	
	$("#theme_options ul.backgrounds li").click(function() {
			switchBg($(this).attr("class"));
	});
	
	$("#theme_options ul.colors li").click(function() {
			switchStylesheet($(this).attr("class"));
	});
	
	$(".show_switcher").hover(function() {
		$(this).stop(true, true).hide();
		$(".color_picker").fadeIn(200);
	});
	$(".color_picker").live("mouseleave", function() {
		$(this).stop(true, true).fadeOut(200, function() { 
				$(".show_switcher").stop(true, true).fadeIn(200); 
		});
	});
});
function switchStylesheet(styleName) {
			if(styleName == "") {
				
			} else {
				$("link[media='screen']").attr("href", 'stylesheets/colors/'+styleName+'.css');
			}
			createCookie('limon_style', styleName, 365);
}
function switchBg(bg) {
			if(bg == "") {

			} else {
				$("body").css("background", "#eee url('images/backgrounds/" + bg + ".png')");
				if(bg == "linen" || bg == "dots") {
					$(".bg_color").hide();
				} else {
					$(".bg_color").show();
				}
			}
			//createCookie('limon_bg', bg, 365);
}		
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

