var grad_bar_width=512;
var maxColors=13;
var minColors=2;
var currentElement=0;
var currentColors=10;
var color=new Array(maxColors);
var grad_dragObj=new Object();
grad_dragObj.zIndex=0;
function grad_dragStart(event,element){
	var id;
	for(var i=0; i<currentColors; i++){
		if(color[i][2]==element){id=i;}
	}
	document.getElementById('display_color').style.backgroundColor='#'+color[id][0];
	document.getElementById('input_color').value='#'+color[id][0];
	currentElement=id;
	grad_dragObj.elNode=color[id][2];
	if(browser.isIE){
		grad_dragObj.cursorStartX=window.event.clientX+document.documentElement.scrollLeft+document.body.scrollLeft;
	}else{
		grad_dragObj.cursorStartX=event.clientX+window.scrollX;
	}
	grad_dragObj.elStartLeft=parseInt(grad_dragObj.elNode.style.left,10);
	if(isNaN(grad_dragObj.elStartLeft)){grad_dragObj.elStartLeft=color[id][1];}
	grad_dragObj.elNode.style.zIndex=++grad_dragObj.zIndex;
	if(browser.isIE){
		document.attachEvent("onmousemove",grad_dragGo);
		document.attachEvent("onmouseup",grad_dragStop);
		window.event.cancelBubble=true;
		window.event.returnValue=false;
	}else{
		document.addEventListener("mousemove",grad_dragGo,true);
		document.addEventListener("mouseup",grad_dragStop,true);
		event.preventDefault();
	}
}
function grad_dragGo(event){
	var x,left_value;
	if(browser.isIE){
		x=window.event.clientX+document.documentElement.scrollLeft+document.body.scrollLeft;
	}else{
		x=event.clientX+window.scrollX;
	}
	left_value=(grad_dragObj.elStartLeft+x-grad_dragObj.cursorStartX);
	if(left_value<-4){left_value=-4;}
	if(left_value>grad_bar_width-5){left_value=grad_bar_width-5;}
	grad_dragObj.elNode.style.left=left_value+"px";
	color[currentElement][1]=left_value;
	if(browser.isIE){
		window.event.cancelBubble=true;
		window.event.returnValue=false;
	}else{
		event.preventDefault();
	}
}
function grad_dragStop(event){
	if(browser.isIE){
		document.detachEvent("onmousemove",grad_dragGo);
		document.detachEvent("onmouseup",grad_dragStop);
	}else{
		document.removeEventListener("mousemove",grad_dragGo,true);
		document.removeEventListener("mouseup",grad_dragStop,true);
	}
	updateGradient();
}
function sortByPos(a,b){
	a=a[1];
	b=b[1];
	return a==b?0:(a<b?-1:1);
}
function grad_init(){	
	color[0]=Array('000000',-4,document.getElementById('slider_000'),document.getElementById('color_000'));	
	color[1]=Array('FF8000',52,document.getElementById('slider_001'),document.getElementById('color_001'));	
	color[2]=Array('FFFF80',109,document.getElementById('slider_002'),document.getElementById('color_002'));	
	color[3]=Array('000000',166,document.getElementById('slider_003'),document.getElementById('color_003'));	
	color[4]=Array('00FF80',223,document.getElementById('slider_004'),document.getElementById('color_004'));	
	color[5]=Array('80FFFF',279,document.getElementById('slider_005'),document.getElementById('color_005'));	
	color[6]=Array('000000',336,document.getElementById('slider_006'),document.getElementById('color_006'));	
	color[7]=Array('8000FF',393,document.getElementById('slider_007'),document.getElementById('color_007'));	
	color[8]=Array('FF80FF',450,document.getElementById('slider_008'),document.getElementById('color_008'));	
	color[9]=Array('000000',507,document.getElementById('slider_009'),document.getElementById('color_009'));	
	color[10]=Array('000000',508,document.getElementById('slider_010'),document.getElementById('color_010'));	
	color[11]=Array('000000',508,document.getElementById('slider_011'),document.getElementById('color_011'));	
	color[12]=Array('000000',508,document.getElementById('slider_012'),document.getElementById('color_012'));
	document.getElementById('select_max_colors').selectedIndex=currentColors-2;
	updateGradient();
}
function getId(input){
	switch(input.toString().length){
		case 1:
			return '00'+input;
			break;
		case 2:
			return '0'+input;
			break;
		case 3:
			return input;
		default:
			alert('Error: ID overflow.');
	}
}
function getColor(r,g,b){
	var value=(r<<16)|(g<<8)|b;
	switch(value.toString(16).length){
		case 1:
			return '#00000'+value.toString(16);
			break;
		case 2:
			return '#0000'+value.toString(16);
			break;
		case 3:
			return '#000'+value.toString(16);
			break;
		case 4:
			return '#00'+value.toString(16);
			break;
		case 5:
			return '#0'+value.toString(16);
			break;
		case 6:
			return '#'+value.toString(16);
		default:
			alert('Error: Color code overflow.');
	}
}
function changeColor(input){
	hex=input.replace(/#/,'');
	if(!isNaN(parseInt(hex,16))){
		color[currentElement][0]=hex;
		hex=parseInt(hex,16);
		hex=getColor( (hex&0xFF0000)>>16, (hex&0xFF00)>>8, (hex&0xFF) );
		color[currentElement][3].style.backgroundColor=hex;
		document.getElementById('display_color').style.backgroundColor=hex;
		updateGradient();
	}
}
function changeColorMax(number){
	currentElement=0;
	document.getElementById('display_color').style.backgroundColor='#'+color[0][0];
	document.getElementById('input_color').value='#'+color[0][0];
	currentColors=number;
	for(var i=0; i<maxColors; i++){
		if(i<currentColors){
			if(i==0){
				color[i][1]=-4;
			}else if(i==currentColors-1){
				color[i][1]=grad_bar_width-4;
			}else{
				color[i][1]=Math.floor(((i/(currentColors-1))*grad_bar_width)-4)
			}
			color[i][2].style.display='block';
			color[i][2].style.left=color[i][1]+'px';
		}else{
			color[i][1]=grad_bar_width-3;
			color[i][2].style.display='none';
		}
	}
	updateGradient();
}
function updateGradient(){
	var i,j,start,end,color_1,color_2,color1_r,color1_g,color1_b,color2_r,color2_g,color2_b,increment_red,increment_green,increment_blue;
	color.sort(sortByPos);
	for(i=0; i<currentColors-1; i++){
		start=color[i][1]+4;
		if(start<0){start=0;}
		end=color[i+1][1]+4;
		if(end>grad_bar_width-1){end=grad_bar_width-1;}
		color1=parseInt(color[i][0],16);
		color2=parseInt(color[i+1][0],16);
		color1_r=(color1&0xFF0000)>>16;	//color 1 red channel
		color1_g=(color1&0x00FF00)>>8;	//color 1 green channel
		color1_b=(color1&0x0000FF);		//color 1 blue channel
		color2_r=(color2&0xFF0000)>>16;	//color 2 red channel
		color2_g=(color2&0x00FF00)>>8;	//color 2 green channel
		color2_b=(color2&0x0000FF);		//color 2 blue channel
		increment_red=(color2_r-color1_r)/(end-start+1);		//red channel increment
		increment_green=(color2_g-color1_g)/(end-start+1);	//green channel increment
		increment_blue=(color2_b-color1_b)/(end-start+1);		//blue channel increment
		if(i==0){
			for(j=0; j<start; j++){
				document.getElementById('g'+getId(j)).style.backgroundColor=getColor(color1_r>>0,color1_g>>0,color1_b>>0);
			}
		}
		for(j=start; j<=end; j++){
			document.getElementById('g'+getId(j)).style.backgroundColor=getColor(color1_r>>0,color1_g>>0,color1_b>>0);
			color1_r+=increment_red;
			color1_g+=increment_green;
			color1_b+=increment_blue;
		}
		if(i==currentColors-2){
			for(j=end+1; j<grad_bar_width; j++){
				document.getElementById('g'+getId(j)).style.backgroundColor=getColor(color1_r>>0,color1_g>>0,color1_b>>0);
			}
		}
	}
	if(jsReady){sendToActionScript()};
}