$(document).ready(function(){

	if($('#content').hasClass('home'))
	{
		$('#lastfm').append('<p class="loading">Loading latest tracks...</p>');

		$("#tweet").getTwitter({
			userName: "chris_vaughan",
			numTweets: 2,
			loaderText: "Loading latest tweets...",
			slideIn: true,
			showHeading: false,
			showProfileLink: false
		});

		$('#lastfm ul').lastFM({
			username: 'chrisvaughan85',
			apikey: 'a5d543fb160623c51fb4ec4db3568baf',
			number: 8,
			onComplete: function(){
				$('#lastfm .loading').remove();
				$('#lastfm ul').hide();
				$('#lastfm ul').slideDown(1000);
			}
		});
	}


	formJS();
	$("#cm a, a#cmf").bind("click", showForm);
	
	hideQuickNav();
	$(".photo ul a").not(".photo ul .first a").bind("click", loadPhoto);
	$(".photo ul .first a").bind("click", loadThumbs);
});

function hideQuickNav(){
	$(".img ul").hide();
	$(".img").hover(
      function () {
        $(".img ul").fadeIn(200);
      },
      function () {
        $(".img ul").fadeOut(200);
      }
    );

}

function loadPhoto(){
	$.ajax({
		type: "GET",
		url: $(this).attr("href"),
		beforeSend: function(){
			$('.photo img').animate({opacity: 0.5}, 0);

			var conH = $('#content').innerHeight();
			$('#content').height(conH);
			
			$('#content .img').prepend('<div id="photoload" class="span8 last"></div>');
			var imgH = $('#content .img').innerHeight();
			$('#photoload').height(imgH);
		},
		success: function(html){
			$("#wrap").remove();
			$("#content").prepend(html);
			$('#content').removeClass('thumbs').addClass('photo');
			$("#content .img").hide().fadeIn(500);

			setTimeout("$.scrollTo('h2.title', 0);", 500);
		},
		complete: function(){
			$('#content').height('auto');
			hideQuickNav();

			$(".photo ul a").not(".photo ul .first a").bind("click", loadPhoto);
			$(".photo ul .first a").bind("click", loadThumbs);
		}

	});
	return false;
}

function loadThumbs(){
	$.ajax({
		type: "GET",
		url: $(this).attr("href"),
		beforeSend: function(){
			$('.photo img').animate({opacity: 0.5}, 0);

			var conH = $('#content').innerHeight();
			$('#content').height(conH);

			$('#content .img').prepend('<div id="photoload" class="span8 last"></div>');
			var imgH = $('#content .img').innerHeight();
			$('#photoload').height(imgH);
		},
		success: function(html){
			$("#wrap").remove();
			$("#content").prepend(html)
			.removeClass('photo')
			.addClass('thumbs');
			$("#content .thu").hide().fadeIn(500);

			setTimeout("$.scrollTo('h2.title', 0);", 500);
		},
		complete: function(){
			$('#content').height('auto');
			$(".thumbs ul a").bind("click", loadPhoto);
		}

	});
	return false;
}

function showForm(){

	if( $("#contact").length > 0 ){
		$("#contact").fadeOut(800);
		setTimeout("$('#contact').remove();", 800);
	}else{

		$.ajax({
			type: "GET",
			url: $(this).attr("href"),
			beforeSend: function(){
				$('#contact').remove();
				$('#page').append('<div id="contact" class="span4"><p class="loading">Loading...</p></div>');
			},
			success: function(html){
				$("#contact").remove();
				$("#page").append(html);
				$("a.email").replaceWith('<a href="mailto:info@chrisvaughanphotography.co.uk" class="icon email"><span>Email <em>info@chrisvaughanphotography.co.uk</em></span></a>');
				$("#contact").hide().fadeIn(800);
				formJS();
			},
			complete: function(){
				$.scrollTo('#contact', 500);
			}

		});
	}
	return false;
}

function formJS(){

	$("button").bind("click", submitForm);

	$(".cform").click(function(){
		$.scrollTo('#form', 500, {
			onAfter: function(){
				$("#name").focus();
			}
		});
		return false;
	});
}

function submitForm(){

	var formData = $("form").serialize();
	$.ajax({
		type: "POST",
		url: "http://www.chrisvaughanphotography.co.uk/index.php/contact_me",
		data: formData,
		beforeSend: function(){
			$('form *').hide();
			$('.sorry').hide();
			$.scrollTo('#form', 500);
			$('form').append('<p class="loading">Sending...</p>');
		},
		success: function(html){
			$("#contact").remove();
			$("#page").append(html);
			$("a.email").replaceWith('<a href="mailto:info@chrisvaughanphotography.co.uk" class="icon email"><span>Email <em>info@chrisvaughanphotography.co.uk</em></span></a>');
			$("#contact").hide().fadeIn(800);
			$("button").bind("click", submitForm);
			formJS();
		},
		complete: function(){
			$.scrollTo('#form', 500);

			if( $(".thanks").length > 0 ){
				setTimeout("$('.thanks').fadeOut(800);", 8000);
				setTimeout("$('#contact').remove();", 8900);
			}
		}

	});
	return false;
}