// JavaScript Setup

// jQuery Init
jQuery.noConflict();
jQuery(function($) {	
	
	// Back to top section links for non-IE browsers
	if ( ! $.browser.msie ) {
		$('.section').children('h3').append("<a href='#jump-top' class='scrollTop hidden' title='Back to Top'>Back to Top</a>");
		$(".section").hover(
			function(){
				$(this).find(".scrollTop").css('display', 'block');
			},
			function(){
				$(this).find(".scrollTop").css('display', 'none');
			}
		);
	}	
	
	// Panel Toggle in Header
	$("#panel_toggle").click(function(){
	  $("#panel").slideToggle(300, function(){ $("#panel_toggle").toggleClass("panel_active"); });	  
	});	
	
	// Used to scroll to a specific position.
	// Edit the variables below to adjust speed and positioning
	var scrollDuration = 500; // 1000 = 1 second
	var scrollGap = 0; // in Pixels, the gap left above the scroll to point
	$('a[href*=#jump-]').click(function() 
	{
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) 
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) 
			{
				var targetOffset = $target.offset().top - scrollGap;
				$('html,body').animate
					(
					{scrollTop: targetOffset}, 
					scrollDuration
					);
				return false;
			}
		}
	});	
	
	// Toggle Content!
	$(".hidden").hide();
	$("a.toggle").click(function(event){
		if( $(this).text() == 'Show More' ) {
			$(this).text("Show Less");
		}else{
			$(this).text("Show More");
		}
		$(this).parents(".toggle-container").find(".hidden").slideToggle("normal");
		return false;
	});	
	
	// Contact form
	$("form#contact_form").submit(function() {
		$("form#contact_form #cf_submit").attr("disabled", "disabled");
		$.ajax({
			type: "POST",
			url: "mail.php",
			data: $(this).serialize(),
			success: function(response) {
				alert(response);
				$("form#contact_form #cf_submit").attr("disabled", "");
			},
			error: function(response) {
				alert(response);
				$("form#contact_form #cf_submit").attr("disabled", "");
			}
		});		
		return false;
	});
	
	// Clear input fields when clicked
	$("form#contact_form input[type='text'], form#contact_form textarea").focus(function() {
		$(this).val("");
	});
	
	// Contact form ajax loading
	$('#cf_loading_div')
    .hide()  // hide it initially
    .ajaxStart(function() {
    	$(this).show();
    })
    .ajaxStop(function() {
    	$(this).hide();
    });	
	
});
