$(function() {


	// check canvas support
	var test_canvas = document.createElement("canvas") //try and create sample canvas element
	var canvascheck=(test_canvas.getContext)? true : false //check if object supports getContext() method, a method of the canvas element
	
	if(canvascheck) {
		var canvas = document.getElementById('canvasLogo');  
		var ctx = canvas.getContext('2d');
		var img = new Image();   // Create new Image object
		img.src = 'images/containerbg_new.png'; // set img src
		var i = 0;
		var t;
		img.onload = function () {
			ctx.drawImage(img, 0, 0); // draw the image from the top left corner
		}
	}
	

	// navigation handler
    $('#navigation a').click(function() {
      var $this = $(this);
	  var thisId = $this.attr("id");
	  var activeId = $(".active").attr("id");
      var elementClicked = $this.attr("href");
      var destination = $(elementClicked).offset().top;


		if(canvascheck) {
			clearInterval(t);
			t=setInterval(function () {
				ctx.save();
				ctx.clearRect(0, 0, canvas.width, canvas.height); // clear canvas
				ctx.translate(canvas.width/2, canvas.height/2); // set context to middle
				if(thisId > activeId) ctx.rotate(Math.PI / 180 * (i += 3));
				else ctx.rotate(Math.PI / 180 * (i -= 3));
				ctx.translate(-canvas.width/2, -canvas.height/2); // set context to middle
				ctx.drawImage(img,0,0);
				ctx.restore();
			}, 16);
		}
		$("nav a").removeClass("active");
		
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000, function() {
			if(canvascheck) {clearInterval(t);}
		});

		$this.addClass("active");
		return false;
	});
	

	
	
		// floating nav
	
	//config
$float_speed=500; //milliseconds

//cache vars
$fl_menu=$("#navigation");


$(window).load(function() {
    FloatMenu();
});

$(window).scroll(function () { 
    FloatMenu();
});



	
	// make each section 100% height of the screen at least
	var windowHeight = $(window).height();
	var navTop = (windowHeight / 2) - 100;
	$("section").css("height", windowHeight);
	$(".section_holder:not(:last-child)").css("padding-bottom", windowHeight);
	$("#navigation").css("top", navTop);
	
	$(window).resize(function() {
		var windowHeight = $(window).height();
			var navTop = (windowHeight / 2) - 100;
      	$("section").css("height", windowHeight);
	  $(".section_holder:not(:last-child)").css("padding-bottom", windowHeight);
	  	$("#navigation").css("top", navTop);
   });
	
	// initialize validator and add a custom form submission logic
/*	$("#contactForm").validator().submit(function(e) {

		var form = $(this);
		
		if (!e.isDefaultPrevented()) {

			var name = $("input#name").val();
			var email = $("input#email").val();
			var message = $('textarea#message').val() ;

			var string = 'name=' + name + '&email=' + email +  '&message=' + message ;
		//	alert(string) ; return false ;                                      

			$.ajax({
				type: "POST",
				url: "sendform.php",
				data: string,
				success: function(){
					$("#contact")
					.html("<h2>Your message has been sent</h2>")
					.append("<p>I will be in touch soon.</p>")
				//    .hide() ;
				}//success ends                                                       
			});

			// prevent default form submission logic
			e.preventDefault();
		}
	}); */
	
	/* form placeholders */
	$("#name").focus(function() {
		if($(this).val() == "Your Name") $(this).val("");
	});
	$("#name").blur(function() {
		if($(this).val() == "") $(this).val("Your Name");
	});
	$("#email").focus(function() {
		if($(this).val() == "Your Email") $(this).val("");
	});
	$("#email").blur(function() {
		if($(this).val() == "") $(this).val("Your Email");
	});
	$("#message").focus(function() {
		if($(this).val() == "Your Message") $(this).val("");
	});
	$("#message").blur(function() {
		if($(this).val() == "") $(this).val("Your Message");
	});
	
	// work item 
	$(".workItem").hover(function() {
		$(this).children("img").stop().animate({
				top: '318px'
			}, 500, function() {
				// Animation complete.
			}
		);
	}, function() {
		$(this).children("img").stop().animate({
				top: '0'
			}, 500, function() {
				// Animation complete.
			}
		);
	});
	
});

function FloatMenu(){
	var windowHeight = $(window).height();
	var navTop = (windowHeight / 2) - 100;
    var scrollAmount=$(document).scrollTop() + navTop;
    $fl_menu.stop().animate({top: scrollAmount}, $float_speed);
}
