var BDA = BDA || {};

/* @group Slideshow */

BDA.featureSlideshow = function(type, scope, slides, pagination, prev, next, caption) {
  
  var s = this;	
  s.scope = $(scope);
  
  s.showSlide = function(id, dir) {

		// WK - Remove click function from nav whilst slider is animating
		$(prev).unbind('click'); 
		$(next).unbind('click'); 
		
    var staged = getSlide(id);
    
    if(!dir) {
      dir = 1;
    }
    
    if(!staged.hasClass('active')) {
		setActivePage(id);
		
		staged.addClass('staged').css({
			opacity: 0,
			left: (50 * dir) + 'px'
		}).stop().show().animate({ // WK - Added .stop() to prevent animation buffering
			opacity: 1,
			left: '0',
			queue: false
		}, 500, 'swing', function() {
			setActiveSlide(id);
			
			// WK - Re-bind click function to slider nav
			$(prev).bind({
				click: function() {paginate(-1);}
			});
			$(next).bind({
				click: function() {paginate(1);}
			});
				
		});/*
		staged.animate({
			opacity: 0,
			queue: false
		}, 500);*/
    }
    
  };
  
  function getSlide(o) {
    if(typeof(o) === 'string' && o.indexOf('#') != 0)
      var o = '#'+o;
    return $(o, s.scope);
  }
  
  function getID(o) {
    return "#" + getSlide(o).attr('id');
  }
  
  function getPage(o) {
    return $(pagination).find('a').filter(function() {
      return $(this).attr('href') === getID(o);
    }).parent();
  }
  
  function setActivePage(o) {
    getPage(o)
      .addClass('active')
      .removeClass('staged')
      .siblings('.active')
      .removeClass('active');
  }
  
  function setActiveSlide(o) {
    
    getSlide(o)
      .addClass('active')
      .removeClass('staged')
      .siblings('.active')
      .removeClass('active');
      
    s.active = getID(o);
  }
  
  function isImg(src) {
    return (/([^\s]+(?=\.(jpg|jpeg|gif|png|bmp))\.\2)/i).test(src);
  };
	
	function bindNav(){
	}
  
  function paginate(dir) {
    var index = getSlide(s.active).prevAll().length + dir,
      highIndex = $(slides, scope).length - 1;
    
    if(index > highIndex) {
      index = 0;
    } else if(index < 0) {
      index = highIndex;
    }
    
    s.showSlide($(slides, scope).eq(index), dir);
  }
  
  // Added by FB
  slide_delay = 200000;
  
  s.start = function() {
    if(s.paused) {
      s.paused = false;
      s.interval = setInterval(function() {paginate(1);}, slide_delay);
      s.timer = new BDA.timer('.content .header', slide_delay);
      s.timer.start();
    }
  };
    
  s.pause = function() {
    if(!s.paused) {
      s.paused = true;
      clearInterval(s.interval);
      s.timer.pause();
    }
  };
  
  
  function init() {
    
    setActiveSlide(s.scope.find(slides).first());
    setActivePage(s.scope.find(slides).first());
    
    
    
    $(pagination).find('li').bind({
      click: function(e) {
        e.preventDefault();
        s.showSlide($(this).find('a').attr('href'));
      }
    });
    
    $(scope).hover(function() {s.pause();}, function() {s.start();});
    
    $(prev).bind({
      click: function() {paginate(-1);}
    });

    $(next).bind({
      click: function() {paginate(1);}
    });
    
    
    // caption
    var captionState = 'down';

    var animateCaption = function(dir, type) {
    	if(dir == 'up') {
    		captionState = 'up';
    		$(caption).stop().animate({ // WK - Added .stop() to prevent animation buffering
    			top: type == 'home' ? 355 : 211
    		}, 300);
    	}
    	else {
    		captionState = 'down';
	    	$(caption).stop().animate({ // WK - Added .stop() to prevent animation buffering
	    		top: type == 'home' ? 415 : 261
	    	}, 300);
    	}    
    } 
    
    $(scope).parent().bind('mouseenter', function() {
			animateCaption("up", type);
    }).bind('mouseleave', function() {
			animateCaption("down", type);
    });
    // end caption
        
    s.paused = true;
    //s.start();
      
  }
  
  init();
  
};

/* @end */
