﻿
var pageArray = new Array();
var dataArray = new Array();
//var pageOrder = new Array();
var menustate = "startup";
var numrows
var requestCount;
var loadCheck = 0;

var dataRequestMax;
var dataRequestCount = 0;

var pwidth;
var pheight;

var curPage = 0;

var curSection = "about";
var fromSection;
var toSection;

var curWCategory = "consumer";
var fromWCategory;
var toWCategory;
var curWorkPage;

var isInputEnabled = true;
var workcategory = "consumer";
var goDirection;
var aboutPage = 0;
var workPage = 300;
var processPage = 3000;
var contactPage = 3000;

var consumerPage = 3000;
var computerPage = 3000;
var appliancesPage = 3000;
var furniturePage = 3000;
var medicalPage = 3000;
var packagingPage = 3000;
var housewaresPage = 3000;

var pageWScaled;
var pageHSpace = 10;
var edgeWidth = 100;
var moveX = 800 + pageHSpace;
var prevX = 800 - 100 + pageHSpace;
var nextX = 800 + 100 + pageHSpace;

var blankImageUrl = "img/_blank.png";


( function($) {	
	$(document).ready(function(){
		disableInput()
		preload();
		getNumrows();
	});
} ) ( jQuery );


function getNumrows()
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	var responseVar
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			responseVar = ajaxRequest.responseText;
			var str = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
			numrows = parseInt(responseVar.replace(str,'')) ;
			getDataAll()
		}
	}
	ajaxRequest.open("GET", "php/data_numrows.php", true);
	ajaxRequest.send(null);	
}


function getDataAll(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var responseVar
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			getDataEach()
		}
	}
	ajaxRequest.open("GET", "php/data_get_all.php", true);
	ajaxRequest.send(null);
}


function getDataEach()
{
	for (i=0; i<= numrows-1; i++)
	{
		dataArray[i] = new Object();
		dataArray[i].num = i;
	}
	
	dataRequestMax = 11; //the number of requests below
	
	getDataResult("id");
	getDataResult("client");
	getDataResult("title");
	getDataResult("imgtitle");
	getDataResult("textcontent");
	getDataResult("imgurl");
	getDataResult("pageorder");
	getDataResult("pagelayout");
	getDataResult("is_used");
	getDataResult("section");
	getDataResult("category");
}


function getDataResult(whichParam){
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	var responseVar
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			responseVar = ajaxRequest.responseText;
			var str = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
			var cleanstr = responseVar.replace(str,'');
			
			var tempArray = new Array();
			tempArray = cleanstr.split("%%");//create array from items delimiter by '%%'
			tempArray.splice (tempArray.length-1, 1);//remove last empty item
			
			for (i=0; i<= dataArray.length-1; i++)
			{
				dataArray[i][whichParam] = tempArray[i]
			}
			
			dataRequestCount ++;
			if(dataRequestCount >= dataRequestMax )
			{
				dataRequestCount = 0;
				initSetup();
			}
			
		}
	}
	var queryString = "?whichParam=" + whichParam;
	ajaxRequest.open("GET", "php/data_get_each.php" + queryString, true);
	ajaxRequest.send(null);
	
}



/////////
function initSetup()
{
	
	
	var tempArray = new Array();
	for (j=0; j< dataArray.length; j++)
	{
		if(dataArray[j].is_used == 1)
		{
			tempArray.push(dataArray[j])
		}
	}
	
	tempArray.sort(function(a, b){
	 	return a.pageorder-b.pageorder
	})
	pageArray = tempArray;
	
	//alert("INIT "+ tempArray.length)
	
	var _projdiv
	var _imgbtn
	var _imgdiv
	var _imgdivHolder
	var _img
	var _imgtitle;
	var _textbox;
	var _titlediv
	var _bodydiv
	
	for (i=0; i<= pageArray.length-1; i++)
	{
		_projdiv = document.createElement("div");
		
		if(pageArray[i].pagelayout == 7)
		{
			_projdiv.className = "boxstyle6"
		}else if(pageArray[i].pagelayout == 6)
		{
			_projdiv.className = "boxstyle6"
		}else if(pageArray[i].pagelayout == 5)
		{
			_projdiv.className = "boxstyle5"
		}else if(pageArray[i].pagelayout == 4)
		{
			_projdiv.className = "boxstyle4"
		}else if(pageArray[i].pagelayout == 3)
		{
			_projdiv.className = "boxstyle3"
		}else if(pageArray[i].pagelayout == 2)
		{
			_projdiv.className = "boxstyle2"
		}else{
			_projdiv.className = "boxstyle1"
		}
		
		_imgbtn = document.createElement("a");
		//_imgbtn.setAttribute("onClick", "onImageClick('" + i + "'); return false;");
		_imgbtn.href = "javascript:onImageClick('" + i + "');"//IE
		
		_imgbtn.className = "projbtn"
		_imgbtn.innerHTML = "&nbsp;"
		
		
		_imgdiv = document.createElement("div");
		_imgdiv.className = "projimgdiv"
		
		_img = document.createElement("img");
		_img.className = "projimg"
		_img.setAttribute('src', pageArray[i].imgurl);
		
		if(pageArray[i].imgtitle == "" || pageArray[i].imgtitle == " ")
		{
		}else{
			_imgtitle = pageArray[i].imgtitle
			_img.setAttribute('title', pageArray[i].imgtitle);
		}
		
		//document.getElementById('resulttext').innerHTML = document.getElementById('resulttext').innerHTML + " " + pageArray[i].imgtitle
		
		_imgdiv.appendChild(_img);
		pageArray[i].img = _img;
		
		
	
		_textbox = document.createElement("div");
		_textbox.className = "projtextbox"
		_textbox.style.display = "none"
		
		_titlediv = document.createElement("div");
		_titlediv.className = "projtitle"
		_titlediv.innerHTML = '<b>' + pageArray[i].client + '</b> ' + pageArray[i].title;
	
		//_rule = document.createElement("div");
		//_rule.className = "projrule"
		
		
		_bodydiv = document.createElement("div");
		_bodydiv.className = "projbody"
		_bodydiv.innerHTML = pageArray[i].textcontent;
		
		_projdiv.appendChild(_imgdiv);
		_projdiv.appendChild(_textbox);
		
		_textbox.appendChild(_titlediv);
		//_textbox.appendChild(_rule);	
		_textbox.appendChild(_bodydiv);
		
		
		_projdiv.appendChild(_imgbtn);
		
		document.getElementById('bbox').appendChild(_projdiv);
		//pageArray[i] = {img:_img, div:_imgdiv, src:'img/img'+i+'.png'}
		pageArray[i].projdiv = _projdiv;
		
		pageArray[i].imgdiv = _imgdiv;
		pageArray[i].imgbtn = _imgbtn;
		pageArray[i].textbox = _textbox;
		pageArray[i].titlediv = _titlediv;
		pageArray[i].imgtitle = _imgtitle;
		
		pageArray[i].bodydiv = _bodydiv;
		
		//Effect.Fade(pageArray[i].imgdiv, { duration: 0.0, from: 1.0, to:0.2 });
	}
	
	document.getElementById('bbox').style.width = (pageArray.length * (810) ) + 'px'
	document.getElementById('copyrightdiv').innerHTML = "&#169; 2011 Studio Backs"
	
	/*document.getElementById('mainmenu').onselectstart = function () { return false; } // ie
	document.getElementById('workmenu').onselectstart = function () { return false; } // ie
	document.getElementById('pagemenu').onselectstart = function () { return false; } // ie
	*/
	
	getSections()
	resizeWindow()
	getpageID()
	
}

//CHECK for URL variable string if it exists
function getpageID(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var responseVar
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			responseVar = ajaxRequest.responseText;
			var str = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
			var cleanstr = responseVar.replace(str,'');
			
			goPageById(cleanstr);
		}
	}
	ajaxRequest.open("GET", "php/getpageid.php", true);
	ajaxRequest.send(null);
}

//CALLED BY getpageID
function goPageById(thepage)
{
	if(isNaN(thepage) )
	{
		thepage = 0
	}
	
	thepage = parseInt(thepage-1);//subtract 1 to match page numbering
	
	if(thepage >= pageArray.length || thepage < 0)
	{
		thepage = 0
	}
	
	//Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });

	var moveAmount 
	moveAmount = -((thepage-curPage) * (pageWScaled + 0));
	var dur = 0.1
	curPage = thepage;
	new Effect.Move('bbox', { x: moveAmount, y:0, mode: 'relative', duration:dur, transition: Effect.Transitions.sinoidal, afterFinish: initialFadeIn });	
}


function initialFadeIn()
{
	
	//Effect.Appear('main', { duration: 1 });
	pageArray[curPage].textbox.style.display = "block";
	
	Effect.Appear('header', { duration: 1 });
	Effect.Appear('contentcontainer', { duration: 1 });
	Effect.Appear('mainmenu', { duration: 1 });
	Effect.Appear('copyrightdiv', { duration: 1 });
	
	
	pageArray[curPage].imgbtn.className = "projbtnInactive"//dsiable
	resetPageButtons();
	pageArray[curPage].pagebtn.className = "page_btn_on";
	updateMenu(); //appear work and page menu
	enableInput();
}

function getSections()
{
	// GET SECTIONS, set up variables
	for (i=0; i<= pageArray.length-1; i++)
	{
		
		if (pageArray[i].section == 1)
		{
			workPage = Math.min(i, workPage)
		} else if (pageArray[i].section == 2)
		{
			processPage = Math.min(i, processPage)
		}else if (pageArray[i].section == 3)
		{
			contactPage = Math.min(i, contactPage)
		}
		
		
		if (pageArray[i].category == 0)
		{
			consumerPage = Math.min(i, consumerPage)
			curWorkPage = consumerPage
	
		} else if (pageArray[i].category == 1)
		{
			computerPage = Math.min(i, computerPage)
		}else if (pageArray[i].category == 2)
		{
			appliancesPage = Math.min(i, appliancesPage)
		}else if (pageArray[i].category == 3)
		{
			furniturePage = Math.min(i, furniturePage)
		}else if (pageArray[i].category == 4)
		{
			medicalPage = Math.min(i, medicalPage)
		}else if (pageArray[i].category == 5)
		{
			packagingPage = Math.min(i, packagingPage)
		}else if (pageArray[i].category == 6)
		{
			housewaresPage = Math.min(i, housewaresPage)
		}
		
		
		var pagebtndiv = document.createElement("div");
		pagebtndiv.className = "page_btn_base";
		pageArray[i].pagebtn = pagebtndiv;
		
		var pagebtnL = document.createElement("div");
		var pagebtnM = document.createElement("div");
		var pagebtnR = document.createElement("div");
		pagebtnL.className = "btn_L";
		pagebtnM.className = "btn_M";
		pagebtnR.className = "btn_R";
		
		pagebtnM.innerHTML = i+1; //offset the page number by 1
		
		pagebtndiv.appendChild(pagebtnL);
		pagebtndiv.appendChild(pagebtnM);
		pagebtndiv.appendChild(pagebtnR);
		
		var pageanchor = document.createElement("a");
		pageanchor.className = "btn_link"; //class sets cursor to pointer
		pageanchor.appendChild(pagebtndiv);
		document.getElementById('pagemenu').appendChild(pageanchor);
		
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
		{ //test for MSIE x.x;
			//document.getElementById('resulttext').innerHTML = "appName " + navigator.appName + " - it's IE"
			pageanchor.href = "javascript:onPageBtnClick('" + i + "');"//IE
			//pageanchor.onclick = onPageBtnClick( i );//IE
			pageanchor.onmouseover = "javascript:pagebtnOver('" + i + "');"//IE
			pageanchor.onmousedown = "javascript:pagebtnDown('" + i + "');"//IE
			pageanchor.onmouseout = "javascript:pagebtnOut('" + i + "');"//IE
		}else{
			//document.getElementById('resulttext').innerHTML = "appName " + navigator.appName
			pageanchor.setAttribute("onClick", "onPageBtnClick('" + i + "');");
			//pageanchor.onclick = onPageBtnClick( i );//IE
			pageanchor.setAttribute("onMouseOver", "pagebtnOver('" + i + "'); return false;");
			pageanchor.setAttribute("onMouseDown", "pagebtnDown('" + i + "'); return false;");
			pageanchor.setAttribute("onMouseOut", "pagebtnOut('" + i + "'); return false;");
			//pageanchor.onclick = function() { onPageBtnClick(); };
		}
		
					
	}
}

function resizeWindow()
{
	var bodyFontSizeScaled;
	var menuFontSizeScaled;
	var leftMargin
	var headerHeight
	var projHeight;
	var logoW
	var logoH
	
	var pageW = getPageWidth();
	var pageH = getPageHeight();
	var pageAR = pageW/pageH
	
	var contentW = 1000;
	var contentH = 1080;// 995 actually
	var contentAR = contentW/contentH
	var scaleF;
	
	var multiplierH = pageH - contentH;
	
	
		
	if(pageW < contentW || pageH < contentH)
	{
		if(contentAR > pageAR)
		{
			scaleF = pageW / contentW;
		}else{
			if (multiplierH < 0)
			{
				scaleF = (pageH / contentH) * ( (4000 - Math.abs(multiplierH)) /4000  );
					
			}else{
				scaleF = pageH / contentH
			}
		}
		
	}else{
		
		scaleF = 1
	}
	
	pageWScaled = 800 * scaleF;
	leftMargin = 100 * scaleF
	headerHeight = 105 * scaleF
	projHeight = 795 * scaleF
	bodyFontSizeScaled = 12 * scaleF
	//menuFontSizeScaled = 10 * scaleF
	menuFontSizeScaled = 11 
	logoW = 161 * scaleF;
	logoH = 35 * scaleF;
	
	document.getElementById('bbox').style.left = leftMargin - (curPage * ((800 * scaleF) + 0)) + 'px'
	document.getElementById('header').style.height = (105 * scaleF) + 'px';
	document.getElementById('headercontent').style.left = leftMargin + 'px';
	document.getElementById('headercontent').style.marginTop = (35 * scaleF) + 'px';
	
	document.getElementById('headerlogo').style.width = logoW + 'px';
	document.getElementById('headerlogo').style.height = logoH + 'px';
	
	document.getElementById('menucontainer').style.top = projHeight + headerHeight + 'px'
	//document.getElementById('menucontainer').style.top = pageArray[0].projdiv.offsetHeight + headerHeight + 'px'
	document.getElementById('menucontainer').style.left = leftMargin + 'px';
	document.getElementById('menucontainer').style.fontSize = menuFontSizeScaled + "px"
	
	document.getElementById('copyrightdiv').style.left = leftMargin + 'px';
	
	for (i=0; i<= pageArray.length-1; i++)
	{
		//IMAGE
		pageArray[i].imgdiv.style.width = (800 * scaleF) + "px"
		pageArray[i].imgdiv.style.height = (600 * scaleF) + "px"
		pageArray[i].imgbtn.style.width = (800 * scaleF) + "px"
		pageArray[i].imgbtn.style.height = (600 * scaleF) + "px"
		
		//TITLE
		if(pageArray[i].pagelayout == 7)
		{
			pageArray[i].textbox.style.marginLeft = (420 * scaleF) + "px"
			pageArray[i].textbox.style.width = (350 * scaleF) + "px"
			pageArray[i].textbox.style.marginTop = (15 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (10 * scaleF) + "px"
			}
		}else if(pageArray[i].pagelayout == 6)
		{
			pageArray[i].textbox.style.marginLeft = (420 * scaleF) + "px"
			pageArray[i].textbox.style.width = (350 * scaleF) + "px"
			pageArray[i].textbox.style.top = (330 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (15 * scaleF) + "px"
			}
			
		}else if(pageArray[i].pagelayout == 5)
		{
			pageArray[i].textbox.style.marginLeft = (20 * scaleF) + "px"
			pageArray[i].textbox.style.width = (350 * scaleF) + "px"
			pageArray[i].textbox.style.top = (330 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (15 * scaleF) + "px"
			}
			pageArray[i].textbox.style.top = (330 * scaleF) + "px"
		}else if(pageArray[i].pagelayout == 4)
		{
			pageArray[i].textbox.style.marginLeft = (420 * scaleF) + "px"
			pageArray[i].textbox.style.width = (350 * scaleF) + "px"
			pageArray[i].textbox.style.top = (30 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (15 * scaleF) + "px"
			}
			
		}else if(pageArray[i].pagelayout == 3)
		{
			pageArray[i].textbox.style.marginLeft = (20 * scaleF) + "px"
			pageArray[i].textbox.style.width = (350 * scaleF) + "px"
			pageArray[i].textbox.style.top = (30 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (15 * scaleF) + "px"
			}
			
		}else if(pageArray[i].pagelayout == 2)
		{
			pageArray[i].textbox.style.marginLeft = (20 * scaleF) + "px"
			pageArray[i].textbox.style.width = (370 * scaleF) + "px"
			if(pageArray[i].client != "" && pageArray[i].client != " ")
			{
				pageArray[i].titlediv.style.marginBottom = (10 * scaleF) + "px"
			}
			
		}else{
			
		}
		
		/*
		pageArray[i].titlediv.style.fontSize = (18 * scaleF) + "px"
		pageArray[i].titlediv.style.lineHeight = (21 * scaleF) + "px"
		pageArray[i].bodydiv.style.fontSize = (12 * scaleF) + "px"
		pageArray[i].bodydiv.style.lineHeight = (15 * scaleF) + "px"
		*/
		
		
		var textScaleMin = 0.7;
		
		if (scaleF > textScaleMin) 
		{
			//pageArray[i].titlediv.style.height = (30 * scaleF) + "px"
			pageArray[i].titlediv.style.fontSize = (18 * scaleF) + "px"
			pageArray[i].titlediv.style.lineHeight = (21 * scaleF) + "px"
			pageArray[i].textbox.style.marginTop = (15 * scaleF) + "px"
			
			pageArray[i].bodydiv.style.fontSize = (12 * scaleF) + "px"
			pageArray[i].bodydiv.style.lineHeight = (15 * scaleF) + "px"
		}else{
			//pageArray[i].titlediv.style.height = (30 * textScaleMin) + "px"
			pageArray[i].titlediv.style.fontSize = (18 * textScaleMin) + "px"
			pageArray[i].titlediv.style.lineHeight = (21 * textScaleMin) + "px"
			pageArray[i].textbox.style.marginTop = (15 * textScaleMin) + "px"
			pageArray[i].bodydiv.style.fontSize = (12 * textScaleMin ) + "px"
			pageArray[i].bodydiv.style.lineHeight = (15 * textScaleMin) + "px"
		}
		
		
		//BODY
		/*
		if(pageArray[i].pagelayout == 1){
			pageArray[i].bodydiv.style.width = (400 * scaleF) + "px"
			pageArray[i].bodydiv.style.marginLeft = (10 * scaleF) + "px"
			pageArray[i].bodydiv.style.marginTop = (15 * scaleF) + "px"
		}
		*/
		
		//PROJECT DIV
		pageArray[i].projdiv.style.width = (800 * scaleF) + "px"	
		pageArray[i].projdiv.style.height = (795 * scaleF) + "px"
		
		//document.getElementById('resulttext').innerHTML = "W " + pageW + " H " + pageH + " MULT " + multiplierH;	

	}
		
	
	
	
	//if (scaleF > 0.7125) 
	//if (scaleF > 0.75) 
	var scaleMin = 0.8;
	
	if (scaleF > scaleMin) 
	{
		document.getElementById('mainmenu').style.width = (790 * scaleF) + 'px'
		//document.getElementById('mainmenu').style.height = (44 * scaleF) + 'px'
		document.getElementById('workmenu').style.width = (790 * scaleF) + 'px'
		//document.getElementById('workmenu').style.height = (44 * scaleF) + 'px'
		document.getElementById('pagemenu').style.width = (790 * scaleF) + 'px'
		//document.getElementById('pagemenu').style.height = (44 * scaleF) + 'px'
		
		document.getElementById('copyrightdiv').style.width = (790 * scaleF) + 'px'
		document.getElementById('resulttext').style.fontSize = bodyFontSizeScaled + "px"
	}else{
		document.getElementById('mainmenu').style.width = (790 * scaleMin) + 'px';
		//document.getElementById('mainmenu').style.height = (44 * 0.75) + 'px'
		document.getElementById('workmenu').style.width = (790 * scaleMin) + 'px';
		//document.getElementById('workmenu').style.height = (44 * 0.75) + 'px'
		document.getElementById('pagemenu').style.width = (790 * scaleMin) + 'px';
		//document.getElementById('pagemenu').style.height = (44 * 0.75) + 'px'
		
		document.getElementById('copyrightdiv').style.width = (790 * scaleMin) + 'px'
		document.getElementById('resulttext').style.fontSize = 10 + "px"
	}
		
	
}


function onImageClick(which)
{
	if(parseInt(which) != curPage)
	{
		if(parseInt(which) > curPage)
		{
			goNext();
			return;
		}else{
			goPrev();
			return;
		}
	}else{
		
	}
}

//PAGE BUTTONS
function pagebtnOver(page)
{
	if(curPage != page){
		
		pageArray[page].pagebtn.className = "page_btn_over";
	}
}

function pagebtnOut(page)
{
	if(curPage != page){
		pageArray[page].pagebtn.className = "page_btn_base";
	}
}


function pagebtnDown(page)
{
	if(curPage != page){
		pageArray[page].pagebtn.className = "page_btn_over";
	}
}

function onPageBtnClick(which)
{
	if(curPage != which)
	{
		resetPageButtons();
		pageArray[which].pagebtn.className = "page_btn_on";
		goPage(which);
	}
}



function goPage(thepage)
{
	if(isNaN(thepage) )
	{
		thepage = 0
	}
	thepage = parseInt(thepage);
	if(thepage >= pageArray.length || thepage < 0)
	{
		thepage = 0
	}
	if(isInputEnabled){
		disableInput();
	
		Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });
		pageArray[curPage].imgbtn.style.display = "none"
		pageArray[curPage].imgbtn.className = "projbtn" //reactivate
		//for transp white overlay
		Effect.Appear(pageArray[curPage].imgbtn, { duration: 0.5 });

		var moveAmount 
		moveAmount = -((thepage-curPage) * (pageWScaled + 0));
		var dur = 0.5
		/*if(Math.abs(moveAmount) > 2500)
		{
			dur = 2.0
		}else{
			dur = 0.5
		}*/
		curPage = thepage;
		new Effect.Move('bbox', { x: moveAmount, y:0, mode: 'relative', duration:dur, transition: Effect.Transitions.sinoidal, afterFinish: goPageComplete });	
	
		Effect.Fade(pageArray[curPage].imgbtn, { duration: 0.5 });
	}
}

function goPageComplete()
{
	Effect.Appear(pageArray[curPage].textbox, { duration: 0.5 });
	pageArray[curPage].imgbtn.className = "projbtnInactive"//dsiable
	resetPageButtons();
	pageArray[curPage].pagebtn.className = "page_btn_on";
	updateMenu()
	enableInput();
}


function goPrev()
{
	if(isInputEnabled){
		if(curPage > 0)
		{
			disableInput();
			
			Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });
			pageArray[curPage].imgbtn.style.display = "none"
			pageArray[curPage].imgbtn.className = "projbtn" //reactivate
			//for transp white overlay
			Effect.Appear(pageArray[curPage].imgbtn, { duration: 0.5 });
		
			goDirection = "prev";
			var moveAmount 
			moveAmount = pageWScaled;
			curPage --;
			new Effect.Move('bbox', { x: moveAmount, mode: 'relative', duration:0.5, transition: Effect.Transitions.sinoidal, afterFinish: goNextPrevComplete });
			
			goNextPrevFadeDown();
			
			//for transp white overlay
			Effect.Fade(pageArray[curPage].imgbtn, { duration: 0.5 });
			
		}
	}
}


function goNext()
{
	if(isInputEnabled){
		if(curPage < pageArray.length-1)
		{
			disableInput();
			
			Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });
			pageArray[curPage].imgbtn.style.display = "none"
			pageArray[curPage].imgbtn.className = "projbtn" //reactivate
			//for transp white overlay
			Effect.Appear(pageArray[curPage].imgbtn, { duration: 0.5 });
			
			goDirection = "next";
			var moveAmount 
			moveAmount = pageWScaled;
			curPage ++;
			new Effect.Move('bbox', { x: -moveAmount, mode: 'relative', duration:0.5, transition: Effect.Transitions.sinoidal, afterFinish: goNextPrevComplete });
			
			
			goNextPrevFadeDown();
			//checkforsectionchange();
			//for transp white overlay
			Effect.Fade(pageArray[curPage].imgbtn, { duration: 0.5 });
			
		}
	}
}

function goNextPrevFadeDown()
{
	var startPages = [workPage, processPage, contactPage, consumerPage, computerPage, appliancesPage, furniturePage, medicalPage, packagingPage, housewaresPage ]
	var endPages = [workPage-1, processPage-1, contactPage-1, consumerPage-1, computerPage-1, appliancesPage-1, furniturePage-1, medicalPage-1, packagingPage-1, housewaresPage-1]
	
	if(goDirection == "next")
	{
		for (i=0; i< startPages.length; i++)
		{
			if(curPage == startPages[i])
			{
				Effect.Fade("pagemenu", { duration: 0.4});
				if(curPage == processPage)
				{
					Effect.Fade("workmenu", { duration: 0.4});
				}
			}
		}
	}else if(goDirection == "prev")
	{
		for (j=0; j< endPages.length; j++)
		{
			if(curPage == endPages[j])
			{
				Effect.Fade("pagemenu", { duration: 0.4});
				if(curPage == workPage-1)
				{
					Effect.Fade("workmenu", { duration: 0.4});
				}
			}
		}
	}
}

function goNextPrevComplete()
{
	Effect.Appear(pageArray[curPage].textbox, { duration: 0.5 });
	pageArray[curPage].imgbtn.className = "projbtnInactive"//dsiable
	resetPageButtons();
	pageArray[curPage].pagebtn.className = "page_btn_on";
	updateMenu(); //appear work and page menu
	enableInput();
}

//Used by goNext/goPrev
function updateMenu()
{
	
	fromSection = curSection;
	fromWCategory = curWCategory;
	
	if(curPage >= aboutPage && curPage <= workPage-1)
	{
		curSection = "about"
		resetMenuButtons()
		document.getElementById("btn_about").className = "btn_on"
	}else if(curPage >= workPage && curPage <= processPage-1)
	{
		curSection = "work"
		resetMenuButtons();
		document.getElementById("btn_work").className = "btn_on"
	}else if(curPage >= processPage && curPage <= contactPage-1)
	{
		curSection = "process"
		resetMenuButtons()
		document.getElementById("btn_process").className = "btn_on"
	}else if(curPage >= contactPage)
	{
		curSection = "contact"
		resetMenuButtons()
		document.getElementById("btn_contact").className = "btn_on"
	}

	if(curSection == "work")
	{
		curWorkPage = curPage;
	}
	
	if(curWorkPage >= consumerPage && curWorkPage <= computerPage-1)
	{
		curWCategory = "consumer"
		resetWorkCategoryButtons()
		document.getElementById("btn_consumer").className = "btn_on"
	}else if(curWorkPage >= computerPage && curWorkPage <= appliancesPage-1)
	{
		curWCategory = "computer"
		resetWorkCategoryButtons()
		document.getElementById("btn_computer").className = "btn_on"
	}else if(curWorkPage >= appliancesPage && curWorkPage <= furniturePage-1)
	{
		curWCategory = "appliances"
		resetWorkCategoryButtons()
		document.getElementById("btn_appliances").className = "btn_on"
	}else if(curWorkPage >= furniturePage && curWorkPage <= medicalPage-1)
	{
		curWCategory = "furniture"
		resetWorkCategoryButtons()
		document.getElementById("btn_furniture").className = "btn_on"
	
	}else if(curWorkPage >= medicalPage && curWorkPage <= packagingPage-1)
	{
		curWCategory = "medical"
		resetWorkCategoryButtons()
		document.getElementById("btn_medical").className = "btn_on"
	}else if(curWorkPage >= packagingPage && curWorkPage <= housewaresPage-1)
	{
		curWCategory = "packaging"
		resetWorkCategoryButtons()
		document.getElementById("btn_packaging").className = "btn_on"
	}else if(curWorkPage >= housewaresPage)
	{
		curWCategory = "housewares"
		resetWorkCategoryButtons()
		document.getElementById("btn_housewares").className = "btn_on"
		
	}
	
	toSection = curSection;
	toWCategory = curWCategory;
	
	if(fromSection != toSection)
	{
		if(curSection == "work")
		{
			Effect.Appear("workmenu", { duration: 0.5 });
		}
		//updatePageButtons(); //Appear page menu
	}else{
		if(fromWCategory != toWCategory)
		{
			//updatePageButtons(); //Appear page menu
		}
	}
	
	updatePageButtons(); //Appear page menu
}
	


function goSection(which)
{
	if(isInputEnabled){
		if(curSection != which){
			disableInput()
			
			fromSection = curSection
			curSection = which
			toSection = curSection
			
			goSectionFadeDown();// Fade page/work menu
			resetMenuButtons();
			
			document.getElementById('btn_'+which).className = "btn_on";
			
			var goto;
			
			if(curSection == "about")
			{
				goto = aboutPage;
			}else if(curSection == "work")
			{
				goto = curWorkPage;
			}else if(curSection == "process")
			{
				goto = processPage;
			}else if(curSection == "contact")
			{
				goto = contactPage;
			}
			
			Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });
			pageArray[curPage].imgbtn.className = "projbtn" //reactivate
			//for transp white overlay
			Effect.Appear(pageArray[curPage].imgbtn, { duration: 0.5 });
			
			var moveAmount 
			moveAmount = -((goto-curPage) * (pageWScaled + 0));
			var dur = 0.5
			
			curPage = goto;
			new Effect.Move('bbox', { x: moveAmount, y:0, mode: 'relative', duration:dur, transition: Effect.Transitions.sinoidal, afterFinish: goSectionComplete });	
		}
	}
}


function goSectionComplete()
{
	goSectionFadeUp()
	
	Effect.Appear(pageArray[curPage].textbox, { duration: 0.5 });
	//for transp white overlay
	Effect.Fade(pageArray[curPage].imgbtn, { duration: 0.5 });
	enableInput();
	//document.getElementById('resulttext').innerHTML = "PAGE " + (curPage+1) + " OF " + numrows;	
}

function goSectionFadeDown()
{
	if(fromSection == "work")
	{
		if(toSection != "work")
		{
			//fade page and work
			Effect.Fade("pagemenu", { duration: 0.5});
			Effect.Fade("workmenu", { duration: 0.5 });
		}
	}else if(toSection == "work")
	{
		if(fromSection != "work")
		{
			Effect.Fade("pagemenu", { duration: 0.5});
		}
	}else{
		Effect.Fade("pagemenu", { duration: 0.5});
	}
}

function goSectionFadeUp()
{
	if(curSection == "work")
	{
		Effect.Appear("workmenu", { duration: 0.5 });
		updatePageButtons(); //Appear page menu
	}else
	{
		updatePageButtons(); //Appear page menu
	}
}


function goWorkCategory(which)
{
	if(isInputEnabled){
		//if(curWCategory != which){
		disableInput()
		
		fromWCategory = curWCategory
		curWCategory = which
		toWCategory = curWCategory
		
		resetWorkCategoryButtons();
		document.getElementById('btn_'+which).className = "btn_on";
		
		var goto;
	
		if(curWCategory == "consumer")
		{
			goto = consumerPage;
		}else if(curWCategory == "computer")
		{
			goto = computerPage;
		}else if(curWCategory == "appliances")
		{
			goto = appliancesPage;
		}else if(curWCategory == "furniture")
		{
			goto = furniturePage;
		}else if(curWCategory == "medical")
		{
			goto = medicalPage;
		}else if(curWCategory == "packaging")
		{
			goto = packagingPage;
		}else if(curWCategory == "housewares")
		{
			goto = housewaresPage;
		}
		
		Effect.Fade("pagemenu", { duration: 0.5 });
		
		Effect.Fade(pageArray[curPage].textbox, { duration: 0.5 });
		pageArray[curPage].imgbtn.className = "projbtn" //reactivate
		//for transp white overlay
		Effect.Appear(pageArray[curPage].imgbtn, { duration: 0.5 });
		
		var moveAmount 
		moveAmount = -((goto-curPage) * (pageWScaled + 0));
		//var dur = (Math.abs(moveAmount) * 0.0001)
		var dur = 0.5;
		
		curPage = goto;
		curWorkPage = curPage;
		new Effect.Move('bbox', { x: moveAmount, y:0, mode: 'relative', duration:dur, transition: Effect.Transitions.sinoidal, afterFinish: goWorkCategoryComplete });
	//}
	}
}

function goWorkCategoryComplete()
{
	updatePageButtons();
	Effect.Appear(pageArray[curPage].textbox, { duration: 0.5 });
	//for transp white overlay
	Effect.Fade(pageArray[curPage].imgbtn, { duration: 0.5 });
	enableInput();		
}





function updatePageButtons()
{
	var startPage;
	var endPage;
	
	if(curSection == "about")
	{
		startPage = aboutPage;
		endPage = workPage - 1;
	}else if(curSection == "work")
	{
		
		if(curWCategory == "consumer")
		{
			startPage = consumerPage;
			endPage = computerPage - 1;
		}else if(curWCategory == "computer")
		{
			startPage = computerPage;
			endPage = appliancesPage - 1;
		}else if(curWCategory == "appliances")
		{
			startPage = appliancesPage;
			endPage = furniturePage - 1;
		}else if(curWCategory == "furniture")
		{
			startPage = furniturePage;
			endPage = medicalPage - 1;
		}else if(curWCategory == "medical")
		{
			startPage = medicalPage;
			endPage = packagingPage - 1;
		}else if(curWCategory == "packaging")
		{
			startPage = packagingPage;
			endPage = housewaresPage - 1;
		}else if(curWCategory == "housewares")
		{
			startPage = housewaresPage;
			endPage = processPage - 1;
		}
		
	}else if(curSection == "process")
	{
		startPage = processPage;
		endPage = contactPage - 1;
	}else if(curSection == "contact")
	{
		startPage = contactPage;
		endPage = pageArray.length;
	}
			
	for (i=0; i< pageArray.length; i++)
	{
		if(i >= startPage && i<= endPage)
		{
			pageArray[i].pagebtn.style.display = "block";
		}else{
			pageArray[i].pagebtn.style.display = "none";
		}
		
	}
	resetPageButtons()
	pageArray[curPage].pagebtn.className = "page_btn_on";
	
	Effect.Appear("pagemenu", { duration: 0.5 });
}



function resetMenuButtons()
{
	document.getElementById("btn_about").className = "btn_base"
	document.getElementById("btn_work").className = "btn_base"
	document.getElementById("btn_process").className = "btn_base"
	document.getElementById("btn_contact").className = "btn_base"
}

function resetWorkCategoryButtons()
{
	document.getElementById("btn_consumer").className = "btn_base"
	document.getElementById("btn_computer").className = "btn_base"
	document.getElementById("btn_appliances").className = "btn_base"
	document.getElementById("btn_furniture").className = "btn_base"
	document.getElementById("btn_medical").className = "btn_base"
	document.getElementById("btn_packaging").className = "btn_base"
	document.getElementById("btn_housewares").className = "btn_base"
}


function resetPageButtons()
{
	for (i=0; i< pageArray.length; i++)
	{
		pageArray[i].pagebtn.className = "page_btn_base";
	}
}


function enableInput()
{
	isInputEnabled = true;
}

//disable input during transitions
function disableInput()
{
	isInputEnabled = false;
}



function getPageWidth() 
{
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function getPageHeight() 
{
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 

function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 

function posRight() {
	return posLeft()+getPageWidth();} function posBottom() {return posTop()+getPageHeight();
}
                    


function prevcontrolRollover()
{
	
	document.getElementById('prevcontrol').className = "prevarrow_over"
}

function prevcontrolRollout()
{
	document.getElementById('prevcontrol').className = "prevarrow"
}

function prevboxClick()
{
	goPrev();
}



function curboxClick()
{
	
}


function nextcontrolRollover()
{
	
	document.getElementById('nextcontrol').className = "nextarrow_over"
}

function nextcontrolRollout()
{
	document.getElementById('nextcontrol').className = "nextarrow"
}

function nextboxClick()
{
	goNext();
}







//MENU BUTTONS
function menubtnOver(btn, section)
{
	if(curSection != section){
		document.getElementById(btn).className = "btn_over" ;
	}
}

function menubtnOut(btn, section)
{
	if(curSection != section){
		document.getElementById(btn).className = "btn_base"
	}
}


function menubtnDown(btn, section)
{
	if(curSection != section){
		document.getElementById(btn).className = "btn_over"
	}
}



//SUB MENU BUTTONS
function subMenubtnOver(btn, category)
{
	if(curWCategory != category){
		document.getElementById(btn).className = "btn_over" ;
	}
}

function subMenubtnOut(btn, category)
{
	if(curWCategory != category){
		document.getElementById(btn).className = "btn_base"
	}
}


function subMenubtnDown(btn, category)
{
	if(curWCategory != category){
		document.getElementById(btn).className = "btn_over"
	}
}




//ARROW BUTTONS
function arrowbtnOver(btn, dir)
{
	if(dir == "next")
	{
		document.getElementById(btn).className = "btn_next_over";
	}else{
		document.getElementById(btn).className = "btn_prev_over";
	}
	
}

function arrowbtnOut(btn, dir)
{
	if(dir == "next")
	{
		document.getElementById(btn).className = "btn_next";
	}else{
		document.getElementById(btn).className = "btn_prev";
	}
}


function arrowbtnDown(btn, dir)
{
	
}



//KEYSTROKES
function keystrokeHilitePrevBtn()
{
	//document.getElementById('btn_arrL').className = "arrLbtn_over"
}

function keystrokeHiliteNextBtn()
{
	//document.getElementById('btn_arrR').className = "arrRbtn_over"
}

function keystrokeUnHilitePrevBtn()
{
	//document.getElementById('btn_arrL').className = "arrLbtn"
	
}

function keystrokeUnHiliteNextBtn()
{
	//document.getElementById('btn_arrR').className = "arrRbtn"
}


