
var magOverlayOpenId = "";
var magOverlayClickedId = "";
var magOverlayMenuOpen = false;

var magOverlayClass = [];


function magInitOverlay() {
	document.body.onmousedown = magMouseDown;

	magAddOverlayEvents("cnnOverlayLnk");

}


function magShowOverlay(menuId) {
	if ($(menuId)) {
		if ($(menuId).style.display == "block") {
			$(menuId).style.display = "none";
		}
		else {
			$(menuId).style.display = "block";
			magOverlayOpenId = menuId;
		    magOverlayMenuOpen = true;
			magOverlayClickedId = "";
		}
	}

}


function magHideOverlay(menuId) {
	if ($(menuId)) {
		$(menuId).style.display = "none";
		magOverlayOpenId = '';
	    magOverlayMenuOpen = false;
	}
}


function magGetOverlayMenuId(btn) {
	return btn.href.substring(btn.href.indexOf("'") + 1, btn.href.lastIndexOf("'"));
}


function magAddOverlayEvents(btnClass) {
	var btnArray = document.getElementsByClassName(btnClass);
	for (var i = 0; i < btnArray.length; i++) {
		var btn = btnArray[i];
		btn.onmousedown = magOverlayMouseDownBtn;
		var menuId = magGetOverlayMenuId(btn);
		if ($(menuId)) {
			$(menuId).onmousedown = magOverlayMouseDownMenu;
		}

	    magOverlayClass[menuId] = btnClass;

		if ((navigator.userAgent.indexOf("Safari") != -1)
		 && (navigator.userAgent.indexOf("Mac") != -1)) {
			if (btn.onmouseout && btn.onmouseout.toString().indexOf("cnnImgSwap") != -1) {
				btn.onclick = function onclick() { this.onmouseout(); return true; };
			}
		}
	}
}


function magOverlayMouseDownBtn(e) {
	var menuId = magGetOverlayMenuId(this);
	magOverlayClickedId = menuId;
	return true;
}


function magOverlayMouseDownMenu(e) {
	magOverlayClickedId = this.id;
	return true;
}


function magOverlayMouseDownBody(e) {
	if (magOverlayOpenId != magOverlayClickedId) {
		magHideOverlay(magOverlayOpenId);
	}
	magOverlayClickedId = "";
	return true;
}

function magMouseDown(e) {
	if (magDropdownOpen) magDD.mouseDownBody(e);
	if (magOverlayMenuOpen) magOverlayMouseDownBody(e);
	return true;
}
var magDropdownOpen = false;
var magDD = {
	curId: "", 
	ignoreMouseDownBody: false,
	menus: [],

	rowHeight: 17,
	combinedBorderWidth: 20,
	scrollbarWidth: 18,

	minMenuWidth: 105,
	maxMenuWidth: 350,
	defaultMenuWidth: 205,
	defaultRowWidth: 150,
	combinedRowLRPad: 18,
	scrollbarRPad: 12,


	buildDisabledDropdown: function(menuId, buttonWidth, buttonClass, hiddenListSuffix) {

		if (!buttonWidth) buttonWidth = 140;
		if (!buttonClass) buttonClass = 'magDDWireLtg';

		var wrapId = menuId + "_wrap";
		var listId = menuId + "_list" + (hiddenListSuffix ? '_' + hiddenListSuffix : '');

		if ($(wrapId) && $(listId)) {

			$(listId).style.display = "none";

			var listItems = $(listId).options;
			var buttonText = listItems[0].innerHTML;

			var buttonTextLPad = 10;
			var buttonTextRPad = 34;
			var buttonTextWidth = buttonWidth - (buttonTextLPad + buttonTextRPad);

			var leftBgStyle = '';
			var rightBgStyle = '';




			var strContent = "\n\n\n\n";
			strContent += '	<div class="magDDContainer" style="width:'+buttonWidth+'px;">'+"\n";

			strContent += '		<div class="'+buttonClass+'">'+"\n";
			strContent += '			<div class="magDDBtn" onmousedown="return magDD.mouseDownBtn(event, \''+menuId+'\');" onclick="return magDD.open(\''+menuId+'\')" style="'+rightBgStyle+'">'+"\n";
			strContent += '				<table width="'+buttonWidth+'" border="0" cellspacing="0" cellpadding="0">'+"\n";
			strContent += '					<tr>'+"\n";
			strContent += '						<td width="'+buttonTextLPad+'"><div class="magDDBtnLeft" style="'+leftBgStyle+'"></div></td>'+"\n";
			strContent += '						<td width="'+buttonTextWidth+'">'+"\n";
			strContent += '							<div class="magDDValueContainer">'+"\n";
			strContent += '								<div id="'+menuId+'_Val" class="magDDValue" style="width:'+buttonTextWidth+'px;color:#c5c5c5;">'+buttonText+'</div>'+"\n";
			strContent += '						</td>'+"\n";
			strContent += '						<td width="'+buttonTextRPad+'"><img src="btn_icon_disabled.gif" alt="" border="0"></td>'+"\n";
			strContent += '					</tr>'+"\n";
			strContent += '				</table>'+"\n";
			strContent += '			</div><!--/magDDBtn -->'+"\n\n";
			strContent += '		</div><!--/'+buttonClass+' -->'+"\n\n";

			strContent += '	</div><!--/magDDContainer -->'+"\n";
			strContent += "\n\n";

			$(wrapId).innerHTML = strContent;

			$(listId).selectedIndex = 0;

		}
	},

	buildDropdown: function(menuId, buttonWidth, menuWidth, numVisibleRows, buttonClass, hiddenListSuffix) {
		
		if (!buttonWidth) buttonWidth = 140;
		if (!menuWidth) menuWidth = this.defaultMenuWidth;
		if (!numVisibleRows) numVisibleRows = 10;
		if (!buttonClass) buttonClass = 'magDDWireLtg';

		if (menuWidth < this.minMenuWidth) menuWidth = this.minMenuWidth;
		if (menuWidth > this.maxMenuWidth) menuWidth = this.maxMenuWidth;

		var wrapId = menuId + "_wrap";
		var listId = menuId + "_list" + (hiddenListSuffix ? '_' + hiddenListSuffix : '');

		this.menus[menuId] = new Array();
		this.menus[menuId].listId = listId;
		this.menus[menuId].updateFirstRow = false;

		if ($(wrapId) && $(listId)) {
		
			$(listId).style.display = "none";

			var displayedValue = new Array();
			var internalValue = new Array();
			var disabledRow = new Array();

			var listItems = $(listId).options;
			for (var i=0;i<listItems.length;i++) {
				displayedValue[i] = listItems[i].innerHTML;
				internalValue[i] = listItems[i].value;
				disabledRow[i] = listItems[i].disabled;
			}
			var selectedRow = $(listId).selectedIndex;

		
			if (selectedRow == 0) {
		
				for (i=1;i<displayedValue.length;i++) {
					if (displayedValue[i] == displayedValue[0]) {
						selectedRow = i;
						this.menus[menuId].updateFirstRow = true;
						break;
					}
				}
			}
			var buttonText = displayedValue[selectedRow];
			var numRows = displayedValue.length;

			var buttonTextLPad = 10;
			var buttonTextRPad = 34;
			var buttonTextWidth = buttonWidth - (buttonTextLPad + buttonTextRPad);

		
			var fullRowWidth = menuWidth - this.combinedBorderWidth;

		
			var visibleRowsHeight = numRows * this.rowHeight;
			var rowWidth = fullRowWidth;

		
			if (numRows > numVisibleRows) {
				visibleRowsHeight = numVisibleRows * this.rowHeight;
				rowWidth -= 10;
			}

			var leftBgStyle = '';
			var rightBgStyle = '';



		
			var strContent = "\n\n\n\n";
			strContent += '	<div class="magDDContainer" style="width:'+buttonWidth+'px;">'+"\n";

			strContent += '		<div class="magDDBoxContainer">'+"\n";
			strContent += '		<div class="magDDBox" id="'+menuId+'" style="width:'+menuWidth+'px;" onmousedown="return magDD.mouseDown(event, \''+menuId+'\');">'+"\n";
			strContent += '			<div class="magDDBoxHeader"><div class="magDDBoxHeaderTL"></div><div class="magDDBoxHeaderTR"></div></div>'+"\n";
			strContent += '			<div class="magDDBoxContent">'+"\n";

			strContent += '				<div class="magDDContent" style="width:'+fullRowWidth+'px;">'+"\n";
			strContent += '					<div class="cnnPad6Top"></div>'+"\n";
			strContent += '					<div class="magDDList" style="height:'+visibleRowsHeight+'px; width:'+rowWidth+'px;">'+"\n";
			strContent += '						<ul>'+"\n";

			for (var i=0;i<displayedValue.length;i++) {
				if ((i==0) && (this.menus[menuId].updateFirstRow)) {
					strContent += '						<li id="'+menuId+'_hdnVal"><a href="javascript:magDD.select('+i+',\''+this.encodeAttr(displayedValue[i])+'\',\''+this.encodeAttr(internalValue[i])+'\');">'+displayedValue[i]+'</a></li>'+"\n";
				}
				else if (disabledRow[i]) {
					strContent += '						<li class="magDDSeparator"><span>'+displayedValue[i]+'</span></li>'+"\n";
				}
				else {
					strContent += '						<li><a href="javascript:magDD.select('+i+',\''+this.encodeAttr(displayedValue[i])+'\',\''+this.encodeAttr(internalValue[i])+'\');">'+displayedValue[i]+'</a></li>'+"\n";
				}
			}
			strContent += '						</ul>'+"\n";
			strContent += '					</div>'+"\n";
			strContent += '					<div class="cnnPad8Top"></div>'+"\n";
			strContent += '				</div><!-- /magDDContent -->'+"\n";

			strContent += '			</div><!-- /magDDBoxContent -->'+"\n";
			strContent += '			<div class="magDDBoxFooter"><div class="magDDBoxFooterBL"></div><div class="magDDBoxFooterBR"></div></div>'+"\n";
			strContent += '		</div><!--/magDDBox-->'+"\n";
			strContent += '		</div><!--/magDDBoxContainer-->'+"\n";

			strContent += '		<div class="'+buttonClass+'">'+"\n";
			strContent += '			<div class="magDDBtn" onmousedown="return magDD.mouseDownBtn(event, \''+menuId+'\');" onclick="return magDD.open(\''+menuId+'\')" style="'+rightBgStyle+'">'+"\n";
			strContent += '				<table width="'+buttonWidth+'" border="0" cellspacing="0" cellpadding="0">'+"\n";
			strContent += '					<tr>'+"\n";
			strContent += '						<td width="'+buttonTextLPad+'"><div class="magDDBtnLeft" style="'+leftBgStyle+'"></div></td>'+"\n";
			strContent += '						<td width="'+buttonTextWidth+'">'+"\n";
			strContent += '							<div class="magDDValueContainer">'+"\n";
			strContent += '								<div id="'+menuId+'_Val" class="magDDValue" style="width:'+buttonTextWidth+'px;">'+buttonText+'</div>'+"\n";
			strContent += '						</td>'+"\n";
			strContent += '						<td width="'+buttonTextRPad+'">&nbsp;</td>'+"\n";
			strContent += '					</tr>'+"\n";
			strContent += '				</table>'+"\n";
			strContent += '			</div><!--/magDDBtn -->'+"\n\n";
			strContent += '		</div><!--/'+buttonClass+' -->'+"\n\n";

			strContent += '	</div><!--/magDDContainer -->'+"\n";
			strContent += "\n\n";

			$(wrapId).innerHTML = strContent;

			document.body.onmousedown = magMouseDown;
		}
	},

	buildOverlay: function(menuId, menuWidth, numVisibleRows, dx, dy) {
		
		if (!menuWidth) menuWidth = this.defaultMenuWidth;
		if (!numVisibleRows) numVisibleRows = 10;

		if (menuWidth < this.minMenuWidth) menuWidth = this.minMenuWidth;
		if (menuWidth > this.maxMenuWidth) menuWidth = this.maxMenuWidth;

		var leftPos = -20;
		var topPos = 1;
		if (dx) leftPos += dx;
		if (dy) topPos += dy;

		var wrapId = menuId + "_wrap";
		var listId = menuId + "_list";
		var titleId = menuId + "_title";
		
		if ($(wrapId) && $(titleId) && $(listId)) {
			$(listId).style.display = "none";

			var title = $(titleId).innerHTML;

			var listItems = $(listId).getElementsByTagName('li');
			var displayedList = new Array();
			for (var i=0;i<listItems.length;i++) {
				displayedList[i] = listItems[i].innerHTML;
			}

			var numRows = displayedList.length;

			var menuTitleRPad = 60;
			var menuTitleWidth = menuWidth - menuTitleRPad;

			var fullRowWidth = menuWidth - this.combinedBorderWidth;

			var visibleRowsHeight = numRows * this.rowHeight;
			var rowWidth = menuWidth - this.combinedBorderWidth;

			if (numRows > numVisibleRows) {
				visibleRowsHeight = numVisibleRows * this.rowHeight;
				rowWidth -= 10;
			}


			var strContent = "\n\n\n\n";
			strContent += ' <div class="magDDOvrBoxContainer">'+"\n";
			strContent += '		<div class="clear"><img src="1.gif" width="1" height="1" border="0" alt=""></div>'+"\n";
			strContent += '		<div class="magDDOvrBox" id="'+menuId+'" style="width:'+menuWidth+'px;left:'+leftPos+'px; top:'+topPos+'px;" onmousedown="return magDD.mouseDown(event, \''+menuId+'\');">'+"\n";
			strContent += '			<div class="magDDBoxHeader"><div class="magDDBoxHeaderTL"></div><div class="magDDBoxHeaderTR"></div></div>'+"\n";
			strContent += '			<div class="magDDBoxContent">'+"\n";
			strContent += '				<div class="magDDOvrCloseContainer"><div class="magDDOvrClose" onclick="magDD.close(); return true;"><img src="overlay_close.png" width="12" height="12" alt="" border="0"></div></div>'+"\n";
			strContent += '				<div class="magDDContent" style="width:'+fullRowWidth+'px;">'+"\n";
			strContent += '					<div class="magDDOvrTitle" style="width:'+menuTitleWidth+'px;overflow:hidden;">'+title+'</div>'+"\n";
			strContent += '					<div class="magDDList" style="height:'+visibleRowsHeight+'px;width:'+rowWidth+'px;">'+"\n";
			strContent += '						<ul>'+"\n";

			for (var i=0;i<displayedList.length;i++) {
				strContent += '					<li>'+displayedList[i]+'</li>'+"\n";
			}
			strContent += '						</ul>'+"\n";
			strContent += '					</div><!-- /magDDList -->'+"\n";
			strContent += '					<div class="cnnPad12Top"></div>'+"\n";
			strContent += '				</div><!-- /magDDContent -->'+"\n";
			strContent += '			</div><!-- /magDDBoxContent -->'+"\n";
			strContent += '			<div class="magDDBoxFooter"><div class="magDDBoxFooterBL"></div><div class="magDDBoxFooterBR"></div></div>'+"\n";
			strContent += '		</div><!--/magDDOvrBox-->'+"\n";
			strContent += ' </div><!--/magDDOvrBoxContainer-->'+"\n";
			strContent += "\n\n";
			$(wrapId).innerHTML = strContent;

			document.body.onmousedown = magMouseDown;

		}
	},


	select: function(index, displayedValue, internalValue) {
		if ($(this.curId)) {
			var menuId = this.curId;

			this.close();

			if ($(menuId + '_Val')) {
				$(menuId + '_Val').innerHTML = displayedValue;
			}

			if ((this.menus[menuId].updateFirstRow) && $(menuId + '_hdnVal')) {
				$(menuId+'_hdnVal').innerHTML = '<a href="javascript:magDD.select(' + index + ',\'' + this.encodeAttr(displayedValue) + '\',\'' + this.encodeAttr(internalValue) + '\')">' + displayedValue + '</a>';
			}

			var listId = this.menus[menuId].listId;
			if ($(listId)) {
				if ($(listId).selectedIndex != index) {
					$(listId).selectedIndex = index;

					if ($(listId).onchange) {
						$(listId).onchange();
					}
				}
			}

			try {
				var onChoose = eval(menuId + '_OnChoose');
				if (onChoose) {
					onChoose();
				}
			}
			catch(err) {
			}
		}
	},

	open: function(id) {
		if($(id)) {
			var sameMenu = (this.curId == id);

			this.close();

			if (!sameMenu) {
				$(id).style.display = "block";
				this.curId = id;
				magDropdownOpen = true;
			}
		}
	},

	close: function() {
		if ($(this.curId)) {
			$(this.curId).style.display = "none";
			this.curId = '';
			magDropdownOpen = false;
		}
	},

	encodeAttr: function(str) {
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'&quot;');
		str=str.replace(/\0/g,'\\0');
		return str;
	},

	mouseDown: function(e, id) {
		this.ignoreMouseDownBody = true;
		return true;
	},

	mouseDownBtn: function(e, id) {
		this.ignoreMouseDownBody = (id && (this.curId == id));
		return true;
	},

	mouseDownBody: function(e) {
		if (!this.ignoreMouseDownBody) {
			this.close();
		}
		this.ignoreMouseDownBody = false;
		return true;
	}
}