var xml;
$(document).ready(function() {
	$.ajax({
		type: "GET",
    	url: "V6B.com/nav.xml",
		dataType: "xml",
   		success : function(data){
			xml = data;
			displayLoading();
		}
		//complete : constructSite()
	});
});


var	fadeTime = 500,
	images = [],
	count0 = 0,
	count1 = 0,
	ptIsOpen = false,
	ssIDs = [],
	preloadIndices = [],
	notVisible = true;
	mapIsOpen = false,
	mapToOpen = false,
	ptPrevInvis = true,
	ptNextInvis = false,
	newPntSlide = 0,
	newPtSlide = 0,
	currentPntSlide = 0,
	currentPtSlide = 0,
	splashToOpen = false,
	t = '',
	splashTimerStarted = false,
	mapStartOpen = false,
	graphicsCounter = 0;

function displayLoading(){
	repositionSite();
	var img = new Image();
	$(img).load(function () {
		$('#loading').prepend(this).fadeIn((fadeTime*1), function(){
			compileContent(0, 0, function(){
				constructSite();
			});
		});
	}).attr('src', 'V6B.com/assets/loading_bar.gif');
}

function constructSite(){
	//may need to readdress (cIndx, scIndx, cb)	
	if (document.location.hash == '' || document.location.hash == '#'){
		splashToOpen = true;
		//displaySplash();
		preloadSiteGraphics(function(){compileSplash()});
		displayNav();
	} else if (document.location.hash == '#$map'){
		splashToOpen = true;
		mapStartOpen = true;
		preloadSiteGraphics(function(){compileSplash()});
		displayNav();
	} else preloadSiteGraphics(function(){displayNav();});
}

function repositionSite(){
	var pageWidth = $(window).width(), 
		pageHeight = $(window).height();
  	var marginTop = (pageHeight - 600)/2;
	if(marginTop < 0) marginTop = 0;
	var gradHeight = marginTop + 300;
	var colourHeight = marginTop*2 + 600;
	if(marginTop < 10){ marginTop = 10; gradHeight += 20; colourHeight += 40;}
	var marginLeft = (pageWidth - 1000)/2;
  	if(marginLeft < 10) marginLeft = 10;
	var gradLeft = marginLeft -50;
  	$('#siteContainer').css({'top': marginTop+'px', 'left': marginLeft+'px', 'marginTop': '0px', 'marginLeft': '0px'});
	$('#topGrad').css({'left': gradLeft+'px'});
	$('#bottomGrad').css({'top': gradHeight+'px', 'left': gradLeft+'px', 'height': gradHeight+'px', 'marginLeft': '0px'});
	$('#leftColour,#rightColour').css({'height': colourHeight+'px'});
}

function preloadSiteGraphics(cb){
	var siteGraphics = [
			'V6B.com/assets/top_1px.png', 
			'V6B.com/assets/contentEdge.png', 
			'V6B.com/assets/bottom_1px.jpg', 
			'V6B.com/assets/bottom_1px_ng.png', 
			'V6B.com/assets/tabs/grad3LeftEdge.png', 
			'V6B.com/assets/tabs/grad3grad.png', 
			'V6B.com/assets/gEdge.png', 
			'V6B.com/assets/tabs/grad2LeftEdge.png', 
			'V6B.com/assets/tabs/grad2grad.png', 
			'V6B.com/assets/lEdge.png', 
			'V6B.com/assets/tabs/grad1LeftEdge.png', 
			'V6B.com/assets/tabs/grad1grad.png', 
			'V6B.com/assets/black.jpg',
			'V6B.com/assets/tabs/gNavGrad.jpg',
			'V6B.com/assets/tabs/gNavEdge.jpg',
			'V6B.com/assets/tabs/gNavShadow.jpg',
			'V6B.com/assets/tabs/lNavGrad.jpg',
			'V6B.com/assets/tabs/lNavEdge.jpg',
			'V6B.com/assets/tabs/lNavShadow.jpg',
			'V6B.com/assets/leftArrow.png',
			'V6B.com/assets/rightArrow.png',
			'V6B.com/assets/leftArrowSm.png',
			'V6B.com/assets/rightArrowSm.png',
			'V6B.com/assets/map.png'
		],
		divsToFade = [
			'#logo',
			'#splashTitle',
			'#contentGrad',
			'#bottomGradInternal',
			'#backgroundGrad',
			'#backgroundColour',
			'#footer'
		];
	graphicsCounter = 0; //reset the graphicsCounter
	$.each(siteGraphics, function(i){
		var img = new Image();
		$(img).load(function () {
			if((i+1) == siteGraphics.length){
				fadeDivs(divsToFade, false, cb);			  
			}
			//checkLoad(siteGraphics.length, divsToFade, cb);
    	}).attr('src', String(this));
	});
	//if(cb) cb();
}

function checkLoad(count, divs, cb){
	//alert(Number((count-1))+', '+graphicsCounter);
	if(Number(count-1) == graphicsCounter){
		fadeDivs(divs, cb);
	} else graphicsCounter++;	
}

function fadeDivs(divs, cb, cb2){
	if(cb) cb();
	$.each(divs, function(i){
		if((i+1) == divs.length && cb2) $(String(this)).fadeIn((fadeTime*1), cb2);
		else $(String(this)).fadeIn((fadeTime*1));
	});
}

function compileSplash(){
	var $splash = $(xml).find('splash:0');
	var newHtml = "<div id='splash'>";
	newHtml += pt($splash);
	newHtml += "</div>";
	$('#splash').html(newHtml);
	$('#slideshow_0').cycle({
		fx: 		'fade',
		timeout:	0,
		speed:      500
	});
	currentPtSlide = 0;
	var length = $('#slideshow_0').cycle('length');
	var padLeft = (12-length)*47;
	padLeft += 93 + 55;
	$('#ptNav_0').css("padding-left", padLeft+"px");
	$('#ptNav_0').css("width", (750-padLeft)+"px");
	$('#ptTag_0').css({'display': 'inline'});
	preloadMap(function(){preloadSplash();});
//	$('#splash').fadeIn((fadeTime*1), function(){changeLoadText(); splashTimer();});
//	$('#sub_content, #navigation').fadeIn((fadeTime*1));
//	$('#loading').fadeOut((fadeTime*1));
//	notVisible = false;
}

function preloadMap(cb){
	var mapHtml = '<div id="exitMap" ><a onclick="mapClose()"><img src="V6B.com/assets/x.png"/></a></div>'
	//mapHtml += '<iframe width="750" height="375" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&amp;q=@49.2658,-123.11185&amp;t=h&amp;ll=49.264815,-123.11231&amp;spn=0.005251,0.016072&amp;z=16&amp;output=embed"></iframe>';//q=V6B+Design+Group:+363+West+6th+Avenue@49.2658,-123.11185&amp; http://mapki.com/wiki/Google_Map_Parameters
	//mapHtml += '<iframe width="750" height="375" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&amp;t=h&amp;ll=49.267815,-123.10231&amp;spn=0.005251,0.016072&amp;z=16&amp;output=embed"></iframe>';
	mapHtml += '<iframe  id="mapIframe"  frameborder="0"  vspace="0"  hspace="0"  marginwidth="0"  marginheight="0" width="750"  scrolling="no"  height="375"  src="http://www.v6b.com/V6B.com/map.html"></iframe>'
	$('#map').css({'z-index': '-10000'});
	$('#map').html(mapHtml);
	if(cb) cb();
}

function preloadSplash(){
	var counter = 0,
		divsToFade = [
			'#splash',
			'#sub_content',
			'#navigation'
		];
	graphicsCounter = 0; //reset the graphicsCounter
	$('#splash').find('img').each(function(i){if($(this).attr('src') == 'V6B.com/assets/black.jpg') counter++;});
	$('#splash').find('img').each(function(i){
		if($(this).attr('src') == 'V6B.com/assets/black.jpg'){								   
			var img = new Image();
			$(img).load(function () {
				//if((i+1) == siteGraphics.length) fadeDivs(divsToFade, cb);			  
      			if((i+1) == counter){
					if(mapStartOpen) mapOpen();
					$('#loading').fadeOut((fadeTime*1), function(){
						changeLoadText();
					}); 
				}
				checkLoad(counter, divsToFade, function(){
					splashTimer(); 
					notVisible = false;
				});
    		}).attr('src', $(this).attr('title'));
			$(this).attr('src', $(this).attr('title'))
		}
	});
	testHash();
}

function changeLoadText(){
	$('#loading > p').empty().append('Your content is now loading...')	;
}

function preloadContent(lIndx, gIndx, cb){
	var counter = 0,
		numDivs = 0,
		prevDivs = 0,
		divsToFade = [
			'#content-container',
			'#sub_content'
		];
	graphicsCounter = 0; //reset the graphicsCounter
	for(i=0; i<gIndx; i++)$(xml).find('global:eq('+i+')').find('local').each(function(){prevDivs++;});
	$(xml).find('global:eq('+gIndx+')').find('local').each(function(){numDivs++;});
	$('#content > div').slice(Number(prevDivs), Number(prevDivs+numDivs)).find('img').each(function(i){if($(this).attr('src') == 'V6B.com/assets/black.jpg') counter++;});
	$('#content > div').slice(Number(prevDivs), Number(prevDivs+numDivs)).find('img').each(function(i){
		if($(this).attr('src') == 'V6B.com/assets/black.jpg'){
			$(this).load(function () {
//				if(Number(i+1)==counter){
//					fadeDivs(divsToFade, function(){
//						if(cb) cb();
//						displayLocal(lIndx, gIndx);
//						$('#splashTitle').fadeOut((fadeTime*1));
//						$('#loading').fadeOut((fadeTime*1));
//					});
//				}
				checkLoad(counter, divsToFade, function(){
					if(cb) cb();
					displayLocal(lIndx, gIndx);
					$('#splashTitle').fadeOut((fadeTime*1));
					$('#loading').fadeOut((fadeTime*1));
				});
   			}).attr('src', $(this).attr('title'));
		}
	});
}


function preloadSubContent(){
	$('#sub_content').find('img').each(function(i){
		if($(this).attr('src') == 'V6B.com/assets/black.jpg'){
			$(this).load().attr('src', $(this).attr('title'));
		}
	});
}

function compileContent(cIndx, scIndx, cb){
	//var i = 0;
	var newHtml = '';
	var newScHtml = '';
	$(xml).find('subContent').each(function(){
		var scType = $(this).attr('scType');
		if (scType == "c+l") {
			newScHtml += "<div id='subcontent'>";
			newScHtml += scContactLocation($(this));
			newScHtml += "</div>";	
		} else if(scType == "linksProducts"){
			newScHtml += "<div id='subcontent'>";
			newScHtml += scProducts($(this));
			newScHtml += "</div>";	
		} else if(scType == "linksMembership"){
			newScHtml += "<div id='subcontent'><div id='stickToBottom'>";
			newScHtml += scMembership($(this));
			newScHtml += "</div></div>";	
		} else if (scType == "process") {
			newScHtml += "<div id='subcontent'>";
			newScHtml += scProcess($(this));
			newScHtml += "</div>";	
		}	
	});
	
	$(xml).find('global').each(function(){
		$(this).find('local').each(function(){
			var $lTab = $(this),
				cText = parseText($lTab.text()),
				cType = $lTab.attr('cType'),
				scType = $lTab.attr('scType'),
				title = $lTab.attr('title');
			newHtml += "<div style='height:375px'>";
			if (cType == "txtRight") {
				newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $lTab.attr('image') + "'/></div>";
				newHtml += txtRight(title, cText);
			} else if(cType == "txtLeft")  {
				newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $lTab.attr('image') + "'/></div>";
				newHtml += txtLeft(title, cText);
			} else if(cType == "slide")  {
				newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $lTab.attr('image') + "'/></div>";
			} else if(cType == "portfolioThumbs")  {
				newHtml += pt($lTab);
			} else if(cType == "portfolioNoThumbs")  {
				newHtml += pnt($lTab);
			}  else if(cType == "mainSuppliers")  {
				newHtml += mainSuppliers($lTab);
			} 
//			else if(cType == "tcc")  {
//				tcc(tccCounter, obj);
//				tccCounter++;
			//} 
			else {
				newHtml += "";		
			}
			newHtml += "</div>";
			
			newScHtml += "<div id='subcontent'>";
			if(scType == "t+t"){
				newScHtml += "<div id='stickToBottom'>";
				newScHtml += scThumbsText($lTab);
				newScHtml += "</div>";
			} else if(scType == "linksCompany"){
				newScHtml += scCompany($lTab);
			}
			newScHtml += "</div>";	
		});
	});
	$('#content').html(newHtml);
	$('#sub_content').html(newScHtml);
	$('#content').cycle({
		fx: 			'fade',
		startingSlide: 	cIndx,
		timeout:		0,
		speed:      	500
	});
	$('#sub_content').cycle({
		fx: 			'fade',
		startingSlide: 	scIndx,
		timeout:		0,
		speed:      	500
	});
	$('#allMSSlideshows').cycle({
		fx: 		'fade',
		timeout:	0,
		speed:      500
	});
	$.each(ssIDs, function(){
		$(String(this)).cycle({
			fx: 		'fade',
			timeout:	0,
			speed:      500
		});
	});
	if(cb) cb();
}


function parseText(string){
	var fullArray = new Array();
	var txtArray = new Array();
	var regExpArray = new Array();
	var patterns = new RegExp("{beginTable/}|{beginTableIndent/}|{beginRow/}|{endRow/}|{endTable/}|{grayTable/}|{grayTableBold/}|{grayBody/}|{grayTextLink/}|{grayText/}|{grayTextBold/}|{grayHeading/}|{blueHeading/}|{blackHeading/}|{blueLink/}|{grayBar/}|{grayBarBottom/}|{grayBarMiddle/}|{thumbTitle/}|{beginThumbs/}|{endThumbs/}|{tagline/}|{title/}|{imageSRC/}|{thumbnailSRC/}|{fullSlideSRC/}|{beginSlides/}|{endSlides/}|{fakethumbnail/}|{map/}|{openDiv/}|{closeDiv/}|{beginRollOver/}|{endBody/}|{endRollOver/}|{blueRollOver/}|{beginRollOverText/}|{endRollOverText/}|{a}|{/a}|{grayWordsStart/}|{grayWords/}|{grayWordsBold/}|{grayWordsEnd/}", "g");
	var whiteSpaces = new RegExp("\n|\t|\r", "g");
	if(string){
		var noWsString = string.replace(whiteSpaces, '');
		txtArray = noWsString.split(patterns);
		for (s=0; s<txtArray.length; s++) {
			if (txtArray[s] == "{noShow}"){ txtArray[s] = " ";}
		}
		if (!txtArray[0]){
			txtArray.reverse();
			var blank = txtArray.pop();
			txtArray.reverse();
		}
		regExpArray = noWsString.match(patterns);
		fullArray.push(regExpArray, txtArray);
		return fullArray;
	} else {
		return;	
	}
}

function txtRight(title, cText){
	var newHtml = "<div id='txtRight' class='contentWindow'><h1 class='slideTitle'>" + title + "</h1>";
	for (p=0;p<cText[0].length;p++){
		if (cText[0][p] == "{grayText/}"){
			newHtml += "<p class='grayText'>" + cText[1][p];					
		} else if (cText[0][p] == "{beginTable/}"){
			newHtml += "<table class='grayTable'>";
		} else if (cText[0][p] == "{endBody/}"){
			newHtml += "</p>";
		} else if (cText[0][p] == "{beginTableIndent/}"){
			newHtml += "<table class='grayTableIndent'>";
		} else if (cText[0][p] == "{beginRow/}"){
			newHtml += "<tr>";
		} else if (cText[0][p] == "{endRow/}"){
			newHtml += "</tr>";
		} else if (cText[0][p] == "{grayTable/}"){
			newHtml += "<td class='grayText'>" + cText[1][p] + "</td>";
		} else if (cText[0][p] == "{endTable/}"){
			newHtml += "</table>";
		} else if (cText[0][p] == "{grayBody/}"){
			newHtml += "<p class='grayBody'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{blueHeading/}"){
			newHtml += "<p class='blueHeading'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{grayHeading/}"){
			newHtml += "<p class='grayHeading'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{beginRollOver/}"){
			newHtml += "<div class='contentRollOver'>";					
		} else if (cText[0][p] == "{blueRollOver/}"){
			newHtml += "<span class='blueRollOver' <span class='grayHeading' onmouseover='contentRollOver(event)' onmouseout='contentRollOut(event)'>" + cText[1][p] + "</span>";					
		} else if (cText[0][p] == "{beginRollOverText/}"){
			newHtml += "<div id='rollOverText' class='grayRollOver'>";					
		} else if (cText[0][p] == "{endRollOverText/}"){
			newHtml += "</span></p></div>";					
		} else if (cText[0][p] == "{endRollOver/}"){
			newHtml += "</div>";					
		} 
	}
	newHtml += "</div>";
	return newHtml;
}

function txtLeft(title, cText){
	var newHtml = "<div id='txtLeft' class='contentWindow'><h1 class='slideTitle'>" + title + "</h1>";
	for (p=0;p<cText[0].length;p++){
		if (cText[0][p] == "{grayText/}"){
			newHtml += "<p class='grayText'>" + cText[1][p];					
		} else if (cText[0][p] == "{beginTable/}"){
			newHtml += "<table class='grayTable'>";
		} else if (cText[0][p] == "{endBody/}"){
			newHtml += "</p>";
		} else if (cText[0][p] == "{beginTableIndent/}"){
			newHtml += "<table class='grayTableIndent'>";
		} else if (cText[0][p] == "{beginRow/}"){
			newHtml += "<tr>";
		} else if (cText[0][p] == "{endRow/}"){
			newHtml += "</tr>";
		} else if (cText[0][p] == "{grayTableBold/}"){
			newHtml += "<td class='grayTextBold'>" + cText[1][p] + "</td>";
		} else if (cText[0][p] == "{grayTable/}"){
			newHtml += "<td class='grayText'>" + cText[1][p] + "</td>";
		} else if (cText[0][p] == "{endTable/}"){
			newHtml += "</table>";
		} else if (cText[0][p] == "{grayBody/}"){
			newHtml += "<p class='grayBody'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{grayWordsStart/}"){
			newHtml += "<p class='grayBody'>";
		} else if (cText[0][p] == "{grayWords/}"){
			newHtml += cText[1][p];
		} else if (cText[0][p] == "{grayWordsBold/}"){
			newHtml += "<span class='grayTextBold'>" + cText[1][p] + "</span>";
		} else if (cText[0][p] == "{grayWordsEnd/}"){
			newHtml += "</p>";
		} else if (cText[0][p] == "{blueHeading/}"){
			newHtml += "<p class='blueHeading'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{grayHeading/}"){
			newHtml += "<p class='grayHeading'>" + cText[1][p] + "</p>";
		} else if (cText[0][p] == "{beginRollOver/}"){
			newHtml += "<div class='contentRollOver'>";					
		} else if (cText[0][p] == "{blueRollOver/}"){
			newHtml += "<span class='blueRollOver' <span class='grayHeading' onmouseover='contentRollOver(event)' onmouseout='contentRollOut(event)'>" + cText[1][p] + "</span>";					
		} else if (cText[0][p] == "{beginRollOverText/}"){
			newHtml += "<div id='rollOverText' class='grayRollOver'>";					
		} else if (cText[0][p] == "{endRollOverText/}"){
			newHtml += "</span></p></div>";					
		} else if (cText[0][p] == "{endRollOver/}"){
			newHtml += "</div>";					
		} 
	}
	newHtml += "</div>";
	return newHtml;
}

function contentRollOver(e){
	var targ;
	if (!e){var e=window.event;}
	if (e.target){targ=e.target;
	}else if (e.srcElement){targ=e.srcElement;}
	if (targ.nodeType==3) // defeat Safari bug
  	{targ = targ.parentNode;}
	$('.grayRollOver').css({'display': 'block'});
}

function contentRollOut(e){
	var targ;
	if (!e){var e=window.event;}
	if (e.target){targ=e.target;
	}else if (e.srcElement){targ=e.srcElement;}
	if (targ.nodeType==3) // defeat Safari bug
  	{targ = targ.parentNode;}
	$('.grayRollOver').css({'display': 'none'});
}
//
//<!--11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111-->
//
function pt($obj){
	var	ptText = [],
		slideIndx = 0,
		title = $obj.attr('title'),
		inst = $obj.attr('instance'),
		ptDivId = title+'_ptHidden',
		newHtml = 	"<div id='" + ptDivId + "'>";
		newHtml += 	"<div id='" + title + "Container'>";
		if($obj.attr('c2Type') != null){
			var cText = parseText($obj.find("text:eq(0)").text());
			newHtml += "<div id='ptCText'>";
			if($obj.attr('c2Type')=='txtLeft') newHtml += txtLeft(title, cText);
			else if ($obj.attr('c2Type')=='txtRight') newHtml += txtRight(title, cText);
			newHtml += "</div>";
		}
	if($obj.contents() != null){
		ptText = parseText($obj.text());
		if(ptText[0] != null){
			$.each(ptText[0], function(p){
				if (ptText[0][p] == "{tagline/}"){
					newHtml += "<div id='ptTag_"+slideIndx+"' class='splashTags' style='display:none;'><p class='blueHeading'>"+ptText[1][p]+"</p></div>";
					slideIndx ++;
				}
			});
			slideIndx = 0;
			newHtml +=	"<div id='ptNav_"+inst+"' class='ptNav'>" +
						"<div id='ptPrevious'><a onclick='previousPtSlide("+inst+")'><img src='V6B.com/assets/leftArrowSm.png'/></a></div>";
//						"<p class='blackHeading'>" + slides[q].getAttribute('subTitle') + "</p>";
			$.each(ptText[0], function(p){
				if (ptText[0][p] == "{thumbnailSRC/}"){
					newHtml += "<div id='ptThumb_"+slideIndx+"' ";
					if(slideIndx == 0) newHtml += "class='selectedPtThumb'";
					else newHtml += "class='ptThumb'";
					newHtml += "onclick='changeSlideHash("+inst+", "+slideIndx+");'><img src='V6B.com/assets/black.jpg' title='"+ptText[1][p]+"'/></div>";
					slideIndx ++;
				} else if (ptText[0][p] == "{tagline/}"){
					newHtml += "<div id='ptTag_"+slideIndx+"' class='splashTags' style='display:none;'><p class='blueHeading'>"+ptText[1][p]+"</p></div>";
				}
			});
			newHtml += "<div id='ptNext'><a onclick='nextPtSlide("+inst+")'><img src='V6B.com/assets/rightArrowSm.png'/></a></div></div>";
			
			$.each(ptText[0], function(p){
				if (ptText[0][p] == "{beginSlides/}"){
					var ssID = 'slideshow_'+inst;
					newHtml += "<div id="+ssID+">";
					ssIDs.push('#'+ssID);
				} else if (ptText[0][p] == "{fullSlideSRC/}"){
					newHtml += "<div><img src='V6B.com/assets/black.jpg' title='"+ptText[1][p]+"'/></div>";
				} else if (ptText[0][p] == "{endSlides/}"){
					newHtml += "</div>";
				}
			});
		}
	}
	newHtml += "</div></div>";
	
	return newHtml;
	//allPtElements.push(thesePtElements);
	
}

function previousPtSlide(inst){
	currentPtSlide = $('#slideshow_'+inst).cycle('index');
	if (currentPtSlide != 0) {
		if(splashToOpen)changeSlide(inst, Number(currentPtSlide)-1, false);
		else changeSlideHash(inst, Number(currentPtSlide)-1);
	}	
}

function nextPtSlide(inst){
	currentPtSlide = $('#slideshow_'+inst).cycle('index');
	var length = $('#slideshow_'+inst).cycle('length');
	if (currentPtSlide != (length-1)) {
		if(splashToOpen)changeSlide(inst, Number(currentPtSlide)+1, false);
		else changeSlideHash(inst, Number(currentPtSlide)+1);
	} else {
		if(splashToOpen)changeSlide(0, 0, false);
	}
}
//
//function ptOpen(title, inst){
//	//alert(splashToOpen);
//	//splashToOpen = false;
//	currentPtSlide = allPtElements[counter].length - 1;
//	mainContent = document.getElementById("main_content").innerHTML;
//	currentPtDivId = title+"Container";
//	document.getElementById("main_content").innerHTML += document.getElementById(title+"_ptHidden").innerHTML;
//	document.getElementById('ptThumb_'+currentPtSlide).className = "selectedPtThumb";
//	ptStoredClick = document.getElementById('ptThumb_0').onclick;
//	document.getElementById('ptThumb_'+currentPtSlide).onclick = "";
//	document.getElementById('ptThumb_'+currentPtSlide).style.cursor = "default";
//	document.getElementById('ptThumb_'+currentPtSlide).className = "selectedPtThumb";
//	var ID = '#ptNav_'+inst;
//	ptPrevInvis = true;
//	currentPtSlide = $('#slideshow_'+inst).cycle('index');
//	$(ID+' #ptThumb_'+currentPtSlide).css({'cursor': 'pointer'});
//	$(ID+' #ptThumb_'+currentPtSlide).removeClass("selectedPtThumb");
//	$(ID+' #ptThumb_'+currentPtSlide).addClass("ptThumb");

//	
//	if(splashToOpen){
//		document.getElementById('ptTag_'+currentPtSlide).style.display = "inline";
//		document.getElementById('ptNext').style.display = "none";
//		document.getElementById('ptNav').style.paddingRight = "15px";
//		document.getElementById('ptNav').style.width = "735px"
//		ptNextInvis = true;
//	}
//	ptPrevInvis = true;
//	ptIsOpen = true;
//	Portfolio.open(allPtElements[counter]);
//	Portfolio.change((allPtElements[counter].length - 1));
//	//if(splashToOpen){ 
////		splashTimer();
////	}
//}

function splashTimer(){
	if(splashTimerStarted) clearTimeout(t);
	t = setTimeout("nextPtSlide(0)", 8000);
	splashTimerStarted = true;
}

//function ptClose(){
//	var node = document.getElementById(currentPtDivId);
//	node.parentNode.removeChild(node);
//	ptIsOpen = false;
//	//Portfolio.close();
//}
//
function changeSlide(inst, indx, pnt){
	//alert(inst+", "+indx+", "+pnt);
	if(pnt && inst != 0)$('#pntSlideshow_'+inst).cycle(Number(indx));
	else{
		if(pnt){
			var ID = '#pntNav_'+inst;
			var length = $('#pntSlideshow_'+inst).cycle('length');
			$('#pntSlideshow_'+inst).cycle(Number(indx));
		}else {
			var ID = '#ptNav_'+inst;
			var length = $('#slideshow_'+inst).cycle('length');
			$('#slideshow_'+inst).cycle(Number(indx));
		}
		var padLeft = (12-length)*47;
		if(splashToOpen) padLeft += 55;
		//if(!splashToOpen){
		if(indx == 0){
			padLeft += 93;
			
			$(ID).css("padding-left", padLeft+"px");
			$(ID).css("width", (750-padLeft)+"px");
			$(ID+' #ptPrevious').css({'display': 'none'});
			ptPrevInvis = true;
			//alert(ID);
		} else {
			padLeft += 64;
			//alert(ID);
			$(ID).css("padding-left", padLeft+"px");
			$(ID).css("width", (750-padLeft)+"px");
			$(ID+' #ptPrevious').css({'display': 'inline'});
			ptPrevInvis = false;
			//alert($(ID).css("padding-left")+", "+$(ID).css("width"));
		}
		if (indx == (length-1)){
			$(ID+' #ptNext').css({'display': 'none'});
			ptNextInvis = true;
		} else if (ptNextInvis) {
			$(ID+' #ptNext').css({'display': 'inline'});
			ptNextInvis = false;
		}
		//}
		//$('#ptThumb_'+currentPtSlide).onclick = ptStoredClick;
		$(ID+' #ptThumb_'+currentPtSlide).css({'cursor': 'pointer'});
		$(ID+' #ptThumb_'+currentPtSlide).removeClass("selectedPtThumb");
		$(ID+' #ptThumb_'+currentPtSlide).addClass("ptThumb");
		//ptStoredClick = document.getElementById('ptThumb_'+indx).onclick;
		//$('#ptThumb_'+indx).onclick = "";
		$(ID+' #ptThumb_'+indx).css({'cursor': 'default'});
		$(ID+' #ptThumb_'+indx).removeClass("ptThumb");
		$(ID+' #ptThumb_'+indx).addClass("selectedPtThumb");
		//alert("bug");
		if(splashToOpen){ 
			$('#ptTag_'+currentPtSlide).css({'display': 'none'});
			$('#ptTag_'+indx).css({'display': 'inline'});
			splashTimer();
		}
		//$('.slideshow').cycle(indx);
		currentPtSlide = indx;
	}
//	if(tccIsOpen) TrippleColumn.change(indx);
//	else Portfolio.change(indx);
}
//
function changeSlideHash(inst, indx){
	//alert(inst+", "+indx);
	if(splashToOpen){
		changeSlide(inst, indx);
		return;
	}
	var oldHash = document.location.hash;
	var pos = oldHash.lastIndexOf(':');
	if(pos != -1) var newString = oldHash.slice(0, pos);
	else var newString = oldHash;
	var newHash = newString +":"+inst+","+indx+";";
	document.location.hash = newHash;
}
//
//<!--2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222-->
//
function pnt($obj){
	var inst = $obj.attr('instance'),
		ID = 'pntSlideshow_'+inst,
		newHtml = "<div id="+ID+">";
	ssIDs.push('#'+ID);
	$obj.find("slide").each(function(){
		var pntText = parseText($(this).text()),
			pntDivId = $(this).attr('title')+'_pntHidden';
		newHtml += "<div id='" + pntDivId + "'>";
		newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $(this).attr("image") + "'/></div>";
//		if ($(this).attr('cType')){
//			var title = $(this).attr('title');
//			cType = $(this).attr('cType');
//			if (cType == "txtRight") {
//				newHtml += txtRight(title, pntText);
//			} else if(cType == "txtLeft")  {
//				newHtml += txtLeft(title, pntText);
//			}
//		} else {
			newHtml += 	"<div class='whoWeAre'><h1 class='slideTitle'>" + $(this).attr('title') + "</h1>"	+
						"<p class='grayHeading'>" + $(this).attr('subTitle') + "</p>" +
						"<div style='padding-top: 30px'>";			
			$.each(pntText[0], function(i){
				if (pntText[0][i] == "{grayText/}"){
					newHtml += "<p class='grayText'>" + pntText[1][i] + "</p>";					
				} else if (pntText[0][i] == "{grayBody/}"){
					newHtml += "<p class='grayBody'>" + pntText[1][i] + "</p>";					
				} else if (pntText[0][i] == "{blueHeading/}"){
					newHtml += "<p class='blueHeading'><a onclick=\"pntSbGallery('"+i+"')\">" + pntText[1][i] + "</a></p>";
				}		
			});
			newHtml += "</div></div>";
//		}
		newHtml += '</div>';
	});
	newHtml += '</div>';
//	if(inst == 0){
//		var ptText = parseText($obj.find('thumbnails').text());
//		newHtml +=	"<div id='pntNav_"+inst+"' class='ptNav'>" +
//				"<div id='ptPrevious'><a onclick='previousPtSlide("+inst+")'><img src='V6B.com/assets/leftArrowSm.png'/></a></div>";
////				"<p class='blackHeading'>" + slides[q].getAttribute('subTitle') + "</p>";
//		$.each(ptText[0], function(p){
//			if (ptText[0][p] == "{thumbnailSRC/}"){
//				newHtml += "<div id='ptThumb_"+p+"' ";
//				if(p == 0) newHtml += "class='selectedPtThumb'";
//				else newHtml += "class='ptThumb'";
//				newHtml += "onclick='changeSlideHash("+inst+", "+p+");'><img src='"+ptText[1][p]+"'/></div>";
//				//slideIndx ++;
//			}
//		});
//		newHtml += "<div id='ptNext'><a onclick='nextPtSlide("+inst+")'><img src='V6B.com/assets/rightArrowSm.png'/></a></div></div>";
//	}
	return newHtml;
}

function mainSuppliers($obj){
	var	newHtml = "",
		c2Type = $obj.attr('c2Type'),
		title = $obj.attr('title'),
		cText = parseText($obj.find("text:eq(0)").text());
	if (c2Type == "txtRight") {
		newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $obj.attr('image') + "'/></div>";
		newHtml += txtRight(title, cText);
	} else if(c2Type == "txtLeft")  {
		newHtml += "<div id='contentImage'><img src='V6B.com/assets/black.jpg' title='" + $obj.attr('image') + "'/></div>";
		newHtml += txtLeft(title, cText);
	} else if(c2Type == "slide")  {
		newHtml += "<div id='contentImage><img src='V6B.com/assets/black.jpg' title='" + $obj.attr('image') + "'/></div>";
	}
	newHtml += "<div id='allMSSlideshows' style='display: none;'>";
	$obj.find("slideshow").each(function(i){
		newHtml += "<div id='msSlideshow_"+i+"'>";
		newHtml += '<div id="exitMS" ><a onclick="closeMSSlideshow()"><img src="V6B.com/assets/x.png"/></a></div>'
		newHtml += pt($(this));
		newHtml += "</div>";
	});
	newHtml += "</div>";
	return newHtml;
}

function displayMSSlideshow(indx, openSlide){
	var inst = Number(indx+9);
	if(!openSlide){
		var slide = $('#slideshow_'+inst).cycle('index');
		changeSlideHash(inst, slide);
	} else $('#slideshow_'+inst).cycle(Number(indx));
	$("#allMSSlideshows").fadeIn(fadeTime*1);
	$("#allMSSlideshows").cycle(Number(indx));
	linkEffects(1, 1, indx);
}

function closeMSSlideshow(){
	var indx = $("#allMSSlideshows").cycle('index'),
		pntLinkID = '#pntLink_'+ indx;
	$(pntLinkID+' > *:first').removeClass("selectedPntLinkImage");
	$(pntLinkID+' > *:first').addClass("pntLinkImage");
	$(pntLinkID+' > *:first').css({'cursor': 'pointer'});
	$("#allMSSlideshows").fadeOut(fadeTime*1);
	var oldHash = document.location.hash;
	var pos = oldHash.lastIndexOf(':');
	var newHash = oldHash.slice(0, pos);
	document.location.hash = newHash;
}



//function pntOpen(inst){
//	currentPtSlide = $('#slideshow_'+inst).cycle('index');
//}
//
//function pntClose(){
//	pntIsOpen = false;
//	Portfolio.close();
//}
//<!--3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333-->
//
//function tcc(counter, obj){
//	var slides = obj.getElementsByTagName("slide"),
//		theseTccElements = [];
//	for (q=0;q<slides.length;q++){
//		var tccText = [],
//			theseTccSbElements = [],
//			tccDivId = slides[q].getAttribute('title')+'_tccHidden',
//			newHtml = "<div id='" + tccDivId + "'>";
//		newHtml += 	"<img class='tccImage' src='" + slides[q].getAttribute('image') + "'/>" +
//					"<p class='blueHeading'>" + slides[q].getAttribute('title') + "</p>"	+
//					"<p class='blackHeading'>" + slides[q].getAttribute('subTitle') + "</p>";
//		if(slides[q].childNodes[0].nodeValue != null){
//			tccText = parseText(slides[q].childNodes[0].nodeValue);
//			for (p=0;p<tccText[0].length;p++){
//				if (tccText[0][p] == "{grayText/}"){
//					newHtml += "<p class='grayText'>" + tccText[1][p] + "</p>";					
//				} else if (tccText[0][p] == "{blueHeading/}"){
//					newHtml += "<p class='blueHeading'><a onclick=\"tccSbGallery('"+q+"')\">" + tccText[1][p] + "</a></p>";
//				} else if (tccText[0][p] == "{thumbnailSRC/}"){
//					var element = {
//						player:     'img',
//	       				content:    tccText[1][p+1],
//						source:		tccText[1][p]
//						//onChange:	updateHashPortfolio(counter, indx)
//					};
//					theseTccSbElements.push(element);
//				} 		
//			}
//			allTccSbElements.push(theseTccSbElements);
//		}
//		newHtml += "</div>";
//		document.getElementById("hiddenDivs").innerHTML += newHtml;
//			
//		var element = {
//				player:     		'html',
//	    		content:    		document.getElementById(tccDivId).innerHTML,
//				source:				'resources/graySquare.jpg',
//				width:	 			250,
//				height:				345,
//				handleOversize: 	'none'
//		};
//		theseTccElements.push(element);
//	}
//	allTccElements.push(theseTccElements);
//}
//
//function tccOpen(counter, slide){
//	tccIsOpen = true;
//	TrippleColumn.open(allTccElements[counter]);
//	if(slide != 0){
//		changeSlide(slide);
//		newTccSlide = 0;	
//	}
//}
//
//function tccClose(){
//	tccIsOpen = false;
//	TrippleColumn.close();
//}
//
//function tccSbGallery(counter){
//	Shadowbox.open(allTccSbElements[counter]);
//	//updateHashPortfolio(counter, indx);
//}
//
//
function scContactLocation($obj){
	var scText = parseText($obj.text()),
		newHtml = '';
	$.each(scText[0], function(i){
		if (scText[0][i] == "{blueHeading/}"){
			newHtml += "<p class='blueHeadingContact'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayText/}"){
			newHtml += "<p class='grayTextContact'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayBody/}"){
			newHtml += "<p class='grayBody'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{blueLink/}"){
			newHtml += "<p class='blueHeading'><a href='mailto:info@v6b.com'>E-mail Us</a> / <a class='map' onclick='mapOpen()'>Click for Map</a></p>";
		} else if (scText[0][i] == "{map/}"){
			newHtml += "<img id='mapButton' class='map' onclick='mapOpen()' src='"+scText[1][i]+"'/>";
		}
//		else if (scText[0][i] == "{openDiv/}"){
//			newHtml += "<div id='stickToBottom'>";
//		} else if (scText[0][i] == "{closeDiv/}"){
//			newHtml += "</div>";
//		}
	});
	return newHtml;
}

function mapOpen(){
	if (mapIsOpen){
		mapClose();
	} else {
		//$('#map').hide();
		$('#map').css({'z-index': '5000'});
		$('#map').fadeIn(fadeTime);
		//$('#map').css({'display': 'inline'});
		//if(slidesConstructed){
			//document.getElementById('mapButton').onclick = function(){mapClose();};
		if(!mapStartOpen && !/map/g.test(document.location.hash)) document.location.hash += "$map";
		currentHash = document.location.hash;
		mapStartOpen = false;
		mapIsOpen = true;
		//}
	}
}

function mapClose(){
	mapIsOpen = false;
	//document.getElementById('map').onclick = function(){mapOpen();};
	$('#map').fadeOut(fadeTime, function(){$('#map').css({'z-index': '0'});});
	//$('#map').css({'display': 'none'});
	var hash = document.location.hash;
	if (hash =='#$map'){
		document.location.hash = '';
		currentHash = document.location.hash;
	}
	if (/map/g.test(hash)){
		var pos = hash.indexOf('$');
		var truncHash = hash.slice(0, pos);
		document.location.hash = truncHash;
		currentHash = document.location.hash;
	}
	//if(splashToOpen)ptOpen('splash', 0);
}

//function addHashMap(){
	//if(!new RegExp('$map').test(document.location.hash)) document.location.hash += '$map';
//}

//function removeHashMap(){
	//if(!new RegExp('$map').test(document.location.hash)) document.location.hash -= '$map';
//}

function scProducts($obj){
	var scText = parseText($obj.text()),
		newHtml = '';
	$.each(scText[0], function(i){
		newHtml += "<div id='pntLink_"+i+"'><img id='pntButton' src='V6B.com/assets/black.jpg' title='" + scText[1][i] + "' class='pntLinkImage' onclick='displayMSSlideshow("+i+");' style='margin-bottom: 10px; cursor: pointer;'/></div>";
	});
	return newHtml;
}

function scMembership($obj){
	var scText = parseText($obj.text()),
		newHtml = '';
	$.each(scText[0], function(i){
		newHtml += "<div id='pntLink_"+i+"'><img id='pntButton' src='V6B.com/assets/black.jpg' title='" + scText[1][i] + "' style='margin-top: 20px; margin-left: 15px;'/></div>";
	});
	return newHtml;
}

function scProcess($obj){
	var scText = parseText($obj.text());
	var newHtml = '';
	$.each(scText[0], function(i){
		if (scText[0][i] == "{grayBar/}"){
			newHtml += "<div id='scGrayBar'></div>";
		} else if (scText[0][i] == "{blueHeading/}"){
			newHtml += "<p class='blueHeading'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayText/}"){
			newHtml += "<p class='grayText'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayBody/}"){
			newHtml += "<p class='grayBody'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{blueLink/}"){
			newHtml += "<p class='blueHeadingLink'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayBarMiddle/}"){
			newHtml += "<div id='scGrayBarMiddle'></div>";
		} else if (scText[0][i] == "{grayBarBottom/}"){
			//newHtml += "<div id='stickToBottom'><div id='scGrayBarBottom'></div></div>";
			newHtml += "<div id='scGrayBarBottom'></div>";
		}
	});
	return newHtml;
}

function scThumbsText($obj){
	var $obj = $obj.find('subContent'),
		scText = parseText($obj.text()),
		newHtml = '';
	$.each(scText[0], function(i){
		if (scText[0][i] == "{blueHeading/}"){
			newHtml += "<p class='scBlueHeading'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayText/}"){
			newHtml += "<p class='grayText'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayBody/}"){
			newHtml += "<p class='grayBody'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayBarMiddle/}"){
			newHtml += "<div id='scGrayBarMiddle'></div>";
		} else if (scText[0][i] == "{grayBarBottom/}"){
			newHtml += "<div id='scGrayBarBottom'></div>";
		}
	});
	return newHtml;
}

function scCompany($obj){
	var $obj = $obj.parent().find('subContent'),
		scText = parseText($obj.text()),
		newHtml = '';
		counter = 0;
	$.each(scText[0], function(i){
		if (scText[0][i] == "{grayText/}"){
			newHtml += "<p class='grayText'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayTextBold/}"){
			newHtml += "<p class='grayTextBold' style='font-size: 9px;'>" + scText[1][i] + "</p>";
		} else if (scText[0][i] == "{grayTextLink/}"){
			newHtml += "<p><a id='pntLinkC_"+counter+"' class='pntLink' onclick='changeProductSlide(2, 4, "+counter+");'>" + scText[1][i] + "</a></p>";
			counter++;
		} else if (scText[0][i] == "{grayBar/}"){
			newHtml += "<div id='scGrayBar'></div>";
		} else if (scText[0][i] == "{grayBarMiddle/}"){
			newHtml += "<div id='scGrayBarMiddle' style='margin-top: 10px; margin-bottom: 5px;'></div>";
		} else if (scText[0][i] == "{grayBarBottom/}"){
			newHtml += "<div id='stickToBottom'><div id='scGrayBarBottom'></div></div>";
			//newHtml += "<div id='scGrayBarBottom'></div>";
		}
	});
	return newHtml;
}

function changeProductSlide(lIndx, gIndx, slide){
	if(currentLIndx != lIndx) {
		var $ltab = $(xml).find('global:eq('+gIndx+')').find('local:eq('+lIndx+')');
		if($ltab.attr('scType') == "linksProducts" )$('#pntSlideshow_'+$ltab.attr('instance')).cycle(Number(slide));
		newPntSlide = slide;
		changeGTab(lIndx, gIndx);
	} else changeSlideHash(Number(0), slide);
	//linkEffects(lIndx, gIndx, slide)
}

function linkEffects(lIndx, gIndx, slide){
	var $lTab = $(xml).find('global:eq('+gIndx+')').find('local:eq('+lIndx+')');
	var pntLinkID = '';
	if(currentPntSlide != null){
		if($lTab.attr('tab') == "Main Suppliers"){ 
			pntLinkID = '#pntLink_'+currentPntSlide;
			$(pntLinkID+' > *:first').removeClass("selectedPntLinkImage");
			$(pntLinkID+' > *:first').addClass("pntLinkImage");
			$(pntLinkID+' > *:first').css({'cursor': 'pointer'});
		} else if($lTab.attr('tab') == "Who We Are"){
			pntLinkID = '#pntLinkC_'+currentPntSlide;
			$(pntLinkID).removeClass("selectedPntLink");
			$(pntLinkID).addClass("pntLink");
			$(pntLinkID).css({'cursor': 'pointer', 'color':'#AAA'});
			$(pntLinkID).hover(function(){$(this).css('color','#AAA');},function(){$(this).css('color','#888');}); 
		}
	}
	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 = '#pntLinkC_'+slide;
		$(pntLinkID).removeClass("pntLink");
		$(pntLinkID).addClass("selectedPntLink");
		$(pntLinkID).css({'cursor': 'default', 'color':'#369'});
		$(pntLinkID).hover(function(){$(this).css('color','#369');},function(){$(this).css('color','#369');}); 
	}
	currentPntSlide = slide;
}