




function NavTab(URL, ImgOn, ImgOnWidth, ImgOnHeight, ImgOff, ImgOffWidth, ImgOffHeight, ALT, base) {	
	this.imgOn = new MCImage(relative(ImgOn, base), ImgOnWidth, ImgOnHeight);
	this.imgOff = new MCImage(relative(ImgOff, base), ImgOffWidth, ImgOffHeight);
	this.hasImage = (ImgOff.length > 0)?true:false;
	this.key = URL;
	this.url = relative(URL, base);
	this.alt = ALT;
	this.inPath = false;
	this.subnav = new Array();	
}

function MCImage(s, w, h) {
	this.src = s;
	this.width = new Number(w);
	this.height = new Number(h);	
}

function NavLink(URL, Label, base) {
	this.label = Label;
	this.key = URL;
	this.url = relative(URL, base);
	this.inPath = false;
	this.subnav = new Array();	
}

function NavSystem() {
	this.nPrimary = null;
	this.nSecondary = null;
	this.nTertiary = null;
	this.baseURL = "";
	this.subnav = new Array();
}	

function StripWhiteSpace (s) {   var i;
    var returnString = "";
    bag = " \t\n\r";;
    
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function writeNavTabs() {
	var navObj = getNavObject(this.baseURL, this);
	baseURLTokens = this.baseURL.split("/");
	
	if (navObj) navObj.url = baseURLTokens[baseURLTokens.length-1];
							
	function getNavObject(key, base) {
		var i; 
		var myKey = key;
		var myBase = base;
		var myObj = null;
		for (i=0; i < myBase.subnav.length; i++) {		
			if (myKey == myBase.subnav[i] || myKey == myBase.subnav[i] + "index.html") {
				setNavPath(myBase.subnav[i].item);
			 	return myBase.subnav[i].item;
			} 
			if (myBase.subnav[i].item.subnav.length) {
				myObj = getNavObject(myKey, myBase.subnav[i].item)
				if (myObj != null) {
					setNavPath(myObj);
					return myObj;
				}
			}					
		}
	}
	
	function setNavPath($obj) {
		var obj = $obj;
		obj.inPath = true;
		if (obj.parent) setNavPath(obj.parent);
	}			
	
	//document.write("<table border='0' cellpadding='0' cellspacing='0'>");
	//document.write("<tr>");
	var selectedTabIdx;

	// Primary Navigation Tabs	

	var wp = new Array();
	var wpi = new Array();	
        var sdts_rk = "";
	for (i=0; i < this.subnav.length; i++) {
		if (this.subnav[i].item.inPath)  {		
			if (this.subnav[i].item.hasImage)  sdts_rk="O"; //document.write("<td><a href='" + wsmlMakeWebServiceHref(this.subnav[i].item.url) + "'><img src='" + this.subnav[i].item.imgOn.src + "' alt='" + this.subnav[i].item.alt + "' width='" + this.subnav[i].item.imgOn.width + "' height='" + this.subnav[i].item.imgOn.height + "' border='0'></a></td>");	
						//if (this.subnav[i].item.hasImage) document.write("<td><a href='" + wsmlMakeWebServiceHref(this.subnav[i].item.url) + "' onClick='javascript:setCurrentTab(" + i + ");'" + "><img src='" + this.subnav[i].item.imgOn.src + "' alt='" + this.subnav[i].item.alt + "' width='" + this.subnav[i].item.imgOn.width + "' height='" + this.subnav[i].item.imgOn.height + "' border='0'></a></td>");
						this.nPrimary = this.subnav[i].item;
						this.nPrimaryIMG = this.subnav[i].item.url;
						wpi[i] = this.subnav[i].item.url;
						wp[i] = this.subnav[i].item.imgOn.width;
			selectedTabIdx = i;
		} else {
			if (this.subnav[i].item.hasImage) sdts_rk="K"; //document.write("<td><a href='" + wsmlMakeWebServiceHref(this.subnav[i].item.url) + "'><img src='" + this.subnav[i].item.imgOff.src + "' alt='" + this.subnav[i].item.alt + "' width='" + this.subnav[i].item.imgOff.width + "' height='" + this.subnav[i].item.imgOff.height + "' border='0'></a></td>");	
			//if (this.subnav[i].item.hasImage) document.write("<td><a href='" + wsmlMakeWebServiceHref(this.subnav[i].item.url) + "' onClick='javascript:setCurrentTab(" + i + ");'" + "><img src='" + this.subnav[i].item.imgOff.src + "' alt='" + this.subnav[i].item.alt + "' width='" + this.subnav[i].item.imgOff.width + "' height='" + this.subnav[i].item.imgOff.height + "' border='0'></a></td>");
						this.nPrimaryIMG = this.subnav[i].item.url;
						wpi[i] = this.subnav[i].item.url;
			wp[i] = this.subnav[i].item.imgOn.width;
		}				
	}
	
	//document.write("<td width='100%' background='" + relative("/us/images/spacer.gif", this.baseURL) + "'><img src='" + relative("/us/images/spacer.gif", this.baseURL) + "' alt='rule' width='100%' height='16' border='0'></td>");
	//document.write("</tr>");
	//document.write("</table>");
	
	document.write("<table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#FFFFFF'>");
	document.write("<tr>");	
	
	// Secondary Navigation Tabs			
	if (this.nPrimary && this.nPrimary.subnav.length) {
		var L = 0;
		for (i=0; i < this.nPrimary.subnav.length; i++) {
			L += this.nPrimary.subnav[i].item.imgOff.width;
		}
				
				var secondNavTabWidth = (L/2);
				var currentPrimaryTabWidth = getWidth(selectedTabIdx,wp);

		
//		document.write("<td><img src='" + this.spacer.src + "' width='" + (((2*this.nPrimary.index+1)*this.subnav[this.nPrimary.index].item.imgOff.width-L)/2) + "' height='26' alt='' border='0'></td>");
	
			if ((currentPrimaryTabWidth - secondNavTabWidth) != -1 ) {
				document.write("<td><img src='" + this.spacer.src + "' width='" + (currentPrimaryTabWidth - secondNavTabWidth) + "' height='26' alt='' border='0'></td>");
			}
			else {
				if (( currentPrimaryTabWidth - secondNavTabWidth ) > window.screen.availWidth) {
				document.write("<td><img src='" + this.spacer.src + "' width='" + (window.screen.availWidth - (secondNavTabWidth*2)) + "' height='26' alt='' border='0'></td>");
			}}

	
        if (this.nPrimary.subnav[0].item.hasImage)
           // document.write("<td><img src='" + relative("/us/images/nav_sub_left-border.gif", this.baseURL) + "' width='1' height='16' border='0' alt=''></td>");

		for (i=0; i < this.nPrimary.subnav.length; i++) {
			if ((this.nPrimary.subnav[i].item.inPath) || (this.nPrimary.subnav[i].item.url.indexOf(glbBaseURL) > 0) ){
				if (this.nPrimary.subnav[i].item.hasImage) document.write("<td><img src='" + this.nPrimary.subnav[i].item.imgOn.src + "' width='" + this.nPrimary.subnav[i].item.imgOn.width + "' height='" + this.nPrimary.subnav[i].item.imgOn.height + "' alt='" + this.nPrimary.subnav[i].item.alt + "' border='0'></td>");
				this.nSecondary = this.nPrimary.subnav[i].item;
			} else {
				if (this.nPrimary.subnav[i].item.hasImage) document.write("<td><a href='" + wsmlMakeWebServiceHref(this.nPrimary.subnav[i].item.url) + "'><img src='" + this.nPrimary.subnav[i].item.imgOff.src + "' width='" + this.nPrimary.subnav[i].item.imgOff.width + "' height='" + this.nPrimary.subnav[i].item.imgOff.height + "' alt='" + this.nPrimary.subnav[i].item.alt + "' border='0'></a></td>");	
			}				
		}
	
		document.write("<td width='100%'>&nbsp;</td>");
		document.write("</tr>");
		document.write("<tr>");
		document.write("<td width='100%' bgcolor='#FFFFFF' colspan='" + (this.nPrimary.subnav.length + 3) + "'><img src='" + this.spacer.src + "' width='1' height='26' alt='' border='0'></td>");
		document.write("</tr>");
		
	} else {
		document.write("<td width='100%' bgcolor='#FFFFFF'><img src='" + this.spacer.src + "' width='1' height='26' alt='' border='0'></td>");
		document.write("</tr>");
	}
	
	document.write("</table>");
	document.write("</td>");
	document.write("</tr>");
	document.write("<tr>");
	document.write("<td bgcolor='#FFFFFF' valign='top'>");
}

function writeSubNav() {
	if (this.nSecondary && this.nSecondary.subnav.length) {
		document.write("<table border='0' cellspacing='0' cellpadding='0' width='200'>");
		document.write("<tr>");
		document.write("<td rowspan='100%'><img src='" + this.spacer.src + "' width='25' height='1' alt='' border='0'/></td>");
		document.write("<td><img src='" + this.spacer.src + "' width='15' height='15' alt='' border='0'/></td>");
		document.write("<td width='100%'><img src='" + this.spacer.src + "' width='1' height='1' alt='' border='0'/></td>");
		document.write("<td rowspan='100%'><img src='" + this.spacer.src + "' width='15' height='1' alt='' border='0'/></td>");
		document.write("</tr>");
		for (i=0; i < this.nSecondary.subnav.length; i++) {
		    var strs = StripWhiteSpace(this.nSecondary.subnav[i].item.label);
		    if (strs != "") {
			if ( (this.nSecondary.subnav[i].item.inPath)  || (this.nSecondary.subnav[i].item.url.indexOf(glbBaseURL) > 0) ) {
				this.nTertiary = this.nSecondary.subnav[i].item;
				document.write("<tr>");
				document.write("<td valign='top'><img src='/us/images/arrow_down.gif' width='13' height='8' alt='' border='0' vspace='3'></td>");
				document.write("<td>");
				document.write("<a href='" + wsmlMakeWebServiceHref(this.nSecondary.subnav[i].item.url) + "' class='linkselected'>" + this.nSecondary.subnav[i].item.label + "</a>");
				if (this.nTertiary.subnav.length) {
					for (j=0; j < this.nTertiary.subnav.length; j++) {
						document.write("<br /><img src='/us/images/arrow_right_small.gif' width='9' height='7' alt='' border='0'><a href='" + wsmlMakeWebServiceHref(this.nTertiary.subnav[j].item.url) + "' class='linksmall'>" + this.nTertiary.subnav[j].item.label + "</a>");
					}
				}
				document.write("</td>");
				document.write("</tr>");
			} else {
				document.write("<tr>");
				document.write("<td valign='top'><img src='/us/images/arrow_right_large.gif' width='6' height='11' alt='' border='0' hspace='3' vspace='3'></td>");

				if(this.nSecondary.subnav[i].item.url == "../../../promotions/dineout/search.do" ) {
				document.write("<td><a href='" + wsmlMakeLeakHref(this.nSecondary.subnav[i].item.url) + "' class='link'>" + this.nSecondary.subnav[i].item.label + "</a></td>");
				}
				else {
				document.write("<td><a href='" + wsmlMakeWebServiceHref(this.nSecondary.subnav[i].item.url) + "' class='link'>" + this.nSecondary.subnav[i].item.label + "</a></td>");
				
				}

				document.write("</tr>");
			}
			document.write("<tr>");
			document.write("<td colspan='2'><img src='" + this.spacer.src + "' width='1' height='3' alt='' border='0'></td>");
			document.write("</tr>");
		    }
		}
		document.write("<tr>");
		document.write("<td colspan='2'><img src='" + this.spacer.src + "' width='1' height='20' alt='' border='0'></td>");
		document.write("</tr>");
		document.write("</table>");
	}
}

function writeFooterLinks() {
	var home = this.subnav[0].item;
	for (i=0; i<home.subnav.length; i++) {
		document.write("<a href='" + wsmlMakeWebServiceHref(home.subnav[i].item.url) + "' class='globalHeaderLinks'>" + home.subnav[i].item.alt + "</a>" + "&nbsp;&nbsp;&nbsp;&nbsp;");
            if (i != home.subnav.length-1) document.write(" | " + "&nbsp;&nbsp;&nbsp;&nbsp;");
	}	
}

function getCookie(name) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(name+"=");
	var value = "";
	if (pos != -1) {
		var start = pos + name.length + 1;
		var end = allcookies.indexOf(";", start);
		if (end == -1) end = allcookies.length;
		var value = allcookies.substring(start, end);
		value = unescape(value);
	}
	return value;
}

function setCookie(name, value) {
	var nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear() + 1);
	
	var sCookie = name + "=" + escape(value);
	sCookie += "; expires=" + nextyear.toGMTString();
	sCookie += "; path=/";
	//sCookie += "; domain=mastercard.com";
	
	document.cookie = sCookie;
}

function deleteCookie(name) {
	var lastyear = new Date();
	lastyear.setFullYear(lastyear.getFullYear() - 1);
	
	document.cookie = name + "=; expires=" + lastyear.toGMTString();
}

function pop(url) {
	window.open(url); 
	return void(null);
}

function relative(resourceURL, locationURL) {
	var relativePath = "";
	
	if (resourceURL.indexOf("http://") == 0) {
		relativePath = 'javascript:pop("' + resourceURL + '")';
              //relativePath = resourceURL;
		return relativePath;
	}
	
      if (resourceURL.indexOf("https://") == 0) {
		//relativePath = 'javascript:pop("' + resourceURL + '")';
              relativePath = resourceURL;
		return relativePath;
	}

	aResource = resourceURL.substring(1, resourceURL.length).split("/");
	aLocation = locationURL.substring(1, locationURL.length).split("/");

	// first, remove anything in either path that is common
	var start = 0;
	
	for (m=0; m<aResource.length; m++) {
		if (m <= aLocation.length && aResource[m] != aLocation[m]) {
			start = m;			
			break;					
		}
	}
	
	aResource = aResource.slice(start, aResource.length);
	aLocation = aLocation.slice(start, aLocation.length);
	
	// then, get the relativePathative path from locationURL to the root
	var n = 0;
	while (n < aLocation.length-1) {
		relativePath += "../";
		n++;		
	}

	// lastly, add each remaining element of the resource path to the relativePath path.
	for (p=0; p<aResource.length; p++) {
		relativePath = (p==aResource.length-1)?relativePath + aResource[p]:relativePath + aResource[p] + "/";
	}

	return relativePath;
}

function addNavSystemObject(obj) {
	index = this.subnav.length;
	key = this.subnav[index] = new String(obj.key);
	key.item = obj;
	key.item.index = index;
	return key.item;
}

function addNavTabObject(obj) {
	index = this.subnav.length;
	key = this.subnav[index] = new String(obj.key);
	obj.parent = this;
	//if(!obj.hasImage) this.imgOn = this.imgOff;
	key.item = obj;
	return key.item;
}

function addNavLinkObject(obj) {
	index = this.subnav.length;
	key = this.subnav[index] = new String(obj.key);
	obj.parent = this;
	key.item = obj;
	return key.item;
}

function setBaseURL(URL) {
	this.baseURL = URL;
	this.spacer = new Image();
	this.spacer.src = relative("/us/images/spacer.gif", this.baseURL);
			
			
			navL1 = this.add(new NavTab("/us/specialoffers/index.html","/us/images/cobrands_nav/savings_sub_offers_on.gif","98","13","/us/images/cobrands_nav/savings_sub_offers.gif","98","13","Home", this.baseURL));

			navL2 = navL1.add(new NavTab("/us/specialoffers/index.html","/us/images/cobrands_nav/cobrands_sub_home_on.gif","42","13","/us/images/cobrands_nav/cobrands_sub_home.gif","42","13","Home", this.baseURL));

			navL2 = navL1.add(new NavTab("/us/specialoffers/backyard/off_rules.html","","","","","","","", this.baseURL));
			navL2 = navL1.add(new NavTab("/us/specialoffers/win12cars/off_rules.html","","","","","","","", this.baseURL));
			navL2 = navL1.add(new NavTab("/us/specialoffers/bedbathbeyond/official_rules.html","","","","","","","", this.baseURL));
			
							
				
				
								//if (Promotion_5 == true) {
								//navL2 = navL1.add(new NavTab("/us/specialoffers/freenights/index.html","/us/wce/GIF/7239_freenights_subtab_on.gif","72","13","/us/wce/GIF/7240_freenights_subtab_off.gif","72","13","Free Nights", this.baseURL));
								//}
								if (Promotion_5 == true) {
								navL2 = navL1.add(new NavTab("/us/specialoffers/Icons/index.html","/us/wce/GIF/6966_specialoffers_comm_on.gif","78","23","/us/wce/GIF/6967_specialoffers_comm_off.gif","78","23","Commercials", this.baseURL));
								}
								//if (Promotion_6 == true) {							
								//navL2 = navL1.add(new NavTab("/us/specialoffers/priceless_experience_2005/index.html","/us/images/priceless_sub_on.gif","114","13","/us/images/priceless_sub_off.gif","114","13","Priceless Experience", this.baseURL));
								//}
								
								//navL3 = navL2.add(new NavLink("/us/specialoffers/priceless_experience_2005/index.html","Priceless Experience 2005", this.baseURL));							
								//navL3 = navL2.add(new NavLink("/us/specialoffers/priceless_experience_2005/off_rules.html","Official Rules", this.baseURL));			
								
								if (Promotion_4 == true) {
								navL2 = navL1.add(new NavTab("/us/specialoffers/savings_discounts/index.html","/us/images/cobrands_nav/savings_discounts_on.gif","113","13","/us/images/cobrands_nav/savings_discounts_off.gif","113","13","Savings & Discounts", this.baseURL));
								}								
								if (Promotion_2 == true) {
								navL2 = navL1.add(new NavTab("/us/specialoffers/premiumoffers/index.html","/us/images/nav/premium_collection_on.gif","107","13","/us/images/nav/premium_collection_off.gif","107","13","Premium Collection Offers", this.baseURL));
								}
					

}


NavSystem.prototype.setBaseURL = setBaseURL;
NavSystem.prototype.writeNavTabs = writeNavTabs;
NavSystem.prototype.writeSubNav = writeSubNav;
NavSystem.prototype.writeFooterLinks = writeFooterLinks;
NavSystem.prototype.add = addNavSystemObject;
NavTab.prototype.add = addNavTabObject;
NavLink.prototype.add = addNavLinkObject;

function getTabNumber(selectedTabUrl, tabUrls) {
	alert("Selected Tab Url: " +selectedTabUrl);
	
	for(m=0; m < tabNames.length; m++) {
	alert("Current Loop Tab Url: " +tabUrls[m]);
		if (selectedTabUrl == tabUrls[m])  {
//			wpt += (wp[m]/2);
			alert("M " +m);
			return m;
//			document.write("<td><img src='" + this.spacer.src + "' width='" + (wpt-b) + "' height='26' alt='' border='0'></td>");	
		}
	}
}	





// this function is called whenever a tab is clicked in order to obtain the index of the selected tab
function setCurrentTab(tabIdx) {
    this.selectedTabIdx = tabIdx;
    //alert("tabIndex: " + tabIdx);
//    alert("tabIndex: " + this.selectedTabIdx);
}


// obtain the width for the currently selected tab
function getWidth(tabIdx,wp) {

   var width = wp[tabIdx]/2;
//   alert("last tab width: " + width);
   
   if( tabIdx > 0 ) {
      for( var i=0; i < tabIdx; i++ ) {
         width = width + wp[i];
//         alert("width + tabIdx(" + i + "): " + width);
      }
   }
   
   return width;   
}



var nav = new NavSystem();



