// JavaScript Document

var theNum;
var folderName;

var firstTime; //lets system know if a portfolio has already been gone through

//this variable tests to see if a menu item has been clicked
//i.e. the item with the focus
var theMenuItemWithFocus;

//variable 'theId' appears in more than one function
//must be set differently for iPad version
theId='theImageArea';

/*function setTheId() {
	if (isIpad) 
		{
			theId='theImageArea_ipad'
		} else {
				theId='theImageArea';
			}
}

setTheId();*/

theString='frazonc-mic_cog';

awaitingDownloadId='hideAwaitingDownload'; 

var nextImageInProcess;
var imageExists;

//this variable is a boolean to indicate whether the splash image is up
//and if so it allows the swipe functionality to be turned off
splashIsUp=false

//testImage is a holder image object into which each the image source will be placed for testing
testImage = new Image();
testImage.onerror = function () {
	//alert('image does not exist');
	imageExists = false; 
	
	//if error generated, image does not exist and must complete nextImage function call
	//by resetting counter and starting with the first image
	if (nextImageInProcess) {
		theNum=0;
		firstTime=false;
		nextImage();
	}
	nextImageInProcess=false;
}

/* UPDATE 10/29/11 - PRELOAD TURNED OFF - REMOVE MOUSEOVER

//preload rollover images

splashPic = new Image();
portraitsPic = new Image();
nightCandyPic = new Image();
anomaliesPic = new Image();
redhookPic = new Image();
porgyPic = new Image();
ftrainPic = new Image();
chickenMarketPic = new Image();

splashPic.src = "img/imageSplash.jpg";
portraitsPic.src = "img/portraits/portraits1.jpg";
nightCandyPic.src = "img/nightCandy/nightCandy1.jpg";
anomaliesPic.src = "img/anomalies/anomalies1.jpg";
redhookPic.src = "img/redhook/redhook1.jpg";
porgyPic.src = "img/porgy/porgy1.jpg";
ftrainPic.src = "img/ftrain/ftrain1.jpg";
chickenMarketPic.src = "img/chickenMarket/chickenMarket1.jpg";

*/

function showTheString() {
	re1='-';
	re2='_';
	re3='g';
	re4='i';
	re5='k';
	re6='f';
	re7='z';
	theString2=((((((theString.replace(re1, '@')).replace(re2, '.')).replace(re3, 'm')).replace(re4, 'a')).replace(re5, 'd')).replace(re6, 'd')).replace(re7, 'g');
	document.getElementById('theStringId').innerHTML=theString2;
}

function changeTheString() {
	re1='-';
	re2='_';
	re3='g';
	re4='i';
	re5='k';
	re6='f';
	re7='z';
	theURL = 'mailto:' + ((((((theString.replace(re1, '@')).replace(re2, '.')).replace(re3, 'm')).replace(re4, 'a')).replace(re5, 'd')).replace(re6, 'd')).replace(re7, 'g');
	location.href=theURL;
}

function resizeWindow() {
	//load splash image
	document.getElementById(theId).style.background="url('img/imageSplash.jpg') no-repeat center center";
	
	//check to see if platform is ipad/ipod/iphone
	//if not then turn off contain to keep image from intersecting nav arrows
	if (isIpad) {
		document.getElementById(theId).style.backgroundSize='contain';
	}
	
	//turn off click and swipe functionality over the splash image
	splashIsUp=true;
	
	//make sure to clear a menu selection if viewer clicks before splash loads
	document.getElementById(theId).innerHTML="";
	folderName="";
	theNum=undefined;
	
	//set browser for iPad/iPhone/iPod
	if (isIpad)
		{
			theWidth=1000;
			theHeight=760;
			this.resizeTo(theWidth,theHeight);
			return;
		}
	
	chromeHeight=this.outerHeight-this.innerHeight;
	chromeWidth=this.outerWidth-this.innerWidth;
	
	theWidth=1280;
	theHeight=900;
	
	theInnerWidth=1100;
	theInnerHeight=760;
	
	//check to see if innerWidth and innerHeight defined
	if(isNaN(this.innerWidth) || isNaN(this.innerHeight)) {
		
		//innerHeight and innerWidth not defined
		//set height and width
		this.resizeTo(theWidth,theHeight);
	}
	
	
	//check to see if screen is large enough for height and width
	if (screen.availWidth>=theWidth && screen.availHeight>=theHeight) {
		theWidth=theInnerWidth+chromeWidth;
		theHeight=theInnerHeight+chromeHeight;
		this.resizeTo(theWidth,theHeight);
	}
	
	//screen not wide enough
	if (screen.availWidth<theWidth && screen.availHeight>=theHeight) {
		theWidth=screen.availWidth;
		theHeight=theInnerHeight+chromeHeight;
		this.resizeTo(theWidth,theHeight);
	}
	
	//screen not tall enough
	if (screen.availWidth>=theWidth && screen.availHeight<theHeight) {
		theWidth=theInnerWidth+chromeWidth;
		theHeight=screen.availHeight;
		this.resizeTo(theWidth,theHeight);
	}
	
	//both not enough
	if (screen.availWidth<theWidth && screen.availHeight<theHeight) {
		//ipad screen is 1024x768 and causes wrapping
		//so 1045 is the limit
		if (screen.availWidth<=1040) 
			{
				screen.outerWidth=1100;
			} else {
				theWidth=screen.availWidth;
			}
		theHeight=screen.availHeight;
		this.resizeTo(theWidth,theHeight);
	}
}

function chooseFolder(theFolderName) {
	
	//UPDATE 10/29/11 - ADD TEST TO SEE IF THERE IS A CHANGE IN FOLDER AND RESET THENUM - REMOVE MOUSEOVER
	
	if(folderName!=theFolderName) {
		theNum=undefined;
		
		//reset firstTime boolean so that intro text will show
		firstTime=true;
	}
	
	folderName=theFolderName;
	
	//set to false so that a new image can be loaded
	splashIsUp=false;
	
	//test to see if menu item has already been clicked
	//if so do not allow nextImage function to proceed
	//if (theMenuItemWithFocus!=folderName) {
		nextImage();
	//}
}

function nextImage() {
	//This statement is for onclick in imageContainer
	//Allows image to advance only if a folder has been selected from menu
	if (folderName==undefined) return;
	
	//if splash image is clicked do nothing
	if (splashIsUp) {
		return;
	}
	
	theFileName= 'txt/'+ folderName + '.txt';	
	startNum=0;
		
	if (theNum == undefined) {
		//delete background image  also theId defined at top of file
		document.getElementById(theId).style.backgroundImage=""; 
		
		//make sure no captions are left over
		clearCaption();
		
		theNum=startNum;       //theNum is defined outside the function so it retains its value
		
		//UDATE 10/29/11 - NEXTIMAGE SHOWS IMAGE BEFORE SHOWING TEXT - REMOVE MOUSEOVER
		showImage(folderName);
		
		//make just arrow Next image navigation visible
		document.getElementById('arrowNext').style.visibility='visible';
		document.getElementById('arrowPrev').style.visibility='hidden';
		
		turnOnMenuItemFocus(folderName);
		return;
	} else {
		
		//UDATE 10/29/11 - SHOW TEXT WHEN THENUM = 0 - REMOVE MOUSEOVER
		
		if (theNum == 0 && firstTime==true) {
			//delete background image  also theId defined at top of file
			document.getElementById(theId).style.backgroundImage="";
		
			//make sure no captions are left over
			clearCaption();
			
			loadExternalFile(theFileName); //load intro file
			//alert('file loaded');
			
			//make just arrow Next image navigation visible
			document.getElementById('arrowNext').style.visibility='visible';
			document.getElementById('arrowPrev').style.visibility='hidden';
			
			theNum++;
			
			return
		}
		
		//reveal previous arrow in image navigation
		document.getElementById('arrowPrev').style.visibility='visible';
		
		//get rid of intro text
		document.getElementById(theId).innerHTML='';
		
		
		//increment theNum to reflect next image
		theNum++;
		
		theImagePath = 'img/' + folderName + '/' + folderName + theNum + '.jpg';
		testTheImage(theImagePath)
		
		//if image test fails onerror function does not return to this function
		//need trigger that will indicate that function needs completing
		nextImageInProcess = true;
		
		//test to see if theNum points to an actual image using image.onerror function
		//if test fails onerror function does not return to this function
		if(imageExists) {
			
			//clear old caption in case of slow connection - can cause confusion
			clearCaption();
			
			theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
			document.getElementById(theId).style.background=theBackgroundCssCode;
			
			//check to see if platform is ipad/ipod/iphone
			//if not then turn off contain to keep image from intersecting nav arrows
			if (isIpad) {
				document.getElementById(theId).style.backgroundSize='contain';
			}
			
			//turn on swipe functionality
			splashIsUp=false;
			
			//theCaptionName is the file where the caption is loacated
			theCaptionName='caption/' + folderName + '/' + theNum + '.txt';
			//alert(theCaptionName);
			loadCaption(theCaptionName);
		
		} 
	}
}

function prevImage() {
	if (theNum>1) {
		--theNum;
		theImagePath = 'img/' + folderName + '/' + folderName + theNum + '.jpg';
		theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
		document.getElementById(theId).style.background=theBackgroundCssCode;
		
		//check to see if the platform is ipad/ipod/iphone
		//contain statement makes image hit nav arrows on computer version
		if (isIpad) {
			document.getElementById(theId).style.backgroundSize='contain';
		}
		
		//theCaptionName is the file where the caption is loacated
		theCaptionName='caption/' + folderName + '/' + theNum + '.txt';
		loadCaption(theCaptionName);
		//if the image is 2 and becomes 1 this prevents it
		//from going into the next code block
		return;
	}
	
	//THIS IS WHERE THE PORTFOLIO SIZES ARE RECORDED
	if (theNum==1) {
		switch(folderName) {

			case 'portraits':
				theNum=23;
				firstTime=false;
				break;
			
			case 'nightCandy':
				theNum=8;
				firstTime=false;
				break;
				
			case 'anomalies':
				theNum=17;
				firstTime=false;
				break;
				
			case 'sa':
				theNum=25;
				firstTime=false;
				break;
			
			case 'redhook':
				theNum=26;
				firstTime=false;
				break;
				
			case 'porgy':
				theNum=15;
				firstTime=false;
				break;
				
			case 'ftrain':
				theNum=14;
				firstTime=false;
				break;
		}
		
		theImagePath = 'img/' + folderName + '/' + folderName + theNum + '.jpg';
		theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
		document.getElementById(theId).style.background=theBackgroundCssCode;
		
		//check to see if platform is ipad/ipod/iphone
		//if not then turn off contain to keep image from intersecting nav arrows
		if (isIpad) {
			document.getElementById(theId).style.backgroundSize='contain';
		}
		
		//turn on swipe functionality
		splashIsUp=false;
		
		//theCaptionName is the file where the caption is loacated
		theCaptionName='caption/' + folderName + '/' + theNum + '.txt';
		loadCaption(theCaptionName);
	}
}

function testTheImage(theImageName) {
	//set variable to true if an error is thrown variable state will be set to false
	imageExists = true;
	//if image does not exist it will now generate an error - see top of file
	testImage.src=theImageName;
}

/*function test() {
	//document.getElementById('imageArea').innerHTML="";
	//document.getElementById('imageArea').style.backgroundImage="";
	document.getElementById('hideAwaitingDownload').style.visibility="visible";
}*/

function showImage(theFolderName) {
	folderName=theFolderName;
	
	//test to see if menu item has been moused over already
	//alert('menuItemFocus='+ menuItemFocus + ' and theMenuItemWithFocus=' + theMenuItemWithFocus + ' and the folderName=' + folderName);
	if (theMenuItemWithFocus!=folderName) {
		
		//delete any text in the image area  theId defined at top of file
		document.getElementById(theId).innerHTML='';
	
		//remove image navigation arrows
		document.getElementById("arrowNext").style.visibility="hidden";
		document.getElementById("arrowPrev").style.visibility="hidden";
		//ipad cannot do mouseover and so navigation right nav arrow should appear on "first tap"
		
		//If platform is ipad show nav arrow and set set splashIsUp to false
		//so swipe will work
		if (isIpad) {
			document.getElementById("arrowNext").style.visibility="visible";
			splashIsUp=false;
		}
		
		//clear out the captions
		document.getElementById('theCaption').innerHTML="";
	
		theImagePath = 'img/' + theFolderName + '/' + theFolderName + '1.jpg';
		theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
	
		//delete any text in the image area  theId defined at top of file
		//second time to make sure it really goes
		document.getElementById(theId).innerHTML='';
		
		//make sure no captions are left over
		clearCaption();
	
		document.getElementById(theId).style.background=theBackgroundCssCode;
		
		//check to see if platform is ipad/ipod/iphone
		//if not then turn off contain to keep image from intersecting nav arrows
		if (isIpad) {
			document.getElementById(theId).style.backgroundSize='contain';
		}
		
		//UPDATE 10/29/11 - COUNTER NO LONGER RESET HERE BUT GOES TO 0 SO THAT TEXT CAN SHOW - REMOVE MOUSEOVER
		theNum=0;

	} else {
		//nothing happens menu item is inactive
		
		if (theMenuItemWithFocus==folderName && theNum==undefined) {
			//reset menu item to be active again
			menuItemFocus=false;
			theMenuItemWithFocus="";
			
			//delete any text in the image area  theId defined at top of file
			document.getElementById(theId).innerHTML='';
	
			//remove image navigation arrows
			document.getElementById("arrowNext").style.visibility="hidden";
			document.getElementById("arrowPrev").style.visibility="hidden";
	
			theImagePath = 'img/' + theFolderName + '/' + theFolderName + '1.jpg';
			theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
	
			//delete any text in the image area  theId defined at top of file
			//second time to make sure it really goes
			document.getElementById(theId).innerHTML='';
	
			document.getElementById(theId).style.background=theBackgroundCssCode;
			
			//check to see if platform is ipad/ipod/iphone
			//if not then turn off contain to keep image from intersecting nav arrows
			if (isIpad) {
				document.getElementById(theId).style.backgroundSize='contain';
			}
			
		}
	}
}

function turnOnMenuItemFocus(theFolderName) {
	folderName=theFolderName;
	theMenuItemWithFocus=theFolderName;
}

function loadExternalFile(theFileName)
{
	//remove background image
	document.getElementById(theId).style.backgroundImage="";

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
 			document.getElementById(theId).innerHTML=xmlhttp.responseText;
	    }
	}
	xmlhttp.open("GET",theFileName,true);
	xmlhttp.send();
}

/*NOT SURE IF THIS IS ALL LEFT-OVER GARBAGE
//delete any text in the image area  theId defined at top of file
		document.getElementById(theId).innerHTML='';
	
		//remove image navigation arrows
		document.getElementById("arrowNext").style.visibility="hidden";
		document.getElementById("arrowPrev").style.visibility="hidden";
	
		theImagePath = 'img/' + theFolderName + '/' + theFolderName + '1.jpg';
		theBackgroundCssCode='url(' + theImagePath + ') no-repeat center center';
	
		//delete any text in the image area  theId defined at top of file
		//second time to make sure it really goes
		document.getElementById(theId).innerHTML='';
	
		document.getElementById(theId).style.background=theBackgroundCssCode;
*/

function loadCaption(theFileName) {

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
 			document.getElementById('theCaption').innerHTML=xmlhttp.responseText;
	    }
	}
	xmlhttp.open("GET",theFileName,true);
	xmlhttp.send();
	
}

function clearCaption() {
	document.getElementById('theCaption').innerHTML="";
}

function backToSplash() {
	//load splash image
	document.getElementById(theId).style.background="url('img/imageSplash.jpg') no-repeat center center";
	
	//check to see if platform is ipad/ipod/iphone
	//if not then turn off contain to keep image from intersecting nav arrows
	if (isIpad) {
		document.getElementById(theId).style.backgroundSize='contain';
	}
	
	//turn off click and swipe functionality over the splash image
	splashIsUp=true;
	
	//make sure to clear a menu selection if viewer clicks before splash loads
	document.getElementById(theId).innerHTML="";
	folderName="";
	theNum=undefined;
	
	clearCaption();
}

//GOOGLE ANALYTICS CODE - ASYNCHRONOUS

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22079453-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
