//******************** Overlay **********************/
Comcast.namespace('Comcast.Ui.Menu');

(function() {
	Comcast.Ui.Menu = function() { 
		// Define Constants
		this.MENU_HEIGHT = 25;
		this.MENU_PADDING = 6;
		this.MENU_HEIGHT_IE5 = 19;
		this.MENU_PADDING_IE5 = 10;
		this.MENU_TOP_MARGIN = 0;
	
		var browserInfo = new DF.browserInformation();	
		
		this.menuIsIE = (browserInfo.browser.searchStr == "MSIE");
		this.menuIsV3 = (this.menuIsIE  && browserInfo.version < 4);
		this.menuIsV5 = (this.menuIsIE  && browserInfo.version >= 5 && browserInfo.version < 6);
		this.menuIsV4 = (this.menuIsIE  && browserInfo.version >= 4 && browserInfo.version < 5);
		this.isIE6Up = (this.menuIsIE  && !this.menuIsV4 && !this.menuIsV5 && !this.menuIsV3);
		
		if (!this.isIE6Up)
		{
			this.MENU_HEIGHT = this.MENU_HEIGHT_IE5;
			this.MENU_PADDING = this.MENU_PADDING_IE5;
		}

		this.LOC_NAME_LENGTH = 42;
	};
	
	Comcast.Ui.Menu.prototype = {
		init : function(primaryMenuList, menuArray) {
			this.menu = DF.$(primaryMenuList);
			this.menuArray = menuArray;
			if((this.menu != null) && this.menu.style.display != "none" && this.menu.childNodes.length > 0)
			{
				// add debug window
				/*
				this.debug = document.createElement("div");
				this.debug.style.position = "absolute";
				this.debug.style.width = "250px";
				this.debug.style.height = "200px";
				this.debug.style.border = "solid 1px green";
				this.debug.style.top = "10px";
				this.debug.style.right = "10px";
				this.debug.style.backgroundColor = "#cccccc";
				this.debug.style.padding = "2px 2px 2px 2px";
				document.body.appendChild(this.debug);
				*/
				// end debug window
				var menuCss;
				var selectedMenu;
				var menuBackground;			
				 
				// add programming item if missing
				var navItems = this.menu.getElementsByTagName("li");
				
				for (var i=0; i < navItems.length; i++)
				{
					var node = navItems[i];
					var link = node.getElementsByTagName("a")[0];
					if(link != null)
						link.setAttribute("index", i);
					if (this.menuIsIE && !this.isIE6Up)
					{
						for (var n=0; n < document.styleSheets.length; n++)
						{
							var menuCss = document.styleSheets[n].href.match(/(comcast_)(\w+)(\.css$)/);
							if (menuCss)
							{
								var selectedMenu = menuCss[2].toLowerCase();
								var menuName = node.className.toLowerCase();
								if (menuName.indexOf(selectedMenu) > -1)
								{
									var menuBackground = document.styleSheets[n].cssText.match(/(BACKGROUND-COLOR: )(#[\w\d]+)/);
									if (menuBackground)
										node.style.backgroundColor = menuBackground[2];
									MENU_TOP_MARGIN = 42;
									node.getElementsByTagName("img")[0].style.marginBottom = "1px";
									break;
								}
							}
							
						}
					}
					DF.evt.Event.addListener(node,"mouseout",this.outItem.createDelegate(this));
					DF.evt.Event.addListener(node,"mouseover",this.overItem.createDelegate(this));
					//node.onmouseout = this.outItem;
				}
			}
		},
		addPageNameToSearch : function(formId) {
			// Add PageName field to the search form
			var pageNameField = document.createElement("input");
			pageNameField.setAttribute("type","hidden");
			pageNameField.setAttribute("name","PageName");
			pageNameField.setAttribute("value", document.title.replace(" ", "+"));
			var searchForm = DF.$(formId);
			if(searchForm != null)
				searchForm.appendChild(pageNameField);
		},
		overItem : function(evt)
		{	
			evt = (evt) ? evt : ((window.event) ? window.event : null);
			// Add list children to uls
			function InsertChildren(ListItem, Index, menuArray)
			{
				var ul = document.createElement("ul");
				ul.innerHTML = menuArray[Index];
				ul.style.zIndex = "99";
				ListItem.appendChild(ul);
			}
			
			var targetElt = (evt.target) ? evt.target : evt.srcElement;
			while(targetElt.parentNode != this.menu)
			{
				targetElt = targetElt.parentNode;
			}	
			var oLink = targetElt.getElementsByTagName("a")[0];
			if( oLink && oLink.getAttribute("index") != null)
			{
				var index = oLink.getAttribute("index")
				// if there 
				if(index >= this.menuArray.length || DF.string.isNullOrEmpty(this.menuArray[index]))
					return;
				
				// Create child lists if needed
				if(targetElt.getElementsByTagName("ul").length == 0)
				{
					InsertChildren(targetElt,index,this.menuArray);			
				}
			}			
			
			var iframe = targetElt.getElementsByTagName("iframe")[0];
			if (iframe) 
			{
				iframe.style.visibility = "visible";
			}
			else
			{	
				iframe = document.createElement("iframe");
				iframe.setAttribute("src", window.location.protocol + "//www.comcast.com/images/dot.gif");
				iframe.setAttribute("scrolling", "no");
				iframe.setAttribute("frameborder", "0");
				iframe.style.zIndex="98";
				iframe.style.borderWidth="0";

				var firstC = targetElt.firstChild;
				while(firstC.nodeName=="#text" && firstC.nextSibling != null)
					firstC = firstC.nextSibling;
				if (firstC.nodeName=="#text")
					firstC = null;
				
				if (firstC != null && firstC.nodeName.toUpperCase() == "A") 
				{
					var ul = targetElt.getElementsByTagName("ul")[0].childNodes.length; 
					//iframe.width = targetElt.offsetWidth * 2 - 13;
					iframe.width = 202;
					iframe.height = this.MENU_HEIGHT * ul + this.MENU_PADDING;
				}
				else
				{
					iframe.width = 0;
					iframe.height = 0;
				}
				if (this.MENU_TOP_MARGIN > 0)
					iframe.style.top = this.MENU_TOP_MARGIN + "px";
				iframe.style.position = "absolute";
				targetElt.appendChild(iframe);
			}
			
			// IE hack for rollover state
			if(document.all && document.getElementById)
			{
				DF.Dom.addClass(targetElt,"sfhover");				
			}
			//this.debug.innerHTML += "overItem " + targetElt.tagName + " " + targetElt.className + ", <br />";
		},

		outItem : function(evt)
		{
			evt = (evt) ? evt : ((window.event) ? window.event : null);
			var targetElt = (evt.target) ? evt.target : evt.srcElement;
			
			//evt.toElement.style.border = "solid 1px red";
			while(targetElt.parentNode != this.menu)
			{
				targetElt = targetElt.parentNode;
			}			
			DF.Dom.removeClass(targetElt,"sfhover");
			//this.debug.innerHTML += "outItem " + targetElt.tagName + " " + targetElt.className + ", toElement: " + evt.toElement.tagName + "<br />";
			
			var iframe = targetElt.getElementsByTagName("iframe")[0];
			if (iframe)
			{
				//iframe.style.display = "none";
				iframe.style.visibility = "hidden";
			}
		}, 
		
		setHeader : function()
		{
		    if(document.getElementById)
            {
                var zipInfo = document.getElementById("ZipServInfo");
                var locCookie = getCookieVal("Workplace");
                if(locCookie.length > 0)
                {
		            if(GetCookie("Workplace","Serv") != false)
		            {
			            zipInfo.style.display = "block";
			            var zipHeader = document.getElementById("zipInfo");
			            var zip = GetCookie("Workplace","ZIP");
			            zipHeader.innerHTML = zip;
                    }
                }
            }
		},
		
		setMenuItems : function()
		{
		    if(document.getElementById)
		    {
		        var zipServCookie = getCookieVal("Workplace");
		        if(zipServCookie.length > 0)
		        {
		            var servType = GetCookie("Workplace", "Serv");
		            if(servType != false)
		            {
		                var el
		                if((servType & 2) == 0)
		                {
		                    el = $("Serv_2");
		                    if(el)
		                        el.style.display = "none";
		                }
		                if((servType & 4) == 0)
		                {
		                    el = $("Serv_4");
		                    if(el)
		                        el.style.display = "none";
		                }
		                if((servType & 8) == 0)
		                {
		                    el = $("Serv_8");
		                    if(el)
		                        el.style.display = "none";
		                }
		            }
		        }
		    }
		}
	};

})();