/*--------------------------------------------------------------------------
 *  Prototype: an object-oriented Javascript library, version 1.2.1
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *
 *  THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff
 *  against the source tree, available from the Prototype darcs repository. 
 *
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *
 *  For details, see the Prototype web site: http://prototype.conio.net/
 *
/*--------------------------------------------------------------------------*/

//I hacked this actually, previous looked like utter rubbish
if (!Function.prototype.apply) {
  
  Function.prototype.apply = function(object, parameters) {
    var parameterStrings = "";
    if (!object)     object = window;
    if (!parameters) parameters = new Array();
    
    for (var i = 0; i < parameters.length; i++) {
    	parameterStrings += parameter[i];
		if (i != parameters.length) {
			 parameterStrings += ", ";
		}
	}
    
    object.__apply__ = this;
    var result = eval('object.__apply__(' + parameterStrings + ')');
    object.__apply__ = null;
    
    return result;
  }
}
 

Function.prototype.bind = function(object) {
  var method = this;
  return function() {
    method.apply(object, arguments);
  }
}

//mine
Function.prototype.bindWith = function(object, argArray) {
	var method = this;
	return function() {
		method.apply(object, argArray);
	}
}

Function.prototype.bindAsEventListener = function(object) {
  var method = this;
  return function(event) {
    method.call(object, event || window.event);
  }
}

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}





/*--------------------------------------------------
 *
 *  End of imported prototype stuff, it's all my stuff now!
 *
 *--------------------------------------------------*/ 


function BrowserImpl() {}
BrowserImpl.prototype.isWin = function() { return navigator.platform.indexOf("Win") != -1;};
BrowserImpl.prototype.isOpera = function() { return navigator.userAgent.indexOf("Opera") != -1;};
BrowserImpl.prototype.isIE = function() { return this.isWin() && navigator.appName.indexOf("Microsoft") != -1 && !this.isOpera();};
BrowserImpl.prototype.isGecko =  function() { return navigator.userAgent.indexOf("Gecko") != -1;};
BrowserImpl.prototype.isSafari =  function() { return navigator.appVersion.indexOf("AppleWebKit") != -1;};
BrowserImpl.prototype.needsPNGHack = function() {return this.isIE() && navigator.appVersion.indexOf("7") != -1;};

var Browser = new BrowserImpl();
 
var Assets = {
	blankGIF : "images/blank.gif"
};





/****************************************************************************************
*****************************************************************************************
*** Onload and initialization ***********************************************************
*****************************************************************************************
*****************************************************************************************/

function Runner() {
	this.runnables = new Array();
	this.priorityRunnables = new Array();
}
	
Runner.prototype.add = function(newFuncRef) {				
	this.runnables.push(newFuncRef);
};

Runner.prototype.addPriority = function(newFuncRef) {	
	this.priorityRunnables.push(newFuncRef);
};

Runner.prototype.addLater = function(newFuncRef, later) {
	this.add(
		function() {
			setTimeout(newFuncRef, later);
		}
	);
};

Runner.prototype.run = function() {	
	for (var i = 0; i < this.priorityRunnables.length; i++) {
		this.priorityRunnables[i]();
	}	
	for (var j = 0; j < this.runnables.length; j++) {
		this.runnables[j]();
	}	
};


var INITIALIZER = new Runner();
var FINALIZER = new Runner();

window.onload = INITIALIZER.run.bind(INITIALIZER);
window.onunload = FINALIZER.run.bind(FINALIZER);


/**********************************

	Core object extras

**********************************/

if (!Array.prototype.push) {
	Array.prototype.push = function(value) {
    	this[this.length] = value; 
	}
}

Array.prototype.getIndexOfValue = function(value) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == value) return i;
	}
	return -1;
}

String.prototype.trim = function() {
	if (this.length == 0) return this;
	
	var startAt = 0;
	var endAt  = this.length;

	while (this.charAt(startAt) == ' ' && startAt < endAt) {
		startAt++;
	}

	while (this.charAt(endAt-1) == ' ' && endAt > startAt) {
		endAt--;
	}

	return ((startAt > 0 || endAt < this.length)?this.substring(startAt, endAt):this);
}

String.prototype.endsWith = function(ref) {
	if (this.length == 0) return false;
	if (!ref || ref.length == 0) return false;
    if (this.length < ref.length) return false;
	if (this == ref) return true;//trivially

	return (this.substr(this.length - ref.length) == ref);
}



/**********************************

	Util

**********************************/

function EventUtilImpl() {}

EventUtilImpl.prototype.getEvent =  function(evt) {
	return (evt)? evt: ((event)? event: null);
};

EventUtilImpl.prototype.getEventSrc =  function(evt) {
	var elem = null;
	evt = this.getEvent(evt);
	if (evt) {
		elem = (evt.target? evt.target : ((evt.srcElement) ? evt.srcElement : null));
	}
	return elem;
};


EventUtilImpl.prototype.cancelEvent =  function(evt) {
	if (evt.preventDefault) {
		evt.preventDefault();		
	} 
	else if (evt.returnValue) {
		evt.returnValue = false;
	}
};

EventUtilImpl.prototype.cancelBubble =  function(evt) {
	if (evt.stopPropagation) {
		evt.stopPropagation();
	}
	else {
		evt.cancelBubble = true;
	}
};

EventUtilImpl.prototype.multicast =  function(funcA, funcB) {
	if (!funcA) return funcB;
	if (!funcB) return funcA;

	return function(evt) {		
		evt = (evt != null?evt:window.event);		
		funcA.call(this, evt);
		funcB.call(this, evt);
	};
};

//these two half-inched from prototype (renamed and modded slighlty)
EventUtilImpl.prototype.addListener =  function(elemRef, name, listener, useCapture) {    	
	useCapture = useCapture || false;

	if (name == 'keypress') {
		if (Browser.isSafari()) {
			elemRef.addEventListener('keydown', listener, useCapture);
			return;
		}

		if (elemRef.addEventListener) {
			elemRef.addEventListener('keypress', listener, useCapture);
		} else if (elemRef.attachEvent) {
			elemRef.attachEvent('onkeydown', listener);
		}
	} else {
		if (elemRef.addEventListener) {
			elemRef.addEventListener(name, listener, useCapture);
		} else if (elemRef.attachEvent) {
			elemRef.attachEvent('on' + name, listener);
		}
	}
};

EventUtilImpl.prototype.removeListener =  function(elemRef, name, listener, useCapture) {		
	useCapture = useCapture || false;

	if (name == 'keypress') {
		if (Browser.isSafari()) {
			elemRef.removeEventListener('keydown', listener, useCapture);
			return;
		}
		if (elemRef.removeEventListener) {
			elemRef.removeEventListener('keypress', listener, useCapture);
		} else if (elemRef.detachEvent) {
			elemRef.detachEvent('onkeydown', listener);
		}
	} else {
		if (elemRef.removeEventListener) {
			elemRef.removeEventListener(name, listener, useCapture);
		} else if (element.detachEvent) {
			elemRef.detachEvent('on' + name, listener);
		}
	}
};

var EventUtil = new EventUtilImpl();


function UtilImpl() {}

UtilImpl.prototype.fromSomething = function(something, value) {
	var index = value.indexOf(something);
	if (index == -1) return value;

	return value.substring(0, index) + value.substring(index+something.length, value.length);
};

UtilImpl.prototype.toSomething = function(something, value) {
	return value + something;	
};

UtilImpl.prototype.fromSmall = function(value) {
	return this.fromSomething("_small", value);
};

UtilImpl.prototype.fromPX = function(value) {
	//Debug.print("fromPX doing " + value);
	var index = value.indexOf("px");
	if (index == -1) return value;

	return value.substring(0, index);
};

UtilImpl.prototype.toPX =  function(value) {
	return value + "px";
};

var Util = new UtilImpl();

/*function setClassSelected(elemRef, className) {
	replaceClass(elemRef, className, toSomething("Selected", className));
}

function setClassUnSelected(elemRef, className) {
	replaceClass(elemRef, toSomething("Selected", className), className);
}*/

document.getElementsByTagAndClass = function(tag, className) {
	var elems = this.getElementsByTagName(tag);
	var elemsOut = new Array();
	for (var i=0; i < elems.length; i++) {
		if (Element.isClass(elems[i], className)) {
			elemsOut.push(elems[i]);
		}
	}
	return elemsOut;
}

document.getDivByClass = function(className) {
	var divs = this.getElementsByTagName("DIV");
	var divOut = null;
	for (var i = 0; i < divs.length; i++) {
		if (Element.isClass(divs[i], className)) {
			divOut = divs[i];
			break;
		}
	}
	return divOut;
}

function findFirstTextChild(elemRef) {	
	return findChildByTag(elemRef, "#text");
}

function findChildByTag(elemRef, tagName) {
	if (elemRef && elemRef.childNodes.length != 0) {
		var nodes = elemRef.childNodes;
		for (var i=0; i < nodes.length; i++) {
			if (nodes[i].nodeName && nodes[i].nodeName == tagName) {
				return nodes[i];
			}
		}
	}
	return null;
}

function findChildByClass(elemRef, cName) {
	if (elemRef && elemRef.childNodes.length != 0) {
		var nodes = elemRef.childNodes;
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			if (nodes[i].className && node.className == cName) {
				return node;
			}
		}
	}
	return null;
}


function ElementImpl() {
	this.counter = 0;
	this.counterPrefix = "elem";
}

	ElementImpl.prototype.getNextId = function() {
		return this.counterPrefix + this.counter++;
	}

	ElementImpl.prototype.getElemId = function(elem) {
		if (elem.id == null || elem.id == "") {
			elem.id = this.getNextId();
		}
		return elem.id;
	}	

	ElementImpl.prototype.isClass = function(elemRef, className) {
		return (elemRef.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
	}

	ElementImpl.prototype.replaceClass = function(elemRef, oldClass, newClass) {
		var fullClass = elemRef.className;
		var pos = fullClass.indexOf(oldClass);
		if (pos == -1) return;

		var newFullClass = 
			fullClass.substring(0, pos) +
			newClass +
			fullClass.substr(pos + oldClass.length);

		elemRef.className = newFullClass;
	}

	ElementImpl.prototype.setClassTo = function(elemRef, something) {
		this.replaceClass(elemRef, elemRef.className, Util.toSomething(something, elemRef.className));
	}

	ElementImpl.prototype.setClassFrom = function(elemRef, something) {
		this.replaceClass(elemRef, elemRef.className, Util.fromSomething(something, elemRef.className));
	}

	ElementImpl.prototype.setOpacity = function(elemRef, percentOpacity) {
		if (Browser.isIE()) {
			elemRef.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + percentOpacity + ",style=0)";
		//} else if (isGecko) {
		} else if (elemRef.style.opacity) {
			elemRef.style.opacity = percentOpacity/100;
		//Safari < 1.2 && Konqueror
		} else if (elemRef.style.KHTMLOpacity) {			
			elemRef.style.KHTMLOpacity = percentOpacity/100;
		//old mozilla and firefox
		} else if (elemRef.style.MozOpacity) {
			elemRef.style.MozOpacity = percentOpacity/100;
		} else {
			//need to add more browser support, if none then round to visible or not, PNG -> GIF stylee			
			elemRef.style.visibility = percentOpacity > 50?"visible":"hidden";			
		}	
	}

var Element = new ElementImpl();


/************************
*** Text area clearer ***
************************/

function clearTextArea(elemId) { 	
	var elem = $(elemId);	
	if (elem.value != null && 
		elem.value.length == 1 &&
		(
			elem.value.search(/^\s+$/) != -1 || 
			elem.value.charCodeAt(0) == 160
		)				  //Regexp in IE fails
	) {			
		elem.value = "";		
	}
}

function findTextAreas() {
	var tas = document.getElementsByTagName("TEXTAREA");
	for (var i=0; i < tas.length; i++) {
		var e = tas[i];
		var id = Element.getElemId(e);
		var f = clearTextArea.bindWith(null, [id]);
        EventUtil.addListener(e, "focus", f);
		EventUtil.addListener(e, "click", f);
	}
}




/****************************************************************************************
*** Timer controlled menus **************************************************************
*****************************************************************************************/


function MenuImpl() {
		this.STYLE_DELAY =  1000;
		this.selectors = new Array();	}
MenuImpl.prototype.createSelector =  function(tag, className, something) {
	this.selectors.push(new MenuSelector(tag, className, something));
}

MenuImpl.prototype.clearSelectors = function() {			
	//Debug.print("Menu.clearSelectors");
	for (var i =0; i < this.selectors.length; i++) {
		this.selectors[i].runStyleSetter();
	}
}

MenuImpl.prototype.clearSelectorsExcept = function(current) {
	for (var i=0; i < this.selectors.length; i++) {
		if (this.selectors[i] == current) continue;
		this.selectors[i].runStyleSetter();
	}
}	


var Menu = new MenuImpl();

EventUtil.addListener(window, "click", Menu.clearSelectors.bind(Menu));

function MenuSelector(tag, className, something) {
	this.tag = tag;
	this.className = className;
	this.something = something;
	this.currentElemId = null;		
	this.currentTimerRef = null;


	var elems = document.getElementsByTagAndClass(tag, className);
	for (var i=0; i < elems.length; i++) {
		var elem = elems[i];

		if (elem.id == null || elem.id == "") {
			elem.id = Element.getNextId();
		}

		EventUtil.addListener(elem, "mouseover" , this.setOver.bindWith(this, [elem.id]));
		EventUtil.addListener(elem, "mouseout" ,this.setOut.bindWith(this, [elem.id]));			
		//init
		//elem.className = this.className;
	}


}

MenuSelector.prototype.runStyleSetter = function() {				
	if (this.currentElemId != null) {
		elemRef = $(this.currentElemId);
		//Debug.print("runStyleSetter for id: " + elemRef.id + " and class: " + this.className);
		elemRef.className = this.className;
		this.currentElemId = null;
		this._clearTimerRef();
	}		
}

MenuSelector.prototype._clearTimerRef = function() {
	if (this.currentTimerRef != 0) {
		//Debug.print("clear timeout");
		clearTimeout(this.currentTimerRef);
		this.currentTimerRef = 0;
	}
}

MenuSelector.prototype.setOver = function(elemId) {
	//var elemRef = EventUtil.getEventSrc(evt);		
	var elemRef = $(elemId);
	//Debug.print("-----");
	//Debug.print("setOver : " + elemRef.id + " with style " + this.className + this.something);
	//always clear any timers
	//Menu.clearSelectorsExcept(this);
	this._clearTimerRef();
	//clears out the old one, it is exists.
	//if old one is null, or different element then we do it.
	if (!this.currentElemId || this.currentElemId !== elemId) {
		//Debug.print("no currentElem(" + (!this.currentElemId) + ") or current and elemRef different");
		//do old one
		this.runStyleSetter();			
		elemRef.className = this.className + this.something;
	} else {
		//Debug.print("currentElem that is the same");
	}
}

MenuSelector.prototype.setOut = function(elemId) {
	//var elemRef = EventUtil.getEventSrc(evt);
	var elemRef = $(elemId);
	//Debug.print("setOut : " + elemRef.id + " with style " + this.className);
	this.currentElemId = elemId;				
	//Debug.print("Set timeout");
	this.currentTimerRef = setTimeout(this.runStyleSetter.bind(this), Menu.STYLE_DELAY);
}


INITIALIZER.add(						 
		function() {			
			Menu.createSelector("div", "mainMenu", "Selected");
			Menu.createSelector("li", "subMenuItem", "Selected");
			findTextAreas();
	});


