function addHandler(whichEvent, whichObject, attachThis)
{
	if(document.addEventListener)
	{
		whichObject.addEventListener(whichEvent, attachThis, 0);
	}
	else if(document.attachEvent)
	{
		whichObject.attachEvent("on"+whichEvent, attachThis);
	}
}

var MN = {
	
	init:function() 
	{
		if(!document.getElementById || !document.getElementsByTagName)
		{
			return;
    	}
    	
    	MN.siteSearch();
    	MN.newWindow();	
	},
	
	loadPopup:function(link, elemId, addOverlay)
	{
		var popup = document.createElement("div");
		popup.id  = elemId;
	
		var iFrame    = document.createElement("iframe");
		iFrame.id     = "MN_iframe";
		iFrame.name   = "MN_iframe";
		iFrame.width  = '100%';
		iFrame.height = '100%';
		iFrame.src    = link;
		
		document.body.insertBefore(popup, document.body.firstChild);
		
		popup.appendChild(iFrame);
		
		//if(window.frames['MN_iframe'].document.getElementById('popcloses'))
		//{
		//	alert('s');
		//}
	
		//center the window
		var popupWidth  = -(popup.offsetWidth / 2);
		var popupHeight = -(popup.offsetHeight / 2);
		popup.style.marginLeft = popupWidth  + 'px';
		popup.style.marginTop  = popupHeight + 'px';
		
		if(addOverlay == true)
		{
			MN.overlay();
		}
	},

	unloadPopup:function(elemId)
	{
		while(document.getElementById(elemId))
		{
			document.body.removeChild(document.body.firstChild);
		}
	},
	
	overlay:function()
	{
		var body = document.body;
		var div  = document.createElement("div");
		div.id   = "MN_overlay";
		
		body.insertBefore(div, body.firstChild);
	
		div.style.position        = 'absolute';
		div.style.backgroundColor = '#000000';
		div.style.opacity         = '0.50';
		div.style.top             = '0px';
		div.style.left            = '0px';
		div.style.height          = '100%';
		div.style.width           = '100%';
		div.style.zIndex          = 999;
		
		if(body.scrollTop)
		{
			div.style.height = body.offsetHeight + body.scrollTop  + 'px';
		}
		
		/*window.onscroll = function()
		{
			if(body.scrollTop)
			{
				overlayDiv.style.height= body.offsetHeight + body.scrollTop + 'px';
			}
			else {
				overlayDiv.style.height= '100%';
			}
		}*/
	},
	
	siteSearch:function() 
	{	
		if(document.getElementById('search_text'))
		{
			var search;
			
			search = document.getElementById('search_text');
			
			search.onfocus = function() 
			{
				if(search.value == 'Search:')
				{
					search.value = '';
				}
			}	
		
			if(search.nextSibling)
			{
				search.nextSibling.onclick = function() 
				{
					if(search.value == 'Search:' || search.value == '')
					{
						alert('Please enter a search query');
						return false;
					}
					
					search.parentNode.submit();
				}
			}
		}
	},
	
	newsflash:function()
	{
		if(document.getElementById('newsflash'))
		{
			var content = document.getElementById('newsflash');
			var divs    = content.getElementsByTagName('div');
		
			for (var i = 0; i < divs.length; i++) 
			{
				divs[i].style.display = 'none';
			}
			
			divs[0].style.display = 'block';
			MN.fade('newsflash', 0,100, 1000);
			setTimeout('MN.fade(\'newsflash\', 100,0, 1000);',7000);
			
			var u = content.firstChild;		
			content.insertBefore(u, content.lastChild.nextSibling);
			setTimeout('MN.newsflash();',8000);
		}
	},
	
	fade:function(eID, startOpacity, stopOpacity, duration) 
	{
    	var speed = Math.round(duration / 100);
    	var timer = 0;
    	if (startOpacity < stopOpacity)
    	{ // fade in
        	for (var i=startOpacity; i<=stopOpacity; i++) 
        	{
            	setTimeout("MN.setOpacity('"+eID+"',"+i+")", timer * speed);
            	timer++;
        	} return;
    	}
    		for (var i=startOpacity; i>=stopOpacity; i--) 
    		{ // fade out
        		setTimeout("MN.setOpacity('"+eID+"',"+i+")", timer * speed);
        		timer++;
    		}
	},
	
	setOpacity:function(eID, opacityLevel) 
	{
    	var eStyle = document.getElementById(eID).style;
    	eStyle.opacity = opacityLevel / 100;
    	eStyle.filter = 'alpha(opacity='+opacityLevel+')';
	},
	
	newWindow:function()
	{
		if(document.getElementById('newwindow'))
		{
			var param  = document.getElementById('newwindow').className.split('_');
			var scroll = (param[2] == 'scroll') ? ',scrollbars=YES' : '';
			
			document.getElementById('newwindow').onclick = function()
			{
				window.open(document.getElementById("newwindow").href,'','width=' + param[0] + ',height=' + param[1] + scroll);
				
				return false;
			}
		}
	}
}

addHandler('load', window, MN.init);
