// EXTENDED FROM WHAT WAS FOUND AT YEBOCREATIVE.COM
// ALL HAIL YEBOCREATIVE.COM
var autoScrollSpeed = 1800;
var easingMethod = "easeInOutExpo";
var footer_height = $("#follow.footer").height();

// FANCY FUNCTION WRITTEN BY YEBOCREATIVE.COM
function getScrollTop() { 
	if( typeof pageYOffset != "undefined" ) {
		return pageYOffset;
	} else {
		var B = document.body;
		var D = document.documentElement;
		D = ( D.clientHeight ) ? D : B;
		return D.scrollTop;
	}
}

// MOVES THE .block .contain DIVS
function blockController() {
	var total_blocks = $(".block").length;
	$(".block").each(function(i) {
		if( i == 0 ) { // if first
			div_bottom = $(this).offset().top - $(window).height();
			div_top = $(this).offset().top + $(this).height();
			if( getScrollTop() > div_bottom && getScrollTop() < div_top ) {
				$(this).find(".contain").css("marginTop", ( (getScrollTop()) * 0.3 ) + "px");
			}
		} else if( i == total_blocks - 1 && footer_height != null ) { // if last (with footer)
			div_center = $(this).offset().top - ( $(window).height() - $(this).height() )/2;
			div_bottom = $(this).offset().top - $(window).height();
			div_top = $(this).offset().top + $(this).height() + footer_height;
			if( getScrollTop() > div_bottom && getScrollTop() < div_top ) {
				$(this).find(".contain").css("marginTop", ( (getScrollTop() - div_center + footer_height) * 0.3 ) + "px")
			}
		} else { // all others
			div_center = $(this).offset().top - ( $(window).height() - $(this).height() )/2;
			div_bottom = $(this).offset().top - $(window).height();
			div_top = $(this).offset().top + $(this).height();
			if( getScrollTop() > div_bottom && getScrollTop() < div_top ) {
				$(this).find(".contain").css("marginTop", ( (getScrollTop() - div_center) * 0.3 ) + "px");
			}
		}
	});
}

// GOOGLE FONTS
WebFontConfig = {
	google: { families: [ 'Rokkitt', 'Tangerine' ] }
};
(function() {
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
	'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();

//ONLOAD
$(function(){
// DETERMINE RESOLUTION
	$(window).resize(function() {
	
		$("body").removeClass("twelveeighty tentwentyfour");
		
		if( $(window).width() >= 1190 )
			$("body").addClass("twelveeighty");
		else
			$("body").addClass("tentwentyfour");
			
		if( $(window).height() < 800 ) {
			$("div.block").height( $(window).height() );
			$("div.block .contain").height( $(window).height() );
		} else {
			$("div.block").height(800);
			$("div.block .contain").height(690);
		}
		
	});
	
	$(window).resize();	

// CALLS blockController() (ABOVE)
	$(window).scroll(function() {
		blockController();
	});

// WORKAROUND TO STOP INITIAL CHROME JITTER
	$("html, body").stop().animate({
		scrollTop: "+=1px"
	}, 1, easingMethod);

	
// BINDS .click TO RIGHT-NAV
	$(".control, .scroll-to, .header .logo").click(function(event) { // ... on click
	
		if( $(this).parent("#navigation") ) {
			$("#navigation .control").removeClass("white-text");
			$(this).addClass("white-text");
		}
	
		var assoc_div = "#" + $(this).attr("rel");
		var div_height = $(assoc_div).height() > $(assoc_div).innerHeight() ? $(assoc_div).height() : $(assoc_div).innerHeight();
	
		if( $(this).parent("li").is(":first-child") ) { // if first
			$("html, body").stop().animate({
				scrollTop: $(assoc_div).offset().top 
			}, autoScrollSpeed, easingMethod);	
		} else if( $(this).parent("li").is(":last-child") && footer_height != null ) { // if last (with footer)
			$("html, body").stop().animate({
				scrollTop: $(assoc_div).offset().top - ( $(window).height() - div_height ) + footer_height
			}, autoScrollSpeed, easingMethod);	
		} else { // all others
			$("html, body").stop().animate({
				scrollTop: $(assoc_div).offset().top - ( $(window).height() - div_height )/2
			}, autoScrollSpeed, easingMethod);
		}
		
		event.preventDefault();
		
	});
	
// POPUPS
	$("a.popup").fancybox({
      margin:0,
      padding:0,
      scrolling:'no',
      centerOnScroll:true
	});
	
// AJAX FORM
// Sendmail Ajax
		
	$("div.contact-form form").submit(function(e) {
		
		error = false;
		
		$(this).find(":input").each(function() {
			if( $(this).val() == "" ) {
				error = true;
				$(this).prev().css({ color:"#f00" });
				$(this).keypress(function(e) {
					$(this).prev().css({ color:"#FF7800" });
				});
			}
		});
		
		if( !error ) {
		
			$.ajax({
				url:	"sendmail.php",
				type:	"GET",
				data: {
					to:		$(this).find("input[name=mailto]").val(),
					name:		$(this).find("input[name=name]").val(),
					email:	$(this).find("input[name=email]").val(),
					message:$(this).find("textarea[name=message]").val()
				},
				cache: false,
				success: function(html) {					
					if( html == "true" ) {
						$("#form-result").html("<p style=\"color:#fff;font:2em/1.8em 'Rokkitt';text-align:right;\">Message Sent!</p>");
					} else {
						$("#form-result").html("<p style=\"color:#f00;font:2em/1.8em 'Rokkitt';text-align:right;\">There Was A Problem :(</p>");
					}
				}
			});
		
		}
		
		e.preventDefault();
		return false;
		
	});
	
});

