/**
*  utils.js
*  (c)2007 Newtrax Technologies inc.
*/

var browser = {
 	    ie:     !!(window.attachEvent && !window.opera),
 	    opera:  !!window.opera,
 	    webkit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
 	    gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
 	  };


//-- EXTENSIONS TO STANDARD JAVASCRIPT OBJECTS ---------------------

/**
* Removes white spaces at the beginning and end of the string.
* The string is not modified, the method returns the padded string.
*/
String.prototype.trim = function(){
	return (this.replace(/^[\s]+/g,'')).replace(/[\s]+$/g,'');
};

/**
* Adds a given character at the beginning or end of the string. 
* The string is not modified, the method returns the padded string.
*/
String.prototype.pad = function(filler,fillToLength,fillDirection){
	theStr = this + '';
	if (fillDirection == 'right') {
		while (theStr.length < fillToLength) {
			theStr += filler;
		}
	}
	else {
		// default fill is to the left
		while (theStr.length < fillToLength) {
			theStr = filler + theStr;
		}
	}
	return (theStr);
};

/**
* Tells if an element is within an array and returns its index if found
*/
Array.prototype.search = function(element,forceIntCheck){
	if (forceIntCheck == undefined) forceIntCheck = false;
	for (i = 0; i < this.length; ++i) {
		if (forceIntCheck) {
			if (Number(this[i]) == Number(element)) return i;
		}
		else {
			if (this[i] == element) return i;
		}
	}
	return -1;
};

// precision is the number of digits after decimal point
Array.prototype.average = function(precision){
	var sumVal = parseFloat(this[0]);
	for (i = 1; i < this.length; ++i) {
		sumVal += parseFloat(this[i]);
	}
	avgVal = (sumVal/this.length);
	if (avgVal.toString().search('.') != -1) {
		avgValParts = avgVal.toString().split('.');
		if (avgValParts.length > 1) avgValParts[1] = avgValParts[1].slice(0,precision);
		avgVal = parseFloat(avgValParts.join('.'));
	}
	return avgVal;
};

// removes the first occurence of a value from the array
Array.prototype.removeEntry = function(toRemove,forceIntCheck){
	if (forceIntCheck == undefined) forceIntCheck = false;
	idx = this.search(toRemove,forceIntCheck);
	if (idx != -1) this.splice(idx,1);
	return true;
};



//--------------------------------------------------
// DROP MENUS
//--------------------------------------------------

function showDropMenu(menuElmId,launchElm,refCorner) {
	// place the drop menu in the correct position
	// posAnchor gives the corner of the launch element to be used
	// to position the drop menu
	
	// if refCorner = 'top-right', bottom-right corner of menuElm is aligned with top-right corner of launchElm
	// if refCorner = 'top-left', bottom-left corner of menuElm is aligned with top-right corner of launchElm
	// if refCorner = 'bottom-right', top-right corner of menuElm is aligned with bottom-right corner of launchElm
	// if refCorner = 'bottom-left', top-left corner of menuElm is aligned with bottom-left corner of launchElm
	
	var anchor = refCorner.split('-');
	var launchPosition = Position.positionedOffset(launchElm);
	if (browser.ie) var launchDims = {width: launchElm.width, height: launchElm.height};
	else var launchDims = launchElm.getDimensions();
	var menuTop, menuLeft;
	var menuElm = $(menuElmId);
	var menuDims = menuElm.getDimensions();

	if (menuElm.visible()) {
		menuElm.hide();
	}
	else {

		// derive positioning of drop menu
		if (anchor[0] == 'top') menuTop = launchPosition[1] - menuDims.height;
		else menuTop = launchPosition[1] + launchDims.height;
		if (anchor[1] == 'left') menuLeft = launchPosition[0];
		else menuLeft = launchPosition[0] + launchDims.width - menuDims.width;
	
		// position top menu
		menuElm.setStyle({top: menuTop+'px', left: menuLeft+'px'});
		if (menuElm.getWidth() < launchElm.getWidth()) menuElm.style.width = (launchElm.getWidth() - 2)+'px';
		
		// show the drop menu element
		new Effect.BlindDown(menuElm,{duration: 0.5});
		//if (browser.gecko) new Effect.Grow(menuElm,{direction: 'bottom-right', duration: 0.5});
		//else menuElm.style.display = 'block';
	}

};

function hideDropMenu(menuElm,evt,forceHide) {
	if (typeof menuElm == 'string') menuElm = $(menuElm);
	if (forceHide) menuElm.hide();
	else {
		Position.prepare();
		if (!Position.within(menuElm,Event.pointerX(evt),Event.pointerY(evt))) menuElm.hide();
	}
};