// Rotates.org ColorShift 0.71 - by Lewis 'SEPTiMUS' Lane - www.rotates.org
// Excuse the less shitty (but still a bit shitty) JS :(
//
// Requires:
// jQuery: http://www.jquery.com
// jQuery UI (sliders): http://jqueryui.com
// jQuery Cookies: http://code.google.com/p/cookies

var rotColShiftOpts = {
	hsl: {h: 59, s: 55, l: 46}, // default settings for hue (0-360), saturation (0-100) and brightness/luminescence (0-100)
	sliderAnimSpeed: 120, // speed of handle animation when clicking slider
	useCookies: false, // store cookies to remember user's colour choice
	cookieHours: 8760, // keep cookie alive for this many hours (default one year)
	cssTransforms: [ // add your transforms here as an array of objects matching the format { selector: "#foo", offsets: {h: hue, s: saturation, l: lightness}, attributes: ["foo-color", "bar-background"] }
		{
			selector: "body, #wrapper, #wrapper #content, a.btn",
			attributes: [
				"background-color"
			]
		},
		{
			selector: "#wrapper p a",
			offsets: {
				l: 15
			},
			attributes: [
				"color"
			]
		},
		{
			selector: "#wrapper h1, #flashMessage",
			offsets: {
				l: 25
			},
			attributes: [
				"color"
			]
		}
	]
};

var ieStyle;

function setCookies(h,s,l) {
	if (rotColShiftOpts.useCookies) {
		var cookieOptions = { hoursToLive: rotColShiftOpts.cookieHours };
		$.cookies.set('rotates_hue', h, cookieOptions );
		$.cookies.set('rotates_saturation', s, cookieOptions );
		$.cookies.set('rotates_brightness', l, cookieOptions );
	}
}

// the following functions weren't written by me, as maths is very much _not_ my strong point

function huetorgb(m1, m2, hue) {
	var v;
	if (hue < 0) {
		hue += 1;
	}
	else if (hue > 1) {
		hue -= 1;
	}
	if (6 * hue < 1) {
		v = m1 + (m2 - m1) * hue * 6;
	}
	else if (2 * hue < 1) {
		v = m2;
	}
	else if (3 * hue < 2) {
		v = m1 + (m2 - m1) * (2/3 - hue) * 6;
	}
	else {
		v = m1;
	}
	return Math.round(255 * v);
}

function hsl2rgb(h, s, l) {
	var m1, m2, hue;
	var r, g, b
		s /= 100;
		l /= 100;
	if (s == 0)
		r = g = b = (l * 255);
	else {
		if (l <= 0.5)
			m2 = l * (s + 1);
		else
			m2 = l + s - l * s;
			m1 = l * 2 - m2;
			hue = h / 360;
			r = huetorgb(m1, m2, hue + 1/3);
			g = huetorgb(m1, m2, hue);
			b = huetorgb(m1, m2, hue - 1/3);
	}
	return "rgb(" + Math.round(r) + "," + Math.round(g) + "," + Math.round(b) + ")";
}

function getColorWithOffsets(h, s, l, o) {
	var ho = so = lo = 0;
	if (o) {
		if (o.h) {
			ho = o.h;
		}
		if (o.s) {
			so = o.s;
		}
		if (o.l) {
			lo = o.l;
		}
	}
	ho = Math.max(-360, Math.min(360, (h+ho)));
	so = Math.max(0, Math.min(100, (s+so)));
	lo = Math.max(0, Math.min(100, (l+lo)));

	return hsl2rgb(ho, so, lo);
}

function updateColors(value, saturation, brightness) {
	var hue = Math.round((value/100)*360); // work out the hue from the slider value

	$("#saturation").css({"background-color" : hsl2rgb(hue, 100, 50)}); // change the saturation slider's background color

	var tmpTransforms = "";

	$.each(rotColShiftOpts.cssTransforms, function(i, ttrans) {
		newColor = getColorWithOffsets(hue, saturation, brightness, ttrans.offsets);
		var tstyles = "";
		$.each(ttrans.attributes, function(i, ts) {
			tstyles += ts + ": " + newColor + ";";
		});
		tmpTransforms += ttrans.selector + "{ " + tstyles + " } ";
	});

	if ($.browser.msie) { // *sigh*
		ieStyle.cssText = tmpTransforms;
	}
	else {
		$("style[title='colshift']").text(tmpTransforms);
	}
}

function updateColorsHex(hex)
{
	var tmpTransforms = "";

	$.each(rotColShiftOpts.cssTransforms, function(i, ttrans) {
		var tstyles = "";
		$.each(ttrans.attributes, function(i, ts) {
			tstyles += ts + ": " + hex + ";";
		});
		tmpTransforms += ttrans.selector + "{ " + tstyles + " } ";
	});

	if ($.browser.msie) { // *sigh*
		ieStyle.cssText = tmpTransforms;
	}
	else {
		$("style[title='colshift']").text(tmpTransforms);
	}
}



$(document).ready(function()
{
	if ($.browser.msie) {
		ieStyle = document.createStyleSheet();
	} else {
		$('head').append('<style type="text/css" title="colshift"></style>');
	}

	// update colours
	updateColors(rotColShiftOpts.hsl.h, rotColShiftOpts.hsl.s, rotColShiftOpts.hsl.l);
});

