/* Author: Alto Clef Productions */

$(document).ready(function () {
	$("body").queryLoader2({
		barColor: "#6e6d73",
		backgroundColor: "#ffffff",
		percentage: true,
		barHeight: 30,
		completeAnimation: "grow"
	});
	$('.content').hide();
	$('#home').delay(1200).fadeIn('slow');
	
	//find window height and suggest it be larger if necessary
	if($(window).height() <= 650 || $(window).width() <= 1200) {
		alert("Psssst. Hey.  You should make your browser window larger.  It will make our site look prettier.");
	}
	
});

//Nav links
$('#navigation ul li').click(function () {			
	//animate the wrapper and content
	$targ = $(this).find("a").attr("href");
	$targbg = '';
	$targcon = '';
	switch($targ) {
		case '#home': $targbg = "0", $targcon = "#home";
		break;
		case '#about': $targbg = "1300", $targcon = "#about";
		break;
		case '#contact': $targbg = "2600", $targcon = "#contact";
		break;
		case '#pricing': $targbg = "3900", $targcon = "#pricing";
		break;
		case '#portfolio': $targbg = "5300", $targcon = "#portfolio";
		break;
		default: $targbg = "0", $targcon = "0";
	}
	//alert ("target: "+$targx);
	$('.content').fadeOut();
	$('#backgroundPane').scrollTo($targbg, 1200);
	//$('#contentPane').scrollTo($targcon, 1600);
	$($targcon).delay(1200).fadeIn('slow');
	
	$('#navigation ul li').removeClass('selected'); 
	$(this).addClass('selected');
	return false;  
});

//CONTACT FORM
var messageDelay = 3000;  // How long to display status messages (in milliseconds)

// Init the form once the document is ready
$( init );


// Initialize the form

function init() {
  
  // Make submitForm() the form's submit handler.
  $('#contactForm')	.submit( submitForm );  
  
  //for fancybox
  $("a[rel=portfolio]").fancybox({
		'opacity'			: 'true',
		'cyclic'			: 'true',
		'overlayOpacity'	: '0.6',
		'overlayColor'		: '#444',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">'+ title +'</span>';
		}
	});
  $("a.more").fancybox({
		'opacity'			: 'true',
		'cyclic'			: 'true',
		'overlayOpacity'	: '0.6',
		'overlayColor'		: '#444',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'autoDimensions'	: false,
		'width'				: 600,
		'height'			: 300
	});
  
}


// Submit the form via Ajax

function submitForm() {
  var contactForm = $(this);

  // Are all the fields filled in?

  if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#subject').val() || !$('#message').val() ) {
    // No; display a warning message and return to the form
    $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
    contactForm.fadeOut().delay(messageDelay).fadeIn();

  } else {

    // Yes; submit the form to the PHP script via Ajax

    $('#sendingMessage').fadeIn();
    contactForm.fadeOut();

    $.ajax( {
      url: contactForm.attr( 'action' ) + "?ajax=true",
      type: contactForm.attr( 'method' ),
      data: contactForm.serialize(),
      success: submitFinished
    } );
  }

  // Prevent the default form submission occurring
  return false;
}


// Handle the Ajax response

function submitFinished( response ) {
  response = $.trim( response );
  $('#sendingMessage').fadeOut();

  if ( response == "success" ) {

    // Form submitted successfully:
    // 1. Display the success message
    // 2. Clear the form fields
    // 3. Fade the content back in

    $('#successMessage').delay(500).fadeIn().delay(messageDelay).fadeOut();
    $('#senderName').val( "" );
    $('#senderEmail').val( "" );
	$('#subject').val( "" );
    $('#message').val( "" );

     $('#contactForm').delay(messageDelay+500).fadeIn();

  } else {

    // Form submission failed: Display the failure message,
    // then redisplay the form
    $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
    $('#contactForm').delay(messageDelay+500).fadeIn();
  }
}
