/*
 * Site Constants  
 */
 menuTimeout = 250;
 IMG_ROOT = '/images/';
 IMG_SHARE = '/images/'
 var PAGE_ID = '';
 var MODEL_ID = '';
 var PAGE_LOAD = false;
/**
 * Function when called will find all Image tags and cache all images and any Over states that follows a given naming convention.
 * Such as xxxxxx.gif and xxxxxxOver.gif 
 * @type void
 */
 
DebugOnloadEventTiming = Class.create();
Object.extend(DebugOnloadEventTiming.prototype, {
	initialize: function() {
		this.startTime;
		this.endTime;
	},
	startTime: function() {
		this.startTime = new Date();
	},
	endTime: function() {
		this.endTime = new Date();
	},
	getElapseTime: function() {
		return this.endTime.getTime() - this.startTime.getTime();
	},
	printResults: function() {
		Debug.debug('onLoad Events Start Time:  ' + this.startTime.getDay() + '/' + this.startTime.getMonth() + '/' + this.startTime.getYear()
									   + ' ' + this.startTime.getHours() + ':' + this.startTime.getMinutes() + ':' + this.startTime.getSeconds()+ ':' + this.startTime.getMilliseconds() );
									   
		Debug.debug('onLoad Events End Time:  ' + this.endTime.getDay() + '/' + this.endTime.getMonth() + '/' + this.endTime.getYear()
									   + ' ' + this.endTime.getHours() + ':' + this.endTime.getMinutes() + ':' + this.endTime.getSeconds()+ ':' + this.endTime.getMilliseconds() );								   

		Debug.debug('onLoad Events Total Elapse Time:  ' + this.getElapseTime() + '(' + (this.getElapseTime()/1000) + ' seconds)');		

	}
});
var debugOnloadEventTiming = new DebugOnloadEventTiming();
 
OnPageLoadEvents = Class.create();
Object.extend(OnPageLoadEvents.prototype, {
	initialize: function(){
		this.actions = new Array();
		Event.observe(window, 'load', function(){this._executeActions()}.bind(this), false);
	},
	addAction: function(func) {
		
		this.actions.push(func);
	}, 
	_executeActions: function() {
		for(var i=0; i<this.actions.length; i++) {
			var action = this.actions[i];
			action.apply(window,[]);
		}
	}	
});
var onPageLoadEvents = new OnPageLoadEvents();

onPageLoadEvents.addAction(function() {debugOnloadEventTiming.startTime()});
onPageLoadEvents.addAction(function() {PAGE_LOAD = true;});




function debugOnloadStartTime() {
	var startTime
	
}

function cacheAllImages() {
	var cacheit = function(image, tag){
		if(image.src.indexOf(tag) == -1){
			var img = new Image();
			img.src = image.src.replace(new RegExp('(.*?)(\.(gif|png|jpg))', 'i'), '$1'+tag+'$2');
		}
	}
	$NL(document.getElementsByClassName('hover_Ov', document, 'img')).each(function(element){ 
		cacheit(element, 'Ov'); 
		Event.observe(element, 'mouseover', function(){swapOn(element, 'Ov'); });
		Event.observe(element, 'mouseout', function(){swapOut(element, 'Ov'); });
	});
	$NL(document.getElementsByClassName('hover_Over', document, 'img')).each(function(element){
		cacheit(element, 'Over'); 
		Event.observe(element, 'mouseover', function(){swapOn(element, 'Over'); });
		Event.observe(element, 'mouseout', function(){swapOut(element, 'Over'); });
	});
	$NL(document.getElementsByClassName('hover_On', document, 'img')).each(function(element){ cacheit(element, 'On') });
}
onPageLoadEvents.addAction(cacheAllImages);
//vent.observe(window, 'load', cacheAllImages, false);
/**
 * Function can be used to swap states of any image that follows a given naming convention. 
 * Such as xxxxxx.gif (off state) and xxxxxxOver.gif (on state).
 * You can use the function on an image itself like so:  
 * &gt;img src="./images/vn_btnAll.gif" onmouseOver="swapOver(this);" onmouseOut="swapOver(this);" &lt;
 * or you can add it to any element, like say, an A Href, like so:
 *  &lt;a href="./goSomeWhere.html" onmouseOver="swapOver('imageId');" onmouseOut="swapOver('imageId');" &lt; &gt;img src="..../" id="imageId" &lt; &gt;a&lt;
 * @param img String/Object
 * @type void
 */
function swapOver(img, suffix) {
	if(!suffix) { suffix = 'Over'; }
	var img = $(img);
	if(img.src.indexOf(suffix) > -1 || img.src == img.getAttribute('hover_src')) {
		if(img.getAttribute('nohover_src'))
			img.src = img.getAttribute('nohover_src');
		else
			img.src = img.src.replace(suffix, '');
	}
	else {
		if(img.getAttribute('hover_src')){
			img.setAttribute('nohover_src', img.src);
			img.src = img.getAttribute('hover_src');
		}
		else{
			var array = img.src.split('.');
			var src = "";		
			// Loop through all but the last
			for(var i=0; i<(array.length - 1); i++) {
				if(i!=0)
				 src += '.';
				src += array[i];
			}
			img.src = src + suffix + '.' + array[array.length-1];
		}
	}
}
function prepPNGforSwapOver(img, suffix){
	if(!suffix) { suffix = 'Over'; }
	var array = img.src.split('.');
	var src = "";		
	// Loop through all but the last
	for(var i=0; i<(array.length - 1); i++) {
		if(i!=0)
		 src += '.';
		src += array[i];
	}
	hoverSrc = src + suffix + '.' + array[array.length-1];
	img.setAttribute('nohover_src', img.src);
	img.setAttribute('hover_src', hoverSrc);
	Dom.IEPNG(img);
	//alert(img.src);
}
function swapOn(img, suffix) {
	if(!suffix) { suffix = 'Over'; }
	var img = $(img);
	if(img.src.indexOf(suffix) > -1) {
		// Do nothing.
	}
	else if(img.getAttribute('hover_src')){
		img.src = img.getAttribute('hover_src');
	}
	else {
		var array = img.src.split('.');
		var src = "";		
		// Loop through all but the last
		for(var i=0; i<(array.length - 1); i++) {
			if(i!=0)
			 src += '.';
			src += array[i];
		}
		img.src = src + suffix + '.' + array[array.length-1];
	}
}

function swapOut(img, suffix) {
	if(!suffix) { suffix = 'Over'; }
	var img = $(img);
	if(img.src.indexOf(suffix) > -1 || img.src == img.getAttribute('hover_src')) {
		if(img.getAttribute('nohover_src'))
			img.src = img.getAttribute('nohover_src');
		else
			img.src = img.src.replace(suffix, '');
	}
}

function swapOut_click(event, img, suffix){
	if(!suffix) { suffix = 'Over'; }
	var img = $(img);
	if(img.src.indexOf(suffix) > -1) {
		if(!Dom.isInBounds(img, Event.pointerX(event), Event.pointerY(event))){
			img.src = img.src.replace(suffix, '');
		}
	}

}

function swapClass(domObject, classStr){
	if(!domObject) return;
	domObject.className = classStr;
}

Object.extend(Element, {
	removeChildren: function(element){
		while(element.childNodes.length){
			Element.remove(element.childNodes[0]);
		}
	}
});

/**
*	Replaces ~search_string~ with replace_string in input_string.
*	Returns result.
*/

Object.extend(String.prototype, {
	templateReplace: function(search_string, replace_string){
		//var regEx = new RegExp('~' + search_string + '~', 'g')
		var output = this.replace('~' + search_string + '~', replace_string);

		return output;
	}
});

CursorPosition = Class.create();
Object.extend(CursorPosition.prototype, {
	initialize: function() {
		this.currentX;
		this.currentY;
	},
	setXY: function (e) {
	  this.currentX = (!document.all) ? e.pageX : window.event.clientX;
	  this.currentY = (!document.all) ? e.pageY : window.event.clientY;
	},
	getX: function(){
		return this.currentX;
	},
	getY: function(){
		return this.currentY;
	},
	initXY: function(){
		Event.observe(document, 'mousemove', function(event){
			cursorPosition.setXY(event);
		}.bindAsEventListener(this));
	}
});
var cursorPosition = new CursorPosition();
onPageLoadEvents.addAction(cursorPosition.initXY);
