/* Gallery: Detail
===================================================================== */

(function($) {


	$.fn.Gallery = function(settings) {

        var config = { };
            
        if (settings) $.extend(config, settings);
        
        return this.each(function() {
        
        
          var $self = $(this),
		  	  $details = $(this).find('div[data-gallerydetail]');
            
          $details.GalleryDetail();
        
        });
        
        
   }
   
   $.fn.GalleryDetail = function(settings) {

        var config = { };
            
        if (settings) $.extend(config, settings);
        
        this.each(function() {
        
       
          var $self = $(this),
              $body = $('body'),
              $id = $self.attr('id'),
			  $close = $self.find('.detail-close');
          
          // alert($id)
          $body
           .bind('DETAIL',
				function(e,id){
				
					if (id==$id)
						_open();
					else
						_close();
				
				})
		  	.bind('CLOSE',
				function(e,id){
					if ($self.hasClass('_active'))
						_close();
				
				});
		
		   $close
		   	.bind('click',
				  function(e){
				  
				    _close();
					
				  	e.preventDefault();
				  
				  
				  })
		   
		   function _open() {
			   $self.addClass('_active');
			   $self.stop().animate({left:0},config.speed,'easeInOutQuart')
		   }
		   
		   function _close() {
			   $self.removeClass('_active');
		   		$self.stop().animate({left: config.offset},config.speed,'easeInOutQuart')
		   
		   }
		  
		  
          
          
        });
		
		return this;
        
    }
    
})(jQuery);

/* Gallery: Thumbanils
===================================================================== */


(function($) {


	$.fn.GalleryThumbnails = function(settings) {

        var config = { };
            
        if (settings) $.extend(config, settings);
        
        return this.each(function() {
        
        
          var $self       = $(this),
              $forward    = $(config.forward),
              $back       = $(config.back),
              $pages      = $self.find(config.page_element),
              $thumnbails = $self.find(config.thumbnail_element),
              _activepage = 0;
            
          $thumnbails.GalleryThumbnail();
          
          $forward.bind('click',forward);
          
          $back.bind('click',back);
          
          
          function forward(e) {
          
            _activepage++;
            
            if (_activepage >= $pages.length)
              _activepage=0;
              
            scroll();
            
            e.preventDefault();
          
          }
          
          function back(e) {
          
            _activepage--;
            
            if (_activepage <0)
              _activepage = $pages.length-1;
             
            scroll();
            
            e.preventDefault();
          
          }
          
          function scroll() {
          

              $self.animate({marginTop:-_activepage*config.height},config.speed,'easeInOutQuart');
          
          }
          
          
              
          
        
        
        });
        
        
   }
   
   $.fn.GalleryThumbnail = function(settings) {

        var config = { };
            
        if (settings) $.extend(config, settings);
        
        return this.each(function(id) {
        
        
          var $self       = $(this),
              $body       = $('body'),
              $rel        = $self.attr('rel');
          
          $self
          .bind('click',on_click)
          
          $body
          .bind('DETAIL',on_project)
          
          function on_click(e) {
          
            $body.triggerHandler('DETAIL',$rel)
          
            e.preventDefault();
          
          }
          
          function on_project(e,index) {
          
            if (id==index)
              $self.addClass('active');
            else
              $self.removeClass('active');
          
          }
          
          if ($self.hasClass('active'))
            $body.triggerHandler('DETAIL',$rel)
          
          
        });
        
    }
    
})(jQuery);
