var onloadEvents = new Array();

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() { 
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}
sfHover = function() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {

		var className = this.className;
		if(className == 'first' || className == 'last' || className == '') continue;
	
		sfEls[i].onmouseover=function() {
			var className = this.className;
			if(className.indexOf(' ') != -1) {
				var className = className.substr(0, className.indexOf(' '));
			}
			this.className+=" "+className+"-sfhover";
			hideSelects(this, true);
		}
		sfEls[i].onmouseout=function() {
			var className = this.className;
		
			if(className.indexOf(' ') != false) {
				var className = className.substr(0, className.indexOf(' '));
			}
			this.className=this.className.replace(new RegExp(" "+className+"-sfhover\\b"), "");
			hideSelects(this, false);
		}
	}
}

function hideSelects(element, hide) {
	if(hide) {
		visibility = 'hidden';
	} else {
		visibility = 'visible';
	}
	// are we on the home page.

	if(document.getElementById('home')) {
		var selects = document.getElementsByTagName('select');
		for(var i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	}
	return;
	var shimID = 'shim-'+element.className.substr(4);
	//var selects = document.getElementsByTagName('select');
	//for(var i = 0; i < selects.length; i++) {
		if(!hide) {
			
			if(document.getElementById(shimID) == null) {
				var tmp = document.createElement('iframe');
				tmp.id = shimID;
				tmp.style.background = 'red';
				tmp.style.position = 'absolute';
				tmp.style.top = '30px';
				tmp.style.left = '0';
				tmp.style.width = '310px';
				tmp.style.zIndex = '9998';
				tmp.style.filter = 'progidXImageTransform.Microsoft.Alpha(style=0,op acity=0)';
				tmp.frameBorder = 0;
				
				element.appendChild(tmp);
				
				/*var iframeShim = new Element('div', {
					id: 'shim',
					src: 'javascript: false; document.write("");',
					frameBorder: 0,
					style: {
						'z-index': 9998
					}
				});*/
			} else {
				document.getElementById(shimID).style.display = 'block';
			}
		} else {
			window.status = document.getElementById(shimID);
			if(document.getElementById(shimID) != null) {
				//alert('asdf');
				document.getElementById(shimID).style.display = 'none';
			}
		}

		
		//selects[i].style.visibility = visibility;
	//}
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

// only attach for ie
if (window.attachEvent) window.attachEvent("onload", sfHover);
//uncomment these lines to activate whatever you need
//addToOnload('pageControl.init()');
//addToOnload('searchBar.init()');

//addToOnload('setupZoom()');
//addToOnload('tableControl.init()');


function processKeyPress(e){
	var e = window.event  || e;
	
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
	
}

function closePopup() {
	e = document.getElementById('map-display');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function popupKeyListener() {document.onkeypress = processKeyPress; }

function getWindowDimensions(){
	
	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;
	}
	return [myWidth, myHeight];
}

allHeadingsForMootools = function() {
	return (
			'h1' == this.get('tag') ||
			'h2' == this.get('tag') ||
			'h3' == this.get('tag') ||
			'h4' == this.get('tag') ||
			'h5' == this.get('tag') ||
			'h6' == this.get('tag')
	);
};

addToOnload(function() {
	if (Selectors) {
		Selectors.Pseudo.heading = allHeadingsForMootools;
	}
});


function spamCheckInit(){
    $('spam-tip').setStyle('display', 'none');
    $('spam-check-hint').addEvent('click', function(e){
	if ( $('spam-tip').getStyle('display') == 'block') {
	    $('spam-tip').setStyle('display', 'none');
	    $('spam-check-hint').innerHTML = 'Why';
	} else {
	    $('spam-tip').setStyle('display', 'block');
	    $('spam-check-hint').innerHTML = 'Hide';
	}

    });
}


var BoxTools = {
	postingFeedback : function(element, state, noRefresh){
		if (state == 'processing'){
			BoxTools.displayBusySignal(element);
		} else if (state == 'ready'){
			BoxTools.clearBusySignal(element, noRefresh);
		}
	},
	displayBusySignal: function(element){
		var pNode = $(element);
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';

		var containerWidth = pNode.offsetWidth-4;
		var containerHeight = pNode.offsetHeight;

		var busySignal = document.getElementById(idPrefix+'busySignal');
		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');

		if (!busySignal){
			var inputs = pNode.getElements('input');
			inputs.each(function(i) {
			    /*
				if (i.get('type') == 'text' || i.get('type') == 'password'){
					i.setStyle('background', '#F4F5F5');
					i.setStyle('border', '#F4F5F5 1px solid');
				}
			    */
			   if (i.get('type') == 'text' || i.get('type') == 'password'){
				i.addClass('busyStatus');
			   }
			});
			var textareas = pNode.getElements('textarea');
			textareas.each(function(i) {
			    /*
				i.setStyle('background', '#F4F5F5');
				i.setStyle('border', '#F4F5F5 1px solid');
			    */
			   i.addClass('busyStatus');
			});

			var busySignalContainer = document.createElement('div');
			busySignalContainer.id = idPrefix+'busySignalContainer';
			busySignalContainer.className = 'busySignalContainer';

			var busySignal = document.createElement('div');
			busySignal.id = idPrefix+"busySignal";
			busySignal.className = "busySignal";
			busySignal.style.marginLeft = '2px';

			pNode.parentNode.insertBefore(busySignalContainer, pNode);

			//Get the graph container and append the graph to it
			var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
			busySignalContainer.appendChild(busySignal);
		}

		try {
			busySignal.style.filter = 'alpha(opacity=50)';
			busySignal.style.height = containerHeight + "px";
			busySignal.style.width = containerWidth+'px';
		} catch(e) {
			// do nothing
		}
	},
	clearBusySignal: function (element, noRefresh){
		var pNode = $(element);
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';

		var inputs = pNode.getElements('input');
		inputs.each(function(i) {
			if (i.get('type') == 'text' || i.get('type') == 'password'){
				i.removeClass('busyStatus');
			}
		});
		var textareas = pNode.getElements('textarea');
		textareas.each(function(i) {
			i.removeClass('busyStatus');
		});

		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
		if (busySignalContainer){
			var busySignal = document.getElementById(idPrefix+'busySignal');
			if (busySignal){
				busySignal.parentNode.removeChild(busySignal);
			}
			busySignalContainer.parentNode.removeChild(busySignalContainer);
		}
	},

	attachMovement: function (direction, nodeDepth){
		//Get Id of Containing Div
		//a => span => div

		var nodeDepth = nodeDepth || 1;

		var thisItem = $(this);

		for (var i = 1; i < nodeDepth; i ++	){
			thisItem = thisItem.getParent();
		}
		try {
			var prevItem = thisItem.getPrevious() || null;
			var nextItem = thisItem.getNext() || null;
		} catch(e) {
			var prevItem = null;
			var nextItem = null;
		}

		//parentElement.insertBefore(newElement, referenceElement)
		if (direction == 'down'){
			if (nextItem != null){
				BoxTools.attachAfter(thisItem, nextItem);
			}
		}

		if (direction == 'up'){
			if (prevItem != null){
				BoxTools.attachBefore(thisItem, prevItem);
			}
		}
		return false;
	},
	attachRemove: function(nodeDepth){
		var thisItem = this;
		for (var i = 1; i < nodeDepth; i ++	){
			thisItem = thisItem.parentNode;
		}
		//Remove everything inside
		while (thisItem.firstChild)
		{
			thisItem.removeChild(thisItem.firstChild);
		}
		//Remove the div now
		thisItem.parentNode.removeChild(thisItem);

	},
	attachAfter: function(newElement, referenceElement){
		var pNode = referenceElement.parentNode;
		pNode.insertBefore(newElement, referenceElement.nextSibling);
		AssetManager.moveAsset(referenceElement.id, newElement.id);
		return false;
	},
	attachBefore: function(newElement, referenceElement){
		var pNode = referenceElement.parentNode;
		var refId = referenceElement.id;
		if (refId){
			var idBits = refId.split('_');
			if (idBits[0] == 'LOCKED'){
				return false;
			}
		}
		pNode.insertBefore(newElement, referenceElement);
		AssetManager.moveAsset(newElement.id, referenceElement.id);
		return false;
	}
}


var Message = {
	init : function(){
		Message.getMessage();
	},
	addMessage : function(msg, className){
		var messageContainer = $('message-container');
		if (!messageContainer){
			return;
		}
		var ifMesgExists = false;
		var msgDivs = messageContainer.getElements('div');

		msgDivs.each(function(i){
			if (i.className == className){
				i.set('html', msg);
				ifMesgExists = true;
			}
		});
		if (!ifMesgExists ){
			var messageDiv = document.createElement('div');
			messageDiv.className = className;
			//messageDiv.set('html',msg);
			messageContainer.appendChild(messageDiv);
			messageDiv.innerHTML = msg;
		}
		//Message.getMessage();
	},
	getMessage : function(){
		var messageContainer = $('message-container');
		if (!messageContainer){
			return;
		}
		messageContainer.setStyle('opacity', 1);
		//messageContainer.innerHTML = responseText;
		setTimeout('Message.startMessageContainer()', 800);
	},
	startMessageContainer : function() {
		var messageContainer = $('message-container');
		if(messageContainer) {
			var fx = new Fx.Morph(messageContainer, {duration: 10000, transition: Fx.Transitions.Sine.easeOut});
			fx.start({'opacity' : 0});
			setTimeout('Message.checkMessageContainer()', 2000);
		}
	},
	checkMessageContainer : function() {
		var messageContainer = $('message-container');
		if(messageContainer) {
			var myFx = new Fx.Slide(messageContainer, {
				onComplete: function(){
					messageContainer.dispose();
				}
			}).slideOut();
		}
	}
}

/**
 * @param {Object} field_name - the fieldname to update
 * @param {Object} url - the url this asset browser is opening
 * @param {string} className - the name of asset
 * @param {int} width - the width of jsWindow
 * @param {int} height - the height of jsWindow
 * @param {string} scrolling - scroll statua of jsWindow
 * @param {bool} addBar - bar status of jsWindow
 */
function customAssetBrowser(field_name, url, className, width, height, scrolling, addBar){
	if (JsWindow && AssetManager){
		JsWindow.open({src : url, width: width, height: height, scrolling: scrolling, addBar : addBar});
		AssetManager.assetField = field_name;
		AssetManager.assetClass = className;
	}
	return false;
}

function resizeIframe() {
	var frame = $$('iframe')[0];
	var frameSize = frame.getSize();
	var frameHeight = frameSize.y;
	var finalFrameHeight = frameHeight + 30;
	frame.set('height', finalFrameHeight + 'px');
	SqueezeBox.resize({x:frameSize.x,y:finalFrameHeight },true);
}
