google.setOnLoadCallback(function() {
	$(document).ready(function() {	
		gallery();
		newWindowLinks();
		defaultText();
		clickable();
			
	});	
});

var galleryImages;
var currentImage = 0;
var lastImage;

function gallery() {
	galleryImages = $("#gallery img");
	if(galleryImages.length>1) {
		galleryImages.hide();
		lastImage = galleryImages.length-1;
		currentImage = lastImage;
		setTimeout(swapImage,0);
	} else {
		galleryImages.eq(0).hide().fadeIn(1000)
	}		
}

function swapImage() {	
	var nextImage = (currentImage+1); 
	if(nextImage > lastImage) {
		nextImage = 0;
	}
	galleryImages.eq(currentImage).css("z-index","1");
	galleryImages.eq(nextImage).css("z-index","2").fadeIn(1000,function(){
		galleryImages.eq(currentImage).hide();
		currentImage = nextImage;
		setTimeout(swapImage,4000);
	});
}


/**	
 * Target new windows
 */
function newWindowLinks(){
	$('a.pop').unbind('click').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
}


/* Remove default value and replace */
function defaultText() {
	$(".defaultText").unbind("focus,blur").focus(function () {		
		if ($(this).val() == $(this)[0].defaultValue) {
        	$(this).val("");
        }
	 }).blur(function() {
        if ($(this).val() == "") {
        	$(this).val($(this)[0].defaultValue);
     	}
	 });
}

/* Make a whole box clickable */
function clickable() {
	$(".clickable:has(a)").css({"cursor":"pointer"}).unbind("click").click(function() {
		var link = $("a",this).eq(0).attr("href");
		window.location = link;
	}).hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	})
}

/* Un-obfuscate email */
function fnE(inarr,text) {
	inarr = inarr.split(",");
	var out = "";
	for(i=inarr.length-1;i>=0;i--) {
		out += inarr[i];
	}
	var text = text==undefined || text=='' ? out : text;
	document.write('<a href="mailto:'+out+'">'+text+'</a>');	
}