var IS_SCROLLING = false;

$(document).ready( function() {

  //DEFAULTS TO VIEWPORT TO #INTRO

  $(".intro_link").click(function(){window.location.hash="#intro";});  
  
  //FACTS SHOW/HIDE
  
  $("#pear-cider .facts li").css({
    "left":"-230px",
    "opacity":"0"
  });
  
  $("#apple-cider .facts li").css({
    "right":"-230px",
    "opacity":"0"
  });
  
  function fact_on(num, dir) {
    var classname = ".fact" + num;
    
    if ($(classname).closest("section").hasClass("active")) {
      if (dir === "left") {    
        $(classname).animate({left: "60", opacity: "1"}, 700).animate({left: "30"});    
      } else {
        $(classname).animate({right: "60", opacity: "1"}, 700).animate({right: "30"});
      }
    }
  }
  
  function fact_off(num, dir) {
    var classname = ".fact" + num;

    if ($(classname).closest("section").hasClass("active")) {
      if (dir === "left") { 
        $(classname).animate({left: "60"}, 700).animate({left: "-230", opacity: "0"});
      } else {
        $(classname).animate({right: "60"}, 700).animate({right: "-230", opacity: "0"});    
      }
    }
  }

  var facts = [{show: 200, num: 1, state: "off", dir: "left"},
               {show: 300, num: 2, state: "off", dir: "left"},
               {show: 400, num: 3, state: "off", dir: "left"},
               {show: 500, num: 4, state: "off", dir: "left"},
               {show: 600, num: 5, state: "off", dir: "left"},
               {show: 700, num: 6, state: "off", dir: "left"},
               {show: 200, num: 7, state: "off", dir: "right"},
               {show: 300, num: 8, state: "off", dir: "right"},
               {show: 400, num: 9, state: "off", dir: "right"},
               {show: 500, num: 10, state: "off", dir: "right"},
               {show: 600, num: 11, state: "off", dir: "right"},
               {show: 700, num: 12, state: "off", dir: "right"}];

  // Run animations at certain offsets (see 'facts' data structure).
  $(window).scroll(function(){
    var doctop = $(document).scrollTop();

    for (var i=0; i<facts.length; i++) {
      var fact = facts[i];

      if (doctop > fact.show && fact.state == "off") {
        fact_on(fact.num, fact.dir);
        fact.state = "on";
      } else if (doctop < fact.show && fact.state == "on") {
        fact_off(fact.num, fact.dir);
        fact.state = "off";
      }
    }
  });
  

  //CHECKS WHICH SECTION IS VISIBLE
  
  $("section").bind("inview", function(evt, visible, visiblePartX, visiblePartY){
  
    if (visible) {
      $(this).addClass("active");
      
      if ($(this).attr("id") == "pear-cider") {
        $("#pear-cider .ribbon").animate({left: "0"});
        $("#apple-cider .ribbon").animate({right: "-130"});
      } else if ($(this).attr("id") == "apple-cider") {
        $("#apple-cider .ribbon").animate({right: "0"});
        $("#pear-cider .ribbon").animate({left: "-130"});
      } else {
        $("#pear-cider .ribbon").animate({left:"-130"});
        $("#apple-cider .ribbon").animate({right:"-130"});
      }
      
    } else {
      var index = $("section").index(this);
      if ($(window).scrollLeft() > $(this).offset().left) {
        var new_index = 1;
      } else if ($(window).scrollLeft() < $(this).offset().left) {
        var new_index = -1;
      } else {
        var new_index = 0;
      }

      $(this).removeClass("active");
      
      if (!navigator.userAgent.match(/Android/i) && new_index != 0) {
        var section = $("section:eq(" + (index + new_index) + ")");
        section.addClass("active");
        window.location.hash = $(section).attr("id");
      }
    }
  }); 
 
 
  // OPEN EXTERNAL LINK IN NEW TAB/WINDOW
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });

	//REMOVE DEFAULT VALUE FOR FORM INPUTS
	$('input[alt]').focus(function () {
    if ($(this).attr("value") == $(this).attr("defaultValue")) {
      $(this).attr("value", '');
    }		
	});
	$('input[alt]').blur(function () {
      if ($(this).attr("value") == '') {
    	$(this).attr("value", $(this).attr("defaultValue"));
    }
	});
	
	
	//RESIZING
	
  var windowwidth = $(window).width();
  $("section").width(windowwidth + 20);
  $("body, #content, header").width((windowwidth * 3) + 60);
	
	
	//INTRO NAVIGATION ANIMATIONS
	
	//setting default positions
	$("#apple-hand img, #apple-hand span").css({"right":"-200px"});
	$("#pear-hand img, #pear-hand span").css({"left":"-200px"});
	
	
	//apple hover animations
	$("#intro nav #apple").mouseover(function(){
  	if (!IS_SCROLLING) {
      $(this).stop(true, true).fadeOut();
    	$("#apple-hand").stop(true, true).show();
    	$("#apple-hand img").stop(true, true).animate({right: "0"});
    	$("#apple-hand .line-1").stop(true, true).animate({right: "176"}, 500).animate({right: "146"});
    	$("#apple-hand .line-2").stop(true, true).animate({right: "196"}, 300).animate({right: "146"});
    	$("#apple-hand .line-3").stop(true, true).animate({right: "156"}, 700).animate({right: "146"});
  	}
	});
	
	$("#intro nav #apple-hand").mouseleave(function(){
	  if (!IS_SCROLLING) {
    	$("#apple-hand img").animate({right: "-200"});
    	$("#apple-hand .line-1").animate({right: "176"}, 500).animate({right: "-200"});
    	$("#apple-hand .line-2").animate({right: "196"}, 300).animate({right: "-200"});
    	$("#apple-hand .line-3").animate({right: "156"}, 700).animate({right: "-200"}, function(){
      	$("#intro nav #apple").fadeIn();
      	$("#apple-hand").hide();
      });
    }
	});
	
	//pear hover animations
	$("#intro nav #pear").mouseover(function(){
  	if (!IS_SCROLLING) {
      $(this).stop(true, true).fadeOut();
    	$("#pear-hand").stop(true, true).show();
    	$("#pear-hand img").stop(true, true).animate({left: "0"});
    	$("#pear-hand .line-1").stop(true, true).animate({left: "186"}, 700).animate({left: "146"});
    	$("#pear-hand .line-2").stop(true, true).animate({left: "166"}, 300).animate({left: "146"});
    	$("#pear-hand .line-3").stop(true, true).animate({left: "206"}, 500).animate({left: "146"});
  	}
	});
	
	$("#intro nav #pear-hand").mouseleave(function(){
	  if (!IS_SCROLLING) {
    	$("#pear-hand img").animate({left: "-200"});
    	$("#pear-hand .line-1").animate({left: "186"}, 700).animate({left: "-200"}, function(){
      	$("#intro nav #pear").fadeIn();
      	$("#pear-hand").hide();
      });
    	$("#pear-hand .line-2").animate({left: "166"}, 300).animate({left: "-200"});
    	$("#pear-hand .line-3").animate({left: "206"}, 500).animate({left: "-200"});
    }
	});  
  
  //PEAR CIDER PARALLAX
  
  $('#pear-cider .parallax #pear-large').scrollingParallax({
    staticSpeed: 2,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#pear-cider .parallax #pear-1').scrollingParallax({
    staticSpeed: 0.4,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#pear-cider .parallax #pear-2').scrollingParallax({
    staticSpeed: 0.3,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#pear-cider .parallax #pear-bubbles').scrollingParallax({
    staticSpeed: 0.2,
    reverseDirection : false,
    staticScrollLimit : false,
    loopIt: false
  });
  
  
  //APPLE CIDER PARALLAX
  
  $('#apple-cider .parallax #apple-large').scrollingParallax({
    staticSpeed: 2,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#apple-cider .parallax #apple-1').scrollingParallax({
    staticSpeed: 0.4,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#apple-cider .parallax #apple-2').scrollingParallax({
    staticSpeed: 0.3,
    reverseDirection : true,
    staticScrollLimit : false,
    loopIt: false
  });
  
  $('#apple-cider .parallax #apple-bubbles').scrollingParallax({
    staticSpeed: 0.2,
    reverseDirection : false,
    staticScrollLimit : false,
    loopIt: false
  });
	
});
