var initPosition = 0;
var intStep = 20;
var imgIndex = 1;
var listPos = 0;
var wScreen = screen.width;
var bookWidth = 568;
var displayFrame = 6;
var currentOn = 'mainContent1';
if (typeof(numGalleries) != 'undefined' ) {
	var hiddenGalleries = numGalleries - displayFrame;
}
var step = 57;
var numClicks = 0;
var numClicksTxt = 0;
var mainContentBlockHeight = 308;
var curElementHeight;
var stepTxt = 150;

function slideOpen (sId, finalHeight) {
	var slidingElement = document.getElementById(sId);
	slidingElement.style.height = '1px';
	var actualHeight = 2;
	show (sId);
	var slideInt = setInterval(function () {
		while (actualHeight < finalHeight ) {
			slidingElement.style.height = actualHeight + 'px';
			actualHeight++;
		}
		clearInterval(slideInt);
	}, 500);
}

function slideClose (sId) {
	var slidingElement = document.getElementById(sId);
	var actualHeight = slidingElement.offsetHeight;
	var slideCloseInt = setInterval(function () {
		while (actualHeight >= 1 ) {
			slidingElement.style.height = actualHeight + 'px';
			actualHeight--;
		}
		hide (sId);
		clearInterval(slideCloseInt);
	}, 500);
}

function detectIe6 () {
	if (navigator.appVersion.indexOf('MSIE 6.0') > -1) {
		var ie6Text = '<p><strong>Your Internet is not up to date</strong>.  This is not the last version of Microsoft Internet Explorer</p>\n<p>You can enjoy your web surfing much more bu upgrading your browser, and it is even free!</p>\n<p><a href="http://www.mozilla-europe.org/en/products/firefox/" target="_blank">Install <strong>Firefox</strong></a> | <a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx" target="_blank">Upgrade your Microsoft Internet Explorer</a> | <a href="http://www.uweb.ucsb.edu/~mguidry/firefox/" target="_blank"><strong>what\'s best for me?</strong></a></p>\n<p class="button" onclick ="slideClose(\'ie6Detector\');">Later. Now, please go away.</p>'
		if(document.getElementById('ie6Detector')) {
			document.getElementById('ie6Detector').innerHTML = ie6Text;
			slideOpen('ie6Detector', 65);
		}
		var images = document.getElementsByTagName('img');
		for (var i=0;i < images.length;i++) {
			images[i].src = images[i].src.replace(/png/i,'gif');
		}
	}
}

function displaySwf(theElement,param,swfName) {
	var portfolio = new swfObject(990,347,swfName,'swfContainer',param);
	$(theElement).innerHTML += '<p onclick="hideSwf(\'swfContainer\')" id="close">close</p>';
	$(theElement).style.display = 'block';
	$(theElement).effect('opacity').start(0,1);
	$(theElement).effect('opacity').stop();
}

function changeImage(i) {
	i *= 1;
	$('mainImage').src = 'Images/loading.gif';
	setTimeout(function(){
		$('mainImage').src = 'Images/Dir' + i + '/' + pixArray[i - 1];
	},100);
	$('mainImage').onclick = function() {
		displaySwf('swfContainer',i,'portfolioSmall');
	};
}

function hideSwf(theElement) {
	$(theElement).effect('opacity').start(1,0);
	$(theElement).effect('opacity').stop();
	setTimeout(function() {
		$(theElement).style.display = 'none'
	}, 1000)
}

function putRandomPix() {
		var currentPic = $('currentPhoto');
		var randomPic = Math.round(10*Math.random());
		if (randomPic == 0) {
			randomPic = 4;
		}
		if (randomPic == 10) {
			randomPic = 3;
		}
		currentPic.src = "Images/" + 'photArr' + randomPic + '.jpg';
}
function hide(sId) {
	$(sId).style.display = 'none';
}

function show(sId) {
	currentOn = sId;
	numClick = 0;
	initPosition = 0;
	for (var i = 1;i < 4;i++) {
		hide('mainContent' + i);
		$('mainContent' + i).style.top = '0';
	}
	$(sId).style.display = 'block';
	$('contentUp').style.display = 'none';
	var scrollersInt = setInterval(function() {
		if (onProgress) {
			return;
		}
		else {
			clearInterval(scrollersInt);
			handleScrollers();
		}
	});

}

function handleScrollers() {
	curElementHeight = $(currentOn).offsetHeight;
	/* If the element height is less then it's container's height - hide the scroller */
	if (curElementHeight <= mainContentBlockHeight) {
		$('contentUp').style.display = 'none';
		$('contentDown').style.display = 'none';
	}
	else {
		$('contentDown').style.display = 'block';
	}
}

var noButton = function (element) {
	if (document.getElementById(element).disabled == 'disabled') {
		return true;
	}
	else {
		return false;
	}
}


var onProgress = false;
var initPosition = 0;
var intStep = 20;

function goFwd(movingBox, fwd, bkwd) {
	var movingBox = document.getElementById(movingBox);
	var fwdButton = document.getElementById(fwd);
	var bkButton = document.getElementById(bkwd);
	var tailHeight = movingBox.offsetHeight - movingBox.parentNode.offsetHeight;
	if (bkButton.style.display != 'block') {
		bkButton.style.display = 'block';
	}
	stopAt = -tailHeight;
	var intCounter = 0;
	var slide = setInterval(function() {
		onProgress = true;
		if (intCounter <= step) {
			initPosition -= 5;
			intCounter += 5;
			movingBox.style.top = initPosition + 'px';
		}
		else {
			if (initPosition <= stopAt) {
				fwdButton.style.display = 'none';
				clearInterval(slide);
				onProgress = false;
			}
		}
	},50);
}


function goBack(movingBox, fwd, bkwd) {
	var movingBox = document.getElementById(movingBox);
	var fwdButton = document.getElementById(fwd);
	var bkButton = document.getElementById(bkwd);
	if (fwdButton.style.display != 'block') {
		fwdButton.style.display = 'block';
	}
	var intCounter = 0;
	var slide = setInterval(function() {
		onProgress = true;
		if (intCounter <= step) {
			initPosition += 5;
			intCounter += 5;
			movingBox.style.top = initPosition + 'px';
		}
		else {
			if (initPosition >= 0) {
				movingBox.style.top = 0
				bkButton.style.display = 'none';
				clearInterval(slide);
				onProgress = false;
			}
		}
	},50);
}

function galleryFwd() {
	if($('down').src.indexOf('NA') > -1) {
		return false;
	}
	if($('up').style.cursor != 'pointer') {
		$('up').src = 'Images/arrowUp.gif'
		$('up').style.cursor = 'pointer';
	}
	var tailHeight = $('galleryList').offsetHeight - $('galleryList').parentNode.offsetHeight;
	var step = 57;
	var counter = 0;
	var posInt = setInterval(function() {
		listPos -= 10;
		counter += 10;
		if (counter >= step) {
			clearInterval(posInt);
			if (listPos <= tailHeight * -1) {
				listPos = tailHeight * -1;
				document.getElementById('galleryList').style.top = listPos + 'px';
				document.getElementById('down').src = 'Images/arrowDownNA.gif';
				document.getElementById('down').style.cursor = 'default';
			}
		}
		document.getElementById('galleryList').style.top = listPos + 'px';
	},10);
}

function galleryBk() {
	if($('up').src.indexOf('NA') > -1) {
		return false;
	}
	if($('down').src.indexOf('NA') > -1) {
		$('down').src = 'Images/arrowDown.gif'
		$('down').style.cursor = 'pointer';
	}
	var tailHeight = $('galleryList').offsetHeight - $('galleryList').parentNode.offsetHeight;
	var step = 57;
	var counter = 0;
	var posInt = setInterval(function() {
		listPos += 10;
		counter += 10;
		if (counter >= step) {
			clearInterval(posInt);
			if (listPos >= 0) {
				listPos = 0;
				document.getElementById('galleryList').style.top = '0';
				document.getElementById('up').src = 'Images/arrowUpNA.gif';
				document.getElementById('up').style.cursor = 'default';
			}
		}
		document.getElementById('galleryList').style.top = listPos + 'px';
	},10);
}


function validate(){
	var myForm = new FormValidator(0);
	var errBuffer = new StringBuffer();
	if (!myForm.validateMail('email')) {
		errBuffer.append('Please retype your email address\n')
	}
	if (!myForm.validatePhone('phone')) {
		errBuffer.append('Please retype your phone number\n')
	}
	if (errBuffer != '') {
		alert(errBuffer.toString());
		return false;
	}
	else {
		return true;
	}
}

function init() {
	if($('hp')) {
		var hpSwf = new swfObject(568,426,'hs','swfContainer');
	}
	if($('currentPhoto')) {
		putRandomPix();
	}
	if($('down')) {
		$('down').onclick = galleryFwd;
	}
	if($('up')) {
		$('up').onclick = galleryBk;
	}

	if($('sideMenu')) {
		$('contentUp').onclick = function () {
			goBack(currentOn, 'contentDown', 'contentUp');

		};
		$('contentDown').onclick = function () {
			goFwd(currentOn, 'contentDown', 'contentUp');
		};
	}
	if($(currentOn)) {
		handleScrollers();
	}
	detectIe6();
}


window.addEvent('domready', init)