// JavaScript Document

////////////////////////////////////////////////////////////////////////
// Horizontal Scroll Stuff
var numpics = 5;
var thumbnailStart = 0;
var thumbnailEnd = 0;

//resizePlayer();
prevWindowHeight = getWindowHeight();
prevWindowWidth = getWindowWidth();

/*window.onresize = function() {
	currWindowHeight = getWindowHeight();
	currWindowWidth = getWindowWidth();
	if ((Math.abs(currWindowHeight - prevWindowHeight) + Math.abs(currWindowWidth - prevWindowWidth)) > 10) {
		// IE may fire resize event too many times
		prevWindowHeight = currWindowHeight;
		prevWindowWidth = currWindowWidth;
		//resizePlayer();
		updateThumbnails();
	}
};*/

function resizePlayer() {
	var windowHeight = getWindowHeight();
	var windowWidth = getWindowWidth();
	var thumbnailsWidth = 0.9 * (windowWidth - 200);
	if (thumbnailsWidth < 800) thumbnailsWidth = 800;
	thumbnailsWidth = Math.floor((thumbnailsWidth - 44) / 160) * 160;
	document.getElementById('photothumbs').style.width = thumbnailsWidth + "px";
	document.getElementById('photothumbscontainer').style.width = (thumbnailsWidth + 44) + "px";
	
}

function updateThumbnails( theDirection ) {
		
	var imagelistcount = 0;
	var thestringarray = '';	
	var debugmessage = '';
	
	//alert(theDirection);
	
	imageList = document.getElementById("photothumbs").childNodes;
	
	//debugmessage = debugmessage + '[thumb '+thumbnailStart+' to '+thumbnailEnd+' of '+imagelistcount+']';
	
	//beyond the start of the loop?
	/*
	if ( ( thumbnailEnd >= imagelistcount ) || ( thumbnailEnd <= 0 ) ) {
		
		//debugmessage = debugmessage+'starting over.';
		thumbnailStart = 0;
				
	}
	*/
	
	thumbnailEnd = (thumbnailStart + numpics)-1;
	
	for (i = 0; i < imageList.length; i++) {
		if(imageList[i] && imageList[i].id && imageList[i].getAttribute("id") == "photothumbsfeeds") {
			
			imagelistcount++;
			//add the image indexes to string (to make an array later)
			thestringarray = thestringarray + i + ',';
		}
	}
	
	//make array from the 'photothumbsfeeds' divs
	var thethumbarray = thestringarray.split(",");
	
	//iterate through the full feedimage list, flipping on or off as the 'window' dictates
	for (i2 = 0; i2 < (thethumbarray.length)-1; i2++) {
		
		imageList[thethumbarray[i2]].style.display="none";
		
		if ( ( i2 >= thumbnailStart ) && ( i2 <= thumbnailEnd ) ) {
			
			imageList[thethumbarray[i2]].style.display="inline";
		
		}
		
	}
	
	//debugging div
	debugmessage = debugmessage + '[thumb '+thumbnailStart+' to '+thumbnailEnd+' of '+imagelistcount+']';
	//document.getElementById("debugdiv").innerHTML = debugmessage;
	
	//end of the loop?
	if ( ( thumbnailEnd >= imagelistcount ) || ( thumbnailEnd <= 0 ) ) {
		
		//start loop over
		thumbnailStart = -5;
	
	} else {
	
		//not the end of the loop
		//which way are we going?
		if ( theDirection == 'left' )
		{
			//decrement
			thumbnailStart --;
			//thumbnailStart = thumbnailStart + 1;	//teeny adjustment
			
		} else {
			
			//increment
			//thumbnailStart = thumbnailEnd + 1;
			
			
		}
		
				
	}
	
	
	return;
	
}

function sortNumber(a, b)
{
	return a - b;
}

function calcThumbnailsCount() {
	numImages = document.getElementById('photothumbs').style.width;
	numImages = numImages.substring(0, numImages.length - 2);
	return Math.floor(numImages / 160);
}


function getWindowHeight() {
	var windowHeight = 0;
	if( typeof( window.windowHeight ) == 'number' ) {
		// Non-IE
		windowHeight = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// IE 6 in 'standards compliant mode'
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		// IE 4 compatible
		windowHeight = document.body.clientHeight;
	}
	return windowHeight;
}

function getWindowWidth() {	// Analogous to getWindowHeight()
	var windowWidth = 0;
	if( typeof( window.windowWidth ) == 'number' ) {
		windowWidth = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		windowWidth = document.body.clientWidth;
	}
	return windowWidth;
}
