/*
QUESTIONS FOR ANGAD


*/


$(window).scroll(function() {
  var scroll_position = $(window).scrollTop();
  if (scroll_position >= 3) {
    $('.back_to_top').removeClass('hide').addClass('show');
  } else {
    $('.back_to_top').removeClass('show').addClass('hide');
  };
});



// Generic helper function

function Animations() {
  this.init();
}
  
Animations.prototype.init = function() {
}
  
Animations.prototype.show_hide = function(thing_to_animate, pixels, speed, easing) {
  $(thing_to_animate).animate(
    {
      left: pixels
    },
    {
      duration: speed,
      easing: easing, 
    });
}
    
Animations.prototype.show_hide_captions = function($thing_to_animate, pixels, easing, speed) {
    $thing_to_animate.animate(
    {
      marginLeft: pixels,
      easing: easing
    }, speed );
}

Animations.prototype.show_hide_labels = function($thing_to_animate, pixels, easing, opacity, speed) {
    $thing_to_animate.animate(
    {
      left: pixels,
      easing: easing,
      opacity: opacity
    }, speed );
}




// Controls revealing and hiding the cartoon hand.

Hand.prototype = new Animations();

function Hand() {
  this.init();
}

Hand.prototype.init = function() {
  this.$arm = $('#arm_container');
  this.$hand = $('#hand');
  this.$hand_hidden = $('#hand.hand_hidden');
  this.bind_click();
  this.close_btn();
}
  
Hand.prototype.show_it = function() {
  $('.close_container a.close').fadeIn(700);
  this.$hand_hidden.removeClass("hand_hidden");
  this.show_hide(this.$hand, "766px", 700, "easeOutElastic");
  this.show_hide(this.$arm, "-343px", 700, "easeOutElastic");
}
  
Hand.prototype.hide_it = function() {
  $('.close_container a.close').hide();
  this.$hand.addClass("hand_hidden");
  this.show_hide(this.$hand, "19px", 200, "easeOutQuad");
  this.show_hide(this.$arm, "-1165px", 200, "easeOutQuad");
}

Hand.prototype.close_btn = function() {
  $('#arm_container a.close').click( $.proxy(function(e){
    e.preventDefault();
    this.hide_it();
  }, this));
}

Hand.prototype.bind_click = function() {
  
  $('#hand').click( $.proxy(function() {
	  if (this.$hand.hasClass("hand_hidden")) {
	    this.show_it();
	  } 
	  else {
	    this.hide_it();
	  }
  }, this));

}






// Controls revealing and hiding captions

Captions.prototype = new Animations();

function Captions() {
  this.init();
}

Captions.prototype.init = function() {
  this.bind_click();
  this.bind_click_close_btn();
}

Captions.prototype.show_it = function($target) {
  $target.removeClass('hidden').fadeOut(100).next().next().fadeIn(300);    
  var $thing_to_animate = $target.next();
  this.show_hide_captions($thing_to_animate, '-330', 'easeOutQuad', 300 );
}

Captions.prototype.hide_it = function($target) {
  $target.addClass('hidden').fadeIn(300).next().next().fadeOut(100);
  var $thing_to_animate = $target.next();
  this.show_hide_captions($thing_to_animate, '0', 'easeOutQuad', 300 );
}

Captions.prototype.bind_click = function() {
  $('#posts').delegate('.icon_info', 'click', $.proxy(function(e) {
	   var $target = $(e.target);
	   if ($target.hasClass("hidden")) {
	     this.show_it($target);
	  } 
	  else {
	     this.hide_it($target);
	  }
  }, this));  
}

Captions.prototype.hide_caption_after_close_btn_click = function($target) {
  $target.fadeOut(100).prev().prev().addClass('hidden').fadeIn(300);
  var $thing_to_animate = $target.prev();
  this.show_hide_captions($thing_to_animate, '0', 'easeOutQuad', 300 );
}

Captions.prototype.bind_click_close_btn = function() {
  $('#posts').delegate('.close_caption', 'click', $.proxy(function(e) {
    e.preventDefault();
    var $target = $(e.target);
      this.hide_caption_after_close_btn_click($target);
  }, this));
}







// Controls for icon labels

Labels.prototype = new Animations();

function Labels() {
  this.init();
}

Labels.prototype.init = function() {
	this.$icon = $('#nav li a');
  this.bind_hover();
}

Labels.prototype.bind_hover = function() {
  this.$icon.stop(true, true).hoverIntent($.proxy(function(e){
  
    var $target_class = $(e.target).attr('class');
    var $label = $('#nav_labels li' + '.' + $target_class);
    this.show_hide_labels($label, '25px', 'easeOutQuad', 1, 300);
    //alert('Ready to show');
  
  }, this), $.proxy(function(e){
  
    var $target_class = $(e.target).attr('class');
    var $label = $('#nav_labels li' + '.' + $target_class);
    this.show_hide_labels($label, '-100px', 'easeOutQuad', 0, 700);
    //alert('Ready to hide');
    
  }, this));
}



$(document).ready(function() {
	new Hand();
	new Captions();
	new Labels();
	
	// Scroll to top	
  $('.back_to_top').click(function(){
    $('html, body').animate({scrollTop:0}, 'slow'); 
  }); 

});




