/*
	jQuery SlideSelect plugin
	2009, Paul van Dam
*/

(function($){
$.fn.slideselect = function (options) {
	
	var opt = {
		prevButtonId: 'slideselect_prev',
		nextButtonId: 'slideselect_next',
		visibleItems: 1,
		itemWidth: 317,
		animSpeed: 400,
		timeOut: 5000
	};

	if (options) $.extend(opt, options);

	return this.each (function () {

		var self = $(this);
		var slidediv = $('>div:first',self);
		var elements = $('a',self);
		var elementCount = elements.length;
		var currentElement = 1;
		
		if (elementCount <= opt.visibleItems) {
			$('#slideselect_nav').hide();
			return;
		} else {
			var ind = $('#slideselect_indicator');
			for (var i=0; i<elementCount; i++)
				ind.append('<img src="images/slideselect_indicator'+(i==0?'_active':'')+'.gif">');
		}

		slidediv.css('width',elementCount*opt.itemWidth);

		// bind events to specified buttons
		$('#'+opt.prevButtonId).click(movePrev);
		$('#'+opt.nextButtonId).click(moveNext);

		// bind mousewheel event to container
		self.mousewheel(function(e, delta){
			if (delta < 0) moveNext();
			if (delta > 0) movePrev();
			return false;
		});

		// update active indicator
		function updateIndicator() {
			$('#slideselect_indicator img').attr('src','images/slideselect_indicator.gif');
			$('#slideselect_indicator img:eq('+(currentElement-1)+')').attr('src','images/slideselect_indicator_active.gif');
		}

		// Move handling
		var _moveRunning = false;

		function movePrev() {
			if (_moveRunning) return false;
			_moveRunning = true;

			$('a:last',slidediv).prependTo(slidediv);
			slidediv.css('left','-'+opt.itemWidth+'px');
			slidediv.animate({left:'+='+opt.itemWidth+'px'},opt.animSpeed,'swing',function(){
				currentElement = (currentElement == 1) ? elementCount : currentElement - 1;
				updateIndicator();
				_moveRunning = false;
			});

			return false;
		};

		function moveNext() {
			if (_moveRunning) return false;
			_moveRunning = true;

			slidediv.animate({left:'-='+opt.itemWidth+'px'},opt.animSpeed,'swing',function(){
				$('a:first',slidediv).appendTo(slidediv);
				slidediv.css('left',0);
				currentElement = (currentElement == elementCount) ? 1 : currentElement + 1;
				updateIndicator();
				_moveRunning = false;
			});

			return false;
		};

		// Automated run
		function autoRun() {
			moveNext();
			timer = setTimeout(autoRun, opt.timeOut);
		}		
		var timer = setTimeout(autoRun, opt.timeOut);
		self.mouseover(function(){
			clearTimeout(timer);
		}).mouseout(function(){
			timer = setTimeout(autoRun, opt.timeOut);
		});

	});
};
})(jQuery);

// Mousewheel plugin
// Fixed Opera scroll direction
(function($){$.event.special.mousewheel={setup:function(){var handler=$.event.special.mousewheel.handler;if($.browser.mozilla)$(this).bind('mousemove.mousewheel',function(event){$.data(this,'mwcursorposdata',{pageX:event.pageX,pageY:event.pageY,clientX:event.clientX,clientY:event.clientY});});if(this.addEventListener)this.addEventListener(($.browser.mozilla?'DOMMouseScroll':'mousewheel'),handler,false);else
this.onmousewheel=handler;},teardown:function(){var handler=$.event.special.mousewheel.handler;$(this).unbind('mousemove.mousewheel');if(this.removeEventListener)this.removeEventListener(($.browser.mozilla?'DOMMouseScroll':'mousewheel'),handler,false);else
this.onmousewheel=function(){};$.removeData(this,'mwcursorposdata');},handler:function(event){var args=Array.prototype.slice.call(arguments,1);event=$.event.fix(event||window.event);$.extend(event,$.data(this,'mwcursorposdata')||{});var delta=0,returnValue=true;if(event.wheelDelta)delta=event.wheelDelta/120;if(event.detail)delta=-event.detail/3;if($.browser.opera)delta=event.wheelDelta;event.data=event.data||{};event.type="mousewheel";args.unshift(delta);args.unshift(event);return $.event.handle.apply(this,args);}};$.fn.extend({mousewheel:function(fn){return fn?this.bind("mousewheel",fn):this.trigger("mousewheel");},unmousewheel:function(fn){return this.unbind("mousewheel",fn);}});})(jQuery);
