$(document).ready(function(){
  //sucks but I have to globalize this guy
	var currentPosition = 0;

	//grabs the id to return the arrow position back to the original position
	var thisID = new Array();
	thisID.push($('.active').attr('id'));
 
  var config = {    
       over: gallerySlide, // function = onMouseOver callback (REQUIRED)    
       sensitivity: 30, // number = milliseconds delay before onMouseOut    
       out: function(){} // function = onMouseOut callback (REQUIRED)    
  };
  
  $('#nav li').hoverIntent(config);

  function gallerySlide() {
    //remove active class from previous header
    $('#nav li').each(function() {
      if ($(this).hasClass('active')) {
        $(this).toggleClass('active');
      }
    });
    $(this).toggleClass('active');
    var menuId = $(this).attr('data-menu');
   
    //do magic
    if ($('.slide').is(":visible")) {
      if ($('.slide#' + menuId).length) {
        $('.slide').hide();
        $('.slide#' + menuId).fadeIn("slow");
      }
      else {
        $('.slide').slideUp('slow');
      }
    }
    else {
      $('.slide#' + menuId).slideDown("slow");
    }
    resetGallery();
    
  }
  
 $('#header').mouseleave(function () {
    if ($('.slide').is(":visible")) {
      $('.slide').slideUp("slow");
      
      $('#nav li').each(function() {
        if ($(this).hasClass('active')) {
          $(this).toggleClass('active');
        }
      });
	  
	  //Returns arrow position back to the original position
      $('#nav #'+thisID[0]).toggleClass('active');
      resetGallery();
    }
  });
  
  
  
 $('.gallery.g1, .gallery.g2').each(function() {
   var slideWidth = 200;
   var slides = $(this).find('ul li');
   var numberOfSlides = Math.ceil((slides.length - 3));
   
   
   if(currentPosition == 0) {
     $(this).find('.link-prev').hide();
   }
   
   // no scrolling - center slides plz
   if (numberOfSlides <= 1) {
     $(this).find('.link-next').hide();
     $(this).find('.link-prev').hide();
     if(slides.length == 1){
       $(this).parent().find('.gallery-holder ul').css('margin-left', '300px');
     }
     else if(slides.length == 2){
       $(this).parent().find('.gallery-holder ul').css('margin-left', '200px');
     }
     else if(slides.length == 3){
       $(this).parent().find('.gallery-holder ul').css('margin-left', '100px');
     }
   }
   else {
     $(this).find('.link-next').bind('click', function(){
         if (currentPosition < (numberOfSlides - 1)) {
          currentPosition++;
           $(this).parent().find('.gallery-holder ul').animate({
             'marginLeft' : slideWidth*(-currentPosition)
           });
          if(currentPosition > 0) {
            $(this).parent().find('.link-prev').show();
          }
          if(currentPosition == (numberOfSlides - 1)) {
            $(this).parent().find('.link-next').hide();
          }
         }
      });

      $(this).find('.link-prev').bind('click', function(){
         if (currentPosition > 0) {
           currentPosition--;
           $(this).parent().find('.gallery-holder ul').animate({
             'marginLeft' : slideWidth*(-currentPosition)
           });
           if(currentPosition == 0) {
            $(this).parent().find('.link-prev').hide();
           }
           if(currentPosition >= 0) {
             $(this).parent().find('.link-next').show();
           }
         }
      });
   }
   
 });
 
 function resetGallery () {
   currentPosition = 0;
   $('.gallery.g1').each(function() {
     var slides = $(this).find('ul li');
     if(slides.length > 4) {
       $(this).find('ul').css('margin-left', '0px');
       $(this).find('.link-prev').hide();
       $(this).find('.link-next').show();
     }
    });
   
 }
 
 $('.gallery.g3').each(function() {
   
   $(this).find('.switcher li').click(function() {
     $('.gallery.g3').find('.switcher li').each(function() {
       if ($(this).hasClass('active')) {
         $(this).toggleClass('active');
       }
     });
     $(this).toggleClass('active');
     
     var menuId = $(this).attr('id');
      if(menuId == 'first')
        $(this).parents('.gallery').find('.gallery-holder ul').css('margin-left', '0px');
      if(menuId == 'second')
        $(this).parents('.gallery').find('.gallery-holder ul').css('margin-left', '-269px');
      if(menuId == 'third')
        $(this).parents('.gallery').find('.gallery-holder ul').css('margin-left', '-538px');
   });
 
 });
 
});
