$(document).ready(function(){	
	
	if($.browser.mozilla){
		var flashPlayer = $('video').find('object').clone();
		$('video').remove();
		$('#showreel .feature').append(flashPlayer);
	}	
	
	
	fixTheFooter();
	
	$(window).resize(function(){
		fixTheFooter();
	});	
	
	if(!!document.createElement('audio').canPlayType) {
		
		$('#playList .trackId').click(function(e){		
			togglePlay($(this).parent());		
		});
		$('#playList .trackTitle, #playList .duration').click(function(event){			
			if($(this).closest('li').hasClass('active')){
				setScrub($(this).closest('li'), event);
			}
			else {
				togglePlay($(this).closest('li'));		
				
			}
			
		});
	}
	else {
		$('#playList li').each(function(){
			var trackUrl = $(this).find('audio source:first-child').attr('src');
			$(this).find('audio').remove();
			var attr = {
				'href': trackUrl,
				'title': 'Click to download this track in mp3 format.'
			};
			$(this).children('div').wrapAll('<a />').parent('a').attr(attr);
		});
	}

	
	$(window).scroll(function(){						
		$('section').each(function(){							
			if(Math.abs($(window).scrollTop() - $(this).position().top) < 60 && !$(this).hasClass('current')){
				$('a.current').removeClass('current clicked');							
				$('#' + $(this).attr('id') + 'Link').addClass('current');							
			}
		});
	});
	
	/* Implement smooth scrolling on page anchors */
	$('nav a[href*=#]').click(function(e) { 	
		$(window).unbind('scroll');
		e.preventDefault();
		$('a.current, a.clicked').removeClass('current clicked');
		var newUrl = '/' + $(this).attr('href');
		var clickedNav = $(this);
		$(this).addClass('clicked');
		/*if(typeof window.history.pushState != 'undefined'){
			window.history.pushState('object or string', $(this).attr('title'), newUrl);
		}*/
		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 - 85; // allowing for 20px of margin above the element
				$('html,body').stop().animate({scrollTop: targetOffset, "easing": "easeout"}, 800, function(){						
					$(clickedNav).addClass('current');					
					$(window).scroll(function(){						
						$('section').each(function(){							
							if(Math.abs($(window).scrollTop() - $(this).position().top) < 100 && !$(this).hasClass('current')){
								$('a.current').removeClass('current clicked');							
								$('#' + $(this).attr('id') + 'Link').addClass('current');		
								return;
							}
						});
					});
				}); 								            
            } 
			
        }		
    });	

		
	
	
	
	jQuery.each($('#contactName, #message'), function(index, field) {			
		$(field).blur(function(){
			checkTextField($(this));
		});
	});
	$('#emailAddress').blur(function(){
		checkEmail($(this));	
	});		
	
	$('#contactForm').submit(function(e){
		e.preventDefault();
		jQuery.each($('#contactName, #message'), function(index, field) {			
			checkTextField($(this));
		});		
		checkEmail($('#emailAddress'));	
		
		if(isFormValid($(this))){
			var queryString = $(this).serialize();
			$(this).serialize();		
			$.ajax({
				method: "get",url: "submit.php",data: queryString,
				beforeSend: function(){
					$('#contactForm').fadeTo('fast', 0.5); ; 
					}, //show loading just when link is clicked
				complete: function(){ 
					log('Request Complete');
					}, //stop showing loading when the process is complete
				success: function(result){ 						
					var resultData = jQuery.parseJSON(result);	
					$('#contact .feature h3')
						.html(resultData.heading);
					$('#submitResponse')
						.html(resultData.message);
					if(resultData.status == 'success'){
						$('#contactForm').fadeTo('fast', 0, function(){							
							$('#submitResponse').slideDown();
						});
					}
					else {							
						$('#submitResponse').slideDown();						
						$('#contactForm').fadeTo('fast', 1.0);						
					}
				}				
			}); //close $.ajax(	
		}
			
	});	
	
		
	// Navigation hot keys
	$("body").keypress(function(e) {		
		switch(parseInt(e.which)){
			case 49:
				$('#homeLink').trigger('click'); // 1
			break;
			case 50:
				$('#biographyLink').trigger('click'); // 2
			break;
			case 51:
				$('#audioLink').trigger('click'); // 3
			break;
			case 52:
				$('#showreelLink').trigger('click'); // 4
			break;
			case 53:
				$('#contactLink').trigger('click'); // 5
			break;		  
			}
	});	
	
});


$(window).load(function() {
	
	if(window.pageYOffset != $('#' + setNav()).position().top - 100){		
		var targetOffset = $('#' + setNav()).position().top - 100;
		$('html,body').animate({scrollTop: targetOffset, "easing": "easeout"}, 1000); 
	}
});
