var MN = {
	
	init:function() 
	{
		if(!document.getElementById || !document.getElementsByTagName)
		{
			return;
    	}
    	
    	MN.printFriendlyPage();
		MN.printFriendlyPageTwo();	
    	MN.mailingListForm();
    	MN.newsflash();
    	MN.telephone();
    	MN.loadVideo();
    	MN.loadUTVideo();
	},
	
	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);
	
		//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.filter          = 'alpha(opacity=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%';
			}
		}*/
	},
	
	printFriendlyPage:function()
	{
		if(document.getElementById('print_page'))
		{
			var dimensions = document.getElementById('print_page').className.split('_');
			var scroll 	   = (dimensions[2] == 'scroll') ? ',scrollbars=YES' : '';
			
			document.getElementById('print_page').onclick = function()
			{
				window.open(document.getElementById("print_page").href,'','width=' + dimensions[0] + ',height=' + dimensions[1] + scroll);
				
				return false;
			}
		}
	},

	printFriendlyPageTwo:function()
	{
		if(document.getElementById('print_page_two'))
		{
			var dimensions = document.getElementById('print_page_two').className.split('_');
			var scroll 	   = (dimensions[2] == 'scroll') ? ',scrollbars=YES' : '';
			
			document.getElementById('print_page_two').onclick = function()
			{
				window.open(document.getElementById("print_page_two").href,'','width=' + dimensions[0] + ',height=' + dimensions[1] + scroll);
				
				return false;
			}
		}
	},
	
	mailingListForm:function() 
	{
		var name;
		var email;
		
		if(document.getElementById('mailingListName'))
		{
			name = document.getElementById('mailingListName');
			
			name.onfocus = function() 
			{
				if(name.value == 'Name:')
				{
					name.value = '';
				}
			}
		}
		
		if(document.getElementById('mailingListEmail'))
		{
			email = document.getElementById('mailingListEmail');
			
			email.onfocus = function() 
			{
				if(email.value == 'Email:')
				{
					email.value = '';
				}
			}
		}
		
		if(document.getElementById('mailingListButton'))
		{
			document.getElementById('mailingListButton').onclick = function() 
			{
				if(name.value == '' || name.value == 'Name:')
				{
					alert('Please enter a valid name');
					return false;
				}
				
				if(email.value.indexOf('@') == -1 || email.value.indexOf('.') == -1)
				{
					alert('Please enter a valid email');
					return false;
				}
				
				document.getElementById('mailingListForm').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+')';
	},
	
	telephone:function()
	{
		if(document.getElementById('telno'))
		{
			var content = document.getElementById('telno');
			
			MN.fade('telno', 0,100, 500);
			setTimeout('MN.fade(\'telno\', 100,0, 500);',3000);
			
			setTimeout('MN.telephone();',3500);
		}
	},
	
	loadVideo:function()
	{
		if(document.getElementById('menu_item_50'))
		{
			var vidmenu = document.getElementById('menu_item_50');
			var link    = vidmenu.getElementsByTagName('a');
			
			for (var i = 0; i < link.length; i++) 
			{
				link[i].onclick = function() 
				{
					MN.loadPopup(this.href,'timberland_video',true);
					
					return false;
				}
			}
		}
	},
	
	loadUTVideo:function()
	{
		if(document.getElementById('ut'))
		{
			var vidmenu = document.getElementById('ut');
			var link    = vidmenu.getElementsByTagName('a');
			
			for (var i = 0; i < link.length; i++) 
			{
				link[i].onclick = function() 
				{
					MN.loadPopup(this.href,'timberland_utvideo',true);
					
					return false;
				}
			}
		}
	},
	
	getMotorhome:function(e)
	{
		var ajax = new Ajax();
		ajax.doGet('/products/getmotorhome/' + e.value, MN.handleMotorhomeModels);
	},
	
	handleMotorhomeModels:function(str)
	{
		if(document.getElementById('model_box'))
		{
			var container = document.getElementById('model_box');
			container.innerHTML = str;
		}
	},
	
	getNewMotorhomes:function(e)
	{
		var ajax = new Ajax();
		ajax.doGet('/products/getnewmotorhomes/' + e.value, MN.handleNewMotorhomes);
	},
	
	handleNewMotorhomes:function(str)
	{
		if(document.getElementById('man'))
		{
			var container = document.getElementById('man');
			container.innerHTML = str;
		}
	}
}

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

function addHandler(whichEvent, whichObject, attachThis){

	if(document.addEventListener){

		whichObject.addEventListener(whichEvent, attachThis, 0);
	}
	else if(document.attachEvent){

		whichObject.attachEvent("on"+whichEvent, attachThis);
	}
}
