/*
Author:	Chris Winberry
Date:	07/05/2002
Updated by DFoley on 10/2/2007 for BN Inc store locator pages to work with RGA Site redesign

Desc:	Dynamically builds IFRAME/ILAYER tags for external partner sites to display BN navigation
*/
var jsIframe_js = true; /* For checking if the script has loaded */

function jsIframe_buildIframe (which, querystring, env) {
	var isStoreLocator = (env == "storelocator");
	var width; 
	var height;
	var src;
	var name;
	var ilayer;
	var iframe;
	var hasSourceid;

	if (isStoreLocator){ 
		if (querystring.toString().toLowerCase().indexOf("btob") > -1) {
			env = "btob";
		}
		else {
			env = "web";
		}
	}
	
	hasSourceid = (querystring.toString().toLowerCase().indexOf("sourceid=") >= 0);
	if (querystring.toString().toLowerCase().indexOf("btob") > -1) {
		env = "btob";
		}
	
	var hostname = hostnameToUse(env);

	switch (which) {
		case "jsIframe_nav_top" :
			querystring = "bar=storelocator" + ((querystring.length > 0) ? "&" + querystring : "");
			width	= "990";
			height 	= "119";
			src		= "http://" + hostname + "/include/iframe/partner_navbar.asp?" + querystring;
			name	= "navbar";
			ilayer	= false;
			iframe	= "frameborder=\"0\" scrolling=\"no\"";
			break;
		case "jsIframe_nav_bot" :
			querystring = "bar=storelocator" + ((querystring.length > 0) ? "&" + querystring : "");
			width	= "990";
			height	= "146";
			src		= "http://" + hostname + "/include/iframe/partner_footer.asp?" + querystring;
			name	= "bottombar";
			ilayer	= false;
			iframe	= "frameborder=\"0\" scrolling=\"no\"";
			break;
		default :
			return(false);
		}

	return(_jsIframe_renderIframe(width, height, src, name, ilayer, iframe));
	}

function _jsIframe_renderIframe (width, height, src, name, ilayerAttrib, iframeAttrib) {
	document.write(
		"<iframe width=\"" + width + "\" height=\"" + height + "\" src=\"" + src + "\"" +
		" name=\"" + name + "\"" + (iframeAttrib ? (" " + iframeAttrib) : "") + ">" +
		"</iframe>"
		);
		
	return(true);
	}

function hostnameToUse(env) {
    var btobNav = "btobNav";

    var hostname = "www.barnesandnoble.com";
    var btobNavCookieExists = readCookie(btobNav);
	if (btobNavCookieExists == null && env == "btob") {
	    createCookie(btobNav,"yes");
	}
	
	if (btobNavCookieExists=="yes" || env == "btob")
	    hostname = "btob.barnesandnoble.com";
	    
	return hostname;
}

/* http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
    var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; domain=barnesandnoble.com;path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}