function getRandomHex() { 
	var R = Math.round(Math.random() * 255);
	var G = Math.round(Math.random() * 255);
	var B = Math.round(Math.random() * 255);
	
	var M=Math.max(R,G,B), delta=M-Math.min(R,G,B), H, S, V;
	
    if (M!=0) { 
		S=Math.round(delta/M*100);
        if(R==M) 
			H=(G-B)/delta; 
		else if(G==M) 
			H=2+(B-R)/delta; 
		else if(B==M) 
			H=4+(R-G)/delta;
			
        var H=Math.min(Math.round(H*60),360);
		if(H<0) H+=360;
    }
	
	if (S < 50) S = 50;
	if (S > 50) S = 50;
	
	S = 75;
	M = 200;

    return hsv_hex({'H':H?H:0, 'S':S?S:0, 'V':Math.round((M/255)*100)});
}

function hsv_hex(o) {
    var R, G, A, B, C, S=o.S/100, V=o.V/100, H=o.H/360;
    if (S>0) { 
		if (H>=1) H=0;
        H=6*H; F=H-Math.floor(H);
        A=Math.round(255*V*(1-S));
        B=Math.round(255*V*(1-(S*F)));
        C=Math.round(255*V*(1-(S*(1-F))));
        V=Math.round(255*V);
        switch(Math.floor(H)) {
            case 0: R=V; G=C; B=A; break;
            case 1: R=B; G=V; B=A; break;
            case 2: R=A; G=V; B=C; break;
            case 3: R=A; G=B; B=V; break;
            case 4: R=C; G=A; B=V; break;
            case 5: R=V; G=A; B=B; break;
        }
		return "#"+intToHex(R?R:0)+intToHex(G?G:0)+intToHex(B?B:0);	
    } else {
		return "#"+intToHex(V=Math.round(V*255))+intToHex(V)+intToHex(V);
	}
}

function intToHex(n) {
	n = n.toString(16);
	// eg: #0099ff. without this check, it would output #099ff
	if( n.length < 2) 
		n = "0"+n; 
	return n;
}

jQuery(document).ready(function() {
	var $rcolor = getRandomHex();
	
	jQuery('#logo a span, .page-nav li, .comment-count em, #respond em, .comment-by-author').animate({ color: $rcolor }, 4000);
	jQuery('body').animate({ borderTopColor: $rcolor }, 4000);
	jQuery('#logo em, .post-walk').animate({ backgroundColor: $rcolor }, 4000);
	
	/*jQuery('#logo a').hover(function() {
		jQuery(this).css({ backgroundColor: '#000' });
	}, function() {
		jQuery(this).css({ backgroundColor: $rcolor });
	}).animate({ backgroundColor: $rcolor });
	*/
});
