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 pageWidth;
var currentPage = 0;
//var $('about-pages');
var TOTALPAGES = 2;
var start;
var target;
var direction;
var mytimeout;
var toPage;
var browserHeight;

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

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


// FOR THE MAIN PROJECT PAGE
function rolloverProjects(value) {
	if (text_on !== undefined) {
		pages[text_on].style.display = 'none';
	}
	pages[value].style.display = 'block';
	text_on = value;
}

// FOR THE HOMEPAGE / WORK PAGES
function changePage(value) {
	if (text_on !== undefined || value != text_on) {
		lastpage = page;
		pages[text_on].className = " offLeft";
		//linkButtons[text_on].className = "off";
		pages[value].className = " backOn";
		if (value == 0) {
			subnav.className = " offLeft";
			popout=false;
		}
		if (!popout && value > 0) {
			subnav.className = " backOn";
			popout=true;
		}
		if (value == 1) {
			// turns clicked tab on
			linkButtons[2].className = " on";
			linkButtons[3].className = " off";
		}
		if (value == 2) {
			linkButtons[3].className = " on";
			linkButtons[2].className = " off";
			}
	}

	// turns clicked tab on
	//linkButtons[value].className = " on";
	
	// sets state of what the current tab is
	text_on = value;
	firstrun = false;
	text_on = value;
	setOffset();
}

// FOR THE ABOUT PAGE

function changeBullet(_from,_to) {
	linkButtons[_from].className = " off";
	linkButtons[_to].className = " on";
}

function changeNextPrev() {
	switch (toPage) {
		case 0:
			$('prev-link').className = " blank";
			$('next-link').className = " pressR";
			break;
		case 1:
			$('prev-link').className = " statementL";
			$('next-link').className = " resumeR";
			break;
		case 2:
			$('prev-link').className = " pressL";
			$('next-link').className = " blank";
		default:
			break;
	}
}

function pageSlider() {
	//foo = pages.style.left;
	if (direction == -1) {
		start = (parseInt($('about-pages').style.left,10)*-1) - 20;
	}else{
		start = (parseInt($('about-pages').style.left,10)*-1) + 20;
	}
	
	if (start > target && direction == 1) {
		$('about-pages').style.left = target * -1 + "px";
		return false;
	}else{
		$('about-pages').style.left = start * -1 + "px";
	}
	
	if (start < target && direction == -1) {
		$('about-pages').style.left = target * -1 + "px";
		return false;
	}else{
		$('about-pages').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);
	}
}


function slidePageNext() {
	if (currentPage < TOTALPAGES) {
		//currentPage++;
		//pages.style.left = currentPage * pageWidth * -1 + "px";
		target = (currentPage + 1) * pageWidth;
		start = currentPage * pageWidth;
		direction = 1;
		pageSlider();
		changeBullet(currentPage,currentPage+1);
		toPage = currentPage + 1;
		changeNextPrev();
		currentPage++;
		return false;
	}else{return false;}
}

function slidePagePrev() {
	if (currentPage > 0) {
		//currentPage--;
		//pages.style.left = currentPage * pageWidth * -1 + "px";
		target = (currentPage - 1) * pageWidth;
		start = currentPage * pageWidth;
		direction = -1;
		pageSlider();
		//pageSlider(currentPage,currentPage*pageWidth,-1);
		changeBullet(currentPage,currentPage-1);
		toPage = currentPage - 1;
		changeNextPrev();
		currentPage--;
		return false;
	}else{return false;}
}

function slidePageTo(n) {
	if (currentPage != n) {
		if (currentPage < n) {
			direction = 1;
			target = (currentPage - (currentPage - n)) * pageWidth;
			start = currentPage * pageWidth;
		}else{
			direction = -1;
			target = (currentPage + (n - currentPage)) * pageWidth;
			start = currentPage * pageWidth;
		}
		pageSlider();
		changeBullet(currentPage,n);
		toPage = n;
		changeNextPrev();
		currentPage=n;
	}
	return false;
}

// Attach event listeners to the links
function findPages() {
	all_divs = document.getElementsByTagName("div");
	all_links = document.getElementsByTagName("a");
	var cur;
	var t;
	h = 0; 
	for (i=0; i<all_divs.length; i++) {
		cur = all_divs[i];
		t = all_divs[i].id;
		if (t.match("_page")) {
			pages[h] = cur;
			h++;
		}
	}
	h = 0;
	for (i=0; i<all_links.length; i++) {
		cur = all_links[i];
		t = all_links[i].id;
		if (t.match("_link")) {
			linkButtons[h] = cur;
			h++;
		}
	}
	
	if (whatPage.match("home-page")) {
		subnav = document.getElementById("subnav");
	
		linkButtons[0].onclick = function () {changePage(0);};
		linkButtons[1].onclick = function () {changePage(1);};
		linkButtons[2].onclick = function () {changePage(1);};
		//linkButtons[3].onclick = function () {changePage(2);};
	}
	if (whatPage.match("about-page")) {
		var number_of_link = linkButtons.length;
		for(i=0;i<number_of_link;i++){
			linkButtons[i].onfocus = new Function("this.blur();");
			linkButtons[i].onclick = new Function("slidePageTo("+(i)+"); return false;");
	}
	}
}

/*
function listenHREF () {
	if (lastpage != undefined) {
		page = gup();
		if (lastpage != page) {
			changePageFromHREF();
			lastpage = page;
		}
	}
}
*/

/*
function changePageFromHREF () {
	switch (page) {
		case "collage":
			changePage(1);
			break;
		case "assemblage":
			changePage(2);
			break;
		default:
			changePage(0);
			break;
	}
}
*/

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

/*
function pageLoaded() {
	if (document.body.id.match("home-page")) {
		findPages();
		//findThumbs();
		page = gup();
		linkButtons[2].className = "off";
		linkButtons[3].className = "off";
		text_on = 0;
		changePageFromHREF();
	}	
}
*/

var pc;
var isIE;

// Initialize the rollovers/tab effects
window.onload = function() {
	pc = $('photo_container');
	browserHeight = window.innerHeight;
	//if (browserHeight == undefined) {browserHeight = document.body.clientHeight}
	var myWidth = 0;
	var myHeight = 0;
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		browserHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  browserHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  browserHeight = document.body.clientHeight;
	}
	$('contact').onclick = function () {showContact(); return false;};
	whatPage = document.body.id;
	if (whatPage.match("home-page") || whatPage.match("assem-page")) {
		/* pageLoaded(); */
		subnav = document.getElementById("subnav");
		if (browserHeight < 800) {
			pc.style.marginTop = "5px";
			pc.style.marginBottom = "5px";
			pc.style.height = "517px";
			if (browserHeight < 630) {
				$('caption').style.height = "15px";
				$('controller').style.width = "770px";
				$('collages_page').style.position = "relative";
				$('controller').style.position = "absolute";
				$('controller').style.top = "500px";
				$('controller').style.left = "0";
				$('controller').style.margin = "0";
			}
		}
		//captureHistory = setInterval("listenHREF()",500);
	}
	if (whatPage.match("about-page")) {
		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();
			}
		});
		//theNext = document.getElementById("next-link");
		//thePrev = document.getElementById("prev-link");
		findPages();
		//$('about-pages') = document.getElementById("about-pages");
		$('about-pages').style.left = "0px";
		pageWidth = document.getElementById("about_container").offsetWidth + 20;
		//slidePageTo(1);
	}
};