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.mailingListForm();
    	MN.siteSearch();
    	MN.newWindow();
    	MN.newsflash();
    	MN.priceCalculator();
	},
	
	
	mailingListForm:function()
	{
		if(document.getElementById('mailingListName'))
		{
			var name;
			var email;
			
			name  = document.getElementById('mailingListName');
			email = document.getElementById('mailingListEmail');
			
			name.onfocus = function() 
			{
				if(name.value == 'Name:')
				{
					name.value = '';
				}
			}
			
			name.onblur = function() 
			{
				if(name.value == '')
				{
					name.value = 'Name:';
				}
			}
			
			email.onfocus = function() 
			{
				if(email.value == 'Email:')
				{
					email.value = '';
				}
			}
			
			email.onblur = function() 
			{
				if(email.value == '')
				{
					email.value = 'Email:';
				}
			}
			
			if(email.nextSibling)
			{
				email.nextSibling.onclick = function() 
				{
					if(name.value == 'Name:' || name.value == '')
					{
						alert('Please enter a valid name');
						return false;
					}
					
					if(email.value == 'Email:' || email.value == '')
					{
						alert('Please enter a valid email');
						return false;
					}
					
					email.parentNode.submit();
				}
			}
		}
	},
	
	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;
			}
		}
	},
	
	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.style.border = 'none';
		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();
			MN.isScrolling = setInterval("MN.checkScroll()",500);
		}
	},
	
	checkScroll:function()
	{
		var body = document.body;
		document.getElementById('MN_overlay').style.height = body.offsetHeight + body.scrollTop + 'px';
	},

	unloadPopup:function(elemId)
	{
		while(document.getElementById(elemId))
		{
			document.body.removeChild(document.body.firstChild);
		}
		
		clearInterval(MN.isScrolling);
	},
	
	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%';
			}
		}*/
	},
	
	priceCalculator:function()
	{
		if(document.getElementById('pricecalcbox'))
		{
			var content = document.getElementById('pricecalcbox');
			var input   = content.getElementsByTagName('input');
			var type    = 'input';
			if(input.length == 0)
			{
				input = content.getElementsByTagName('select');
				type  = 'select';
			}
			
			var initVal;
			
			for (var i = 0; i < input.length; i++) 
			{
				input[i].onclick = function()
				{
					initVal = this.value;
					//alert(this.parentNode.nextSibling.innerHTML);
					this.parentNode.nextSibling.id = 'active_price_calc';
				}
				
				if(type == 'input')
				{
					input[i].onkeyup = function()
					{
						if(this.value != '')
						{	
							if(this.value =='')
							{
								this.value = 1;
							}
							
							var ajax = new Ajax();
							ajax.doGet('/products/getpricediscount/' + this.name + '/' + this.value + '/', MN.handlePriceCalc);
						}
					}
				}
				else
				{
					input[i].onchange = function()
					{
						if(this.value != '' && this.value != initVal)
						{
							if(this.value =='')
							{
								this.value = 1;
							}
							
							var ajax = new Ajax();
							ajax.doGet('/products/getpricediscount/' + this.name + '/' + this.value + '/', MN.handlePriceCalc);
						}
					}
				}
				
				input[i].onblur = function()
				{
					this.parentNode.nextSibling.id = '';
				}
			}
		}
	},
	
	handlePriceCalc:function(str)
	{
		if(document.getElementById('active_price_calc'))
		{
			var active_price_field = document.getElementById('active_price_calc');
			var input_field = parseInt(active_price_field.previousSibling.firstChild.value);
			//var total_field = active_price_field.nextSibling;
			
			//total_field.innerHTML = '&pound;' + (input_field * parseFloat(str)).toFixed(2);
			
			active_price_field.innerHTML = '&pound;' + str;
		}
		
		
	}
}

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