$(document).ready(function(){

	/*
	** home contact links that open the contact form
	*/
	$(".collapsable_contact_form").hide();
	$(".home_contact a").click(function(event){
		event.preventDefault();
		var $link = $(this);
		$(".collapsable_contact_form").slideUp("fast", function (){
			$(this).appendTo($link.parents("p")).slideDown("slow");
			
			$("#contact_form_to").val($link.attr('alt'));
		});
		
		
	});
	
	/*
	** contact addresses that changes the contact form TO select
	*/
	$(".contact_address").click(function(event){
		$("#contact_form_to").val($(this).attr('alt'));
	});
	
	/*
	** Contact form
	*/
	$("#contact_form_submit").click(function(event){
		event.preventDefault();
		$("#contact_form_submit").hide();
		$("#ButtonLoader").show();

		//clear inputs
		$("#contactForm").children(":input").each(function(){
			if($(this).val() == $inputs[$(this).attr('id')])
				$(this).val('');
		});
		
		$.post("./sendMessage.php", $("#contactForm").serialize(), function(data){

			if(data.messageResult==true )
			{
				$("#contactFormErrors").hide();
				$("#contactForm").slideUp();
				$("#contactFormConfirmation").slideDown();
			}
			else
			{
				$("#contactFormErrors").slideDown();
			}
				
			$("#ButtonLoader").hide();
			$("#contact_form_submit").show();
		}, "json");
	
	});
	
	/*
	** inputs clearing
	*/
	var $inputs = [];
	$(":input").each(function(){
		$inputs[$(this).attr('id')] = $(this).val();
	});
	
	$(":input").focus(function(){
		if($(this).val() == $inputs[$(this).attr('id')])
			$(this).val('');
	});
	$(":input").blur(function(){
		if($(this).val() == '')
			$(this).val($inputs[$(this).attr('id')]);
	});
	
	/*
	** Collapsable sections
	*/
	$(".collapsable_div").hide();
	$(".collapsable_div_slide_first").slideDown();
	$(".collapse_next_div").click(function(event){
		event.preventDefault();
		var $link = $(this);
		var $nextDiv = $(this).next(".collapsable_div");
		$nextDiv.slideToggle(function(){
			if($nextDiv.hasClass('collapsable_div_scroll')) $.scrollTo($link, 400);
		});
	});
	
	/*
	** Featured
	*/
	$("#featuredTable").hide();
	$("#featuredChartButton").click(function(event){
		event.preventDefault();
		$("#featuredChartButton").addClass("link-arrowed");
		$("#featuredTableButton").removeClass("link-arrowed");
		
		$("#featuredTable").slideUp('slow');
		$("#featuredChart").slideDown('slow');
	});
	$("#featuredTableButton").click(function(event){
		event.preventDefault();
		$("#featuredChartButton").removeClass("link-arrowed");
		$("#featuredTableButton").addClass("link-arrowed");
		
		$("#featuredChart").slideUp('slow');
		$("#featuredTable").slideDown('slow');
	});

});