$(document).ready(function(){

	$("a#zoom").fancybox();

	$("a.email").each(function(){ //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text(e);
	});

	$("a.external").click(function(){ //Open link in new window
		window.open(this.href);
		return false;
	});
	
	$("input.input-text").each ( //Define default text for each input element
		function() {
			this.rel=this.value;
		}
	);

	$("input.input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$("input.input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
	
	contentCarousel();
});

//Scrolling carousel
function contentCarousel() {
	var numberOfItems=$("#carousel div.content").length; //Define number of items

	var currentItem=0;
	
	if($.query.get("content")) { //Get initial content index
		if (isInteger($.query.get("content")) && $.query.get("content")<=numberOfItems) {
			var currentItem=$.query.get("content");
		}
	}
	
	$("#carousel").css("left",currentItem*-960+"px"); //Set content position
	
	//checkPagination();

	$("#pagination li#prev a").click(function(){ //Previous item
		prevItem();
		return false;
	});

	$("#pagination li#next a").click(function(){ //Next item
		nextItem();
		return false;
	});

	function nextItem() { //Scroll functions
		if (currentItem<numberOfItems-1) {
			currentItem++;
			scrollCarousel();
		} else {
			//alert(currentItem);
			currentItem = 0;
			scrollCarousel();
		}
	};

	function prevItem() {
		if (currentItem>0) {
			currentItem--;
			scrollCarousel();
		} else {
			currentItem = numberOfItems-1;
			scrollCarousel();
		}
	};

	function scrollCarousel() { //Tween carousel to destination
		$("#carousel").animate({left: (960*(currentItem)*-1)}, {duration: 1000});
		if ($("#flash_video_1").length>0) {
			resetVideos();
		}
		//checkPagination();
	};
	
	/*function checkPagination() { //If we need to disable prev/next buttons
		if (currentItem==0) {
			$("#pagination li#prev").addClass("prev-inactive");
		} else {
			$("#pagination li#prev").removeClass();
			$("#pagination li#prev").addClass("prev");
		}

		if (currentItem==numberOfItems-1) {
			$("#pagination li#next").addClass("next-inactive");
		} else {
			$("#pagination li#next").removeClass();
			$("#pagination li#next").addClass("next");
		}
	};*/
};

function resetVideos($numberOfVideos) { //Stop and reset flash carousel videos
	var $numberOfVideos=$("#carousel div.content div.col-left").length;

	for ($i=1; $i<=$numberOfVideos; $i++) {
		var thisVideo=document.getElementById("flash_video_"+$i);
		thisVideo.resetMe();
	}
};

function isInteger(str) {
	return (str.toString().search(/^-?[0-9]+$/) == 0);
};