// JavaScript Document

$(document).ready(function() { 

	// Login link

	$('.planroomlink').click(function() {
		$('body').append('<div id="loginbox" style="display:none; cursor:default;"></div>');

		$('#loginbox').load('/plan_room/form.php #planroomlogin', function() {
			$.blockUI({ message: $('#loginbox'), css: { width: '490px', border: 'none', textAlign: 'left', left: '25%' } });
			$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
		});
		return false;
	}); 

	// Home Page //
	
	currentThreeProjects = 0;
	$('#threeprojects .thumbnails a').each(function(index){
		$(this).attr('rel', index);
	});
	
	$('#threeprojects ul li').each(function(index){
		$(this).addClass('item' + index);
	});
	
	$('#threeprojects .thumbnails a img:not(:first)').fadeTo(0, .4);
	$('#threeprojects .thumbnails a').hover(function() {
		// Mouse over
		$('#threeprojects .thumbnails a img').fadeTo(0, .4);
		$('#threeprojects ul li').hide();
		$(this).children('img').fadeTo(0, 1);
		$('.item' + $(this).attr('rel')).fadeIn('medium');
		currentThreeProjects = $(this).attr('rel');
	}, function() {
		// Mouse out
	});
	
	
	// Case Studies //
	
	var images=new Array();
	var currentSlide=new Array();
	$('.case_study').each(function(index) {
		if($(this).children('.slideshow').find('img').size() > 1) {
			images[index] = $(this).children('.slideshow').find('img').size() - 1;
			
			$(this).children('.slideshow').append('<a href="#" rel="' + index + '" class="nextbtn"><span class="text">Next</span></a>');
		
			$(this).children('.slideshow').find('img').each(function(index){
				$(this).addClass('image' + index);
				if (index != 0) {
					$(this).hide();
				}
			});
			
			$(this).find('.nextbtn').click(function() {
				advance($(this).parent());
				return false;
			});
		
			currentSlide[index] = 0;
		}
	});
	
	function advance(parentObject) {
		currentIndex = $(parentObject).children('.nextbtn').attr('rel');
		$(parentObject).children('img').hide();
	
		advanceTo = currentSlide[currentIndex]+1;
	
		if(currentSlide[currentIndex] >= images[currentIndex]) {
			// On last image, move to first
			advanceTo = 0;
		}
		
		currentSlide[currentIndex] = advanceTo;
	
		$(parentObject).children('.image' + advanceTo).fadeIn();
	}


}); 

