/* Utility functions:
 * 
 * testAjax
 * toggleDiv
 * toggleDivPair
 * getValFromPx
 * setSelectInputValue
 * rgb2hsv
 * invertColor
 * getMouseX
 * getMouseY
 * getImgSrc
 * getImgFilename
 * makeVisible
 * makeInvisible
 * isIE
 */


// Test for AJAX support
function testAjax () {
	if (!Ajax.getTransport) {
		alert ("You cannot view this page: your browser does not support AJAX. Please upgrade your browser.");
		window.location = '/';
	}
}

function toggleDiv(divName) {
	var e = $(divName);
	e.visible() ? e.hide() : e.show();
}

function toggleDivPair(div1, div2) {
	var e1 = $(div1); 
	var e2 = $(div2);
	if (e1.visible()) {
		e1.hide(); e2.show();
	} else {
		e1.show(); e2.hide();
	}
}

function getValFromPx(px) {	
	try
	{
		px = px + ''; // numbers and strings are NOT treated the same
		px = px.replace(/px/, '');
		px = px.replace(/pt/, '');
		return eval(px);
	}
	catch (err)
	{
		return 0;
	}

}

function setSelectInputValue(ddList, selValue) {
	for (i = 0; i < ddList.length; ++i)
	{
		val = ddList.options[i].value;
		if (val == selValue) {
			ddList.selectedIndex = i;
			return;
		}
		ddList.selectedIndex = 0;
	}
}

function rgb2hsv(r, g, b) {
	hsv = new Array();
	
	min = Math.min(r, Math.min(g, b));
	max = Math.max(r, Math.max(g, b));
	
	hsv[2] = max/255;
	delta = max - min;
	if (max > 0) {
		hsv[1] = delta/max;
	} else {
		hsv[0] = 0;
		hsv[1] = 0;
		hsv[2] = 0;
		return new Array(0, 0, 0);
	}
	
	if (r == max) {
		hsv[0] = (g - b) / delta;
	} else if (g == max) {
		hsv[0] = 2 + (b - r) / delta;
	} else {
		hsv[0] = 4 + (r - g) / delta;
	}
	
	hsv[0] = hsv[0] * 60;
	
	if (hsv[0] < 0) {
		hsv[0] += 360;
	}
	
	return [eval(hsv[0]), eval(hsv[1]), eval(hsv[2])];
}

function invertColor(rgbColor) {
	r = 0;
	g = 0;
	b = 0;
	if (rgbColor.length > 4) {
		rgbColor = rgbColor.substr(4, rgbColor.length);
		rgbColor = rgbColor.substr(0, rgbColor.length - 1);
		c = rgbColor.split(',');
		r = c[0];
		g = c[1];
		b = c[2];
	}
	r = 255 - r;
	g = 255 - g;
	b = 255 - b;
	return "rgb(" + r + "," + g + "," + b + ")";
}

// The next 2 methods are horrible...
// see http://www.quirksmode.org/js/events_properties.html
function getMouseX(e) {
	if (e.pageX) 	{
		return e.pageX;
	}
	else if (e.clientX) 	{
		return e.clientX + document.body.scrollLeft	+ document.documentElement.scrollLeft;
	}
	
	return e.screenX;
}

function getMouseY(e) {
	if (e.pageY) 	{
		return e.pageY;
	}
	else if (e.clientY) 	{
		return e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
	}
	
	return e.screenY;
}


// split out the src text...i can't get childNodes[0].src to work properly for images which are already on the board
function getImgSrc(divID) {
	// note that inside the div there might be a span to handle the IE png problem...strip that out
	html = $(divID).innerHTML;
	srcRx = new RegExp("src=['\"](.*?)['\"]", "i");
	m = srcRx.exec(html);
	srcTxt = '';
	if (m != null) {
		srcTxt = m[1];
	}

	return srcTxt;	
}

function getImgFilename(divID) {
	iSrc = getImgSrc(divID);

	rx = new RegExp("([^/]*\.\\D+)$");
	m = rx.exec(iSrc);
	if (m != null) {
		return m[1];
	}
	return '';
}

function isVisible(el) {
	return $(el).getStyle('visibility') == 'visible'; // wrap with $() due to IE/Prototype
}

function setVisibility(divId, bool) {
	$(divId).style.visibility = bool ? 'visible' : 'hidden';
}

function makeVisible(divId) {
	setVisibility(divId, true);
}

function makeInvisible(divId) {
	setVisibility(divId, false);
}

// another horrible thing...
// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var sz = new Array(myWidth, myHeight);
  return sz;
}

function isIE() {
	return navigator.appName == "Microsoft Internet Explorer";
}

function getPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	    while(1) 
	    {
	      curleft += obj.offsetLeft;
	      if(!obj.offsetParent)
	        break;
	      obj = obj.offsetParent;
	    }
	else if(obj.x)
	    curleft += obj.x;
	return curleft;
}

function getPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	    while(1)
	    {
	      curtop += obj.offsetTop;
	      if(!obj.offsetParent)
	        break;
	      obj = obj.offsetParent;
	    }
	else if(obj.y)
	    curtop += obj.y;
	return curtop;
}
