$(document).ready( function()
{
	selectColour($('#colourpicker-form input[name="red"]').attr('value'),
			$('#colourpicker-form input[name="green"]').attr('value'),
			$('#colourpicker-form input[name="blue"]').attr('value')
			);
	
	$('#palette li .box').bind('click', function()
	{
		strRGB=$(this).css('background-color');
		
		selectColour(strRGB);
		
		arrRGB=strRGB.match(/\d+/g);
		$('#colourpicker-advanced-hole').ColorPickerSetColor({r:arrRGB[0], g:arrRGB[1], b:arrRGB[2]});
		
		
	});
	
	$('#palette li .box').each( function() {
		strRGB=$(this).css('background-color');
		strHex = strRGB;
		if (strRGB.match(/^rgb/))
		  strHex = RGBtoHex(strRGB);
		
		// remove #
		if (strHex.match(/#/))
		  strHex.replace("#", "");
		
		href = $(this).attr("href");
		append = '&';
		if (-1 == href.indexOf('?'))
			append = '?';
		
		href += append + "isbox=" + strHex;
		
		$(this).attr("href", href);
		$(this).parent().find("a.name").attr("href", href);
	});

	$('#homepalette li .box').each( function() {
		strRGB=$(this).css('background-color');
    strHex = strRGB;
    if (strRGB.match(/^rgb/))
      strHex = RGBtoHex(strRGB);
    
    // remove #
    if (strHex.match(/^#?/))
    {
      strHex = strHex.replace('#', '');
    }
    
		href = $(this).attr("href");
		append = '&';
		if (-1 == href.indexOf('?'))
			append = '?';
		
		href += append + "isbox=" + strHex;
		
		$(this).attr("href", href);
		$(this).parent().find("a.name").attr("href", href);
	});	
	
		 $('#fakebrowsecatbutton').click(function() {
		
			 $('#slidedownreveal').slideDown();
		
		  });
		 
		 $('#slidebackup').click(function() {
				
			 $('#slidedownreveal').slideUp();
		
		  });

			$('#colourpicker-advanced-hole').ColorPicker({
				flat: true,
				onChange: function (hsb, hex, rgb)
				{
					selectColour(rgb.r, rgb.g, rgb.b);
				}
			});
			
			$('#advancedlauncher').click(function() {
			  $('#overlay').fadeIn('fast');
				$('#advanced-picker-dialog').fadeIn('fast');
				return false;
			});
			
			$('#advanced-picker-dialog .usecolour').click(function() {
				$('#advanced-picker-dialog .colorpicker_submit').click();
				$('#advanced-picker-dialog').fadeOut('fast');
				$('#overlay').fadeOut('fast');
				$('#colourpicker-form').submit();
				return false;
			})
			
			$('#advanced-picker-dialog #close a').click( function() {
			  $('#overlay').fadeOut('fast');
			  $('#advanced-picker-dialog').fadeOut('fast');
			  return false;
			});

});


function selectColour(r, g, b)
{
	
	strColour='rgb('+r+','+g+','+b+')';
	
	
	$('#selectedcolour-colour').css('background-color', strColour);
	$('#drhues-amazing-concoction').css('background-color', strColour);
	$('#drhues-amazing-masthead-underline').css('background-color', strColour);
	
	$('#colourpicker-form input[name="red"]').attr('value', r);
	$('#colourpicker-form input[name="green"]').attr('value', g);
	$('#colourpicker-form input[name="blue"]').attr('value', b);
}

function RGBtoHex(rgbString)
{
	var parts = rgbString
    .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);

	delete (parts[0]);
	for (var i = 1; i <= 3; ++i)
	{
		parts[i] = parseInt(parts[i]).toString(16);
		if (parts[i].length == 1)
			parts[i] = '0' + parts[i];
	}
	var hexString = parts.join(''); 
	
	return hexString;
	
}