$(document).ready(function() {
	$('.top').addClass('hidden');
	$.waypoints.settings.scrollThrottle = 30;
	$('#container').waypoint(function(event, direction) {
		$('.top').toggleClass('hidden', direction === "up");
	}, {
		offset: '-100%'
	}).find('#header').waypoint(function(event, direction) {
		$(this).parent().toggleClass('sticky', direction === "down");
		$('#logo-large').stop(true, true).fadeToggle('fast');
		$('#logo-small').stop(true, true).fadeToggle();
		event.stopPropagation();
	});
	
	//Test for small and mobile browsers
	var mobile = screen.width < 900 || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i);
	
	if(!mobile) {
		$('#whyus article, ul.fish li').hide();
	}
	
	$("#contact label").inFieldLabels();
    
	// Register each section as a waypoint.
	$('section').waypoint({ offset: '10.9%' })
	$('#whyus').waypoint({ offset: '25%' });;
	$('#products').waypoint({ offset: '25%' });
	
	// The same for all waypoints
	$('body').delegate('section', 'waypoint.reached', function(event, direction) {
		var $active = $(this);
		
		if (direction === "up") {
			$active = $active.prev();
		}
		if (!$active.length) $active.end();
		
		$('.section-active').removeClass('section-active');
		
		$active.addClass('section-active');
		$('#header').removeClass();
		$('#header').addClass($active.attr('id'));
			
		$('.link-active').removeClass('link-active');
		$('a[href=#'+$active.attr('id')+']').addClass('link-active');
		if($active.find('h2').text() == '') {
			document.title = 'Quality Seafood, Professional Seafood – ANZ Fisheries';
		} else {
			document.title = $active.find('h2').text() + ' – ANZ Fisheries';
		}
	});
	
	// Show Why Us articles one by one
	$('body').delegate('#whyus', 'waypoint.reached', function(event, direction) {
		if (direction === "down") {
			var fade = setInterval(function() {
				current = $('#whyus article:hidden:first');
				if(current.length > 0){
					current.stop(true, true).fadeIn(800);
				} else {
					clearInterval(fade);
				}
			}, 300);
		} else {
			//clearInterval(fade);
			//$('#whyus article').stop(true, true).fadeOut(500);
		}
	});
	
	// Show products one by one
	$('body').delegate('#products', 'waypoint.reached', function(event, direction) {
		if (direction === "down") {
			var fade = setInterval(function() {
				current = $('.fish li:hidden:first');
				if(current.length > 0){
					current.stop(true, true).fadeIn(800);
				} else {
					clearInterval(fade);
				}
			}, 30);
		} 
	});
	
	
	
	
	// Negates the flash of non-active nav.
	$('body > header nav a').click(function() {
		$(this).addClass('link-active');
	}).eq(0).addClass('link-active');

	var scrollElement = 'html, body';
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}    
	});
	
	// Smooth scrolling for internal links
	$("a[href^='#']").click(function(event) {
		event.preventDefault();
		
		var $this = $(this),
		target = this.hash,
		$target = $(target);
		
		$(scrollElement).stop().animate({
			'scrollTop': $target.offset().top
		}, 800, 'swing', function() {
			window.location.hash = target;
		});
		
	});
	
	$(".anchor").click(function(){
		$(this).toggleClass('bounce');
	});
	
	$(".gull-big").click(function(){
		$(this).addClass('drop');
	});
	
	$(".gull-small").click(function(){
		$(this).addClass('drop');
	});
	
	$(".fish-mount").hoverIntent(function(){
		$(this).parent().find('.tooltip').css({'display':'block','opacity':0});
		$(this).parent().find('.tooltip').stop(true, true).animate({'opacity':1,'top':120},300,'easeInOutSine');
		$('.fish li').not($(this).parent()).not('.fish li li').animate({'opacity':.7},300);
		$(this).find('img').animate({'top':-5},300,'easeInOutSine');
	}, function(){
		$(this).parent().find('.tooltip').stop(true, true).animate({'opacity':0,'top':125},400,'easeInOutSine',function(){
			$(this).hide();
		});
		$('.fish li').not(this).stop(true, true).animate({'opacity':1});
		$(this).find('img').animate({'top':0},300,'easeInOutSine');
	});
//	$(".catch li").hoverIntent(function(){
//		$(this).parents('.catch').after('<span class="text">' + $(this).find('.method').text() + '</span>').parent().find('.text').fadeIn(200);
//		},function(){
//		$(this).parents('.catch').parent().find('.text').fadeOut(200,function(){
//			$(this).remove();
//		});
//	
//	});
	function modal() {
		$('body').append('<div id="modal" />');
		$('#modal').css({'display':'block','opacity':0}).animate({'opacity':.6},300);
		$("#modal,.close").click(function(){
			$('#modal').fadeOut(300,function(){
				$(this).remove();
			});
			$('.modal').fadeOut(300);
			return false;
		});
	}

	$(".catch li").click(function(){
		modal();
		$('#methods').addClass('modal').css({'display':'block','opacity':0,'top':($(window).height()-$('#methods').height())/2,'left':($(window).width()-$('#methods').width())/2}).animate({'opacity':.8},300);
		
	});
	
	$(window).resize(function(){
		$('#methods').css({'top':($(window).height()-$('#methods').height())/2,'left':($(window).width()-$('#methods').width())/2});
	});
	
	
	//Location Map
	function initialize() {
	   	var latlng = new google.maps.LatLng(-37.79817,144.920961);
	    var myOptions = {
	      zoom: 14,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map = new google.maps.Map(document.getElementById("map_canvas"),
	        myOptions);
		
		// Define Marker properties
		var image = new google.maps.MarkerImage('art/marker.png',
		new google.maps.Size(80, 90),
		new google.maps.Point(0,0),
		new google.maps.Point(20, 51)
		);

		// Add Marker
		var marker1 = new google.maps.Marker({
			position: new google.maps.LatLng(-37.7975,144.9185),
			map: map,
			icon: image // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
		});
	
	}
	
	$("#view-map").click(function(){
		$('html, body').animate({scrollTop:$('.contact-details').offset().top +200},800);
		$('footer').animate({'top':1150},600,'easeInOutQuad',function(){
			$('#map').slideDown(600,'easeInOutQuad',function(){
				initialize();
			});	
		});
		$(this).fadeOut();
		return false;
		
			
	});

	
	//Funky parralax stuff.
	
	
	if(!mobile) {

	   	function moveobj() {
	   		var magicnumber = $(window).scrollTop()/($(document).height());
			$('#fish1').css({'top': Math.round(-1800*magicnumber) + 330});
			$('#fish2').css({'top': Math.round(-800*magicnumber) + 330});
			$('.frame.one').css({'top': Math.round(1400*magicnumber) +150});
			$('.frame.two').css({'top': Math.round(2000*magicnumber) +90});
			$('#knife').css({'backgroundPosition': '0px ' + (Math.round(-1800*magicnumber) + 900) + 'px'});
			$('#onion').css({'bottom': Math.round(2500*magicnumber) - 1150});
			$('.gull-big').css({'top': Math.round(-1800*magicnumber)+ 1300});
			$('.gull-small').css({'top': Math.round(-500*magicnumber) + 450});
			$('#contact').css({'backgroundPosition': 'center ' + (Math.round(1000*magicnumber) -100) + 'px'});
	   	}
		
	   	$(window).scroll(function(){
	   		moveobj();
		});
	}
	
	if(screen.width < 600) { 
		$('#home .page').prepend('<div id="mobile-contact"><a href="tel:+61 3 9687 6528">Call Us</a><a href="http://maps.google.com/maps?q=Stall+11,+133+Kensington+Road,+West+Melbourne,+3003&hl=en&sll=-31.917745,115.894883&sspn=0.015299,0.027766&vpsrc=0&hnear=11%2F133-165+Kensington+Rd,+West+Melbourne+Victoria+3003,+Australia&t=m&z=17" title="Find ANZ Fisheries">Find Us</a></div>');
	}
	
	
	//Tweet
	
	/*var username='ANZFisheries'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url

	$.getJSON(url,function(tweet){ // get the tweets
		$("#tweet").html(tweet[0].text); // get the first tweet in the response and place it inside the div
	});*/
	
	//Contact Form
	
	$('.contact-form').validate({
	 submitHandler: function(form) {
			
			$('.contact-form button').fadeOut();
			
			var name = $("#fm_name").val();
			var email = $("#fm_email").val();
			var phone = $("#fm_phone").val();
			var message = $("#fm_message").val();
						
		 	var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&message=' + message;
		 			 	
	 		$.ajax({
		      type: "POST",
		      url: "/contactengine.php",
		      data: dataString,
		      success: function() {

		        $('.contact-form').fadeOut(400, function(){
		        	$(this).after("<div id='thanks' style='display:none;'><h1>Thank you</h1><p>Your enquiry has been submitted and we will be in touch soon!</p></div>");
		        	$('#thanks').fadeIn(200);
		        }); 
		        
		      }
		     });
		    return false;
	  
	}
	});
});
