$(document).ready(function(){
//	default error message
	errorMSG = "<h6>Uh oh, there was an error, try again.</h6>"

//	ajax setup
    $(document).ajaxSend(function(){
       $("#loading").fadeIn("slow");
    })
    $(document).ajaxStop(function(){
       $("#loading").fadeOut("slow");
    })
    $.ajaxSetup ({
		cache: false
	});
   
//	lazy load for images
	$('img.loadlate').appear(function() {
	    var loadlate = $(this).attr('loadlate');
	    if (loadlate) {
	        $(this).attr('src', loadlate);
	        $(this).removeAttr('loadlate');
	    }
	});
	$('img.loadlate.hidden').removeClass('hidden');


//	sticky float
/*
	$('#nav').stickyfloat({ 
		duration	: 400,
		offsetY		: 3,
		lockBottom	: false
	});
*/

//	on first load 
// parse url
	var linkedFrom = $.url().fsegment(1); 
	if ( !linkedFrom ) var linkedFrom = 'home';
	$("#stuff").load('pages/' + linkedFrom + '.php');
/* 	$("#nav").hide().slideDown(700); */

//	click events 
	$("body").live("click",function(oEvent){
		var oTarget = oEvent.target;
		
		if ( $(oTarget).attr('navLink') ) {
			var myLink = $(oTarget).attr('id');
//				var myPage = 'clevercouch | freelance creative ' + $(oTarget).attr('page');
			//jQuery.easing.def = 'easeOutElastic';
			// load page
			$.ajax({
				type	: "GET",
				url		: "pages/" + myLink + ".php",		
				success	: function(data) {
							$("#stuff").remove("#stuff > *").hide().html(data).fadeIn('fast');
							//slideDown({ duration: 2000 })
							$("body").find('.active').removeClass('active')
							$(oTarget).addClass('active')
							$('html,body').animate({scrollTop:0},1000)
						  },
				error	: function(data) {
//						  	alert('there was a problem')
							$("#error").empty().html(errorMSG).slideDown().delay(8000).slideUp()
						  }
			})
			return true;
/* 			$("#stuff").hide().fadeIn('medium').load('pages/' + myLink + '.php'); */
//				document.title = myPage;
		}
	})	

	
}) // dom ends

function formSubmit() {
  $("#myForm").submit(function() {

  var dataString = $("#myForm").serialize();

    $.ajax({
      type: "POST",
      url: "work/processContact.php",
      data: dataString,
      timeout: 2000,
      success: function(data) {
        $("#myForm").slideUp(1500);
        $("#formSuccess").delay(1500).fadeIn(2500);
      },
      error: function(data) {
		errorMSG = "<h6>Please confirm the form fields are correctly filled out.</h6>"
		$("#error").empty().html(errorMSG).slideDown().delay(8000).slideUp()
      }
    });
  return false;
  })
}

function attachEvents() {
	if ( $("#rotation") ) {
		$("#rotation").cycle({
			fx				: 'uncover',
			speed			: 2000,
			//delay			: 4000,
			random			: true
		})
	}
//	fancybox
	$(".fancybox").fancybox({
		helpers: {
			title : {
				type : 'float'
		  	}
		},
		openEffect	: 'none',
		closeEffect	: 'none'
	});
}
