var autoRunTimeOut = null;
(function($) {
	$.fn.carousel = function(opts) {
		if (typeof startAutoRun == "undefined") var startAutoRun = window.location.pathname=="/";
		
		if(!opts.prev || !opts.next || !this.children().length) return false;
		var set = $.extend({ mod: 'left', disabledClass: 'carousel-disabled', currentClass: 'carousel-current', behave: 'single'}, opts);
		var target = $(this).css('position', 'absolute');
		if($(this).children('.'+set.currentClass).length != 1)$(this).children().removeClass(set.currentClass).first().addClass(set.currentClass)
		target.parent().css('position', 'relative');
		if(set.mod == 'left') target.width(target.children(':last').position().left+target.children(':last').width());
		else target.height(target.children(':last').position().top+target.children(':last').height());
		if(typeof set.offsetPre == "undefined") set.offsetPre = target.css(set.mode == ' left' ? 'margin-left' : 'margin-top').substring(0, target.css(set.mode == ' left' ? 'margin-left' : 'margin-top').length - 2) * 1;
		if(typeof set.offsetPost == "undefined") set.offsetPost = target.css(set.mode == ' left' ? 'margin-right' : 'margin-bottom').substring(0, target.css(set.mode == ' left' ? 'margin-right' : 'margin-bottom').length - 2) * 1;
//		console.log(set.offsetPost);
		var behaviours = {
			single : {
				setNext : function() {
					if(!target.children().last().is("."+set.currentClass)) return target.find("."+set.currentClass).removeClass(set.currentClass).next().addClass(set.currentClass); else return false;
				}, setPrev: function() {
					if(!target.children().first().is("."+set.currentClass)) return target.find("."+set.currentClass).removeClass(set.currentClass).prev().addClass(set.currentClass); else return false;
				}, checkBounds: function() {
					if(target.children().first().is("."+set.currentClass)) set.prev.addClass(set.disabledClass);
					else set.prev.removeClass(set.disabledClass);
					if(target.children().last().is("."+set.currentClass)) set.next.addClass(set.disabledClass);
					else set.next.removeClass(set.disabledClass);
					if(set.callback) set.callback(target.children('.'+set.currentClass).prevAll().length, target.children().length);
				}, animTo : function() { return   -(target.children("."+set.currentClass).position()[set.mod] + set.offsetPre); }
			},	singleRightAlign : {
				setNext : function() {
					if(!target.children().last().is("."+set.currentClass)) return target.find("."+set.currentClass).removeClass(set.currentClass).next().addClass(set.currentClass); else return false;
				}, setPrev: function() {
					if(!target.children().first().is("."+set.currentClass)) return target.find("."+set.currentClass).removeClass(set.currentClass).prev().addClass(set.currentClass); else return false;
				}, checkBounds: function() {
					if(target.children().first().is("."+set.currentClass)) set.prev.addClass(set.disabledClass);
					else set.prev.removeClass(set.disabledClass);
					if(target.children().last().is("."+set.currentClass)) set.next.addClass(set.disabledClass);
					else set.next.removeClass(set.disabledClass);
					if(set.callback) set.callback(target.children('.'+set.currentClass).prevAll().length, target.children().length); //*/
				}, animTo : function() {
					return -(target.children("."+set.currentClass).position()[set.mod] + target.children("."+set.currentClass).width()-target.parent().width() + set.offsetPre);
				}
			},	full : {
				setNext : function() {
					if(target.children("."+set.currentClass).position()[set.mod] < 
							target.children(":last").position()[set.mod]
							+target.children(":last")[set.mod=="top"?'height':'width']()
							-target.parent()[set.mod=="top"?'height':'width']())
					 	return target.find("."+set.currentClass).removeClass(set.currentClass).next().addClass(set.currentClass); else return false;
				}, setPrev: function() {
					if(!target.children().first().is("."+set.currentClass)) return target.find("."+set.currentClass).removeClass(set.currentClass).prev().addClass(set.currentClass); else return false;
				}, checkBounds: function() {
					if(target.children().first().is("."+set.currentClass)) set.prev.addClass(set.disabledClass);
					else set.prev.removeClass(set.disabledClass);
					if(target.children("."+set.currentClass).position()[set.mod] >= 
							target.children(":last").position()[set.mod]
							+target.children(":last")[set.mod=="top"?'height':'width']()
							-target.parent()[set.mod=="top"?'height':'width']()) set.next.addClass(set.disabledClass);
					else set.next.removeClass(set.disabledClass);
					if(set.callback) set.callback(target.children('.'+set.currentClass).prevAll().length, target.children().length);
				}, animTo: function() {
					if(target.parent()[set.mod=="top"?'height':'width']() > target.children(":last").position()[set.mod]+target.children(":last")[set.mod=="top"?'height':'width']()) return 0;
					return -Math.min(target.children("."+set.currentClass).position()[set.mod], 
						target.children(":last").position()[set.mod]
						+target.children(":last")[set.mod=="top"?'height':'width']()
						-target.parent()[set.mod=="top"?'height':'width']()
						+set.offsetPost
					);
				}
			},	numeric : {
				pos: 0
				, setNext: function() {
					if(target.width() < target.parent().width()) return true;
					set.behave.pos = Math.max(-target.width() + target.parent().width(), set.behave.pos - target.parent().width());
					return true;
				}, setPrev: function() {
					if(target.width() < target.parent().width()) return true;
					set.behave.pos = Math.min(0, set.behave.pos + target.parent().width());
					return true;
				}, checkBounds: function() {
					if(target.width() < target.parent().width()) {
						set.prev.addClass(set.disabledClass);
						set.next.addClass(set.disabledClass)
					} else {
						if(set.behave.pos >= 0) set.prev.addClass(set.disabledClass);
						else set.prev.removeClass(set.disabledClass);
						if(set.behave.pos <= -(target.width() - target.parent().width())) set.next.addClass(set.disabledClass);
						else set.next.removeClass(set.disabledClass);
					}
				}, animTo: function() {
					return '' + set.behave.pos;
				}
			}
		}
		
		
		set.behave = behaviours[set.behave] ? behaviours[set.behave] : behaviours['single'];
		
		var stopAutoRunner = function() {
			if (autoRunTimeOut != null) window.clearTimeout(autoRunTimeOut);
			startAutoRun=false;
		}
		
		
		var animate = function() {
			var animObj = {}; animObj[set.mod] = set.behave.animTo();
			target.animate(animObj);
			set.behave.checkBounds()
		}
		set.prev.click(function() { if(set.behave.setPrev()) animate(); stopAutoRunner(); return false; });
		set.next.click(function() { if(set.behave.setNext()) animate(); stopAutoRunner(); return false; });
		if(set.selector) set.selectors = set.selector.children();
		if(set.selectors) {
			set.selectors.click(function() {
				set.selectors.filter('.'+set.currentClass).removeClass(set.currentClass);
				$(this).addClass(set.currentClass);
				target.children('.'+set.currentClass).removeClass(set.currentClass);
				$(target.children()[jQuery.inArray(this, set.selectors)]).addClass(set.currentClass);
				animate(); 
				//if(set.callback) set.callback(target.children('.'+set.currentClass).prevAll().length, target.children().length);
				return false;
			});
		}
		animate();
		
		// autorun code
		var autoRunnerDirection = null;
		var autorunner = function() {
			
			if (autoRunnerDirection == null) {
				autoRunnerDirection = true;
				set.behave.setNext();
			}
			
			animate();
			
			if(autoRunnerDirection && !set.behave.setNext()) { autoRunnerDirection = false; }
			
			if (!autoRunnerDirection && !set.behave.setPrev()) { autoRunnerDirection = null}
			
			if (startAutoRun) {
				if (autoRunTimeOut != null) window.clearTimeout(autoRunTimeOut);
				autoRunTimeOut = window.setTimeout(autorunner, 5000);
			}
		}
		
		if (autoRunTimeOut == null && startAutoRun) {
			autoRunTimeOut = window.setTimeout(autorunner, 5000);
		}
		
		return this;
	}
})(jQuery);

