	/***********************************************************************************************
	
	Copyright (c) 2005 - Alf Magne Kalleland post@dhtmlgoodies.com
	
	Get this and other scripts at www.dhtmlgoodies.com
	
	You can use this script freely as long as this copyright message is kept intact.
	
	version 1.01, September, 17th, 2005
	
	***********************************************************************************************/
		
	var activePalette = false;
	var busyDisplayingScheme = false;
	function setPalette(idOfDiv)
	{
		if(activePalette)activePalette.style.display='none'; else document.getElementById('namedColors').style.display='none';
		
		var obj = document.getElementById(idOfDiv);
		obj.style.display='block';
		activePalette = obj;
		
	}
		
	
	function colorClick(rgbColor)
	{
		if(!rgbColor.match(/^\#[a-f0-9]{6}$/gi))return;
		pickColor(rgbColor);
		displayScheme();
	}
	
	
	function pickColor(rgbColor,ignoreHSB)
	{
		
		if(!rgbColor.match(/^\#[a-f0-9]{6}$/gi))return;
		document.getElementById('RGB_RGB').value = rgbColor;
		document.getElementById('color_rgb').value = rgbColor;
		
		var red = baseConverter(rgbColor.substr(1,2),16,10);
		var green = baseConverter(rgbColor.substr(3,2),16,10);
		var blue = baseConverter(rgbColor.substr(5,2),16,10);
		document.getElementById('RGB_R').value = red;
		document.getElementById('RGB_G').value = green;
		document.getElementById('RGB_B').value = blue;
		
		if(!ignoreHSB || ignoreHSB=='undefined'){
			var hsbArray = toHSV(rgbColor);
			if(hsbArray[0]<0)hsbArray[0] = hsbArray[0]/1 + 360;
			document.getElementById('HSB_H').value = Math.round(hsbArray[0]);
			document.getElementById('HSB_S').value = Math.round(hsbArray[1]*100);
			document.getElementById('HSB_B').value = Math.round(hsbArray[2]*100);
		}
		document.getElementById('previewPanel').style.backgroundColor = rgbColor;
		checkColorButton(rgbColor);

		document.advancedColor.red.value = red;
		document.advancedColor.green.value = green;
		document.advancedColor.blue.value = blue;

			
		positionSliderImage(false,1);
		positionSliderImage(false,2);
		positionSliderImage(false,3);
					

	}
	
	function checkColorButton(rgbColor)
	{
		return;
		var myColorDiv = document.getElementById('myColors_colorList');
		var subDivs = myColorDiv.getElementsByTagName('DIV');
		var colorFound = false;
		for(var no=0;no<subDivs.length;no++){	
			var theColor = subDivs[no].getAttribute('value_bgColor');
			if(theColor == rgbColor){
				colorFound = true;
				break;	
			}			
		}
		document.getElementById('button_removeColor').style.display='none';
		document.getElementById('button_addColor').style.display='none';

		if(!colorFound){		
			document.getElementById('button_addColor').style.display='inline';
		}else{
			document.getElementById('button_removeColor').style.display='inline';
			
		}		
		
	}
	
	function showColorByNumericHSB()
	{
		var hue = document.getElementById('HSB_H').value;
		var sat = document.getElementById('HSB_S').value;
		var bri = document.getElementById('HSB_B').value;
		
		if(!hue.match(/^[\d]+$/g))return false;
		if(!sat.match(/^[\d]+$/g))return false;
		if(!bri.match(/^[\d]+$/g))return false;		
		
		if(hue/1>360) return false;
		if(sat/1>100) return false;
		if(bri/1>100) return false;
		
		sat = sat/100;
		bri = bri/100;
		
		var rgbColor = toRgb(hue,sat,bri);
		
		pickColor(rgbColor,true);
		displayScheme();		
	}
	
	
	function showColorByNumericRGB()
	{
		var red = document.getElementById('RGB_R').value;
		var green = document.getElementById('RGB_G').value;
		var blue = document.getElementById('RGB_B').value;
		
		if(!red.match(/^[\d]+$/g))return false;
		if(!green.match(/^[\d]+$/g))return false;
		if(!blue.match(/^[\d]+$/g))return false;
		
		if(red/1>255)return false;
		if(green/1>255)return false;
		if(blue/1>255) return false;
		
		var rgbColor = '#' + baseConverter(red,10,16) + baseConverter(green,10,16) + baseConverter(blue,10,16);

		pickColor(rgbColor);
		busyDisplayingScheme=false;
		displayScheme();
	}
	
	function manualOnchange()
	{

		if(this.id.indexOf('HSB')>=0){
			showColorByNumericHSB();
			
		}else if(this.id=='RGB_RGB'){
			if(this.value.match(/^[a-f0-9]{6}$/gi))this.value = '#' + this.value;
			if(this.value.match(/^\#[a-f0-9]{6}$/gi))pickColor(this.value);

			
		}else{ // RGB value
			showColorByNumericRGB();
		}
		busyDisplayingScheme=false;
		displayScheme();
		
	}
	
	
	
	function displayScheme()
	{
		if(busyDisplayingScheme){
			return;
		}
		busyDisplayingScheme = true;
		var color = document.getElementById('RGB_RGB').value;
		if(!color.match(/^\#[a-f0-9]{6}$/gi))return;
		
		var targetDiv = document.getElementById('colorScheme_colorBox');
		var subDivs = targetDiv.getElementsByTagName('DIV');
		for(var no=subDivs.length-1;no>=0;no--){
			subDivs[no].parentNode.removeChild(subDivs[no]);
		}
		


		var colorSchemes = ['complementary','splitcomplementary','triade','tetrade','analogous','monotone'];
		var colorSchemeNames = ['Complementary','Split complementary','Triade','Tetrade','Analogic','Monotone'];
		
		for(var schemeCounter=0;schemeCounter<colorSchemes.length;schemeCounter++){
			var degreesArray = new Array();
			degreesArray.push(0);
					
			var newDiv = document.createElement('DIV');
			newDiv.innerHTML = '<b>' + colorSchemeNames[schemeCounter] + '</b>';
			newDiv.style.cssText = 'width:100%;float:none;clear:both;padding-left:5px';
			newDiv.style.clear = 'both';
			targetDiv.appendChild(newDiv);
			
			inputValue = colorSchemes[schemeCounter];
			switch(inputValue){
				case "complementary":		
					degreesArray.push(180);	
					break;			
				case "splitcomplementary":
					degreesArray.push(160);
					degreesArray.push(-160);
					break;
					
				case "triade":
					degreesArray.push(120);
					degreesArray.push(-120);			
					break;
					
					
				case "tetrade":
					degreesArray.push(90);
					degreesArray.push(180);	
					degreesArray.push(270);	
					break;
				case "analogous":
					degreesArray.push(25);
					degreesArray.push(-25);	
					break;
				
				case "doubleContrast":
					degreesArray.push(30);
					degreesArray.push(180);	
					degreesArray.push(210);
					break;
				case "monotone":
					var hsvColor = toHSV(color);	// Get brightness
					var saturationAndBrightnessDistance = 10;
					for(var no=0;no<4;no++){
						degreesArray.push((no+1)*saturationAndBrightnessDistance);	
					}
					break;
				
				
			}
			var lastColor = false;
			for(no=0;no<degreesArray.length;no++){
				if(degreesArray[no]==0){
					var newColor = color;
				}else{
					var newColor = findColorByDegrees(color,degreesArray[no]);	
				}
				
				
				
				if(newColor!=lastColor && newColor.length==7){
					var newDiv = document.createElement('DIV');
					newDiv.style.backgroundColor = newColor;
					newDiv.setAttribute('value_bgColor',newColor);
					newDiv.onclick = colorSquareClick;
					newDiv.className = 'bigColorSquare';
					//begin:radu
					newDiv.onclick = function(){
						//old_hex = color.substr(1);
						//old_hex = $(this).parents('td:first').prev('td').contents().find('table').find('tr').eq(1).children('td').children('div').css('background-color');
						old_hex = $(this).parents('td:first').prev('td').children('table').find('tr').eq(1).find('td').eq(1).html();
						old_hex = old_hex.substr(2,6);
						_hex = $(this).attr('value_bgcolor').substr(1);
						//alert('hex:'+_hex+'", old_hex:"'+old_hex+'"');
						//alert($(this).parents('td:first').prev('td').contents().find('tr').eq(2).children('td').eq(2).children('div').attr('id'));
						//$(this).parents('td:first').prev('td').contents().find('tr').eq(2).children('td').eq(2).children('div').css('background-color', $(this).attr('value_bgcolor'));
						$('#screenshot').attr('src', '/img/ajax-loader.gif');
						$.get('/ajax.cs.php', {'img':img,'old_hex':old_hex,'hex':_hex}, function(msg) {
							var date = new Date();
							var ms = date.getTime();
							//$(document.body).append(msg+'<br/>');
							$('#screenshot').attr('src', msg + '?t='+ms);
							});
					};
					targetDiv.appendChild(newDiv);
					
					var newSpan = document.createElement('SPAN');
					newSpan.innerHTML = newColor;
					newDiv.appendChild(newSpan);
					lastColor = newColor;
				}
			}
		}
		
		setTimeout('busyDisplayingScheme = false',5);
		
	}
	
	function colorSquareClick()
	{
		var rgbColor = this.getAttribute('value_bgColor');;
		if(!rgbColor.match(/^\#[a-f0-9]{6}$/gi))return;
		pickColor(rgbColor);
		//alert($(this).prev('td').children('table').children('tr').eq(2).children('td').css('background-color'));
		//alert($(this).parents('td').prev('td').children('table').contents().find('tr').eq(2).children('td').eq(1).text());
	}
	
	function setManual()
	{
		var red = parseInt(document.advancedColor.red.value);
		var green = parseInt(document.advancedColor.green.value);
		var blue = parseInt(document.advancedColor.blue.value);
		
		
		var redHex = baseConverter(red,10,16)+'';
		var greenHex = baseConverter(green,10,16) + '';
		var blueHex = baseConverter(blue,10,16) + '';
		if(redHex.length==1)redHex = '0' + redHex;
		if(greenHex.length==1)greenHex = '0' + greenHex;
		if(blueHex.length==1)blueHex = '0' + blueHex;
		currentColor = '#' + redHex + greenHex + blueHex;
		
	
		document.getElementById('color_rgb').value = currentColor.replace('#','');
		
		pickColor(currentColor);
		displayScheme();
		
		return false;
			
	}
		
	function removeColor()
	{
		var color = document.getElementById('RGB_RGB').value;
		if(!color.match(/^\#[a-f0-9]{6}$/gi)){
			return;
		}			
		var obj = document.getElementById('myColors_colorList');
		var subDivs = obj.getElementsByTagName('DIV');
		for(var no=0;no<subDivs.length;no++){
			if(subDivs[no].getAttribute('value_bgColor') == color){
				subDivs[no].parentNode.removeChild(subDivs[no]);
				checkColorButton(color);
				return;
			}
		}
		checkColorButton(color);
		
	}
	
	function addColor()
	{
		var color = document.getElementById('RGB_RGB').value;
		if(!color.match(/^\#[a-f0-9]{6}$/gi)){
			alert('Invalid color - the color has to be specified in the format #RRGGBB where R,G and B are hexadecimal numbers');	
			return;
		}	

		var newDiv = document.createElement('DIV');
		newDiv.setAttribute('value_bgColor',color);
		newDiv.style.backgroundColor = color;
		newDiv.className = 'bigColorSquare';
		newDiv.onclick = colorSquareClick;
		document.getElementById('myColors_colorList').appendChild(newDiv);
		
		var newSpan = document.createElement('SPAN');
		newSpan.innerHTML = color;
		newDiv.appendChild(newSpan);
					
		checkColorButton(color);
		
	}
	
	function initColorPicker()
	{
		var inputs = document.getElementsByTagName('INPUT');
		for(var no=0;no<inputs.length;no++){
			if(inputs[no].type=='text'){
				inputs[no].onchange = manualOnchange;	
			}	
		}		
	}
	
	window.onload = initColorPicker;
