//viewport
function setViewPort() {

	var viewPortWidth;
	var viewPortHeight;
	 
	 if (typeof window.innerWidth != 'undefined') { //mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		  viewPortWidth = window.innerWidth,
		  viewPortHeight = window.innerHeight
	 }	
	 else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)	 { // IE6 in standards compliant mode
		   viewPortWidth = document.documentElement.clientWidth,
		   viewPortHeight = document.documentElement.clientHeight
	 }
	 else {// older versions of IE
		   viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
	 }
	
	document.getElementById('grid').style.left = (((viewPortWidth-1040)/2) + 'px');

}

//Pop up addThis window
function popWindow() {
window.open('http://www.addthis.com/bookmark.php','addThis','width=608,height=456,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=yes');
}

//Check contact form
function checkEmail(string) {
return (string.indexOf(".") > 2) && (string.indexOf("@") > 0);
}

function validateForm() {
var errorMessage = "";

	for (i = 0; i<document.contactform.elements.length; i++) {
	
		if (document.contactform.elements[i].name == "email") {
		
			if (!checkEmail(document.contactform.elements[i].value)) {
			errorMessage = ('The email address is not properly formatted.');
			break;
			}
		
		}
		else if (!document.contactform.elements[i].value && (document.contactform.elements[i].className == 'requiredfield')){
		errorMessage = ('Please enter a ' + document.contactform.elements[i].name + '.');	
		break;
		}
		
	}
	
	if (errorMessage) {
	alert(errorMessage);
	return false;
	}
	else {
	return true;
	}

}


//Site nav
var prevClick = "00";

function checkClick(paneTarget,thisTop,thisLeft) {
	
	var thisClick = String(thisTop) + String(thisLeft);
	
	//if (thisClick != prevClick) {
		prevClick = thisClick;
		paneTarget.stop().scrollTo( {top: thisTop, left: thisLeft}, 1000 );
		return true;
	//}
	//else {
	//	return false;
	//}
	
}

$(document).ready(function() {

	var $paneTarget = $('#pane');	
	
	$('a.home').click(function(){
		$(this).blur();
		checkClick($paneTarget,0,0);
		return false;
	});
	
	$('a.link2').click(function(){
		$(this).blur();
		checkClick($paneTarget,0,1600);
		return false;
	});
	
	$('a.link3').click(function(){
		$(this).blur();
		checkClick($paneTarget,0,3200);
		return false;
	});
	
	$('a.back').click(function(){
		$(this).blur();
		checkClick($paneTarget,0,0);
		return false;
	});
	
	$.scrollTo.defaults.axis = 'xy'; 			
	//reset scroll on refresh
	$('#pane').scrollTo( {top:0, left:0} );//reset all scrollable panes to (0,0)
	$.scrollTo( {top:0, left:0} );//reset the screen to (0,0)
	
	
/* PRELOAD IMAGES */
	$(window).bind("load", function() {
	$(".headerright img").each(function(key, elm) {
	$().attr( "src", $(this).attr("src").replace(".png", "_on.png"));
	});
	});
	
	/*MENU GUI*/
	$(".button, .menu").hover(function() {
		if ($(this).attr("src").indexOf("_on") == -1) {
		var newSrc = $(this).attr("src").replace(".png","_on.png");
		$(this).attr("src",newSrc);
		}
	},
	function() {
		if($(this).attr("src").indexOf("_on.png") != -1) {
		var oldSrc = $(this).attr("src").replace("_on.png",".png");
		$(this).attr("src",oldSrc);
		}
	});
	
	$(".button, .menu").click(function() {
	$(this).blur();	
	});
	
	/*SWITCH GUI*/
	$(".switch").hover(function() {
	$(this).addClass("switchon");					
	},
	function() {
	$(this).removeClass("switchon");
	});
	
	
	/*REQUIRED FORM FIELDS*/
	$().find(".requiredfield, .requiredfield2").each(function(i){
		$(this).parent().prev().prepend("<span class=\"highlight\">*</span> ");
	});
	
		
	/*SLIDESHOW GUI*/
	$('#slideshow').slideshow();
	
});

/*SLIDESHOW FUNCTION*/
jQuery.fn.slideshow = function(options) {
	var settings = {
		timeout: '4000',
		type: 'sequence',
		pauselink: null,
		playcallback: null,
		pausecallback: null
	}
	if(options)
		jQuery.extend(settings, options);
	
	var pauseState = 0;
	var current = 1;
	var last = 0;
	var timer = '';
	
	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			jQuery(slides[last]).css('display', 'block').css('zIndex', '0').fadeOut('slow');
			jQuery(slides[current]).css('zIndex', '1').fadeIn('slow');
			
			if ( settings.type == 'sequence' ) {
				if ( ( current + 1 ) < slides.length ) {
					current = current + 1;
					last = current - 1;
				}
				else {
					current = 0;
					last = slides.length - 1;
				}
			}
			else if ( settings.type == 'random' ) {
				last = current;
				while (	current == last ) {
					current = Math.floor ( Math.random ( ) * ( slides.length ) );
				}
			}
			else {
				alert('type must either be \'sequence\' or \'random\'');
			}
			timer = setTimeout(change, settings.timeout);
		}
	}
	
	var pause = function() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
			if ( settings.playcallback != null ) {
				settings.pausecallback(jQuery('#' + settings.pauselink));
			}
		}
		else {
			pauseState = 0;
			change();
			if ( settings.playcallback != null ) {
				settings.playcallback(jQuery('#' + settings.pauselink));
			}
		}
		return false;
	}
	
	this.css('position', 'relative');
	var slides = this.find('.slide').get();
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
	if ( settings.type == 'sequence' ) {
		timer = setTimeout(change, settings.timeout);
	}
	else if ( settings.type == 'random' ) {
		do { current = Math.floor ( Math.random ( ) * ( slides.length ) ); } while ( current == 0 )
		timer = setTimeout(change, settings.timeout);
	}
	else {
		alert('type must either be \'sequence\' or \'random\'');
	}
	
	if ( settings.pauselink != null ) {
		jQuery('#' + settings.pauselink).click(pause);
	}
	
	return this;
};
