$(document).ready(function(){						   
	
	var moving = false;
	
	var moving_news = false;
							   
	$("#move_down").click(function(){
		var top = $("#projectScroller").css("top");	
		var height = $('#projectScroller').innerHeight();	
		var containerHeight = $("#projectsContent").css("height");
	
		top = top.replace("px","")
		containerHeight = containerHeight.replace("px","")
			
		top = parseInt(top)
		containerHeight = parseInt(containerHeight)
		
		var maxTop = height - containerHeight;
			
		maxTop = maxTop - (maxTop*2);
			
		var nextTop = top - 276;
	
		if (nextTop < maxTop) {			
			nextTop = maxTop;	
		}
		
		if (moving == false) {	
			moving = true;
			$("#projectScroller").animate({"top": nextTop}, "slow",function(){
				 moving = false;
			});
		}	
	});
	
	$("#move_up").click(function(){
		var top = $("#projectScroller").css("top");	
		top = top.replace("px","")	
		top = parseInt(top)
			
		maxTop = 0;
		
		var nextTop = top + 276;
		
		if (nextTop > maxTop) {		
			nextTop = maxTop;	
		}
		
		if ((top < 0) && (moving == false)) {
			moving = true;
			$("#projectScroller").animate({"top": nextTop}, "slow",function(){
				 moving = false;
			});
		}	
	});
	
	$("#move_down_news").click(function(){
		var top = $("#newsScroller").css("top");	
		var height = $('#newsScroller').innerHeight();	
		var containerHeight = $("#newsContent").css("height");
	
		top = top.replace("px","")
		containerHeight = containerHeight.replace("px","")
			
		top = parseInt(top)
		containerHeight = parseInt(containerHeight)
		
		var maxTop = height - containerHeight;
			
		maxTop = maxTop - (maxTop*2);
			
		var nextTop = top - 90;
	
		if (nextTop < maxTop) {			
			nextTop = maxTop;	
		}
		
		if (moving_news == false) {	
			moving_news = true;
			$("#newsScroller").animate({"top": nextTop}, "slow",function(){
				 moving_news = false;
			});
		}	
	});
	
	$("#move_up_news").click(function(){
		var top = $("#newsScroller").css("top");	
		top = top.replace("px","")	
		top = parseInt(top)
			
		maxTop = 0;
		
		var nextTop = top + 90;
		
		if (nextTop > maxTop) {		
			nextTop = maxTop;	
		}
		
		if ((top < 0) && (moving_news == false)) {
			moving_news = true;
			$("#newsScroller").animate({"top": nextTop}, "slow",function(){
				 moving_news = false;
			});
		}	
	});
	
	
	//portfolio links
	$("#piedpiper").click(function(){
		$("#projectScroller").animate({"top": '0px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});

	$("#kambo").click(function(){
		$("#projectScroller").animate({"top": '-276px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});
	
	$("#nickyoung").click(function(){
		$("#projectScroller").animate({"top": '-552px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});	
	
	$("#tb").click(function(){
		$("#projectScroller").animate({"top": '-828px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});
	
	$("#logolink").click(function(){
		$("#projectScroller").animate({"top": '-1104px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});	
	
	$("#ww").click(function(){
		$("#projectScroller").animate({"top": '-1380px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});	
	
	$("#westaways").click(function(){
		$("#projectScroller").animate({"top": '-1656px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});
	
	$("#abbeycourt").click(function(){
		$("#projectScroller").animate({"top": '-1932px'}, "slow");
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");		
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});	
	
	//text information	
	$("#ppTextLink").click(function(){
		$("#piedpiperText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});
	
	$("#kamboTextLink").click(function(){
		$("#kamboText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});		
	
	$("#nickyoungTextLink").click(function(){
		$("#nickyoungText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});
	
	$("#tbTextLink").click(function(){
		$("#tbText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});	
	
	$("#logoTextLink").click(function(){
		$("#logoText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});	
	
	$("#wwTextLink").click(function(){
		$("#wwText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});	
	
	$("#westawaysTextLink").click(function(){
		$("#westawaysText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});	
	
	$("#acTextLink").click(function(){
		$("#abbeycourtText").animate({"left": '0px'}, "slow");
		$("#move_up").css("visibility","hidden");
		$("#move_down").css("visibility","hidden");
	});		
	
	$(".close").click(function(){
		$("#piedpiperText").animate({"left": '479px'}, "slow");
		$("#kamboText").animate({"left": '479px'}, "slow");
		$("#nickyoungText").animate({"left": '479px'}, "slow");
		$("#tbText").animate({"left": '479px'}, "slow");
		$("#logoText").animate({"left": '479px'}, "slow");
		$("#wwText").animate({"left": '479px'}, "slow");
		$("#westawaysText").animate({"left": '479px'}, "slow");
		$("#abbeycourtText").animate({"left": '479px'}, "slow");
		$("#move_up").css("visibility","visible");
		$("#move_down").css("visibility","visible");
	});	
	
	
	//menu code//
	$('LI.drawer UL:not(:first)').hide(); // hide all ULs inside LI.drawer except the first one
	  $('H2.drawer-handle').click(function () {
		// hide all the drawer contents
		$('LI.drawer UL:visible').slideUp().prev().removeClass('open');

		// show the associated drawer content to 'this' (this is the current H2 element)
		// since the drawer content is the next element after the clicked H2, we find
		// it and show it using this:
		$(this).addClass('open').next().slideDown();
	  });
	  
	  
	//image loading	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project1').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/piedpiper.jpg');
	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project2').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/kambo.jpg');
	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project3').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/nickyoung.jpg');
	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project4').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/tombruty.jpg');
	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project5').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/logodesign.jpg');
	
	var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project6').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/whitewash.jpg');
	
var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project7').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/westaways.jpg');
 

var img = new Image();
	$(img).load(function () {
		$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
		$(this).hide();
		$('#project8').removeClass('loading').append(this);
		$(this).fadeIn();
	}).error(function () {
		// notify the user that the image could not be loaded
	}).attr('src', 'images_new/projects/abbeycourt.jpg'); 
	
	// email code
	
	$("#sendButton").click(function(){
		$("input#sendButton").val("Sending");
	});	
	
	$('#contactForm').ajaxForm({ 					   	
	// target identifies the element(s) to update with the server response 
	target: '#emailMessage', 

	// success identifies the function to invoke when the server response 
	// has been received; here we apply a fade-in effect to the new content 
	success: function() { 
		$('#emailMessage').fadeIn('slow');
		if ( $("input#emessage").val() == "Thank you for your comments" ) {
			$("input#name").val("");
			$("input#email").val("");
			$("textarea#comments").val("");
			$("input#sendButton").val("Send");
			
		} else  {
			$("input#sendButton").val("Send");
		}
	} 
    }); 
	
});
