// Madgex Limited
// Copyright (c) 2005 Madgex Limited. All Rights Reserved.
// Drop Menu 
// 12 Aug 2004 (23 Nov 2004)
// Version 1

// This code has been tested on IE 5-6 win, NS 6+ win, Firefox 1 win, and Mozilla 1+ win
// It does not conform to W3C DOM2 standard as it supports IE4. Do not use
// with XHTML strict doctype.

var is = new Is();

function Is () {
	var agt=navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.gecko = (agt.indexOf('gecko') != -1);
	this.safari = (agt.indexOf('safari') != -1);
	this.camino = (agt.indexOf('camino') != -1);
    this.ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
    this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);
   	this.win = ((agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1));
    this.mac = (agt.indexOf("mac")!=-1);
}

var EventArray = new Array();
function MenuSystem(id){
	this.id = id;
	this.objectType = 'MenuSystem';
	this.childItems = new Array();
	AddEventArray(this);
	this.timeOut = 1000;
	this.menuPoped = false;
	this.menuTimer = '';
	this.timerState = false;
}

MenuSystem.prototype.AddChild = function(oRef){
	this.childItems[this.childItems.length] = oRef
}

MenuSystem.prototype.Build = function(){
	if((is.ie5up)||(is.gecko)){
		if((is.mac==false) || (is.ie==false)){
			var i;
			for (i = 0; i < this.childItems.length; i++) {
				if(arguments.length == 1){
					this.childItems[i].Build(arguments[0]);
				}else{
					this.childItems[i].Build();
				}
			}
		}	
	}
}

MenuSystem.prototype.SetTimer = function(){
	if(this.timerState){
		window.clearTimeout(this.menuTimer);
		this.menuTimer = window.setTimeout(this.id + '.Close()',this.timeOut);
	}else{
		this.menuTimer = window.setTimeout(this.id + '.Close()',this.timeOut);
		this.timerState = true;
	}	
}

MenuSystem.prototype.DisableTimer = function(){
	if(this.timerState){
		window.clearTimeout(this.menuTimer);
		this.timerState = false;
	}
}

MenuSystem.prototype.Close = function(){
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		this.childItems[i].Close();
	}
}

function Menu(oParent,id){
	var me;
	if (this.constructor == Menu){
		me = this;
	}else{
		me = arguments[arguments.length-1]
	}
	me.id = id;
	this.objectType = 'Menu';
	me.childItems = new Array();
	me.isSubMenu = false;
	me.fixedPosition = false;
	me.permanentlyVisible = false;
	me.buildType = "All";
	me.isBuilt = false;
	me.left = 0;
	me.top = 0;
	me.zIndex = 100; 
	me.menuTopOffSet = -3;
	me.menuLeftOffSet = 0;
	me.sClassRef = CSSClassRef();
	me.textWidth =-1;
	me.onMenuOpen = '';
	me.onMenuClose = '';	
	me.parent = oParent;
	oParent.AddChild(me);
}

Menu.prototype.CopyProperties = function(oRef){
	this.textWidth = oRef.textWidth;
	this.menuTopOffSet = oRef.textWidth;
	this.menuLeftOffSet = oRef.textWidth;
	this.menuTopOffSet = oRef.menuTopOffSet
	this.menuLeftOffSet = oRef.menuLeftOffSet 
}

Menu.prototype.AddChild = function(oRef){
	if(this.parent.objectType != "MenuSystem"){
		oRef.CopyProperties(this.parent.parent);
	}
	else{
		for (i = 0; i < this.childItems.length; i++) {
			if(this.childItems[i].objectType == oRef.objectType){
				oRef.CopyProperties(this.childItems[i]);
			}
		}
	}
	this.childItems[this.childItems.length] = oRef;
}

Menu.prototype.Build = function(){
	
	//8888888888888888888888888888888888888888
	if(document.getElementById("breakingnewsbutton"))
	{
		oATag = document.getElementById("breakingnewsbutton");
		oATag.onclick = popmymenu;
		oATag.setAttribute("href","#");
	}
	//8888888888888888888888888888888888888888

	if(arguments.length == 1){
		this.buildType = arguments[0]
	}
	if(((this.buildType == "Visible") && (this.permanentlyVisible == true)) || ((this.buildType == "Hidden") && (this.permanentlyVisible == false)) || (this.buildType == "All")) {
	
		var textTableWidth = '';
		if((is.ie5_5up)&&(is.win)){
			document.write ('<iframe id="' + this.id + 'iframe" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; display:none;"></iframe>');
		}
		var menuStyle = "";
		if(this.permanentlyVisible == false) menuStyle = 'position: absolute; display: none;'
		if(this.fixedPosition) {
			menuStyle += 'position: absolute; left: ' + this.left + 'px; top: ' + this.top + 'px;'
		}
		if(this.isSubMenu){
			menuStyle += ' z-index: ' + (this.zIndex+1) + ';' 
		}else{
			menuStyle += ' z-index: ' + this.zIndex + ';'
		}	
		document.write ('<div id="' + this.id + '"  onmouseout="mmMenuOut(\'' + this.id + '\')" onmouseover="mmMenuOver(\'' + this.id + '\')" style="' + menuStyle + '">');
		document.write ('<div class="mmInternal">');
		document.write ('<img src="../images/breakingnewsdrop.gif" width="290" height="35">');
		document.write ('<table id="' + this.id + 'Table" class="mmMenu" border="0" cellpadding="0" cellspacing="0"><tr><td>');
		document.write ('<table border="0" cellpadding="0" cellspacing="0" width="270">');
		var i;
		for (i = 0; i < this.childItems.length; i++) {
			if(arguments.length == 1){
				this.childItems[i].Build();
			}else{
				this.childItems[i].Build(arguments[0]);
			}
		}

		document.write ('</table></div></td></tr></table>');
		document.write ('<img src="../images/breakingnewsline.gif" width="290" height="4"><br>');
		document.write ('<div id="morenews" onmousedown="document.location.href=\'http://www.mad.co.uk/Main/News/Default.html\'; return false">more news');
		document.write ('<img src="../images/breakingnewsarrow.gif" width="5" height="10">');		
		document.write ('</div></div>');
		oDiv = MMGetElementById(this.id);
		if(this.fixedPosition){
			this.DisplayIFrameOverlay();
		}
		this.isBuilt = true;
	}
}

Menu.prototype.Display = function(ileft,itop,iwidth){
	if(arguments.length < 3){
		ileft = 200;
		itop = 200;
		iwidth = 100;
	}
	if(this.isBuilt)
	{
		oDiv = MMGetElementById(this.id);
		oDiv.style.left = ileft + this.menuLeftOffSet + 'px';
 		oDiv.style.top = itop + this.menuTopOffSet + 'px';
		oDiv.style.display = 'block';
		this.DisplayIFrameOverlay();
	}
	if(this.onMenuOpen != ''){
		eval(this.onMenuOpen);
	}	
}

Menu.prototype.DisplayIFrameOverlay = function(){
	if((is.ie5_5up)&&(is.win)){
		oIFrame = MMGetElementById(this.id + 'iframe');
		oIFrame.style.width = parseInt(oDiv.offsetWidth) + 'px';
		oIFrame.style.height = parseInt(oDiv.offsetHeight) + 'px';
		oIFrame.style.top = parseInt(oDiv.style.top) + 'px';
		oIFrame.style.left = parseInt(oDiv.style.left) + 'px';
		oIFrame.style.zIndex = parseInt(oDiv.style.zIndex) - 1;
		oIFrame.style.display = 'block';
	}
}

Menu.prototype.RollOver = function(){
	this.parent.DisableTimer();
}

Menu.prototype.RollOut = function(){}

Menu.prototype.Close = function(){

	if(this.permanentlyVisible == false){
		if((is.ie5_5up)&&(is.win)){
			oIFrame = MMGetElementById(this.id + 'iframe');
			if ( oIFrame != null )
					oIFrame.style.display = 'none';
		}
		oDiv = MMGetElementById(this.id);
		if ( oDiv ){
			oDiv.style.display = 'none';
		}
		if(this.onMenuClose != ''){
			eval(this.onMenuClose);
		}	
	}
	if(this.Parent){
		this.Parent.RemoveHighLight();
	}
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		if(this.childItems[i].hasChild){
			this.childItems[i].childMenu.Close();
		}
	}

}

Menu.prototype.CloseSubMenus = function(id){
	var i;
	for (i = 0; i < this.childItems.length; i++) {
		if((this.childItems[i].hasChild) && (this.childItems[i].id != id)){
			this.childItems[i].childMenu.Close();
		}
	}

}

function MenuItem(oParent,id,link,text,iconSrc){
	var me;
	if (this.constructor == MenuItem){
		me = this;
		this.objectType = 'MenuItem';
	}else{
		me = arguments[arguments.length-1];
	}
	me.id = id;
	me.link = link;
	me.text = text;
	me.highLighted = false;
	me.hasChild = false;
	me.childMenu = '';
	me.textWidth = -1;
	me.sClassRef = CSSClassRef();
	me.parent = oParent
	me.parent.AddChild(me);
}

MenuItem.prototype.CopyProperties = function(oRef){
	this.textIndent = oRef.textIndent;
	this.textWidth = oRef.textWidth;
}

MenuItem.prototype.AddChild = function(oRef){
	oRef.CopyProperties(this.parent);
	this.childMenu = oRef;
	this.hasChild = true;
	oRef.isSubMenu = true;
	oRef.Parent = this;
}

MenuItem.prototype.Build = function(){
	var textCellWidth = '';
	this.SetCurrentStyle();
	document.write ('<tr class="mmItem" id="' + this.id + '" onmousedown="mmDown(\'' + this.id + '\')" onmouseover="mmOver(\'' + this.id + '\')" onmouseout="mmOut(\''  + this.id + '\')">');
	document.write ('<td class="' + this.textCSSCurrent + '" nowrap="nowrap">' + this.text + '</td>');	
	document.write ('</tr>'); 
}

MenuItem.prototype.Link = function(){
	if(this.link != '')
	{
		if((is.ie5up)||(is.gecko)){
			oBaseColl = document.getElementsByTagName('BASE');
		}
		if(is.ie4){
			oBaseColl = document.all.tags['BASE'];
		}
		var sBase = '';
		if ( oBaseColl && oBaseColl.length) 
		{
			sBase = oBaseColl[0].href ;
		}
		document.location.href = sBase + this.link;	
	}	
}

MenuItem.prototype.RollOver = function(){
	var oTR
	this.parent.parent.DisableTimer();
	this.parent.CloseSubMenus(this.id);
	oTR = MMGetElementById(this.id);
	if(this.highLighted == false){
		oTR.cells[0].setAttribute(this.sClassRef,"mmTextHighLighted");
	}
	if(this.hasChild){
		//Fixes safari bug, cannot get pos on a tr
		oTD = oTR.cells[1];
		var ileft = getPageOffsetLeft(oTD) + oTD.offsetWidth;
 		var itop = getPageOffsetTop(oTD);
 		var iwidth = oTD.offsetWidth;
		this.childMenu.Display(ileft,itop,iwidth);
	}
}

MenuItem.prototype.RollOut = function(){
	if((this.highLighted == false)&&(this.hasChild == false)){
		this.RemoveHighLight();
	}
}

MenuItem.prototype.RemoveHighLight = function(){
	if(this.highLighted == false){
		var oTR = MMGetElementById(this.id);
		if ( oTR )
		{
			oTR.cells[0].setAttribute(this.sClassRef,"mmText");
		}
	}	
}

MenuItem.prototype.SetCurrentStyle = function(){
	this.itemCSSCurrent = 'mmItem';
	this.textCSSCurrent = 'mmText';
	this.arrowCSSCurrent = 'mmArrow';
	if(this.highLighted){
		this.itemCSSCurrent = 'mmItem';
		this.textCSSCurrent = 'mmTextOn';
		this.arrowCSSCurrent = 'mmArrowOn';
	}
}

function Inherit( base, derived ){
	for ( var i in base.prototype ){
		eval( 'derived.prototype.' + i + '= base.prototype.' + i );
	}
}	

function MMGetElementById(strRef){
	var oRef = '';
	if((is.ie5up)||(is.gecko)){
		oRef = document.getElementById(strRef);
	}
	if(is.ie4){
		oRef = document.all[strRef];
	}
	return oRef;
}

function CSSClassRef(){
	if (is.gecko) sRef =  "class" ;
	if (is.ie4up) sRef = "className";
	return sRef;
}

function getPageOffsetLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getPageOffsetTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function mmOver(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOver();
}

function mmOut(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOut();
}

function mmDown(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.Link();
}

function mmMenuOver(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOver();
}

function mmMenuOut(vid){
	var oCurrentObject = eval(vid);
	oCurrentObject.RollOut();	
}

function mmCloseMenus(event){	
	DocumentMouseDown();
}

function AddEventArray(oRef){
	EventArray[EventArray.length] = oRef;
}

function DocumentMouseDown(){
	for (i = 0; i < EventArray.length; i++) {
		EventArray[i].Close();
	}
}

function DocumentMouseOver(){
	for (i = 0; i < EventArray.length; i++) {
		EventArray[i].SetTimer();
	}
}

if (is.ie4up){
	document.onmousedown = DocumentMouseDown;
}else{
	document.addEventListener("mousedown", DocumentMouseDown, true);
}

function popmymenu(){
	iLeft = parseInt(getPageOffsetLeft(document.getElementById("breakingnewsbutton"))) + 4;
	iTop = 48; 
	M.Display(iLeft,iTop,0);
}

