/* -------------------------------------------------------------- 
VGD.gallery.js
Copyright 2010 Antoine Doury @ AREA 17

For the homepage slideshow module
-------------------------------------------------------------- */

/********** DOM READY ************/

 // -------------------- VGDgallery
 (function($) {
  $.fn.extend({
    VGDgallery: function(options) {
      
      // options
      var opts = $.extend({}, $.fn.VGDgallery.defaults, options);

      var timing     = opts.timing,
          container  = opts.container,
          controller = opts.controller;
      
      // default vars
      var $self           = $(this),
          $container      = $("#" + container),
          $slides         = $container.find('.slide'),
          $slide_number   = $slides.length,
          $current        = 0;
          $next_a         = $container.find(".next a"),
          $prev_a         = $container.find(".previous a"),
          $next_bg        = $container.find(".next_bg"),
          $prev_bg        = $container.find(".previous_bg"),
          $currentSlide   = $slides.find('.slide').eq($current);
          
      
      init();
        
      function init() {
        
        $('img:first', $container).onImagesLoad({
          itemCallback: initController
        });
      }
      
      function autoPlay() {
        $(document).everyTime(6000, "play", function() {
          
          if($current < ($slide_number - 1)) $current++;
          else $current = 0;
          
          showSlide();
        });
      }
      
      function stopPlay() {
        $(document).stopTime("play");
      }
      
      function showSlide(stoped) {
        
        var stoped = stoped || false;
        
        $slides.removeClass('current');
        var newSlide = $container.find('.slide').eq($current);

        newSlide.addClass("current").stop(true, false).animate({
          opacity: 1
        }, timing, function() {
          $currentSlide.css('opacity' , 0);
          $currentSlide = newSlide;
          
          if(stoped) autoPlay();
        });
      }
      
      function nextSlide(e) {
        if(e != undefined) e.preventDefault();
        
        stopPlay();
        
        if($current < ($slide_number - 1)) $current++;
        else $current = 0;

        showSlide(true);
      }
      
      function prevSlide(e) {
        if(e != undefined) e.preventDefault();
        
        stopPlay();
        
        $(document).stopTime("play");
        
        if($current > 0) $current--;
        else $current = $slide_number-1;

        showSlide(true);
      }
      
      // init the controller
      function initController() {
        
        if($slide_number > 1) {
          
          $next_a.parent().animate({opacity: 1}, timing);
          $prev_a.parent().animate({opacity: 1}, timing);


          $next_a.bind('click', nextSlide)
                 .hover(function () {
                   $next_bg.addClass('hover');
                 }, 
                 function () {
                   $next_bg.removeClass('hover');
                 }
          );

          $prev_a.bind('click', prevSlide)
                 .hover(function () {
                    $prev_bg.addClass('hover');
                  }, 
                  function () {
                    $prev_bg.removeClass('hover');
                  }
           );

           $next_bg.animate({opacity: 1}, timing, function() {

              $next_bg.bind('click', nextSlide)
                      .hover(function () { $(this).addClass('hover'); }, 
                      function () { $(this).removeClass('hover'); })
           });


           $prev_bg.animate({opacity: 1}, timing, function() {

              $prev_bg.bind('click', prevSlide)
                      .hover(function () { $(this).addClass('hover'); }, 
                      function () { $(this).removeClass('hover'); })
           });
          
           // Keyboard events
            $(document).bind('keyup', function(e) {
            	switch(e.keyCode) {
              case 37: prevSlide()
                break;
              case 39: nextSlide();
                break;
              }
            });
          
          autoPlay();
        } else {
          $prev_a.remove();
          $next_a.remove();
        }
         showSlide();
      }  
   } 
  });
  
  // default options
  $.fn.VGDgallery.defaults = {
    timing      : 500,
    container   : "gallery-content",
    controller  : "gallery-prevnext"
  };
  
})(jQuery);




/* -------------------------------------------------------------- 
VGD.slideshow.js
Copyright 2010 Antoine Doury @ Zona Grafica

For the project page slideshow module
-------------------------------------------------------------- */

/********** DOM READY ************/

 // -------------------- VGDslideshow
 (function($) {
  $.fn.extend({
    VGDslideshow: function(options) {
      
      // options
      var opts = $.extend({}, $.fn.VGDslideshow.defaults, options);
	    
	    var timing = opts.timing;
	    
	    // vars
	    var $list         = $(this),
	        $li_thumbs    = $list.find('li.thumbs'),
	        $slide_number = $li_thumbs.length,
	        $thumbs       = $li_thumbs.find('a'),
	        $target       = $("#slide_fullsize"),
	        $caption      = $("#slide_caption"),
          $imgSlides    = new Array(),
          $next_a       = $list.find(".next_img a"),
          $prev_a       = $list.find(".prev_img a"),
          $isLoaded     = true,
          $current      = 0;
      
      $target.parent().prepend('<div id="slideshowLoader" ><span>Loading</span></div>');
      $loader = $("#slideshowLoader");
      
      var isIE = false;
      if ($('html').hasClass('ie')) isIE = true;

      $thumbs.each(function(i) {
        var image_info = new Array();
        image_info['url'] = $(this).attr('href');
        image_info['caption'] = $(this).attr('title');
        $imgSlides.push(image_info);
      });
      
      if($li_thumbs.length == 0) {
        $target.animate({ opacity: 1 }, timing);
      }
      else {
        
        $list.addClass('active');
        
        $thumbs.each(function(i) {
  	    	$(this).click(function(e) {
  	    		e.preventDefault();
  	    		showImg(i);
  	    	});
  	    });
        
        init();
      }
      
	    // load the new image
	    function loadNewImg(num) {
	      
	      $caption.text($imgSlides[num].caption);
	      
	    	$loader.stop(true, true).fadeIn();
	    	
	    	$target.css('height', $target.height())
	    	      .animate({ opacity: 0 }, timing, function() {
	    		        $target.empty().append('<img src="' + $imgSlides[num].url + '" alt="">');
                  $('img', $target).onImagesLoad({ itemCallback: showNewImg });
	    	});
	    }
      
	    function showNewImg($selector) {
        $loader.stop(true, true).hide();
        var h = getImgHeight($selector);
	    	$target.animate({ opacity: 1, height: h }, timing, function() {
	    	  //to be sure that the size is ok - chrome fixe ;)
	    	  $target.height(getImgHeight($selector));
	    	});
	    	$isLoaded = true;
	    }
	    
	    function getImgHeight(img) {
	      var h = $(img).height();
        if(isIE) h++;
        
        return h;
	    }
      
	    // click events
      
	    function showImg(num) {
        
	    	var li = $li_thumbs.eq(num);
	    	$current = num;
      
	    	if(!li.hasClass('selected') && $isLoaded) {
	    		$isLoaded = false;
	    		$li_thumbs.removeClass('selected');
	    		loadNewImg(num);
	    		li.addClass('selected');
	    		
	    		//autoPlay();
	    	}
	    }
	    
	    
	    function nextSlide(e) {
        if(e != undefined) e.preventDefault();
        
        //stopPlay();
        
        if($current < ($slide_number - 1)) $current++;
        else $current = 0;

        showImg($current);
      }
      
      function prevSlide(e) {
        if(e != undefined) e.preventDefault();
        
        //stopPlay();
        
        if($current > 0) $current--;
        else $current = $slide_number-1;

        showImg($current);
      }
	    
      function init() {
        
        
        $thumbs.eq(0).trigger('click');
        
        $target.bind('click', nextSlide);
        $next_a.bind('click', nextSlide);
        $prev_a.bind('click', prevSlide);
        
        // Keyboard events
         $(document).bind('keyup', function(e) {
         	switch(e.keyCode) {
           case 37: 
             if($(window).scrollTop() > 80) prevSlide()
             break;
           case 39:
            if($(window).scrollTop() > 80) nextSlide();
             break;
           }
         });
      }
   }
  });
  
  // default options
  $.fn.VGDslideshow.defaults = {
    timing      : 300
  };
  
})(jQuery);
