$(function() {
	var $paneTarget = $("body");
	var current_rel = 0;
	
	var portfolio_count = $(".portfolio_list .wrap").length;
	var list_width = portfolio_count * 1100;
	$(".portfolio_list").width(list_width);
	
	if (portfolio_count == 1) {
		$(".next").hide();
		$(".previous").hide();
	}
	
	$(".next").click(function () {
		var rel = ++current_rel;
		if (rel == $(".portfolio_list .wrap").length) {
			rel = 0;
			current_rel = rel;
		}
		var new_positon = rel*1100;
		$(".portfolio_list").animate({"left" : -new_positon + "px"}, {"duration": 1000, "easing" : "easeInBack", queue: false }); //easeInOutQuad, easeInOutCubic, 1200 easeInOutElastic, easeInBack
		$('.portfolio_current .portfolio_ss').cycle('pause');
		$(".portfolio_current").removeClass("portfolio_current");
		$(".portfolio_list .wrap").eq(rel).addClass("portfolio_current");
		$('.portfolio_current .portfolio_ss').cycle('resume');
		return false;
	});
	
	$(".previous").click(function () {
		var rel = --current_rel;
		if (rel < 0) {
			rel = $(".portfolio_list .wrap").length - 1;
			current_rel = rel;
		}
		var new_positon = rel*1100;
		$(".portfolio_list").animate({"left" : -new_positon + "px"}, {"duration": 1000, "easing" : "easeInBack", queue: false }); //easeInOutQuad, easeInOutCubic, 1200 easeInOutElastic, easeInBack
		$('.portfolio_current .portfolio_ss').cycle('pause');
		$(".portfolio_current").removeClass("portfolio_current");
		$(".portfolio_list .wrap").eq(rel).addClass("portfolio_current");
		$('.portfolio_current .portfolio_ss').cycle('resume');
		return false;
	});
	
	$('.portfolio_ss').cycle({ 
		fx:     'fade', 
		speed:   1000, 
		timeout: 5000, 
		next:   '.portfolio_ss', 
		pause:   1 
	});
	$('.portfolio_ss').cycle('pause');
	$('.portfolio_current .portfolio_ss').cycle('resume');
	
	
	$('.site_menu a, .hire_me').click(function(){
		var scrollto = $(this).attr("href");
		$paneTarget.stop().scrollTo(scrollto, 800);
		pageTracker._trackPageview($(this).attr("href"));
		return false;
	});
	
	$("#tweets").tweet({
		join_text: "",
		username: twitterlogin,
		avatar_size: 0,
		count: 1,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	
	$("#send_me_a_message .submit").click( function(){
		$(".error").removeClass("error");
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailToVal = $("#your_email").val();
		if (emailToVal == '') {
			$("#your_email").addClass("error");
			hasError = true;
		} else if (!emailReg.test(emailToVal) && emailToVal != undefined) {	
			$("#your_email").addClass("error");
			hasError = true;
		}
		
		var nameVal = $("#your_name").val();
		if (nameVal == '' || nameVal == 'What is your name?' ) {
			$("#your_name").addClass("error");
			hasError = true;
		}
		
		var messageVal = $("#your_message").val();
		if(messageVal == '' || messageVal == 'Your message') {
			$("#your_message").addClass("error");
			hasError = true;
		}
		
		if (hasError == false) {
			var install_url = sitepath;
			
			$author = nameVal;
			$email = emailToVal;
			$message = messageVal;
			$randval = Math.random();
			
			$.ajax({
				 type: "POST",
				 url: install_url + "contact.php",
				 data: "author="+$author+"&email="+$email+"&comment="+$message+"&randval="+$randval,
				 beforeSend:function()
				 {
					$('#your_message').attr("disabled", true);
					$('#your_name').attr("disabled", true);
					$('#your_email').attr("disabled", true);
				  },
				 error:function()
				 {
					$('#your_message').removeAttr("disabled");
					$('#your_name').removeAttr("disabled");
					$('#your_email').removeAttr("disabled");
				  },
				 success: function(response)
				 {								
					$("#form_placeholder").fadeOut(800).fadeIn(800);
					function initialStartFunc() {
						$("#form_placeholder").html(response);
					}
					var initialStart = null;
					if (initialStart) clearTimeout(initialStart);
					initialStart = setTimeout(initialStartFunc, 800);
				 }
	  		});
		}
		
		return false;
	});
	
	$('#send_me_a_message .input_field').each(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('rel'));
		}
	})
	.focus(function () {
		if ($(this).val() == $(this).attr('rel')) {
			$(this).val('');
		}
	})
	.blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('rel'));
		}
	});
	
	$('#send_me_a_message .text_area').focus(function () {
		if ($(this).val() == "Your message") {
			$(this).val('');
		}
	})
	.blur(function () {
		if ($(this).val() == '') {
			$(this).val("Your message");
		}
	});
	
});