google.setOnLoadCallback(function() {
	$(document).ready(function() {	
		newWindowLinks();
		defaultText();
		clickable();		
		carousel();
		buttons();
		imageBlocks();
	});	
});


/**	
 * 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);
		var href = link.attr("href");
		if(link.hasClass("pop")) {
			window.open(href);
		} else {
			window.location = href;
		}		
	}).hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	})
}

/* Carousel */
function carousel() {
	var items 		= $("#carouselItems li");
	if(items.size()>5) {
		var list 		= $("#carouselItems ol");
		var scrollWidth = (items.size() * 190);
		var increment 	= 950;
		var currentX	= 0;
		var maxScroll 	= -(scrollWidth-increment);		
		list.css({width:scrollWidth+"px", marginLeft:"0"})
		$("#next").click(function() {
			var scrollTo = (currentX-increment) < maxScroll ? maxScroll : (currentX-increment);
			list.animate({marginLeft: scrollTo+"px"});
			currentX = scrollTo;
			return false	
		})
		$("#prev").click(function() {
			var scrollTo = (currentX+increment) > 0 ? 0 : (currentX+increment);
			list.animate({marginLeft: scrollTo+"px"});
			currentX = scrollTo;
			return false	
		})
	} else {
		$("#carouselTools p").remove()
	}
	
}

/* Button disable */
function buttons() {
	$("button").click(function() {
		$(this).css({visibility:"hidden"});
		return true
	})	
	$("#unregistersubmit").click(function(){
		result = confirm("Are you sure you want to unregister? This will remove all your details from our database and delete all comments you have made.");
		if(result) {
			return true;
		} else {
			$(this).css({visibility:"visible"});
			return false;
		}
	});
}

/* Image Block  */
function imageBlocks() {
	$("#imageBlockThumbs a").click(function(){
		var targ 		= $("#imageBlockLarge");
		var newimghref 	= $(this).attr("href");
		var title		= $("img",this).attr("alt");
		targ.fadeOut(200,function(){
			newImage = new Image();
			newImage.onload = function() {
				targ.attr("src",newimghref).fadeIn();
				$("#imageBlockTitle").html(title);
			}
			newImage.src = newimghref;
		})		
		return false;
	})
}
