var setSlide = null,
	selectedGlobal = '',
	selectedGIndx = '',
	selectedLocal = '',
	selectedLIndx = '',
	currentGIndx = '',
	currentLIndx = '',
	currentHash = '',
	nextIndx = 0,
	mapStartOpen = false,
	initiated = false,
	timerStarted = false;


function displayNav(){
	var newHtml = '';
	var i = 0;
	newHtml += "<ul>";
	$(xml).find('global').each(function(){
		var $gTab = $(this);
		newHtml += 	"<li id='globalParent_" + i +"' class='globalNav'>" +
						"<div id='gTabGraphics_" + i +"'>" +
							"<img src='V6B.com/assets/tabs/gNavEdge.jpg' style='float:left;'>" +
							"<img src='V6B.com/assets/tabs/gNavShadow.jpg' style='position:absolute; left:-8px; z-indx:-75;'>" +
							"<img src='V6B.com/assets/tabs/gNavEdge.jpg' style='float:right;'>" +
						"</div>";
		if($gTab.attr('tab') == 'News + Resources') newHtml +="<a id='globalNav_" + i +"' class='buttonFill' href='http://www.v6b.com/news/news.html'>" + $gTab.attr('tab') + "</a>";  
		else newHtml +=		"<a id='globalNav_" + i +"' class='buttonFill'>" + $gTab.attr('tab') + "</a>";
		newHtml +=	"</li>";
		i ++;
	});
	i = 0;
	newHtml += "</ul>";
	$("#globalNav").html(newHtml);
	$('#globalNav').find('li').each(function(i){
		$(this).hover(function(){
			$(this).find('div').removeClass("gTabGraphics");
			$(this).find('div').addClass("gRollOver");
			$(this).find('div').css({'display': 'inline'});
		}, function(){
			$(this).find('div').removeClass("gRollOver");
			$(this).find('div').addClass("gTabGraphics");
			$(this).find('div').css({'display': 'none'});
		});
		$("#gTabGraphics_"+i).css({'display': 'none'});
		$("#gTabGraphics_"+i).addClass("gTabGraphics");
		$('#globalNav_' + i).click(function(){checkAndLoad(0, i);});
	});

	var hash = document.location.hash; 
	if (hash != '' || hash != '#' ){
		//alert(hash);
		//var scIndx = 0,
		var	pSlide = '';
		if (/map/g.test(hash)) mapStartOpen = true;
		if (/:/g.test(hash)){
			var pos = hash.indexOf(':');
			var truncHash = hash.slice(0, pos);
			var string = hash.substring((hash.search(/:/)+1), hash.search(/;/));
			var indices  = string.split(/,/);
			var pInstance = indices[0];
			var pSlide = indices[1];
			//alert(pSlide);
			if (mapStartOpen) truncHash += '$map';
		} else var truncHash = hash;
		$(xml).find('global').each(function(k){
			var breakLoop = false;
			//var lTabs=this.find('local');
			var newTab = unescape(document.location.hash);
			$(this).find('local').each(function(m){
				var lTab = $(this).attr("tab"); 
				var testHash = '#'+lTab.replace(/ /g,"_")+'{'+k+','+m+'}';
				if (lTab == "Overview"){
					testHash = '#'+$(this).parent().attr('tab').replace(/ /g,"_")+"_"+lTab.replace(/ /g,"_")+'{'+k+','+m+'}';
					if (mapStartOpen) testHash += '$map';
				} else if (mapStartOpen) testHash += '$map';
				if (truncHash == testHash){
					var $lTab = $(this);
					if (pSlide){
						var pnt = false,
						 	//inst = $lTab.attr("instance");
							inst = pInstance;
						if($lTab.attr("cType") == "portfolioNoThumbs"){ 
							newPntSlide = pSlide;
							//if(!$lTab.attr("tab") == "Main Suppliers") 
							pnt = true;
						}else if($lTab.attr("cType") == "portfolioThumbs") newPtSlide = pSlide;
						//else if($lTab.attr("cType") == "tcc") newTccSlide = pSlide;
						changeSlideHash(inst, pSlide);
						if($lTab.attr("tab") == "Main Suppliers") checkAndLoad(m, k, function(){displayMSSlideshow(Number(inst-9), pSlide)});
						else checkAndLoad(m, k);
					} else checkAndLoad(m, k);
					breakLoop = true;
					return;
				}
				if(breakLoop) return;
			});
			if(breakLoop) return;
		});
	} else {
		if(!splashToOpen) checkAndLoad(0, 0);
	}
}

function checkAndLoad(lIndx, gIndx, cb){
	var string = preloadIndices.toString();
	var expObj = new RegExp(String(gIndx), "g");
	if(!expObj.test(string)){
		if(string == '') preloadSubContent();
		if(!initiated) {
			//$('#splashTitle').css({'display': 'none'});
			$('#splash, #sub_content').fadeOut((fadeTime*1));	
			currentPtSlide = 0;
			clearTimeout(t);
			if(!splashToOpen){
				preloadMap(function(){
					if(mapStartOpen) preloadContent(lIndx, gIndx, function(){mapOpen();});
					else if (cb)preloadContent(lIndx, gIndx, function(){cb();});
					else preloadContent(lIndx, gIndx);
				});
			} else {
				if(mapIsOpen) mapClose();
				$('#loading').fadeIn((fadeTime*1), function(){preloadContent(lIndx, gIndx);});	
			}
		} else {
			$('#content-container, #sub_content').fadeOut((fadeTime*1));
			$('#loading').fadeIn((fadeTime*1), function(){preloadContent(lIndx, gIndx);});
		}
		preloadIndices.push(gIndx);
	} else {
		displayLocal(lIndx, gIndx);
	}
}

function displayLocal(lIndx, gIndx){
	$('#localNav').empty();
	var j = 0;
	var newHtml = "<ul>";
	$(xml).find('global:eq('+gIndx+')').find('local').each(function(){
		var $lTab = $(this);
		var lAttribute = $lTab.attr('tab');
		//var scoredString = lAttribute.replace(/ /g,"_");
		newHtml += "<li id='locParent_" + j +"' class='localNav'>" +
						"<div id='lTabGraphics_" + j +"' class='lTabGraphics'>" +
							"<img src='V6B.com/assets/tabs/lNavEdge.jpg' style='float:left;'>" +
							"<img src='V6B.com/assets/tabs/lNavShadow.jpg' style='position:absolute; left:-10px; z-indx:-75;'>" +
							"<img src='V6B.com/assets/tabs/lNavEdge.jpg' style='float:right;'>" +
						"</div>" +
						"<a id='locNav_" + j + "' class='buttonFill'>" + lAttribute + "</a>" +
					"</li>";
		j++;
	});
	newHtml += "</ul>";
	$('#localNav').html(newHtml);
	$('#localNav').find('li').each(function(i){
		$(this).hover(function(){
			$(this).find('div').removeClass("lTabGraphics");
			$(this).find('div').addClass("lRollOver");
			$(this).find('div').css({'display': 'inline'});
		}, function(){
			$(this).find('div').removeClass("lRollOver");
			$(this).find('div').addClass("lTabGraphics");
			$(this).find('div').css({'display': 'none'});
		});
		$("#lTabGraphics_"+i).css({'display': 'none'});
		$("#lTabGraphics_"+i).addClass("lTabGraphics");
		$(this).find('#locNav_' + i).click(function(){changeLTab(i, gIndx);});
	});
	changeGTab(lIndx, gIndx);
}

function changeGTab(lIndx, gIndx){
	if(selectedGIndx != gIndx || selectedGIndx == ''){
		if (selectedGlobal != '') {
			var indx = selectedGIndx;
			$('#globalNav').find('li:eq('+indx+')').each(function(i){
				$(this).find('a').css({'color': '#939598', 'cursor': 'pointer'});
				$(this).find('a').click(function(){checkAndLoad(0, indx);});
				$(this).find('div').css({'display': 'none'});
				$(this).find('div').addClass("gTabGraphics");
				$(this).hover(function(){
					$(this).find('div').removeClass("gTabGraphics");
					$(this).find('div').addClass("gRollOver");
					$(this).find('div').css({'display': 'inline'});
				}, function(){
					$(this).find('div').removeClass("gRollOver");
					$(this).find('div').addClass("gTabGraphics");
					$(this).find('div').css({'display': 'none'});
				});
			});
		}
		selectedGIndx = gIndx;
		selectedGlobal = '#globalNav_' +gIndx;
		$('#globalNav').find('li:eq('+gIndx+')').each(function(i){
			$(this).find('a').unbind('click');
			$(this).find('a').css({'color': '#369', 'cursor': 'default'});
			$(this).find('div').css({'display': 'inline'});
			$(this).find('div').removeClass("gRollOver");
			$(this).find('div').addClass("gTabGraphics");
			$(this).unbind('mouseenter mouseleave');
		});
	}
	changeLTab(lIndx, gIndx);
}

function changeLTab(lIndx, gIndx){
	if (selectedGIndx != gIndx) selectedLocal = '';
	else if (selectedLocal != '' && selectedGIndx == gIndx) {
		var indx = selectedLIndx;
		$('#localNav').find('li:eq('+indx+')').each(function(i){
			$(this).find('a').css({'color': '#939598', 'cursor': 'pointer'});
			$(this).find('a').click(function(){changeLTab(indx, gIndx);});
			$(this).find('div').css({'display': 'none'});
			$(this).find('div').addClass("lTabGraphics");
			$(this).hover(function(){
				$(this).find('div').removeClass("lTabGraphics");
				$(this).find('div').addClass("lRollOver");
				$(this).find('div').css({'display': 'inline'});
			}, function(){
				$(this).find('div').removeClass("lRollOver");
				$(this).find('div').addClass("lTabGraphics");
				$(this).find('div').css({'display': 'none'});
			});
		});
	}
	selectedLIndx = lIndx;
	selectedLocal = '#localNav_' + lIndx;
	$('#localNav').find('li:eq('+lIndx+')').each(function(i){
		$(this).find('a').unbind('click');
		$(this).find('a').css({'color': '#369', 'cursor': 'default'});
		$(this).find('div').css({'display': 'inline'});
		$(this).find('div').removeClass("lRollOver");
		$(this).find('div').addClass("lTabGraphics");
		$(this).unbind('mouseenter mouseleave');
	});
	displayArrows(lIndx, gIndx);
}

function displayArrows(lIndx, gIndx){
	var $lTab = $(xml).find('global:eq('+gIndx+')').find('local:eq('+lIndx+')');
	var scType = $lTab.attr('scType');
	nextIndex = determineIndex(lIndx, gIndx);
	//	var pntLinkID = '';
	if(!initiated){
		var scIndx = 0;
		if(scType == "c+l") scIndx = 0;
		else if(scType == "linksProducts") scIndx = 1;
		else if(scType == "linksMembership") scIndx = 2;
		else if(scType == "process") scIndx = 3;
		else scIndx = nextIndex+4;
		if(notVisible){
			$('#main_content, #sub_content, #navigation, #footer').fadeIn((fadeTime*1));
			notVisible = false;
		}
		if(splashToOpen){
			splashToOpen = false;
			initiated = true;
		}
	} else if(mapIsOpen) mapClose();
	$('#content').cycle(Number(nextIndex));
	if(scType == "c+l") $('#sub_content').cycle(Number(0));
	else if(scType == "linksProducts") $('#sub_content').cycle(Number(1));
	else if(scType == "linksMembership") $('#sub_content').cycle(Number(2));
	else if(scType == "process") $('#sub_content').cycle(Number(3));
	else $('#sub_content').cycle(Number(nextIndex+4));
	
//	if (scTypes[nextIndex] == "t+t" || (scTypes[nextIndex] != currentScType && scTypes[nextIndex] != null)){
//		if(lTabs[lIndx].getAttribute('cType') == "portfolioNoThumbs") Subcontent.change(nextIndex, function(){linkEffects(lIndx, gIndx, newPntSlide)});
//		else Subcontent.change(nextIndex);
//		currentScType = scTypes[nextIndex];
//	}
	//if (slidesConstructed){
//		if (tccIsOpen){
//			tccClose();
//			newTccSlide = 0;
//		}else if (ptIsOpen){
//			if(splashTimerStarted){
//				clearTimeout(t);
//				splashTimerStarted = false;
//			}
//			ptClose();
//			newPtSlide == null;
//		} else 
		var $currGTab = $(xml).find('global:eq('+Number(currentGIndx)+')');
		var $currLTab = $currGTab.find('local:eq('+Number(currentLIndx)+')');
		if ($currLTab.attr('cType') == 'portfolioNoThumbs'){
			//pntClose();
			//newPntSlide = 0;
			//if (($lTab.attr('scType') == "linksProducts" || $lTab.attr('scType') == "linksCompany") && currentGIndx == gIndx){
				//if($currLTab.attr('tab') == "Main Suppliers"){ 
//					pntLinkID = '#pntLink_'+currentPntSlide;
//					$(pntLinkID+' > *:first').removeClass("selectedPntLinkImage");
//					$(pntLinkID+' > *:first').addClass("pntLinkImage");
//					$(pntLinkID+' > *:first').css({'cursor': 'pointer'});
//				} else 
				if($currLTab.attr('tab') == "Who We Are"){
					pntLinkID = '#pntLinkC_'+currentPntSlide;
					$(pntLinkID).removeClass("selectedPntLink");
					$(pntLinkID).addClass("pntLink");
					$(pntLinkID).css({'cursor': 'pointer'});
				}
				//currentPntSlide = null;
				//newPntSlide = 0;
			//}
		}
//		if(mapIsOpen){
//			mapStartOpen = false;
//			mapClose();
//		}
//		if(mapStartOpen) mapOpen();
//		if (lTabs[lIndx].getAttribute('cType') == "tcc"){
//			tccOpen(Number(lTabs[lIndx].getAttribute('instance')), newTccSlide);
//			//if(newTccSlide != 0){
////				changeSlide(newTccSlide);
////				newTccSlide = 0;	
////			}
//		} else 
		if ($lTab.attr('cType') == "mainSuppliers" && $("#allMSSlideshows").css('display') == 'inline'){
			var indx = $("#allMSSlideshows").cycle('index');
				inst = Number(indx+10),
				slide = $('#slideshow_'+inst).cycle('index');
			//$("#allMSSlideshows").css({'display': 'inline'});
			//$("#allMSSlideshows").cycle(Number(indx));
			//linkEffects(1, 1, indx);
			changeSlideHash(inst, slide);
		} else if ($lTab.attr('cType') == "portfolioThumbs"){
			//ptOpen($lTab.attr('title'), Number($lTab.attr('instance')));
			var inst = $lTab.attr('instance');
			var ID = '#ptNav_'+inst;
			var length = $('#slideshow_'+inst).cycle('length');
			var slide = $('#slideshow_'+inst).cycle('index');
			if (slide == 0) var padLeft = (12-length)*47+93;
			else var padLeft = (12-length)*47+64;
			$(ID).css("padding-left", padLeft+"px");
			$(ID).css("width", (750-padLeft)+"px");
			ptPrevInvis = true;
			currentPtSlide = $('#slideshow_'+inst).cycle('index');
//			if(newPtSlide != (allPtElements[lTabs[lIndx].getAttribute('instance')].length - 1)){
//				//changeSlideHash(newPtSlide);
//				newPtSlide = null;	
		} else if ($lTab.attr('cType') == "portfolioNoThumbs") {
			//pntOpen(Number(lTabs[lIndx].getAttribute('instance')));
			//currentPtSlide = $('#slideshow_'+$lTab.attr('instance')).cycle('index');
			var inst = $lTab.attr('instance');
			var ID = '#pntNav_'+inst;
			var length = $('#pntSlideshow_'+inst).cycle('length');
			var slide = $('#pntSlideshow_'+inst).cycle('index');
			if (slide == 0) var padLeft = (12-length)*47+93;
			else var padLeft = (12-length)*47+64;
			$(ID).css("padding-left", padLeft+"px");
			$(ID).css("width", (750-padLeft)+"px");
			ptPrevInvis = true;
			//currentPtSlide = $('#slideshow_'+inst).cycle('index');
			newPntSlide = $('#pntSlideshow_'+$lTab.attr('instance')).cycle('index');
			linkEffects(lIndx, gIndx, newPntSlide);
//			if($lTab.attr('tab') == "Main Suppliers"){
//				pntLinkID = '#pntLink_'+slide;
//				$(pntLinkID+' > *:first').removeClass("pntLinkImage");
//				$(pntLinkID+' > *:first').addClass("selectedPntLinkImage");
//				$(pntLinkID+' > *:first').css({'cursor': 'default'});
//			}else if($lTab.attr('tab') == "Who We Are"){
//				pntLinkID = '#$pntLink_'+slide;
//				$(pntLinkID).removeClass("pntLink");
//				$(pntLinkID).addClass("selectedPntLink");
//				$(pntLinkID).css({'cursor': 'default'});
//			}
//			if(newPntSlide != 0) {
//				currentPntSlide = newPntSlide;
//				changeSlide(newPntSlide);
//				newPntSlide = 0;
//			} else {
//				currentPntSlide = 0;	
//			}
		}
//	}
	
	currentGIndx = gIndx;
	currentLIndx = lIndx;
	$("#previous").empty();
	$("#previous").html(previousSlide(lIndx, gIndx));
	$("#next").empty();
	$("#next").html(nextSlide(lIndx, gIndx));
	if(!initiated){
		if(!timerStarted)testHash();
		initiated = true;
	} else changeURL(lIndx, gIndx);
	
	//displayContent(lTabs[lIndx]);
}

function determineIndex(lIndx, gIndx){
	var counter = 0;
	$(xml).find('global:lt('+gIndx+')').each(function(){
		$(this).find('local').each(function(){
			counter++;
		});										
	});
	counter += Number(lIndx);
	return counter;
}

function previousSlide(lIndx, gIndx){
	//var lTabs=gTabs[gIndx].getElementsByTagName("local");
	var newHtml = '';
	if(lIndx == 0 && gIndx == 0){
		return newHtml;
	} else if (lIndx > 0){
		lIndx--;
		newHtml = '<a class="arrow" onclick="changeLTab(' + lIndx + ', ' + gIndx + ');"><img src="V6B.com/assets/leftArrow.png"></a>';
		return newHtml;
	} else if (gIndx > 0){
		gIndx--;
		lTabs = $(xml).find('global:eq('+gIndx+')').find('local');
		lIndx = lTabs.length - 1;
		newHtml = '<a class="arrow" onclick="checkAndLoad(' + lIndx + ', ' + gIndx + ');"><img src="V6B.com/assets/leftArrow.png"></a>';
		return newHtml;
	}
}

function nextSlide(lIndx, gIndx){
	var gTabs = $(xml).find('global');
	var lTabs = $(xml).find('global:eq('+gIndx+')').find('local');
	var newHtml = '';
	if(lIndx == lTabs.length-1 && gIndx == gTabs.length-1){
		return newHtml;
	} else if (lIndx < (lTabs.length-1)){
		lIndx++;
		newHtml = '<a class="arrow" onclick="changeLTab(' + lIndx + ', ' + gIndx + ');"><img src="V6B.com/assets/rightArrow.png"></a>';
		return newHtml;
	} else if (gIndx < gTabs.length-1){
		gIndx++;
		if($(xml).find('global:eq('+gIndx+')').attr('tab') == 'News + Resources'){ 
			newHtml ='<a class="arrow" href="http://www.v6b.com/news/news.html"><img src="V6B.com/assets/rightArrow.png"></a>';
			return newHtml;	
		}
		lTabs=$(xml).find('global:eq('+gIndx+')').find('local');
		newHtml = '<a class="arrow" onclick="checkAndLoad(' + 0 + ', ' + gIndx + ');"><img src="V6B.com/assets/rightArrow.png"></a>';
		return newHtml;
	}
}

function changeURL(lIndx, gIndx){
	//alert('changeUrl');
	var isPortfolio = false;
	var $lTab = $(xml).find('global:eq('+gIndx+')').find('local:eq('+lIndx+')');
	var lAttribute = $lTab.attr('tab');
	if ($lTab.attr('instance') || lAttribute == "Main Suppliers"){
		if(lAttribute == "Main Suppliers" && $("#allMSSlideshows").css('display') == 'inline'){
			isPortfolio = true;
			var slideshowIndx = $("#allMSSlideshows").cycle('index');
			var instance = $lTab.find('slideshow:eq('+slideshowIndx+')').attr('instance');
			var slide = $('#slideshow_'+instance).cycle('index');
		} else if(lAttribute != "Main Suppliers") {
			isPortfolio = true;
			var instance = $lTab.attr('instance');
			if($lTab.attr("cType") == "portfolioNoThumbs") var slide = currentPntSlide;
			else if($lTab.attr("cType") == "portfolioThumbs"){
				//alert(newPtSlide);
				//if (newPtSlide == null) var slide = 0;
				//else 
				var slide = currentPtSlide;
				//newPtSlide == null;
			}
		}
		//else if($lTab.attr("cType") == "tcc") var slide = newTccSlide;
	}
	var newString = lAttribute;
	if(lAttribute == "Overview"){
		newString = $(xml).find('global:eq('+gIndx+')').attr("tab")+"_"+lAttribute;
	}
	var scoredString = newString.replace(/ /g,"_");
	if(document.location.hash != null){
		var hash = document.location.hash;
		if (/map/g.test(hash)){
			var pos = hash.indexOf('$');
			var truncHash = hash.slice(0, pos);
			document.location.hash = truncHash;
		}
		var newHash = scoredString+"{"+gIndx+","+lIndx+"}";
		if(isPortfolio){
			newHash += ":"+instance+","+slide+";"
		}
		if($lTab.attr("cType") == "portfolioNoThumbs") currentPntSlide = slide;
		else if($lTab.attr("cType") == "portfolioThumbs") newPtSlide = slide;
		//else if($lTab.attr("cType") == "tcc") newTccSlide = slide;
		//newPntSlide = 0;
		//if(mapStartOpen)newHash += '$map'; 
		document.location.hash = newHash;
		//currentHash = document.location.hash;
	}
	//if(!timerStarted)testHash();
	
	//changeGTab(gIndx);
	//changeLTab(lIndx, gIndx);
}

function testHash(){
	if(document.location.hash != currentHash){
		var hash = document.location.hash;
		if (/map/g.test(currentHash) && !/map/g.test(hash)){
			mapClose();
			currentHash = hash;
			var timer = setTimeout("testHash()",100);
			timerStarted = true;
			return;
		} else if (!/map/g.test(currentHash) && /map/g.test(hash)){
			mapOpen();
			currentHash = hash;
			var timer = setTimeout("testHash()",100);
			timerStarted = true;
			return;
		} 
		else if ((hash=='' || hash =='#') && !(currentHash == '' || currentHash =='#')){
			window.location.replace("http://www.v6b.com/");
		}
		currentHash = hash;
		var string = currentHash.substring((currentHash.search(/{/)+1), currentHash.search(/}/));
		string += ',';
		string += currentHash.substring((currentHash.search(/:/)+1), currentHash.search(/;/));
		var indices  = string.split(/,/);
		var gIndx = indices[0];
		var lIndx = indices[1];
		var pInstance = indices[2];
		var pSlide = indices[3];
		var $lTab = $(xml).find('global:eq('+gIndx+')').find('local:eq('+lIndx+')');
		var pnt = false;
		if($lTab.attr("cType") == "portfolioNoThumbs"){
			//if(!$lTab.attr("tab") == "Main Suppliers") 
			pnt = true;
			newPntSlide = pSlide;
			linkEffects(lIndx, gIndx, newPntSlide);
		}
		//if(lTabs[lIndx].getAttribute("cType") == "portfolioThumbs") newPtSlide = pSlide;
		//if($lTab.attr("cType") == "tcc") newTccSlide = pSlide;
		if(currentGIndx != gIndx) checkAndLoad(lIndx, gIndx);
		else if(currentLIndx != lIndx) changeGTab(lIndx, gIndx);
		else changeSlide(pInstance, pSlide, pnt);
	}
	var timer = setTimeout("testHash()",100);
	timerStarted = true;
}