var pages = [];
var linkButtons = [];
// which tab is currently active
var text_on;
var firstrun = true;
var popout = false;
var subnav;
var all_thumbs;
var page;
var pageSize;
var currentPage = 0;
//var $('row-1');
var start;
var target;
var direction;
var mytimeout;
var toPage;
var browserHeight;
var pl;
var all_pages = [];
var all_pages_temp;
var num_pages;
var prevClass;
var nextClass;
var tipVisible;
var imagesWidths = [];
var paraWidths = [];
var curClass = "";
var qtOn;
var qtInit = false;
var lastpage;
var thehash;
var pageURL = "";
var qtPages = [];
var loops = 0;

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = []; }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = []; }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

gup = function(){
	var regexS = "([\\#][^]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec( window.location.href );
	if(results == null) {
		return "";
	}else{
		return parseInt(results[0].replace("#",""),10);
	}
}

getHash = function() {
	thehash = window.location.hash;
	thehash = thehash.toString();
	//alert(thehash);
	if (thehash.length > 1) {
		return parseInt(thehash.substring(1),10);
	}else{
		return 0;
	}
}

function open_win(_url) {
	window.open(_url);
}

function changeNextPrev() {
	switch (toPage) {
		case 0:
			$('prev-link').className = " blank";
			$('next-link').className = " next";
			break;
		case num_pages:
			$('prev-link').className = " previous";
			$('next-link').className = " home";
			break;
		default:
			$('prev-link').className = " previous";
			$('next-link').className = " next";
			break;
	}
	
	
	if (prevClass != "") {
		if (prevClass.match("-l")) {
			$('prev-link').className = prevClass.substring(2);
		}
	}
	if (nextClass != "") {
		if (nextClass.match("-r")) {
			$('next-link').className = nextClass.substring(2);
		}
	}
	
}

function slideThumbs(n) {
	$('thumbs-list').style.left = 738 * n * -1 + 'px';
	if (n==0) {
		$('prev-link').className = " arrowoff";
		$('next-link').className = "";
		if (currentPage > 4) {$('knob').style.left = '-1000px';}
	}else{
		$('prev-link').className = "";
		$('next-link').className = " arrowoff";
		if (currentPage < 5) {$('knob').style.left = '-1000px';}
	}
}

function hideQT() {
	document.getElementById('qt'+(currentPage + 1)).className = "hideDIV";
}

function showQT() {
	toPage++;
	var tempQT = document.getElementById('qt'+toPage);
	tempQT.className = 'showDIV';
	//window["$('qt"+toPage+"')"].className = 'showDIV';
	// fucking quicktime
	/*
if (qtPages.length > 0) {
		if (qtPages.find(toPage)) {
			$('qt').className = " displayBlock";
		}
	}
*/
	
/*
	if ($('qt') != undefined) {
		if (qtOn && qtInit) {$('qt').className = " offLeft";}
		j = all_pages[toPage].getElementsByTagName("div");
		if (j[0].className.match("QT")) {
			if (!qtInit) {
				$('qt').className = " displayBlock";
				qtInit = true;
			}else{
				$('qt').className = " backOn";
			}
			qtOn = true;
		}
	}	
*/
}

var increment = 30;

function pageSlider() {
	//start = parseInt($('pages-container').style.left);
	if (direction == -1) {
		$('pages-container').style.left = target * -1 + "px";
	}else{
		$('pages-container').style.left = target * -1 + "px";

	}
	//showQT();

/*
	start = parseInt($('row-1').style.left);
	
	
	
	if (direction == -1) {
		start = (parseInt($('row-1').style.left,10)*-1) - increment;
	}else{
		start = (parseInt($('row-1').style.left,10)*-1) + increment;
	}
	
	var numLoops = parseInt(pageSize / increment,10);
	
	if (loops > parseInt((numLoops / 4) * 3,10)) {
		increment -= 7;
	}
	
	loops += 1;d
	
	if (start > target && direction == 1) {
		$('row-1').style.left = target * -1 + "px";
		increment = 30;
		loops = 0;
		return false;
	}else{
		$('row-1').style.left = start * -1 + "px";
	}
	
	if (start < target && direction == -1) {
		$('row-1').style.left = target * -1 + "px";
		increment = 30;
		loops = 0;
		return false;
	}else{
		$('row-1').style.left = start * -1 + "px";
	}
	 
	if (start < target && direction == 1) {
		my_timeout = setTimeout("pageSlider();",10);
	}
	if (start > target && direction == -1) {
		my_timeout = setTimeout("pageSlider();",10);
	}
	showQT();
*/

}

function setClasses() {
	if (toPage > 0) {
		prevClass = all_pages[toPage - 1].className;
	}else{prevClass="";}
	if (toPage < (num_pages) || toPage == 0) {
		if (all_pages.length > toPage + 1) {
			nextClass = all_pages[toPage + 1].className;
		}else{nextClass="";}
	}else{nextClass="";}
}

function updateLocation() {
	pageURL = window.location.href;
	thehash = window.location.hash;
	thehash = thehash.toString();
	if (thehash.length == 1) {thehash = "";}
	if (thehash.length > 1 || currentPage > 0) {
		window.location.href = "#" + currentPage;
		window.location.hash = currentPage.toString();
	}
}

function slidePageNext2() {
	if (currentPage < num_pages) {
		//if (!tipVisible || currentPage > 2) {hideTip();}
		target = (currentPage + 1) * pageSize;
		//start = currentPage * pageSize;
		direction = 1;
		toPage = currentPage + 1;
		pageSlider();
		setClasses();
		changeNextPrev();
		currentPage++;
		//start = currentPage * pageSize * -1;
		//updateLocation();
		return false;
	}else{
		if (whatPage.match("about")) {
			top.location='../';
		}else{
			top.location='../../';
		}
	}
}

function slidePagePrev2() {
	if (currentPage > 0) {
		target = (currentPage - 1) * pageSize;
		//start = currentPage * pageSize;
		direction = -1;
		toPage = currentPage - 1;
		pageSlider();
		setClasses();
		changeNextPrev();
		currentPage--;
		//start = currentPage * pageSize * -1;
		//updateLocation();
		return false;
	}else{return false;}
}

function slidePageNext() {
	if (currentPage == 4) {slideThumbs(1);}
	if (currentPage < num_pages) {
		slidePageTo(currentPage + 1);
	}
}

function slidePagePrev() {
	if (currentPage == 5) {slideThumbs(0);}
	if (currentPage !== 0) {
		slidePageTo(currentPage - 1);
	}
}

function moveKnob() {
	if (toPage > 5) {toPage -= 5;}
	$('knob').style.left = 38 + (148 * (toPage - 1)) + 'px'; 
}

function slidePageTo(n) {
	hideQT();
	$('pages-container').style.left = n * pageSize * -1 + "px";
	toPage = n;
	showQT();
	moveKnob();
	//setClasses();
	//changeNextPrev();
	currentPage=n;
	//updateLocation();
	return false;
}

function findThumbs() {
	//temp = document.getElementById('thumbs_container');
	all_thumbs = $('thumbs-list').getElementsByTagName('a');
	var number_of_link = all_thumbs.length;
	for(i=0;i<number_of_link;i++){
		all_thumbs[i].onfocus = new Function("this.blur();");
		all_thumbs[i].onclick = new Function("slidePageTo("+(i)+"); return false;");
	}	
}


// Attach event listeners to the links
function findPages() {

	all_pages_temp = pl.getElementsByTagName("li");
	
	num_pages = all_pages_temp.length;
	
	var tempImg;
	var tempWidth;
	var tempP;
	var k = 0;
	
	for (i=0;i<num_pages;i++) {
		// Simply Create an Array from the HTMLCollection
		all_pages[i] = all_pages_temp[i];
		if (window.ie) {
			all_pages[i].id = i.toString();
		}
		
		// this determines which pages there is a QT plugin
		//var tempDIV = all_pages[i].getElementsByTagName("div");
		//if (tempDIV[0].className.match("qt")) {
		//	qtPages[k] = i;
			//qtPages[k+1] = tempDIV[0];
		//	k++;
		//}
		
		// This is for the fucking captions that won't align to skinny images
		/*
tempImg = all_pages[i].getElementsByTagName("img");
		tempP = all_pages[i].getElementsByTagName("p");
		if (tempImg[0] !== undefined) {
			tempP[0].style.width = tempImg[0].width + "px";
		}
*/
	}
	num_pages--;
}


function listenHREF () {
	if (lastpage != undefined) {
		page = getHash();
		//alert(window.location.href);
		if (lastpage != page) {
			lastpage = page;
			slidePageTo(page);
		}
	}
}

function showContact() {
	$('contact-info').style.display = 'block';
}

function showTip() {
	if (!whatPage.match("about")) {
		if (currentPage == 0 || currentPage == 1 || currentPage == 2) {
			$('next-tip').style.display = 'block';
			tipVisible = true;
		}
	}
}

function hideTip() {
	$('next-tip').style.display = 'none';
	tipVisible = false;
}

function goBackPage() {
	history.back();
}

var pc;
var pcLI;
var isIE;

// Initialize the rollovers/tab effects
window.onload = function() {
	$('loader').style.display = 'none';
	$('prev-link').onclick = new Function("slideThumbs(0); return false;");
	$('prev-link').onfocus = new Function("this.blur();");
	$('next-link').onclick = new Function("slideThumbs(1); return false;");
	$('next-link').onfocus = new Function("this.blur();");
	findThumbs();
	start = 0;
	browserHeight = window.innerHeight;
	pl = $('row-1');
	whatPage = document.body.id;
	// HOME PAGE
	if (whatPage.match("home-page")) {
		document.addEvent('keydown', function(e) {
			 e = new Event(e); // .stop() // oops don't stop, we might want that!
			 if (e.key == 'left') {
			 goBackPage();
			 }
		});
	}
	// GALLERY AND ABOUT PAGES
	if (whatPage.match("home")) {
		tipVisible = false;
		document.addEvent('keydown', function(e) {
			 e = new Event(e); // .stop() // oops don't stop, we might want that!
			 if (e.key == 'right') {
			 slidePageNext();
			 } else if (e.key == 'left') {
			 slidePagePrev();
			}
		});
		
		// Loops through all LI elements
		findPages();
		pc = $('pages-container');
		pc = pc.getElementsByTagName("li");
		pageSize = pc[0].offsetWidth + 20;
		/*
page = getHash();
		pageURL = window.location.href;
		if (page != 0) {
			pageURL = pageURL.slice(0,pageURL.indexOf("#" + page));
		}
*/
		//slidePageTo(page);
		lastpage = 0;
		//captureHistory = setInterval("listenHREF()",200);
	}
};