var slideShowInterval = 5000;
google.setOnLoadCallback(function() {
	$(document).ready(function() {	
		newWindowLinks();
		clickable();
		defaultText();
		if($("#homeSlideshow").size()>0) {
			setTimeout("slideShow()",slideShowInterval);
		}		
		news();
		gallery();
		$("header hgroup").click(function(){ window.location="/" })
	});	
});


/**	
 * Target new windows
 */
function newWindowLinks(){
	$('a.pop,a[rel=external]').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"}).live("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) {
	inarr = inarr.split(",");
	var out = "";
	for(i=inarr.length-1;i>=0;i--) {
		out += inarr[i];
	}
	document.write('<a href="mailto:'+out+'">'+out+'</a>');	
}

/* Homepage */
function slideShow() {
	
	var speed = 1000;
	var next = slidePosition+1;
	slidePosition++;
	if(next > slides.length-1) {
		next=0;
		slidePosition = 0;
	}
	var nextImage = slides[next];	
	
	var img1 = $("#homeSlideshow img");	
	img1.css({zIndex:1}).attr("id","image1");
	$("#homeSlideshow").append('<img src="' + nextImage + '" id="image2" style="display:none;z-index:2" />');
	
	newImage = new Image();
	newImage.onload = function() {		
		$("#image2").fadeIn(speed,function(){
			$("#image1").remove();
			setTimeout("slideShow()",slideShowInterval);
		});	
	}	
	newImage.src = nextImage;
}

function news() {
	$(".newsGalleryThumbs a").click(function(){
		var target = $(".newsGallery img",$(this).closest(".newsArticle"));
		loadImage($(this).attr("href"),target);
		return false;
	})
}

// Gallery
var galleryTimer;
var galleryPosition = 0;
var galleryLength;

function galleryThumbsScroll(percentage) {

	var x = -Math.round(((($("#galleryThumbs li").size()*95) - 940 - 10)/100)*percentage);
	$("#galleryThumbs ul").css({left:x+"px"})
}

function gallery() {
	galleryLength = $("#galleryThumbs li").size();
	if(galleryLength>10) {
		$("#galleryNav").append('<div id="scrollbarWrapper"><div id="scrollbarTarg"></div></div>');
		$("#scrollbarTarg").slider({max:100,min:0,animate:true,	slide: function(e,ui) { galleryThumbsScroll(ui.value) } });
		$("#galleryThumbs ul").css({width:(galleryLength*95)+"px"})
	}
	$("#galleryThumbs a").click(function(){
		clearTimeout(galleryTimer);
		$("#galleryThumbs ul").animate({top:"60px"})
		loadImage($(this).attr("href"),$("#gallery img:eq(0)"),"autoGallery()");
		$("#scrollbarWrapper").fadeOut()
		return false;
	})
	$("#galleryNav").hover(function(){
		$("#galleryThumbs ul").css({top:"60px"}).animate({top:"0px"},300);
		$("#scrollbarWrapper").fadeIn()		
	},function(){
		$("#galleryThumbs ul").animate({top:"60px"},200);
		$("#scrollbarWrapper").fadeOut()
	}).mousemove(function(){
		$("#scrollbarWrapper").show()
		if($("#galleryThumbs ul").css("top")=="60px") {
			$("#galleryThumbs ul").animate({top:"0px"},300);
		}
	})
	$("#rightArrow").click(function(){
		clearTimeout(galleryTimer);
		var next = galleryPosition+1;
		if(next>=galleryLength) {
			next = 0;
		}
		loadImage($("#galleryThumbs li:eq("+next+") a").attr("href"),$("#gallery img:eq(0)"),"autoGallery()");
		galleryPosition = next;		
	})
	$("#leftArrow").click(function(){
		clearTimeout(galleryTimer);
		var next = galleryPosition-1;
		if(next<0) {
			next = galleryLength-1;
		}
		loadImage($("#galleryThumbs li:eq("+next+") a").attr("href"),$("#gallery img:eq(0)"),"autoGallery()");
		galleryPosition = next;
	})
	autoGallery()
}

// Auto gallery
function autoGallery() {	
	galleryTimer = setTimeout('$("#rightArrow").click()',5000)
}

// Image Loader
function loadImage(src,targ,callback) {
	var i = targ;
	var callback = callback || function() {}; 
	i.fadeOut(300,function() {
		newImage = new Image();
		newImage.onload = function() {
			i.attr("src",src).fadeIn(500)
			eval(callback);
		}
		newImage.src = src;	
	});
}
