var d = document;
function cnnShowLogin(){
	d.getElementById('cnnLoginOverlay').style.visibility = "visible";
}
function cnnLoginCancel(){
	d.getElementById('cnnLoginOverlay').style.visibility = "hidden";
}
if (d.location.href.indexOf('https') != -1) {
	var cnnElHost = 'https://s.cnn.net/nascar';
} else {
	var cnnElHost = 'http://i.cdn.turner.com/nascar';
}
if ((location.hostname.indexOf('nascar.com') == -1) || ((location.hostname.indexOf('.nascar.com') != -1) && (location.hostname.indexOf('www.nascar.com') == -1))) {
	var cnnURLHost = 'http://www.nascar.com';
} else {
	var cnnURLHost = '';
}

/*********************************************************
 * Nascar Homepage Navigation Menu
 * Author: Al McNichol
 * Date: 11/21/2008
 *
 * Change History:
 * AMcNichol: 11/21/2008 - Created
 * AMcNichol: 11/24/2008 - Adde code to set li element's class
 */
window.cnnNavMenu  		= function(){};
window.cnnMenuHASH 		= null;
cnnNavMenu.MenuType = function () {
	this.parentId	= "";
	this.childId	= "";
	this.isActive	= 0;
	this.menuObjDOM	= null;
}
cnnNavMenu.MenuHash = function () {
	var csSubnav = getCookies();
	var user = csSubnav["displayname"];
	var uname = '';
	if(user) {
		uname = user;
	}
	var	browser			 = navigator.appName;
	var cnnHideMenuTimer = null;
	var fwDHFlag 		 = false;
	var fwStart 		 = null;
	var errCode 		 = 0
	var errMsg			 = "";
	var size 			 = 0;
	var submenuData = [
		{
		    id: "ocnnNews",
		    bgPosX: "0",
		    itemdata: [
		        { text: "Sprint Cup Series", url: cnnURLHost + "/news/headlines/cup/" },
		        { text: "Nationwide Series", url: "http://nationwide.nascar.com/nationwide-series/" },
		        { text: "Camping World Trucks", url: cnnURLHost + "/news/headlines/truck/" },
		        { text: "Features", url: cnnURLHost + "/news/features/" },
		        { text: "Opinion", url: cnnURLHost + "/news/opinion/" },
		        { text: "Entertainment", url: cnnURLHost + "/entertainment/" },
		        { text: "NewsWire", url: cnnURLHost + "/news/headlines/newswire/" },
		        { text: "NASCAR 101", url: cnnURLHost + "/kyn/" },
		        { text: "Newsletter", url: cnnURLHost + "/news/newsletter/" },
		        { text: "RSS", url: cnnURLHost + "/services/rss/" }
		    ]
		},
		{
			id: "ocnnVideo",
		    bgPosX: "-60px",
		    itemdata: [
		        { text: "NASCAR on TNT", url: cnnURLHost + "/tnt" },
		    	{ text: "NASCAR on SPEED", url: cnnURLHost + "/video/features/speed" },
				{ text: "NASCAR on iTunes", url: cnnURLHost + "/promos/itunes/" }, // testing 7/6 for itunes
			    { text: "Kyle's Corner", url: cnnURLHost + "/video/features/kyles_corner/" },
			    { text: "Garage Expert", url: cnnURLHost + "/video/features/garage_expert/" },
			    { text: "Beyond Pit Road", url: cnnURLHost + "/video/features/beyond_pitroad/" },
			    { text: "Sights and Sounds", url: cnnURLHost + "/video/features/sights_sounds/" },
			    { text: "Inside NASCAR on SHO", url: cnnURLHost + "/video/features/inside_nascar/" }
			]
		},
		{
			id: "ocnnDrivers",
		    bgPosX: "-124px",
		    itemdata: [
			    { text: "Sprint Cup Series", url: cnnURLHost + "/drivers/list/cup/dps/" },
			    { text: "Nationwide Series", url: "http://nationwide.nascar.com/nationwide-series/drivers" },
			    { text: "Camping World Trucks", url: cnnURLHost + "/drivers/list/truck/dps/" },
			    { text: "Driver Gauge", url: cnnURLHost + "/gauge/" },
			    { text: "Statistics", url: cnnURLHost + "/kyn/nbtn/" },
			    { text: "All-Time List", url: cnnURLHost + "/drivers/list/cup/dps/all_drivers_A.html" },
			    { text: "Video", url: cnnURLHost + "/video/?defaultTab=Drivers" },
			    { text: "Shop", url: "http://store.nascar.com/home/index.jsp?&source=nascar.dhtml:shop" }
			]
		},
		{
		    id: "ocnnStandings",
		    bgPosX: "-203px",
		    itemdata: [
			    { text: "Sprint Cup Series", url: cnnURLHost + "/races/cup/2010/data/standings_official.html" },
		    	{ text: "Nationwide Series", url: "http://nationwide.nascar.com/nationwide-series/races/2010/standings-official" },
			    { text: "Camping World Trucks", url: cnnURLHost + "/races/truck/2010/data/standings_official.html" },
		        { text: "iRacing.com Series", url: cnnURLHost + "/news/headlines/official/iracing.standings/" },
		        { text: "Results - Cup", url: cnnURLHost + "/races/cup/" },
             
                { text: "Results - Nationwide", url: "http://nationwide.nascar.com/nationwide-series/races/2011/schedule" }, 
		        { text: "Results - Truck", url: cnnURLHost + "/races/truck/" }
		    ]
		},
		{
			id: "ocnnSchedule",
		    bgPosX: "-301px",
		    itemdata: [
		        { text: "Sprint Cup Series", url: cnnURLHost + "/races/cup/" },
		        { text: "Nationwide Series", url: "http://nationwide.nascar.com/nationwide-series/races/2011/schedule" },
		        { text: "Camping World Trucks", url: cnnURLHost + "/races/truck/" },
		        { text: "iRacing.com Series", url: cnnURLHost + "/news/110207/iracing-2011-schedule/index.html" },
		        { text: "Broadcast", url: cnnURLHost + "/races/tune_in/" },
				{ text: "Tracks", url: cnnURLHost + "/races/tracks/" },
		        { text: "Tickets", url: cnnURLHost + "/races/cup/" }
		    ]
		},
		{
		    id: "ocnnFantasy",
		    bgPosX: "-395px",
		    itemdata: [
		        { text: "Fantasy Games", url: "http://www.nascar.com/fantasy" },
		        { text: "Online Games", url: "http://www.nascar.com/games" },
		        { text: "Sweepstakes", url: "http://www.nascar.com/games/sweeps" },
		        { text: "Help", url: "http://support.nascar.com" }
		    ]
		},
		{
		    id: "ocnnCommunity",
		    bgPosX: "-474px",
		    itemdata: [
		        { text: "Crews", url: "http://community.nascar.com/go/browse/crews" },
		        { text: "Members", url: "http://community.nascar.com/go/browse/members" },
		        { text: "Blogs", url: "http://community.nascar.com/go/browse/blogs" },
		        { text: "Forums", url: "http://community.nascar.com/go/forum/viewboard" },
		        { text: "Photos", url: "http://community.nascar.com/go/browse/photos" },
		        { text: "Videos", url: "http://community.nascar.com/go/browse/videos" },
		        { text: "Events", url: "http://community.nascar.com/blog/calendar.one" },
		        { text: "Chat", url: cnnURLHost + "/promos/redirects/comm/chat.exclude.html" }
		    ]
		},
		{
			id: "ocnnTrackpass",
		    bgPosX: "-577px",
		    itemdata: [
				{ text: "RaceView", url: cnnURLHost + "/trackpass/about/raceview?sc_cid=hp53" },
				{ text: "PitCommand", url: cnnURLHost + "/trackpass/about/pitcommand?sc_cid=hp52" },
				{ text: "Scanner", url: cnnURLHost + "/trackpass/about/scanner?sc_cid=hp51" },
				{ text: "My TrackPass", url: "http://www.nascar.com/pr/trackpass/" },
				{ text: "My Account", url: "https://secure.audience.nascar.com/services/nascar/flow/overview" },
				{ text: "Help", url: "http://support.nascar.com/" }
		    ]
		},
		{
		    id: "ocnnShop",
		    bgPosX: "-678px",
		    itemdata: [
		        { text: "Shop By Driver", url: "http://store.nascar.com/entry.point?target=z&source=nascar_DHTML:ShopByDriver:03_01_10" },
		        { text: "New Arrivals", url: "http://store.nascar.com/category/index.jsp?categoryId=1408484&source=nascar_dhtml:newarrivals" },
				{ text: "Men's", url: "http://store.nascar.com/category/index.jsp?categoryId=2056459&source=nascar_dhtml:mens" },
		        { text: "Women's", url: "http://store.nascar.com/category/index.jsp?categoryId=2056660&source=nascar_dhtml:womens" },
		        { text: "Kid's", url: "http://store.nascar.com/category/index.jsp?categoryId=2056661&source=nascar_dhtml:kids" },
		        { text: "Die-Cast", url: "http://store.nascar.com/category/index.jsp?categoryId=2056462&source=nascar_dhtml:diecast" },
		        { text: "Custom Shop", url: "http://store.nascar.com/category/index.jsp?categoryId=2747410&source=nascar.dhtml:customshop" },
		        { text: "Sign up and Save", url: "http://store.nascar.com/emailSignup/index.jsp?partner=header&source=nascar.dhtml:signupandsave" },
		        { text: "Gift Cards", url: "http://store.nascar.com/entry.point?entry=3895494&source=nascar_DHTML:GiftCard:10_29_09" }
		    ]
		}
	];

	//INITIALIZE MENU OBJECT POINTER ARRAY
	var hashArray = new Array(submenuData.length);

	this.isHashMenu			= false;
	this.getSubmenuData		= _getSubmenuData;
	this.show				= _show;
	this.hide				= _hide;
	this.hideAll			= _hideAll;
	this.killTimer			= _killTimer;
	this.startTimer			= _startTimer;
	this.doHide				= _doHide;

	function _getSubmenuData(vIndex) {
		return(submenuData[vIndex]);
	}
	function _show(vIndex, vParentElementIdStr) {
		//alert(submenuData[vIndex].id.substring(1));
		d.getElementById(submenuData[vIndex].id.substring(1)).style.background = 'url("' + cnnElHost + '/.element/img/2.1/global/util/sprite.png")' + submenuData[vIndex].bgPosX + ' -98px no-repeat';
		_appendMenuDOM(vIndex, vParentElementIdStr);
	}
	function _hide(vIndex) {
		_removeMenuDOM(vIndex)
	}
	function _hideAll(oIsAll) {
		for (var i = 0; i < hashArray.length; i++) {
			try {
				if (!oIsAll) { //Hide only the one(s) that are active
					if (hashArray[i].isActive == 1) {
						_removeMenuDOM(i)
					}
				} else {
					_removeMenuDOM(i)
				}
			} catch(_eRR) {
				//DO NOTHING alert("can't hide active for " + i);
			}
		}
	}
	function _removeMenuDOM(vMenuIndex) {
		d.getElementById(submenuData[vMenuIndex].id.substring(1)).style.background = 'url("' + cnnElHost + '/.element/img/2.1/global/util/sprite.png")' + submenuData[vMenuIndex].bgPosX + ' -74px no-repeat';
		var pElement = null;
		var cElement = null;
		pElement = d.getElementById(hashArray[vMenuIndex].parentId)
		cElement = d.getElementById(hashArray[vMenuIndex].childId)
		hashArray[vMenuIndex].menuObjDOM = pElement.removeChild(cElement)
		hashArray[vMenuIndex].isActive = 0;
	}
	function _appendMenuDOM(vIndex, vParentElement) {
		var parentDiv = d.getElementById(vParentElement);
		var newDiv 	  = null;
		var newUL 	  = null;
		var newLI 	  = null;
		var newA	  = null;
		var newText	  = null;

		//Clear the timer then Hide All Active Menus
		_killTimer();
		_hideAll(false);

		try {
			//try to append from memory: on error build from scratch
			parentDiv.appendChild(hashArray[vIndex].menuObjDOM);
			hashArray[vIndex].isActive = 1;
			//alert("showing:DOM");
		} catch(eRR) {

			//Create Div element and set the ID attrib to match CSS
			newDiv = d.createElement('div');
			newDiv.setAttribute("id",submenuData[vIndex].id);
			newDiv.onmouseover = new Function("cnnMenuHASH.killTimer();");
			newDiv.onmouseout = new Function("cnnMenuHASH.startTimer();");

			//Create UL element for LI list
			newUL = d.createElement('ul');
			for (var i = 0; i < submenuData[vIndex].itemdata.length; i++) {
				newLI = d.createElement('li');
				newA  = d.createElement('a');
				newText = d.createTextNode(submenuData[vIndex].itemdata[i].text) ;
				newA.setAttribute('href', submenuData[vIndex].itemdata[i].url);
			/* START workaround for indentation of 2010 schedule links -- NSCR-7384
				if (submenuData[vIndex].id == 'ocnnSchedule' && (i == '1' || i == '3' || i == '5')) {
					newSpan = d.createElement('span');
					newSpan.appendChild(newText);
					newSpan.style.paddingLeft = '10px';
					newA.appendChild(newSpan);
				} else {
					newA.appendChild(newText);
				}
			*/

			/* END workaround for indentation of 2010 schedule links -- NSCR-7384
			   remove the code above and uncomment the following to restore to normal*/
				newA.appendChild(newText);
				if (i == (submenuData[vIndex].itemdata.length - 1)) {
					newA.className = 'cnnLastLink';
				}
				newLI.appendChild(newA);
				newUL.appendChild(newLI);
			}
			newDiv.appendChild(newUL);

			//save a pointer to the menu
			hashArray[vIndex] = new cnnNavMenu.MenuType();
			hashArray[vIndex].parentId 	 = vParentElement;
			hashArray[vIndex].childId 	 = submenuData[vIndex].id;
			hashArray[vIndex].menuObjDOM = newDiv;
			hashArray[vIndex].isActive	 = 1;
			//append to the DOM
			parentDiv.appendChild(hashArray[vIndex].menuObjDOM);
		}
	}
	function _killTimer() {
		if (cnnHideMenuTimer) clearTimeout(cnnHideMenuTimer);
		cnnHideMenuTimer = null;
		fwDHFlag = false;
	}
	function _startTimer() {
		fwStart = new Date();
		fwDHFlag = true;
		cnnHideMenuTimer = setTimeout("cnnMenuHASH.doHide()", 250);
	}
	function _doHide() {
		if (!fwDHFlag) return;
		var elapsed = new Date() - fwStart;
		if (elapsed < 250) {
			cnnHideMenuTimer = setTimeout("cnnMenuHASH.doHide()", 275-elapsed);
			return;
		}
		fwDHFlag = false;
		_hideAll(false);
	}
}
/* Initialize the menu */
cnnMenuHASH = new cnnNavMenu.MenuHash();
function cnnSetImgNav() {
    var pEl = d.getElementById('cnnTopNav');
    var navLIs = pEl.getElementsByTagName('li');
    var navAs = pEl.getElementsByTagName('a');

    if (navAs.length > 0 && navAs[0].childNodes[0]) {
        pEl.style.backgroundPosition = '0 -74px';
        for (var j = 0; j < navAs.length; j++) {
            navAs[j].removeChild(navAs[j].childNodes[0]);
            if (((location.hostname.indexOf('nascar.com') == -1) || ((location.hostname.indexOf('.nascar.com') != -1) && (location.hostname.indexOf('www.nascar.com') == -1))) && (location.hostname.indexOf('turner.com') == -1)) {
		document.getElementById('cnnLogoLink').href = "http://www.nascar.com/";
                var subDomain = location.hostname.substr(0,location.hostname.indexOf('.'));
                if (navAs[j].href.indexOf(subDomain) != -1 && navLIs[j].id != 'cnnFantasy' && navLIs[j].id != 'cnnCommunity' && navLIs[j].id != 'cnnShop') {
                    var newHref = 'http://www' + navAs[j].href.split(subDomain)[1];
                 /* alert(subDomain);
                    alert(navAs[j].href.split(subDomain)[1]);
                    alert(newHref); */
                    navAs[j].setAttribute('href', newHref);
                }
            }
        }
    }
}
/* Trigger nav-related functions to run, once the elements have been loaded */
var addLoadEventTimeout = setTimeout(addLoadEvent,1000);
function addLoadEvent() {
	if (d.getElementById('cnnTopNav')) {
		cnnSetImgNav();
		clearTimeout(addLoadEventTimeout);
	} else {
		return addLoadEventTimeout;
	}
}
addLoadEvent();
