//global vars

var nissanTopNavRendered = false;
var nissanMyPath = '/global/nav/';
var nissanMyFileName = 'extendGlobalNav.js';
var nissanScriptTagSrc;
var nissanHost;
var isExternal = false;
var isRemote = true; // regardless of where it is, it's remote for using this file.

if(typeof String.prototype.format != 'function'){
	String.prototype.format=function(){
		var ar=arguments;
		return this.replace(/\{\d+\}/g,function(w){return ar[+w.replace(/[}{]/g,'')]});
	}
}

//Search for nissan script tag and grab src
var nissanScriptTags = document.getElementsByTagName('SCRIPT');
for (n=0; n < nissanScriptTags.length; n++) {
	if ( nissanScriptTags.item(n).src.indexOf( nissanMyPath+nissanMyFileName ) > -1  ) {
		nissanScriptTagSrc = nissanScriptTags.item(n).src;
		break;
	}
}

//set host based on script src
var nissanFullHost = nissanScriptTagSrc.substring(0,nissanScriptTagSrc.indexOf( nissanMyPath+nissanMyFileName ));
// Fixes IE when the full protocol and domain are nto put into the src attribute. IE will only return the actual src attribute source rather than full url to the loaded js file
var nissanHost = (nissanFullHost=='') ? location.hostname : nissanFullHost.split('//')[1];

var nissanRootSrc = location.protocol+'//'+nissanHost;	//For Assets which must follow the current page protocol. Domain is updated during post processing
var nissanRootHref = 'http://'+nissanHost;				//For links which need not follow the current page protocol. Domain is updated during post processing

// added to fix base parameter on external sites
var swfoBaseURL = "";

// Find host pages' url
if (location.href.split('/')[2]!=nissanScriptTagSrc.split('/')[2]) { // page's host is not the same as js script's source
	isExternal = true;
}

if( typeof isSpanishPage == 'undefined'){document.write('<scr'+'ipt src="'+nissanRootSrc+'/js/shared.js" type="text/javascript"></scr'+'ipt>')}

if (typeof swfoWrapper != 'function'){
	document.write('<sc'+'ript src="'+nissanRootSrc+'/js/swfobject/swfnav.js" type="text/javascript"></s'+'cript>');
}

// write CSS to document
document.write("\n<style>\n");
document.write(".tFlashGnav		{position:absolute;display:block;margin:0px auto;left:0px;right:0px;top:0px;width:100%;text-align:center;overflow:visible;z-index:2;}\n");
document.write(".tFlashFnav		{position:relative;z-index:99;margin:0px auto;text-align:center; height:120px;}\n");/* fixed as per SR 17420, removed padding-top:75px; added height:120px;*/
document.write(".tDropDown		{position:relative;display:block;margin:0px auto;left:0px;right:0px;top:75px;width:964px;text-align:center;overflow:visible;z-index:1;}\n");
document.write("table 			{margin:0px auto;}\n");
document.write("body 			{margin:0px auto;text-align:center;}\n");
document.write("</style>\n\n");

// lay down the Flash file - within the div layer - and provide the JS function to expand and shrink it

// Allows proper functionality of the Flash global nav
function swapIndexIn() {
	document.getElementById("dataFlashGlobalNav").style.height = "500px";
}

function swapIndexOut() {
	document.getElementById("dataFlashGlobalNav").style.height = "85px";
}

function renderTopNavigation() {
	var theFlash = '<div id="FlashMovie1" class="tFlashGnav"></div><div class="tDropDown">';
	document.write(theFlash);
}

function renderBottomNavigation() {
	var theFlash = '</div><div id="FlashMovie2" class="tFlashFnav"></div>'.format(nissanRootHref);
	document.write(theFlash);
}

function renderRegionalNavigation() {
	swfoBaseURL = nissanRootHref;
	new swfoWrapper(nissanRootHref+"/flash/regionalnav/regionalnav.swf", 'movFlashRegionalNav', {page:"regionalnav",vehicle:"toolsonly",baseurl:nissanRootHref}, 192, 376);
}

function loadNavs() {
	new swfoWrapper(nissanRootHref+"/flash/globalnav/globalnav.swf", 'FlashMovie1', {page:"globalnav",vehicle:"external",baseurl:nissanRootHref,atr_id:'dataFlashGlobalNav'}, 964, 85);
	new swfoWrapper(nissanRootHref+"/flash/footer/footer.swf", 'FlashMovie2', {vehicle:"external",baseurl:nissanRootHref,atr_id:'FlashMovie2_swf'}, 964, 30);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') window.onload = func;
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function navWrapper(){
	if (typeof swfoWrapper == "function") {
		loadNavs();
	}else{
		setTimeout('loadNavs()','5000');
	}
}

addLoadEvent(navWrapper);

function init() {
	loadNavs();
}