/***************************************************************************
* Copyrighted (c) 2005 Compu-Creations (www.compu-creations.com)
* The software and related user documentation are protected under
* copyright laws and remain the sole property of Compu-Creations. Full license
* is available on the Compu-Creations website or in the original download.
**************************************************************************/

/**
* @author Alexandru Popescu <apopescu@arysa.net>
* @copyright Copyrighted (c) 2005 Compu-Creations
*/


//........... General ...................................................................................


if (typeof findDOM != 'function')
{
	findDOM = $;
}



/**
* Returns the X of an element in relation to the page top left
*
* @param Element - e (The elemente)
*
* @return String - the text content
*
*/
function GetElementX(e, relative)
{
	var x = e.offsetLeft;
	while ((e = e.offsetParent) != relative && e != null)
	{
		x += e.offsetLeft;
	}
	return x;
}


/**
* Returns the Y of an element in relation to the page top left
*
* @param Element - e (The elemente)
*
* @return String - the text content
*
*/
function GetElementY(e, relative)
{
	var y = e.offsetTop;
	while ((e = e.offsetParent) != relative && e != null)
	{
		y += e.offsetTop;
	}
	return y;
}




/**
 * trim
 *
 * Strip whitespace from the beginning and end of a string
 *
 * object string
 * return string
 *
 * example:
 *   test = '\nsomestring\n\t\0'
 *   test.trim()  //returns 'somestring'
 */
String.prototype.trim = function()
{
  return this.replace(/^\s*([^ ]*)\s*$/, "$1");
}



//........... ColorSwatchSelector .......................................................................

/**
* Shows the selector window of a ColorSwatchSelector
*
* @param String - id (the id of the ColorSwatchSelector)
*
*/
function JSUILib_ColorSwatchSelector_ShowSelector(id)
{
	var button = findDOM(id + '_Container');
	//var x = GetElementX(button) - 195;
	//var y = GetElementY(button) + 5;
	var x = '0px';
	var y = '0px';

	var img = findDOM(id + '_ValueImage');
	if (img)
	{
		x = img.style.width;
		//alert(x);
	}

	var selector = findDOM(id + '_SwatchContainer');
	if (selector)
	{
		selector.style.left = x;
		selector.style.top = y;
		//selector.style.zIndex = 21000;
		selector.style.display = 'block';
	}
	else
	{
		alert('Error creating color swatch selector');
	}
}

/**
* Hides the selector window of a ColorSwatchSelector
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param bool - force (true = try to close the selector again in 100ms)
*
*/
function JSUI_ColorSwatchSelector_HideSelector(id, force)
{
	var selector = findDOM(id + '_SwatchContainer');
	if (selector)
	{
		selector.style.display = 'none';
	}
	else
	{
		alert('Error finding color swatch selector');
	}

	if (force)
	{
		setTimeout('JSUI_ColorSwatchSelector_HideSelector("' + id + '", false)', 100);
	}
}

/**
* Sets the value of a ColorSwatchSelector
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param String - col (the color to set it to)
*
*/
function JSUILib_ColorSwatchSelector_SetValue(id, col)
{
	col = col.trim();
	//alert(col);

	var val_field = findDOM(id);
	val_field.value = col;

	if (col == '')
	{
		//var img = findDOM(id + '_ValueImage');
		//img.src = 'JSUI/transparent_color.gif';
	}
	else
	{
		var cell = findDOM(id + '_Container');
		cell.style.backgroundColor = col;

		//var img = findDOM(id + '_ValueImage');
		//img.src = 'JSUI/blank.gif';
	}

	JSUI_ColorSwatchSelector_HideSelector(id, true);
}

/**
* Event handler for when mouse moves over a color swatch
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param String - col (the color the mouse moved over)
*
*/
function JSUILib_ColorSwatchSelector_OnMouseOverColor(id, col)
{
	JSUILib_ColorSwatchSelector_PreviewColor(id, col);
}

/**
* Event handler for when mouse clicks a color swatch
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param String - col (the color the mouse clicked)
*
*/
function JSUILib_ColorSwatchSelector_OnClickColor(id, col)
{
	JSUILib_ColorSwatchSelector_SetValue(id, col);
}

/**
* Event handler for keypresses in the custom value box
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param Event - ev (the keyup event)
*
*/
function JSUILib_ColorSwatchSelector_OnKeyUp(id, ev)
{
	var input = findDOM(id + '_Input');
	var col = input.value;
	if (col.length != 0)
	{
		col = col.strPad(7, '0');
	}

	if (window.event) //IE
	{
		keynum = ev.keyCode;
	}
	else	// Firefox/Netscape/opera
	{
		keynum = ev.which;
	}

	if (keynum == KEYCODE_ENTER)
	{
		JSUILib_ColorSwatchSelector_SetValue(id, col);
	}
	else
	{
		JSUILib_ColorSwatchSelector_PreviewColor(id, col);
	}
}


/**
* Previews a color in the ColorSwatchSelector
*
* @param String - id (the id of the ColorSwatchSelector)
*
* @param String - col (the color)
*
*/
function JSUILib_ColorSwatchSelector_PreviewColor(id, col)
{
	//alert(col);
	var preview = findDOM(id + '_PreviewColor');
	var img = findDOM(id + '_PreviewImage');

	if (img)
	{
		if (col == '')
		{
			img.src = 'JSUI/transparent_color.gif';
		}
		else
		{
			if (preview)
			{
				preview.style.backgroundColor = col;
			}
			img.src = 'JSUI/blank.gif';
		}
	}

	var input = findDOM(id + '_Input');
	if (input)
	{
		input.value = col;
	}
}

//........... DeletableDefault ..........................................................................

/**
* Erases the value of a DeletableDefault control
*
* @param String - id (the id of the DeletableDefault control)
*
*/
function JSUILib_DeletableDefault_DeleteValue(id)
{
	display = $(id + '_Display');
	button = $(id + '_Img');
	e = $(id);

	display.innerHTML = 'None';
	button.style.display = 'none';
	e.value = '';
}

//........... ImageCheckbox ..........................................................................

/**
* Toggles the value of a ImageCheckbox control
*
* @param String - id (the id of the ImageCheckbox control)
*
*/
function JSUILib_ImageCheckbox_Toggle(id)
{
	control_val = $(id);
	control_img = $(id + '_img');

	if (control_val.value == eval(id + '_val_on'))
	{
		control_img.src = eval(id + '_img_off');
		control_val.value = eval(id + '_val_off');
	}
	else
	{
		control_img.src = eval(id + '_img_on');
		control_val.value = eval(id + '_val_on');
	}

	onchange = id + '_onchange';
	eval(onchange + '()');
}

/**
* Sets the value of a ImageCheckbox control
*
* @param String - id (the id of the ImageCheckbox control)
*
*/
function JSUILib_ImageCheckbox_Set(id, val)
{
	control_val = $(id);
	control_img = $(id + '_img');

	if (control_val.value == eval(id + '_val_on'))
	{
		control_img.src = eval(id + '_img_on');
		control_val.value = val;
	}
	else
	{
		control_img.src = eval(id + '_img_off');
		control_val.value = val;
	}

	onchange = id + '_onchange';
	eval(onchange + '()');
}


//........... ImageStrip ................................................................................

var JSUI_ImageStrip_Scrollers = new Array();

/**
* Starts scrolling the ImageStrip
*
* @param String - id (the id of the ImageStrip)
*
*/
function JSUI_ImageStrip_StartScroll(id, pace, delay)
{
	JSUI_ImageStrip_Move(id, pace, delay);
}

function JSUI_ImageStrip_StopScroll(id)
{
	try
	{
		clearTimeout(JSUI_ImageStrip_Scrollers[id]);
	}
	catch (e)
	{
	}
}


function JSUI_ImageStrip_Move(id, pace, delay)
{
	ef = $(id + '_FlexLayer');
	ec = $(id + '_Container');
	if (ef && ec)
	{
		flex_w = ef.offsetWidth;
		flex_x = parseInt(ef.style.left);
		cont_w = ec.offsetWidth;

		ok = JSUI_ImageStrip_UpdateButtons(id, flex_w, flex_x, cont_w, pace);

		if (!ok)
		{
			return;
		}

		if (pace == 0)
		{
			return;
		}

		e = $(id + '_FlexLayer');
		if (e)
		{
			x = parseInt(e.style.left);
			x += pace;
			e.style.left = x + 'px';
		}

		JSUI_ImageStrip_Scrollers[id] = setTimeout("JSUI_ImageStrip_Move('" + id + "', " + pace + ", " + delay + ");", delay);
	}
	else
	{
		alert('Missing element ef(' + ef + ') or ec(' + ec + ') in JSUI_ImageStrip_Move');
	}
}

function JSUI_ImageStrip_ShowImage(id, linked_id, i)
{
	et = $(id + '_Img_' + i);
	ei = $(linked_id);
	if (et && ei)
	{
		ei.src = et.src;
	}
	else
	{
		alert('Missing element ei (' + ei + ') or et (' + et + ') in JSUI_ImageStrip_ShowImage');
	}
}

function JSUI_ImageStrip_UpdateButtons(id, flex_w, flex_x, cont_w, pace)
{
	window.defaultStatus = flex_w + ", " + flex_x + ", " + cont_w;

	el = $(id + '_Left');
	er = $(id + '_Right');

	result = true;

	if (el)
	{
		if (flex_x >= 0)
		{
			if (el.style.opacity == 1.00 || el.style.opacity == '')
			{
				el.style.opacity = 0.40;
	    		el.style.MozOpacity = 0.40;
	    		el.style.KhtmlOpacity = 0.40;
	    		el.style.filter = "alpha(opacity=40)";
			}

			if (pace > 0)
			{
				result = false;
			}
		}
		else
		{
			if (el.style.opacity == 0.40 || el.style.opacity == '')
			{
				el.style.opacity = 1.00;
    			el.style.MozOpacity = 1.00;
    			el.style.KhtmlOpacity = 1.00;
    			el.style.filter = "alpha(opacity=100)";
			}
		}
	}

	if (er)
	{
		if (flex_x + flex_w <= cont_w)
		{
			if (er.style.opacity == 1.00 || er.style.opacity == '')
			{
				er.style.opacity = 0.40;
    			er.style.MozOpacity = 0.40;
    			er.style.KhtmlOpacity = 0.40;
    			er.style.filter = "alpha(opacity=40)";
			}

			if (pace < 0)
			{
				result = false;
			}
		}
		else
		{
			if (er.style.opacity == 0.40 || er.style.opacity == '')
			{
				er.style.opacity = 1.00;
    			er.style.MozOpacity = 1.00;
    			er.style.KhtmlOpacity = 1.00;
    			er.style.filter = "alpha(opacity=100)";
			}
		}
	}

	return result;
}

