jQuery(document).ready(function($){
	if($('#slideshow').length){
		$('#slides').cycle({
			fx: 'fade',
			timeout: 7000,
			prev: '#prev',
			next: '#next'
		});
	};
	$("#footer input[type='text']").focus(function () {
		if ($(this).val() === $(this).attr("alt")) {
			$(this).val("");
		}
	}).blur(function () {
		if ($(this).val() === "") {
			$(this).val($(this).attr("alt"));
		}
	});
	if($('#projectList').length){
		initClickBlocks();
	}
	if($('#projectGallery').length){
		initCarousel();
	}
});

///////////////////////////////////////////////////////////
// init clickblocks
///////////////////////////////////////////////////////////

function initClickBlocks(){
	el = $('#projectList li');
	el.css("cursor", "pointer");
	
	el.each(function(){
		$(this).click(function(event) {
			event.preventDefault();
			var link = $(this).find('a');
			window.location = link.attr('href');
		});
		$(this).hover(
			function () {
				$(this).addClass('hover');
			},
			function () {
				$(this).removeClass('hover');
			}
		)
	});
}

function initCarousel(){
	var currSlide = 0;
	function mycarousel_initCallback(carousel){
		$('#projectGallery li a').click(function(event){
			event.preventDefault();
			var item = getCurrentCarouselItem(this);
			setBigImg(carousel, $(this).parent('li'))
		});
	}
	function setBigImg(carousel, listitem){
		$('#projectGallery li').removeClass('active');
		li = $(listitem);
		li.addClass('active');
		newSlide = li.find('a').attr('href');
		newCaption = li.find('img').attr('alt');
		$('#bigImg').attr('src', newSlide);
		$('#caption span').html(newCaption);
	}

	function getCurrentCarouselItem(listitem){
		$(listitem).parent('li').each(function(){
			var leftPos = $(this).offset().left;
			if( leftPos >= 0){
				var id = $(this).attr('id').split("-");
				currSlide =  id[1]*1+1;
				return false;
			}
		});
		return currSlide;
	}
	jQuery('#projectGallery ul').jcarousel({
		scroll: 4,
		wrap: 'wrap',
		initCallback: mycarousel_initCallback
	});
}
