/*
* app.js - Color Scheme Designer application library

*  Copyright (c) 2009, Petr Stanicek, pixy@pixy.cz ("the author")
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*   * Any commercial use of this software is not allowed unless an exemption
*     was granted by the author.
*
*  This software is provided by the author "as is" and any express or implied
*  warranties, including, but not limited to, the implied warranties or
*  merchantability and fitness for a particular purpose are disclaimed.
*  In no event shall the author be liable for any direct, indirect, incidental,
*  special, exemplary, or consequential damages (including, but not limited to,
*  procurement of substitute goods or services; loss of use, data, or profits;
*  or business interruption) however caused and on any theory of liability,
*  whether in contract, strict liability, or tort (including negligence or
*  otherwise) arising in any way out of the use of this software, even if
*  advised of the possibility of such damage.

*/


// Supporting objects

var drag = {
	on : false,
	dot : null
	}

var defs = {
	FPSlimiter : 10, // msec
	dotSize : 7,  // half the size
	wheelMid : { X:190,Y:190 },
	sliderMid : { X:95,Y:95 },
	sliderWidth : 140,
	CBPreview : 0
	}

var API = {
	on : false
	}

// INIT

$(function() {

	// browser check

	var v = parseInt($.browser.version,10);
	if ($.browser.msie && v<7) {
		alert('This browser is no more supported by this version of Color Scheme Designer. Perhaps previous version would work with your browser.');
		document.location.href = '/previous';
		return;
		}
	
	$('#jscheck').hide();
	$('#canvas').fadeIn(2000);

	// event handlers

	$('a').click( function(){ $(this).blur()} );
	$('#model a').click(function(){ setScheme(this.id);return false });

	$('#tab-wheel').click( function() { togglePane('wheel');return false } );
	$('#tab-vars').click( function() { togglePane('vars');return false } );
	$('#tab-info').click( function() { togglePane('info');return false } );

	$('#wheel,#saturation,#contrast')
		.bind('mousedown',function(e) {
			e.preventDefault();
			var elm = $(this);
			var pos = elm.offset();
			drag.dW = elm.width();
			drag.dH = elm.height();
			drag.dX = pos.left + drag.dW/2;
			drag.dY = pos.top + drag.dH/2;
			elm.bind('mousemove',function(e) {
				e.preventDefault();
				dragMove(e,this);
				});
			if (!drag.dot) dragMove(e,this);
			})
		.bind('mouseup',function(e) {
			e.preventDefault();
			$(this).unbind('mousemove');
			drag.dot = null;
			});

	$('.dot')
		.bind('mousedown',function(e) {
			drag.dot = this;
			})
		.bind('mouseup',function(e) {
			drag.dot = null;
			});
	$('#dot1').bind('dblclick',enterHue);
	$('#hue-val').bind('click',enterHue);
	$('#dot2').bind('dblclick',enterComplHue);
	$('#dot3, #dot4').bind('dblclick',enterDist);
	$('#dist-val').bind('click',enterDist);
	$('#rgb-val').bind('click',enterRGB);

	$('#sample-val')
		.bind('dblclick',function(e) {
			var h = prompt('Enter RGB');
			})

	var i,l, p, s = '<option value="">- custom -</option>';
	for (i=0,l=Palette.Presets.length;i<l;i++) {
		p = Palette.Presets[i];
		s += '<option value="'+p.id+'"';
		if (i==0) s += ' selected';
		s += '>'+p.name+'</option>';
		}
	$('#presets select').append(s).bind('change', function() { usePreset(this.value);return false } ).styledSelect();

	$('#chk-showtext').click( function(){ useShowText();return true } );
	$('#chk-showtooltips').click( function(){ useShowTooltips();return true } );

	$('#export-txt').click( function(){ exportCols('txt');return false } );
	$('#export-html').click( function(){ exportCols('html');return false } );
	$('#export-xml').click( function(){ exportCols('xml');return false } );
	$('#export-aco').click( function(){ exportCols('aco');return false } );
	$('#export-gpl').click( function(){ exportCols('gpl');return false } );

	$('#tab-preview2').click( function(){ pagePreview(1);return false } );
	$('#tab-preview3').click( function(){ pagePreview(2);return false } );

	$('#randomize-now').click( function(){ randomScheme();return false } );
	$('#randomize-set').click( function(){ randomSettings();return false } );

	s = '';
	for (i=0,l=ColorBlind.typeDesc.length;i<l;i++) {
		p = ColorBlind.typeDesc[i];
		s += '<option value="'+i+'"';
		if (i==0) s += ' selected';
		s += '>'+p+'</option>';
		}
	$('#colorblind select').append(s).bind('change', function() { useColorBlind(this.value);return false } ).styledSelect();

	// initialize

	var search = document.location.search.substring(1);
	if (search) {
		search = search.split('&');
		var i,l,s,key,val;
		API.data = {};
		for (i=0,l=search.length;i<l;i++) {
			s = search[i].split('=');
			key = s[0].toString();
			val = s[1].toString();
			API.data[key] = val;
			}
		if (API.data.returnurl) {
			API.on = true;
			if (API.data.format!='hex' && API.data.format!='rgb') API.data.format = 'hex';
			if (API.data.method!='get' && API.data.method!='post') API.data.format = 'get';
			}
		}

	if (API.on) {
		var str = decodeURIComponent(API.data.label);
		if (!str) str = 'RETURN to original site';
		$('#tab-api a').click( function(){ API_finish();return false } ).addClass('sel').html(str + ' &raquo;');
		}
	else {
		$('#tab-api a').click( function(){ loadPage('api');return false } );
		}

	var hash = document.location.hash.substring(1);
	if (hash) {
		Palette.unserialize(hash);
		$('#presets select').get(0).selectedIndex = 0;
		$('#presets select').trigger('change');
		}
	else usePreset('default');

	updateScheme();
	updateWheel();
//	updateVars();
//	updateSchemeInfo();

	});


// API

function API_finish() {

	function col(c,name) {
		var v, s = name+',';
		for (var i=0;i<5;i++) {
			v = c.getVarRGB(i);
			if (i>0) s += ',';
			if (API.data.format=='rgb') s += v.R+'-'+v.G+'-'+v.B;
			else s += v.getHex();
			}
		return s;
		}
	function item(key,val) {
		return '<input type="hidden" name="'+key+'" value="'+val+'">';
		}

	var str = '<form id="api-sender">';
	str += item( 'id', $('#schemeid a').text() );
	str += item( 'col1', col(Palette.Primary, 'primary') );
	var i = 2;
	if (Palette.Sec1) str += item( 'col'+i++, col(Palette.Sec1, 'secondary-a') );
	if (Palette.Sec2) str += item( 'col'+i++, col(Palette.Sec2, 'secondary-b') );
	if (Palette.Compl) str += item( 'col'+i++, col(Palette.Compl, 'complement') );
	str += '</form>';
	$('#canvas').append(str);
	$('#api-sender').attr('action',decodeURIComponent(API.data.returnurl)).attr('method',API.data.method);
	$('#api-sender').submit();
	}

// Actions


function togglePane(id) {
	$('.pane').hide();
	$('#pane-'+id).fadeIn(750);
	$('#tabs-color a').removeClass('sel');
	$('#tab-'+id).addClass('sel').blur();
	if (id=='wheel') updateWheel();
	else if (id=='vars') {
		updateVars();
		$('#presets select').trigger('resize');
		}
	else if (id=='info') updateSchemeInfo();
	}

function setScheme(m) {
	Palette.setScheme(m);
	updateScheme();
	}

function usePreset(id) {
	if (!id) return;
	var p = Palette.usePreset(id);
	updateVars();
	}

function useColorBlind(n) {	
	defs.CBPreview = n;
	if (n>0) $('#cb-warning').show();
	else $('#cb-warning').hide();
	colorize();
	}

function useShowText() {
	var on = $('#chk-showtext:checked').length>0;
	var s;
	if (on) s = '<div class="text"><span class="row1">Lorem ipsum</span><br><span class="row2">Lorem ipsum</span><br><span class="row3">Lorem ipsum</span></div>';
	else s = '';
	$('#preview-palette td').html(s);
	}

function useShowTooltips() {
	var on = $('#chk-showtooltips:checked').length>0;
	if (on) showTooltips();
	else hideTooltips();
	}

// Redraw

var t0;
function checkFPS_on() {
	t0 = new Date();
	}
function checkFPS_off() {
	var t1 = new Date()-t0;
	if (t1>defs.FPSlimiter) defs.FPSlimiter = t1+50;
	else if (t1<defs.FPSlimiter-50 && defs.FPSlimiter>10) defs.FPSlimiter = t1;
	$('#fps').text( 'fps: ' + Math.round(1/defs.FPSlimiter*1000) );
	}

function updateScheme() {
	var m = Palette.Scheme;
	$('#model a').removeClass('sel');
	$('#'+m).addClass('sel').blur();
	$('#dot2').css('display',Boolean(Palette.Compl) ? 'block':'none');
	$('#dot3,#dot4').css('display',Boolean(Palette.Sec1) ? 'block':'none');
	$('#dist-val').css('display',Boolean(Palette.Sec1) ? 'block':'none');
	updateWheel();
	if ($('#tab-info').hasClass('sel')) updateSchemeInfo();
	}


function updateWheel() {
	checkFPS_on();
	moveDot(1,Palette.Primary);
	moveDot(2,Palette.Compl);
	moveDot(3,Palette.Sec1);
	moveDot(4,Palette.Sec2);
	var rgb = Palette.Primary.Col[0].RGB;
	$('#hue-val span').text(Palette.H+'°');
	$('#rgb-val span').text(rgb.getHex());
	$('#rgb-r').html(Math.round(rgb.R/255*100) + ' %');
	$('#rgb-g').html(Math.round(rgb.G/255*100) + ' %');
	$('#rgb-b').html(Math.round(rgb.B/255*100) + ' %');
	$('#dist-val span').text(Palette.Dist+'°');
	colorize();
	checkFPS_off();
	}

function updateVars() {
	updateSV();
	updateContrast();
	}
function updateSV() {
	checkFPS_on();
	var x = Palette.dS, y = Palette.dV;
	x = x<0 ? (x+1)*0.9-0.5 : 0.4+x*0.1;
	y = y<0 ? (y+1)*0.9-0.5 : 0.4+y*0.1;
	moveSliderDot('saturation',x,-y);
	colorize();
	checkFPS_off();
	}
function updateContrast() {
	checkFPS_on();
	var x = Palette.cL, y = Palette.cS;
	moveSliderDot('contrast',x-0.5,0.5-y);
	colorize();
	checkFPS_off();
	}

function updateSchemeInfo() {
	function col(c,id,ttl) {
		var hex,s = '<h4>'+ttl+':</h4>';
		s += '<table class="info-table"><tr>';
		for (var i=0;i<5;i++) {
			hex = c.getVarRGB(i).getHex();
			s += '<td class="cbox bg-'+id+'-'+i+'"></td><td class="code">'+hex+'</td>';
			}
		s += '</tr></table>';
		return s;
		}
	var s = '';
	s += col(Palette.Primary,'pri','Primary Color');
	if (Palette.Sec1) s += col(Palette.Sec1,'sec1','Secondary Color A');
	if (Palette.Sec2) s += col(Palette.Sec2,'sec2','Secondary Color B');
	if (Palette.Compl) s += col(Palette.Compl,'compl','Complementary Color');

	$('#coltable').html(s);

	colorize();
	}


function exportCols(type) {

	function col(c,id,ttl) {
		var s = '"'+id+'":{"ttl":"'+ttl+'","col":[';
		for (var i=0;i<5;i++) {
			v = c.getVarRGB(i);
			code = v.getHex();
			if (i>0) s += ',';
			s += '{"hex":"'+code+'","r":'+v.R+',"g":'+v.G+',"b":'+v.B+'}';
			}
		s += ']}';
		return s;
		}

	var s = '{\
"type":"'+type+'",\
"id":"'+$('#schemeid a').text()+'",\
"scheme":{';
	s += col(Palette.Primary, 'primary','Primary Color');
	if (Palette.Sec1) s += ',' + col(Palette.Sec1, 'secondary-a','Secondary Color A');
	if (Palette.Sec2) s += ',' + col(Palette.Sec2, 'secondary-b','Secondary Color B');
	if (Palette.Compl) s += ',' + col(Palette.Compl, 'complement','Complementary Color');
	s += '}}';
	
	$('#form').attr('action','http://colorschemedesigner.com/export/').attr('method','POST').attr('target','_blank');
	$('#form-data').val(s);
	$('#form').submit();
	
	}


function pagePreview(nr) {
	var url, ttl, s;
	if (nr==1) {
		url = 'sample/index2.html';
		ttl = 'Light page | <a href="#" onclick="pagePreview(2);return false">Dark page</a>';
		}
	else {
		url = 'sample/index.html';
		ttl = '<a href="#" onclick="pagePreview(1);return false">Light page</a> | Dark page';
		}
	s = '<div class="sample-info">'+ttl;
	if (Palette.Sec1) s += ' • Click inside the page to swap secondary colors';
	s += '</div><iframe class="sample" scrolling="no" frameborder="0"></iframe>';
	var fl = new $.floatbox({
		content:s,
		button: '<button type="button" class="close-floatbox">Close</button>',
		fade:false,
		boxConfig : {
			position : ($.browser.msie) ? "absolute" : "fixed",
			zIndex: 999,
			width: "840px",
			marginLeft: "-420px",
			height: "auto",
			top: "50%",
			left: "50%",
			backgroundColor: "transparent",
			display: "none"
			}
		});
	$('iframe.sample').attr('src',url);
	}

function loadPage(url) {
	var s = '<iframe class="sample" src="'+url+'" scrolling="auto" frameborder="0"></iframe>';
	var fl = new $.floatbox({
		content:s,
		button: '<button type="button" class="close-floatbox">Close</button>',
		fade:false,
		boxConfig : {
			position : ($.browser.msie) ? "absolute" : "fixed",
			zIndex: 999,
			width: "840px",
			marginLeft: "-420px",
			height: "auto",
			top: "50%",
			left: "50%",
			backgroundColor: "transparent",
			display: "none"
			}
		});
	$('iframe.sample').attr('src',url);
	}


var Random = {
	Scheme : 1,
	H : 1,
	Dist : 1,
	dS : 1,
	dV : 1,
	cS : 1,
	cL : 1
	}

function randomScheme() {
	var Scheme = Random.Scheme ? 'm' + Math.ceil(Math.random()*6) : Palette.Scheme;
	var H = Random.H ? Math.floor(Math.random()*360) : Palette.H;
	var Dist = Random.Dist ? 5 + Math.round(Math.random()*55) : Palette.Dist;
	var dS = Random.dS ? -1 + Math.random()*2 : Palette.dS;
	var dV = Random.dV ? -1 + Math.random()*2 : Palette.dV;
	var cS = Random.cS ? Math.random() : Palette.cS;
	var cL = Random.cL ? Math.random() : Palette.cL;
	Palette.setAll(Scheme,H,Dist,dS,dV,cS,cL);
	updateScheme();
	updateWheel();
	updateVars();
	updateSchemeInfo();
	}

function randomSettings() {
	function chkbox(id,label,on) {
		return '<p class="input chkbox"><input id="'+id+'" type="checkbox"'+ (on ? ' checked':'') +'> <label for="'+id+'">'+label+'</label></p>';
		}
	var s = '<div id="prompt"><h4>Randomize Parameters:</h4>';
	s += chkbox('rnd-scheme','Color scheme model',Random.Scheme);
	s += chkbox('rnd-h','Hue',Random.H);
	s += chkbox('rnd-d','Angle/Distance',Random.Dist);
	s += chkbox('rnd-ds','Saturation',Random.dS);
	s += chkbox('rnd-dv','Brightness',Random.dV);
	s += chkbox('rnd-cs','Contrast (Shadows)',Random.cS);
	s += chkbox('rnd-cl','Contrast (Lights)',Random.cL);
	s += '<p class="submit"><button id="prompt-cancel" class="close-floatbox">Cancel</button> <button id="prompt-ok" class="close-floatbox">OK</button></p>';
	s += '</div>';
	var fl = new $.floatbox({
		content:s,
		button: '',
		fade:false,
		boxConfig : {
			position : ($.browser.msie) ? "absolute" : "fixed",
			zIndex: 999,
			width: "360px",
			marginLeft: "-180px",
			height: "auto",
			top: "33%",
			left: "50%",
			backgroundColor: "transparent",
			display: "none"
			}
		});
	$('#prompt-ok').click( function(){
		Random.Scheme = $('#rnd-scheme:checked').length;
		Random.H = $('#rnd-h:checked').length;
		Random.Dist = $('#rnd-d:checked').length;
		Random.dS = $('#rnd-ds:checked').length;
		Random.dV = $('#rnd-dv:checked').length;
		Random.cS = $('#rnd-cs:checked').length;
		Random.cL = $('#rnd-cl:checked').length;
		} )
	$('#prompt-input').keypress( function(e){ if(e.keyCode==13) $('#prompt-ok').click() } ).focus().select();
	}



// Colorize

function colorize() {
	var pri,sec1,sec2,compl;
	for (var i=0;i<5;i++) {
		pri = Palette.Primary.Col[i].RGB;
		if (Palette.Compl) compl = Palette.Compl.Col[i].RGB;
		else compl = Palette.Primary.Col[2].RGB;
		if (Palette.Sec1) sec1 = Palette.Sec1.Col[i].RGB;
		else sec1 = Palette.Primary.Col[3].RGB;
		if (Palette.Sec2) sec2 = Palette.Sec2.Col[i].RGB;
		else sec2 = Palette.Primary.Col[4].RGB;
		
		if (defs.CBPreview) {
			pri = '#'+ ColorBlind.getHex(pri.R,pri.G,pri.B,defs.CBPreview);
			compl = '#'+ ColorBlind.getHex(compl.R,compl.G,compl.B,defs.CBPreview);
			sec1 = '#'+ ColorBlind.getHex(sec1.R,sec1.G,sec1.B,defs.CBPreview);
			sec2 = '#'+ ColorBlind.getHex(sec2.R,sec2.G,sec2.B,defs.CBPreview);
			}
		else {
			pri = pri.getCSS();
			compl = compl.getCSS();
			sec1 = sec1.getCSS();
			sec2 = sec2.getCSS();
			}

		$('.col-pri-'+i).css('color',pri);
		$('.col-compl-'+i).css('color',compl);
		$('.col-sec1-'+i).css('color',sec1);
		$('.col-sec2-'+i).css('color',sec2);
		$('.bg-pri-'+i).css('background-color',pri);
		$('.bg-compl-'+i).css('background-color',compl);
		$('.bg-sec1-'+i).css('background-color',sec1);
		$('.bg-sec2-'+i).css('background-color',sec2);
		$('.brd-pri-'+i).css('border-color',pri);
		$('.brd-compl-'+i).css('border-color',compl);
		$('.brd-sec1-'+i).css('border-color',sec1);
		$('.brd-sec2-'+i).css('border-color',sec2);
		
		}
	var sID = Palette.serialize();
	$('#schemeid a').text(sID).attr('href',document.location.pathname+'#'+sID);

	}


// drag & drop

var moveTimer = 0;
function dragMove(e,elm) {
	var t0 = new Date();
	if (t0-moveTimer<defs.FPSlimiter) return false;
	moveTimer = t0;
	if (elm.id=='wheel') moveOnWheel(e);
	else moveOnSlider(e,elm);
	}

function moveOnWheel(e) {
	var x,y,h,r,movedHue = false, movedDist = false;
	if (e) {
		x = e.pageX - drag.dX;
		y = e.pageY - drag.dY;
		h = Math.round(((Math.atan2(-x,y) * 180/Math.PI) + 180) % 360);
		r = Math.sqrt(x*x + y*y);
		}
	else {
		movedHue = true, movedDist = true;
		}
	if (r>60 && r<160) {
		var dot = 'dot1';
		if (drag.dot) dot = drag.dot.id;
		if (dot=='dot1' || dot=='dot2') {
			if (r>125) {
				if (r<135) h = (Math.floor((h-7.5)/15 + 1) * 15) % 360;
				else h = (Math.floor((h-15)/30 + 1) * 30) % 360;
				}
			if (dot=='dot2') h = (h+180)%360;
			movedHue = Palette.setHue(h);
			}
		else {
			if (Palette.Scheme=='m4' && dot=='dot4') h = (h+180)%360;
			movedDist = Palette.setDist(h);
			}
		}
	if (movedHue || movedDist) updateWheel();
	}


function moveOnSlider(e,elm) {
	var x = e.pageX - drag.dX;
	var y = e.pageY - drag.dY;
	x = x / defs.sliderWidth;
	y = y / defs.sliderWidth;
	if (x<-0.5) x = -0.5; if (x>0.5) x = 0.5;
	if (y<-0.5) y = -0.5; if (y>0.5) y = 0.5;
	if (elm.id=='saturation') {
	// -0.5..0.4 => -1..0; 0.4..0.5 => 0..1
		var s = x>0.4 ? (x-0.4)/0.1 : (x+0.5)/0.9-1;
		y = -y;
		var v = y>0.4 ? (y-0.4)/0.1 : (y+0.5)/0.9-1;
		Palette.setSV(s,v);
		updateSV();
		}
	else if (elm.id=='contrast') {
	// -0.5..0.5 => 0..1
		var cL = x + 0.5;
		var cS = 0.5 - y;
		Palette.setContrast(cS,cL);
		updateContrast();
		}
	$('#presets select').get(0).selectedIndex = 0;
	$('#presets select').trigger('change');
	}

function moveDot(n,col) {
	if (!col) return;
	var h = col.Col[0].HSV.H;
	var r = (h-90)/360 * 2*Math.PI;
	x = Math.round( defs.wheelMid.X + 109*Math.cos(r) ) - defs.dotSize;
	y = Math.round( defs.wheelMid.Y + 109*Math.sin(r) ) - defs.dotSize;
	$('#dot'+n).css('left',x+'px').css('top',y+'px');
	}

function moveSliderDot(elmID,x,y) {
	x = defs.sliderMid.X + x * defs.sliderWidth - defs.dotSize + 1;
	y = defs.sliderMid.Y + y * defs.sliderWidth - defs.dotSize + 3;
	$('#'+elmID + ' .dot').css('left',x+'px').css('top',y+'px');
	}


// prompts

function myPrompt(str,val,callback) {
	var s = '<div id="prompt"><p>'+str+'</p>';
	s += '<p class="input"><input id="prompt-input" name="prompt-input" type="text" value="'+val+'"></p>';
	s += '<p class="submit"><button id="prompt-cancel" class="close-floatbox">Cancel</button> <button id="prompt-ok" class="close-floatbox">OK</button></p>';
	s += '</div>';
	var fl = new $.floatbox({
		content:s,
		button: '',
		fade:false,
		boxConfig : {
			position : ($.browser.msie) ? "absolute" : "fixed",
			zIndex: 999,
			width: "360px",
			marginLeft: "-180px",
			height: "auto",
			top: "33%",
			left: "50%",
			backgroundColor: "transparent",
			display: "none"
			}
		});
	$('#prompt-ok').click( function(){ callback($('#prompt-input').val()); } )
	$('#prompt-input').keypress( function(e){ if(e.keyCode==13) $('#prompt-ok').click() } ).focus().select();
	}

function getExpr(s) {
	if (!s) return '';
	if (s.match(/[0-9\.\+\-\*/ ]+/)) s = eval(s);
	return s;
	}

function enterHue() {
	myPrompt('Enter hue (0°-360°)',Palette.H,enterHue2);
	}
function enterHue2(h) {
	h = parseInt(getExpr(h),10)%360;
	if (h>=0 && h<=360) {
		Palette.setHue(h%360);
		updateWheel();
		}
	}
function enterComplHue() {
	myPrompt('Enter complement hue (0°-360°)', (Palette.H+180)%360, enterComplHue2 );
	}
function enterComplHue2(h) {
	h = parseInt(getExpr(h),10)%360;
	if (h>=0 && h<=360) {
		Palette.setHue((h+180)%360);
		updateWheel();
		}
	}
function enterDist() {
	var min = Palette.SchemeModel.minD, max = Palette.SchemeModel.maxD;
	myPrompt('Enter Distance/Angle ('+min+'°-'+max+'°)', Palette.Dist, enterDist2);
	}
function enterDist2(a) {
	var min = Palette.SchemeModel.minD, max = Palette.SchemeModel.maxD;
	a = Math.abs(parseInt(getExpr(a),10));
	if (a>=min && a<=max) {
		Palette.setDistNum(a);
		updateWheel();
		}
	}
function enterRGB() {
	myPrompt('Enter RGB value (000000-FFFFFF)', Palette.Primary.getVarRGB(0).getHex(), enterRGB2);
	}
function enterRGB2(s) {
	if (s && s.match(/^\s*[0-9a-fA-F]{6}\s*$/)) {
		var r = hex2dec(s.substring(0,2))
		var g = hex2dec(s.substring(2,4));
		var b = hex2dec(s.substring(4,6));
		var rgb = new RGB(r,g,b);
		var hsv = ColorWheel.getColorByRGB(rgb);
		Palette.setHSV(hsv);
		updateWheel();
		$('#presets select').get(0).selectedIndex = 0;
		$('#presets select').trigger('change');
		}
	}


function showTooltips() {
	$('.help').tooltip({
		bodyHandler : function() {
			return $('#help-'+$(this).attr('id')).html();
			},
		showURL : false,
		delay: 0,
		fade : 250,
		track : true,
		left: -50
		});
	if (pageTracker) pageTracker._trackPageview("/pseudo/tooltips.html");
	}
function hideTooltips() {
	$('.help').tooltipOff();
	}

