/*
 * This code will correct a problem with ie in mootools' '$$' selector function 
 * that creates a bunch of temporary attributes that should not be printed. 
 */
if (window.ie) $$.unique = function(array){
	var elements = [];
	for (var i = 0, l = array.length; i < l; i++){
		if (array[i].$included) continue;
		var element = $(array[i]);
		if (element && !element.$included){
			element.$included = true;
			elements.push(element);
		}
	}
	for (var n = 0, d = elements.length; n < d; n++) elements[n].removeAttribute('$included');
	return new Elements(elements);
};





function centerHiddenElement(elmnt)
{
	elmnt.setStyle('display', 'block').setOpacity(0.0);
	centerElement(elmnt);
	elmnt.setOpacity(1.0);
}

function centerElement(elmnt) {
	centerElementVertically(elmnt);
	centerElementHorizontally(elmnt);
}

function centerElementHorizontally(elmnt) {
	elmnt.style.position = 'absolute';
	var left = parseInt(getWindowScrollWidth() + (getWindowWidth() / 2) - (elmnt.scrollWidth / 2));
	left = left > 0 ? left : 0;
	elmnt.setStyle('left', left + 'px');
}

function centerElementVertically(elmnt) {
	elmnt.style.position = 'absolute';
	var top = parseInt(getWindowScrollHeight() + (getWindowHeight() / 2) - (elmnt.scrollHeight / 2));
	top = top > 0 ? top : 0;
	elmnt.style.top = top + 'px';
}

function getWindowHeight() { return window.getHeight(); }
function getWindowScrollHeight() { return window.getScrollTop(); }
function getWindowScrollWidth() { return window.getScrollLeft(); }
function getWindowWidth() { return window.getWidth(); }
function getWindowFullHeight() { return window.getScrollHeight(); }
function getWindowFullWidth() { return window.getScrollWidth(); }

function scroll_to(el) {
    var offsetTop = 0;
    while(typeof(el) != "undefined" && el != null) {
        offsetTop += el.offsetTop;
        el = el.offsetParent;
    }
    window.scrollTo(0, offsetTop);
}


function redirect(url) {
    //  This version does NOT cause an entry in the browser's
    //  page view history.  Most browsers will always retrieve
    //  the document from the web-server whether it is already
    //  in the browsers page-cache or not.
 
    window.location.replace( url );
}


/* 
 * make the specified div a windowed control in IE6
 * this masks an iframe (which is a windowed control) onto the div,
 * turning the div into a windowed control itself
 
 * !!!!!!!!!!  THIS FUNCTION IS BROKEN !!!!!!!!!!!!!!!
 * I couldn't get it to work and found a similier solution.
 * I use a single iframe to cover the entire page since I load
 * a shadow div to cover the entire page anyway.
 * This might be worth fixing another time
 
function makeWindowed(p_div) {
//	if (window.ie6)
//	{
		if(!p_div.getElement('iframe'))
		{
			var iframe = new Element('iframe', {
				'styles': {
					'position': 'absolute',
			        'display': 'block',
			        'z-index': 1,
			        'width': '100%',
			        'height': '100%',
			        'top': 0,
			        'left': 0,
			        'filter': 'mask()',
			        'background-color': '#fff',
			        'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
    			},
    			'src': 'javascript:"<html></html>";',
    			'scrolling': 'no',
    			'frameborder': 0
			});

			if(p_div) {
				iframe.injectBefore(p_div.getFirst());
			}
	       
			// force refresh of div
			var olddisplay = p_div.style.display;
			p_div.style.display = 'none';
			p_div.style.display = olddisplay;
		}
//	}
}*/



function addTooltips(obj)
{
	var Tooltips = new Tips(obj, {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
}

function appendError(error_string) {
	var obj = new Element('p');
	obj.appendText(error_string);
	$$('#error div.body').adopt(obj);
	openPopUp('error');
}
function showShadow() {
	var shadow = $('shadow');
	shadow.setOpacity(0.7);
	shadow.setStyle('display', 'block');
	shadow.setStyle('height', getWindowFullHeight());
	shadow.setStyle('width', getWindowFullWidth());
}
function hideShadow() {
	var shadow = $('shadow');
	shadow.setStyle('display', 'none');
}
function openPopUp(boxName) {
	showShadow();
	var box = $(boxName);
	centerHiddenElement(box);
}
function closePopUp(box) {
	hideShadow();
	closeAllWindows();
}
function closeAllWindows() {
	$$('.hoverBox').each(function(item){
		item.setStyle('display', 'none');
	});
}

function linkEmail(email) {
	var replace = {at: '@', dot: '.'};
	email.substitute(replace);
}


// Logins
function loginSite(){
	
		document.getElementById("formLogin").submit();
}









function login(form) {
	if(!$chk(form)) {
		$$('form.login').each( function(item) {
			item.setStyle('display', 'none');
		}); 
		$('loginSelect').setStyle('display', '');
	} else {
		var item = $(form);
		$('loginSelect').setStyle('display', 'none');
		item.setStyle('display', '');
	}
}

			
function checkWebmailLogin() {
	var form = $('webmaillogin');
	var user = $('email');
	var pass = $('pass');
	
	if(user.value.match(/@imperium.ca/)) {
		user.value = user.value.replace(/@imperium.ca/i, '');
		user.set('name', 'username');
		pass.set('name', 'password');
		form.set('action', "https://mail.imperium.ca/exchweb/bin/auth/owaauth.dll");
	}
	form.submit();
}

