var ok = 0;
var mainField;
var mainValue;
var i;

function calc_shadow( r, g, b )
{
	var sum;
	var average;
	var amount;
	var shadowColor;
	
	r = parseInt( r, 10 );
	g = parseInt( g, 10 );
	b = parseInt( b, 10 );
	
	sum = r + g + b - 382;
	average = parseInt( Math.abs( sum / 3 ), 10 );
	amount = 127 + parseInt( Math.abs( average - 63 ) * ( average / 63 ) );
	
	if( sum > 0 )
	{
		r = Math.max( r - amount, 0 );
		g = Math.max( g - amount, 0 );
		b = Math.max( b - amount, 0 );
		shadowColor = ( r < 16 ? '0' : '' ) + r.toString( 16 ) + ( g < 16 ? '0' : '' ) + g.toString( 16 ) + ( b < 16 ? '0' : '' ) + b.toString( 16 ); 
	}
	else
	{
		r = Math.min( r + amount, 255 );
		g = Math.min( g + amount, 255 );
		b = Math.min( b + amount, 255 );
		shadowColor = r.toString( 16 ) + g.toString( 16 ) + b.toString( 16 ); 
	}
	
	return shadowColor;
}

function render_shadow( shadow )
{
	if( shadowtype == 1 )
	{
		document.getElementById( "modcolor2" ).style.textShadow = '#' + shadow + ' 1px 0px 0px, #' + shadow + ' -1px 0px 0px, #' + shadow + ' 0px 1px 0px, #' + shadow + ' 0px -1px 0px';
	}
	else
	{
		document.getElementById( "modcolor2" ).style.filter = 'dropshadow(color=#' + shadow + ',offX=-1,offY=0) dropshadow(color=#' + shadow + ',offX=0,offY=-1) dropshadow(color=#' + shadow + ',offX=1,offY=0) dropshadow(color=#' + shadow + ',offX=0,offY=1)';
	}
}

function modcol( field, value )
{
	var mod;
	
	if( value == '+' ) mod = 1;
	else mod = -1;
	
	if( field != 'all' )
	{
		var newval = parseInt( document.getElementById( "color"+field ).value );
		if( newval + mod >= 0 && newval + mod <= 255 )
		{
			newval += mod;
			document.getElementById( "color"+field ).value = newval;
			document.getElementById( "modcolor" ).style.color = "rgb("+document.getElementById( "colorred" ).value+", "+document.getElementById( "colorgreen" ).value+", "+document.getElementById( "colorblue" ).value+")";
			document.getElementById( "modcolor2" ).style.color = "rgb("+document.getElementById( "colorred" ).value+", "+document.getElementById( "colorgreen" ).value+", "+document.getElementById( "colorblue" ).value+")";
			render_shadow( calc_shadow( document.getElementById( "colorred" ).value, document.getElementById( "colorgreen" ).value, document.getElementById( "colorblue" ).value ) );
		}
	}
	else
		{
			modcol( 'red', value );
			modcol( 'green', value );
			modcol( 'blue', value );
		}
}

function setcol( field )
{
	if( isNaN( document.getElementById( "color"+field ).value ) ) document.getElementById( "color"+field ).value = 0;
	var currval = parseInt( document.getElementById( "color"+field ).value );
	if( currval < 0 || currval > 255 ) currval = 0;
	document.getElementById( "color"+field ).value = currval;
	document.getElementById( "modcolor" ).style.color = "rgb("+document.getElementById( "colorred" ).value+", "+document.getElementById( "colorgreen" ).value+", "+document.getElementById( "colorblue" ).value+")";
	document.getElementById( "modcolor2" ).style.color = "rgb("+document.getElementById( "colorred" ).value+", "+document.getElementById( "colorgreen" ).value+", "+document.getElementById( "colorblue" ).value+")";
	render_shadow( calc_shadow( document.getElementById( "colorred" ).value, document.getElementById( "colorgreen" ).value, document.getElementById( "colorblue" ).value ) );
}

function snapcol( r, g, b )
{
	document.getElementById( "colorred" ).value = r;
	document.getElementById( "colorgreen" ).value = g;
	document.getElementById( "colorblue" ).value = b;
	document.getElementById( "modcolor" ).style.color = "rgb("+r+", "+g+", "+b+")";
	document.getElementById( "modcolor2" ).style.color = "rgb("+r+", "+g+", "+b+")";
	render_shadow( calc_shadow( document.getElementById( "colorred" ).value, document.getElementById( "colorgreen" ).value, document.getElementById( "colorblue" ).value ) );
}

function loadHold( field, value )
{
	mainField = field;
	mainValue = value;
	ok=1;
	i=0;
	modcol( mainField, mainValue );
	holdButton();
}

function holdButton()
{
	if( i > 10 ) modcol( mainField, mainValue );
	i++;
	if( ok == 1 ) setTimeout( "holdButton()", 80 );
}
