
function $id(id){
	if(document.getElementById){
		return document.getElementById(id);
		}
	if(document.all){
		return document.all[id];
		}
	return null;
	}
function $style(id){
	return $id(id).style;
	}
function $head(){
	if(document.getElementsByTagName){
		return document.getElementsByTagName("head")[0];
		}
	return null;
	}
function $script(src){
	if(document.getElementsByTagName && document.createElement){
		var loScript			= document.createElement("script");
		loScript.type			= "text/javascript";
		loScript.src			= src;
		$head().appendChild(loScript);
		}
	else{
		document.write("<scr"+"ipt src=\""+src+"\" type=\"text/javascript\">"+"</scr"+"ipt>");
		}
	}

function IsFramed(){
	return getQueryParamValue("framed") == "true";
	}

function IsSlideShow(){
	return getQueryParamValue("autoplay") == "true";
	}

function IsArchive(){
	return TourDisplay.IsArchive;
	}

function ChangeLanguage(href){
	if(IsArchive() && location.search.length > 1){
		location.href		= href+location.search.substring(1,location.search.length);
		return false;
		}
	if(IsSlideShow() && href.indexOf("autoplay") == -1){
		location.href		= href+(href.indexOf("?") == -1?"?":"&")+"autoplay=true";
		return false;
		}
	if(IsFramed() && href.indexOf("framed") == -1){
		location.href		= href+(href.indexOf("?") == -1?"?":"&")+"framed=true";
		return false;
		}

	return true;
	}

var TourDisplay					= new Object();
TourDisplay.UseFlash			= false;
TourDisplay.UseAolFix			= false;
TourDisplay.Autoplay			= false;
TourDisplay.Branding			= true;
TourDisplay.IsArchive			= false;
TourDisplay.CurrentPhoto		= 0;
TourDisplay.DataUrl				= "http://data.jsvt.net";
TourDisplay.Viewer				= "http://data.jsvt.net/images/TourViewer.swf";
TourDisplay.FlashProxyUrl		= "http://data.jsvt.net/images/JavaScriptFlashGateway.swf";
TourDisplay.TourUrl				= "http://www.jsvt.net";
TourDisplay.Music				= "";
TourDisplay.TourId				= "";
TourDisplay.PhotoWidth			= 450;
TourDisplay.PhotoHeight			= 270;
TourDisplay.AddCommentsHeight	= 0;
TourDisplay.Photos				= [];
TourDisplay.HasComments			= false;
TourDisplay.SaleStatus			= "";
TourDisplay.FlashProxy		 	= null;
TourDisplay.Util				= new Object();

TourDisplay.Util.Min			= function(){
	var loMin					= arguments[0];
	for(var i=0;i<arguments.length;i++){
		if(arguments[i]!=null && arguments[i] < loMin)
			loMin				= arguments[i];
		}

	return loMin;
	}

TourDisplay.Util.Max			= function(){
	var loMax					= arguments[0];
	for(var i=0;i<arguments.length;i++){
		if(arguments[i]!=null && arguments[i] > loMax)
			loMax				= arguments[i];
		}

	return loMax;
	}

TourDisplay.Util.FixText		= function(str){
	if(str == null || str == "")
		return "";
	return str.split("&nbsp;").join("").split("&amp;").join("&").split("&#44;").join(",");
	}

TourDisplay.Util.FormatDollar	= function(arg) {
	var flt						= TourDisplay.Util.FormatAmount(arg);
	var amt						= flt.split(".");
	var dollar					= amt[0];
	var cent					= amt[1];
	var sign					= "";
	var i, str;

	if(dollar.charAt(0) == "-" || dollar.charAt(0) == "+"){
		sign					= dollar.charAt(0);
		dollar					= dollar.substr(1);
		}
	if(dollar.length > 3){
		str						= dollar.substr(dollar.length-3);
		for(i = dollar.length - 6; i > -1; i -= 3){
			str = dollar.substr(i,3) + "," + str;
			}
		if(i < 0 && i > -3){
			str					= dollar.substring(0,(i+3)) + "," + str;
			}
		}
	else{
		str						= dollar;
		}

	return (sign + "$" + str + "." + cent);
	}

TourDisplay.Util.FormatAmount	= function(arg){
	var flt						= (typeof arg == "string") ? arg : arg.toString();
	var amt						= flt.split(".");
	var dollar					= amt[0];
	var cent					= (amt.length > 1) ? amt[1] : "";
	var str;

	if(cent.length > 2){
		str						= "." + cent.substr(2);
		cent					= cent.substr(0,2);
		if(parseFloat(str) > 0.4){
			cent				= parseInt(cent) + 1;
			}
		if(cent == 0){
			dollar				= parseInt(dollar) + 1;
			}
		dollar					= dollar.toString();
		cent					= cent.toString();
		}

	while(cent.length < 2){
		cent					= cent + "0";
		}

	return (dollar + "." + cent);
	}

TourDisplay.Util.IsEmptyString	= function(str){
	if(str == "")
		return true;

	var loWhitespaceRegex		= /^\s{1,}$/g;
	if((str==null) || (str.length==0) || ((str.search(loWhitespaceRegex)) > -1)) {
		return true;
		}
	else{
		return false;
		}
	}
//Stat Counter
var sc_project=1470451;
var sc_invisible=1;
var sc_partition=13;
var sc_security="2c6b886b";
var sc_remove_link=1;


TourDisplay.CollectImages		= function(){
	var loCollection			= [];
	var loPhotoData				= $id("PhotoData");
	if(!loPhotoData){
		return loCollection;
		}

	TourDisplay.HasComments		= false;
	TourDisplay.SaleStatus		= "";

	var loChildren				= loPhotoData.childNodes;
	for(var i=0;i<loChildren.length;i++){
		var loChild				= loChildren.item(i);
		if(loChild.className == "Photo"){
			var lsName			= TourDisplay.GetChildByClass(loChild,"PhotoName").innerHTML;
			var lsType			= TourDisplay.GetChildByClass(loChild,"PhotoType").innerHTML;
			var lsText			= TourDisplay.GetChildByClass(loChild,"PhotoText").innerHTML;

			var lsSource		= loChild.getElementsByTagName("img")[0].src;
			if(TourDisplay.UseAolFix){
				lsSource		= lsSource.replace(".jpg",".jpgaol");
				}

			var lsComments		= "";
			var loCommentsElement = TourDisplay.GetChildByClass(loChild,"PhotoComments");
			if(loCommentsElement && loCommentsElement.innerHTML && loCommentsElement.innerHTML!="" && !TourDisplay.Util.IsEmptyString(loCommentsElement.innerHTML)){
				lsComments		= loCommentsElement.innerHTML;
				TourDisplay.HasComments = true;
				}

			TourDisplay.SaleStatus = lsText;

			loCollection.push({url:lsSource,type:lsType});
			}
		}

	if(!TourDisplay.HasComments){
		$style("Comments").display = "none";
		}

	return loCollection;
	}

TourDisplay.Initialize			= function(){

	//Detect Aol
	var lsUserAgent				= navigator.userAgent.toLowerCase();
	TourDisplay.UseAolFix		= (lsUserAgent.indexOf("aol")!=-1);

	//Stat Counter
	if(!TourDisplay.IsArchive){
		$script('http://www.statcounter.com/counter/counter_xhtml.js');
		}

	TourDisplay.Photos			= TourDisplay.CollectImages();
	var loSerializer			= new FlashSerializer(false);

	//Check Window Size
	if(TourDisplay.WindowWidth){
		var liAvailableWidth	= TourDisplay.Util.Min(screen.width,screen.availWidth);
		var liAvailableHeight	= TourDisplay.Util.Min(screen.height,screen.availHeight);
		var liDesiredWidth		= TourDisplay.WindowWidth;
		var liDesiredHeight		= TourDisplay.WindowHeight;
		if(liAvailableWidth < TourDisplay.WindowWidth)
			liDesiredWidth		= liAvailableWidth;
		if(liAvailableHeight < TourDisplay.WindowHeight)
			liDesiredHeight		= liAvailableHeight;

		try{
			window.resizeTo(liDesiredWidth,liDesiredHeight);
			}
		catch(e){}
		}

	//Update autoplay button
	TourDisplay.OnModeChange(TourDisplay.Autoplay? 'Slideshow':'Standard');

	//Detect flash
	var loFlashVersion			= deconcept.SWFObjectUtil.getPlayerVersion();
	if(loFlashVersion.major >= 7){
		TourDisplay.UseFlash	= true;
		var liMovieHeight		= TourDisplay.PhotoHeight + (TourDisplay.HasComments? 0:TourDisplay.AddCommentsHeight);

		var lsLine1				= null;
		var lsLine2				= null;
		if(TourDisplay.SaleStatus == null || TourDisplay.SaleStatus == "" || TourDisplay.Util.IsEmptyString(TourDisplay.Util.FixText(TourDisplay.SaleStatus))){
			if(!TourDisplay.Util.IsEmptyString(TourDisplay.Banner)){
				lsLine1			= TourDisplay.Util.FixText(TourDisplay.Banner);
				}
			}
		else{
			lsLine1				= TourDisplay.Util.FixText(TourDisplay.SaleStatus);
			lsLine2				= TourDisplay.Util.FixText(TourDisplay.Banner);
			}

		var loPhotoData			= {
			Photos:TourDisplay.Photos,
			MusicUrl:TourDisplay.Music,
			Line1:lsLine1,
			Line2:lsLine2,
			Mode:TourDisplay.GetMode(),
			ModeCallback:'TourDisplay.OnModeChange',
			PhotoCallback:'TourDisplay.OnPhotoChange',
			LoopCallback:'TourDisplay.OnLoop'
			};

		$style('TourDisplay').height = liMovieHeight + "px";

		var loProxyId			= new Date().getTime();
		var loSerializer		= new FlashSerializer(false);
		TourDisplay.FlashProxy	= new FlashProxy(loProxyId, TourDisplay.FlashProxyUrl);
		var loViewer			= new SWFObject(TourDisplay.Viewer, "TourViewer", TourDisplay.PhotoWidth, liMovieHeight, "7");
		loViewer.addParam("wmode","transparent");
		loViewer.addParam("allowScriptAccess","always");
		loViewer.addVariable("ProxyId",loProxyId);
		loViewer.addVariable("data",EncodeAscii(loSerializer._serializeXML(loPhotoData)));
		loViewer.write("TourDisplay");
//alert(escape(loSerializer._serializeXML(TourDisplay.Banner)))
		//Update button and comments
		TourDisplay.OnPhotoChange(TourDisplay.CurrentPhoto);
		}
	else{
		TourDisplay.UseFlash	= false;
		$style("FlashError").display	= "block";
		$style("PhotoData").display		= "block";
		$style("Comments").display		= "none";
		$style("TourDisplay").display	= "none";
		TourDisplay.ShowPhoto(0);
		}
	}

function EncodeAscii(psString){
	var lsReturn	= "";
	var i			= 0;
	var c			= 0;

	while(i < psString.length){
		c			= psString.charCodeAt(i);
		if(c > 126)
			lsReturn += psString.charAt(i);
		else
			lsReturn	+= escape(psString.charAt(i));
		i++;
		}
	return lsReturn;
	}


TourDisplay.ToggleAutoplay		= function(button){
	//Toggle value
	TourDisplay.Autoplay		= !TourDisplay.Autoplay;

	var lsMode					= TourDisplay.Autoplay? 'Slideshow':'Standard';
	//Tell flash player
	try{
		TourDisplay.FlashProxy.call('SetMode',lsMode);
		}
	catch(e){
		alert("Unable to change photos! \n"+e.message);
		}
	}

TourDisplay.OnModeChange		= function(mode){
	TourDisplay.Autoplay		= (mode == 'Slideshow');

	//Toggle button state
	var loAutoplayButton		= $id("Autoplay");
	if(!loAutoplayButton){
		return;
		}
	if(TourDisplay.Autoplay){
		loAutoplayButton.className = "Checked";
		}
	else{
		loAutoplayButton.className = "";
		}
	}

TourDisplay.OnPhotoChange		= function(photo){
	TourDisplay.CurrentPhoto	= photo;

	var loButton				= $id("PhotoButton"+TourDisplay.CurrentPhoto);
	var loPhoto					= $id("Photo"+TourDisplay.CurrentPhoto);

	//Update button to reflect selected element
	if(loButton){
		var loSiblings			= loButton.parentNode.parentNode.getElementsByTagName("a");
		for(var i=0;i<loSiblings.length;i++){
			var loSibling		= loSiblings[i];
			if(loSibling == loButton)
				continue;

			var lsClassName		= loSibling.className;

			if(lsClassName.indexOf("Selected")!=-1){
				lsClassName		= lsClassName.split("Selected").join();
				loSibling.className = lsClassName;
				}
			}

		loButton.className		= loButton.className+" Selected";
		}

	//Update comments
	var lsComments			= "";
	var loCommentsElement	= TourDisplay.GetChildByClass(loPhoto,"PhotoComments");
	if(loCommentsElement && loCommentsElement.innerHTML && loCommentsElement.innerHTML!="")
		lsComments			= loCommentsElement.innerHTML;

	var loCommentsDisplay	= $id("Comments");
	if(!TourDisplay.Util.IsEmptyString(lsComments)){
		loCommentsDisplay.innerHTML		= lsComments;
		loCommentsDisplay.style.display	= "block";
		}
	else{
		loCommentsDisplay.style.display	= "none";
		}
	}

TourDisplay.OnLoop				= function(){
	var liNextPhoto				= TourDisplay.CurrentPhoto+1;

	var loPhoto					= $id("Photo"+liNextPhoto);
	var loPhotoButton			= $id("PhotoButton"+liNextPhoto);

	//Can't find the next photo, go back to the beginning or start another tour
	if(parent.NextTour){
		parent.NextTour();
		}
	else{
		TourDisplay.ShowPhoto(0,loPhotoButton);
		}
	}

TourDisplay.GetMode				= function(){
	if(TourDisplay.Autoplay){
		return 'Slideshow';
		}
	return 'Standard';
	}

TourDisplay.ShowPhoto			= function(index,element){
	//Find photo element
	var loPhoto					= $id("Photo"+index);
	if(!loPhoto){
		alert("Unable to find the specified photo!");
		return;
		}

	TourDisplay.CurrentPhoto	= index;

	if(TourDisplay.UseFlash){
		//Tell flash player
		try{
			TourDisplay.FlashProxy.call('LoadPhoto',TourDisplay.CurrentPhoto);
			}
		catch(e){
			alert("Unable to change photos! \n"+e.message);
			}
		}
	else{
		//Display using html
		if(TourDisplay.CurrentPhotoElement)
			TourDisplay.CurrentPhotoElement.style.display	= "none";

		TourDisplay.CurrentPhotoElement						= loPhoto;
		TourDisplay.CurrentPhotoElement.style.display		= "block";
		}

	//Update gui
	TourDisplay.OnPhotoChange(TourDisplay.CurrentPhoto);

	return false;
	}

TourDisplay.SelectTab			= function(psDisplayId,element,pbAlwaysReload){
	var loDisplayElement		= $id(psDisplayId);
	var lbUseUrl				= (element.href.indexOf('http')!=-1);

	if(lbUseUrl && !loDisplayElement){
		return true;
		}

	if(!loDisplayElement){
		alert("Unable to find specified tab!");
		return;
		}

	//Hide all tabs
	var loDisplayParent			= loDisplayElement.parentNode;
	for(var i=0;i<loDisplayParent.childNodes.length;i++){
		var loDisplayBox		= loDisplayParent.childNodes[i];
		if(loDisplayBox.tagName && loDisplayBox.tagName.toLowerCase() == "div"){
			loDisplayBox.style.display = "none";
			}

		}

	//Show correct tab
	if(lbUseUrl){
		var loIframe			= loDisplayElement.getElementsByTagName("iframe")[0];
		if(loIframe && (loIframe.src == "about:blank" || pbAlwaysReload)){
			loIframe.src		= element.href;
			}
		}

	loDisplayElement.style.display = "block";

	//Deselect tabs
	var loNavigationElements	= element.parentNode.parentNode.getElementsByTagName("li");
	for(var i=0;i<loNavigationElements.length;i++){
		loNavigationElements[i].className = "";
		}

	//Select correct tab
	element.parentNode.className = "Selected";

	//Stop browser from following link
	return false;
	}

//Utility methods
TourDisplay.GetChildByClass		= function(parent,className){
	var loChildren				= TourDisplay.GetChildrenByClass(parent,className);
	if(loChildren.length < 1)
		return new Object();

	return loChildren[0];
	}

TourDisplay.GetChildrenByClass	= function(parent,className){
	var loResults				= new Array();
	for(var i=0;i<parent.childNodes.length;i++){
		var loChild				= parent.childNodes[i];
		if(loChild.className == className){
			loResults[loResults.length]	= loChild;
			}
		}

	return loResults;
	}

//Schools methods
TourDisplay.GetSchoolsUrl		= function(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry){
	if(TourDisplay.IsArchive)
		return psHref;

	if(psValue == "none")
		return "";
	else if(psValue == "nces" || psValue == "")
		return 'http://nces.ed.gov/globallocator/index.asp?search=1&State='+psState+'&zipcode='+psPostalCode+'&miles=5&itemname=&sortby=name&School=1&PrivSchool=1&College=1&Library=1';
	else
		return psValue;
	}

TourDisplay.ShowSchools			= function(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry){
	var lsUrl					= TourDisplay.GetSchoolsUrl(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry);
	if(lsUrl == "")
		return false;

	window.open(lsUrl,'_blank','width=700,height=550,left=10,top=10,status=no,toolbar=yes,menubar=yes,scrollbars=yes,resizable=no');
	return false;
	}

//Map methods
TourDisplay.GetMapUrl			= function(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry){
	if(TourDisplay.IsArchive)
		return psHref;

	if(psValue == "none")
		return "";
	else if(psValue == "googlemaps" || psValue == "")
		return 'http://maps.google.com/maps?q='+escape(psAddress)+'%2C%20'+escape(psCity)+'%20'+escape(psState)+'%20'+escape(psPostalCode);
	else if(psValue == "mapquest")
		return 'http://www.mapquest.com/maps/map.adp?&address='+escape(psAddress)+'&city='+escape(psCity)+'&state='+escape(psState)+'&zipcode='+escape(psPostalCode);
	else if(psValue == "yahoo")
		return 'http://maps.yahoo.com/py/maps.py?&addr='+escape(psAddress)+'&csz='+escape(psCity)+'%'+escape(psState)+'+'+escape(psPostalCode);
	else
		return psValue;
	}

TourDisplay.ShowMap				= function(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry){
	var lsUrl					= TourDisplay.GetMapUrl(psHref, psValue, psAddress, psCity, psState, psPostalCode, psCountry);

	if(lsUrl == "")
		return false;

	window.open(lsUrl,'_blank','width=700,height=550,left=10,top=10,status=no,toolbar=no,menubar=yes,scrollbars=yes,resizable=no');
	return false;
	}

//Flyer
TourDisplay.FlyerWindowSettings	= 'width=685,height=500,left=10,top=10,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no';

TourDisplay.Util.CalculateLoan	= function(psLoan, psMonths, psRate){
	if(	(psLoan == null || psLoan.length == 0) 	||
		(psMonths == null || psMonths.length == 0)	||
		(psRate == null || psRate.length == 0)){
		alert("Incomplete Data");
		return "Error";
		}

	var lLoan  					= psLoan;
	while(lLoan.indexOf(",")!=-1){
		lLoan					= lLoan.replace(",","");//Remove ,'s
		}
	while(lLoan.indexOf("$")!=-1){
		lLoan					= lLoan.replace("$","");//Remove $'s
		}
	lLoan						= parseInt(lLoan);//Make Integer

	var lMonths   				= parseFloat(psMonths);

	var lRate   				= psRate;
	while(lRate.indexOf("%")!=-1){
		lRate=lRate.replace("%","");//Remove ,'s
		}
	lRate						= parseFloat(lRate) / 1200;//Make Float, divide by 1200

	var lPayment				= lLoan * lRate / (1 - (Math.pow(1/(1 + lRate), lMonths)));

	return TourDisplay.Util.FormatDollar(lPayment);
	// payment = principle * MonthlyInterest/(1 - (1/(1+MonthlyInterest)*Months))
	}

