jQuery.fn.extend({ 
    disableSelection : function() { 
            return this.each(function() { 
                    this.onselectstart = function() { return false; }; 
                    this.unselectable = "on"; 
                    jQuery(this).css('user-select', 'none'); 
                    jQuery(this).css('-o-user-select', 'none'); 
                    jQuery(this).css('-moz-user-select', 'none'); 
                    jQuery(this).css('-khtml-user-select', 'none'); 
                    jQuery(this).css('-webkit-user-select', 'none'); 
            }); 
    } 
});

jQuery.fn.easyFader = function( o ) {
	var options = {
		onInit: null,
		onFade: null,
		auto: false,
		start: 1,
		duration: "fast",
		onEnd: null,
		onBeginning: null
	}	
	
	if ( o ) { 
		$.extend( options, o );
	}
	var myInterval, hiderInterval;
	var current = options.start;
	var autoEnabled = options.auto;
	
	var obj = this;
	var $items = $("li", this);
	var myTimeout;
	var currentZ = 1;
	
	$items.css({ opacity: 0, display: "none" }).eq(options.start-1).show().css({ zIndex: ++currentZ });
		
	function __fade(idx, opt)
	{
		var force = typeof(opt)!='undefined' && typeof(opt.force) != "undefined" ? opt.force : false;
		
		if( idx == current && !force ) return false;
		
		clearTimeout(myTimeout);
		myTimeout = setTimeout(function() {
			var myEq = idx - 1;
			
			if( myEq >= $items.size() ) myEq = 0;
			if( myEq < 0 ) myEq = $items.size()-1;
						
			var $newItem = $items.eq(myEq);
			$newItem.stop(true,true).css({ zIndex: ++currentZ, opacity: 0, display: "block" }).animate({
				opacity: 1
			}, force ? 0 : options.duration, function() {
				if(currentZ>32000) $items.css({ zIndex: currentZ = 1 });
			});
			
			current = myEq+1;
			
			if( options.onFade ) options.onFade(obj, current);
		}, 50);
	}
	
	function __stop()
	{
		clearInterval(hiderInterval);
		clearInterval(myInterval);
		$items.stop(true,true);
		autoEnabled = false;
	}
	
	function __start()
	{
		clearInterval(myInterval);
		clearInterval(hiderInterval);
		if( options.auto )
		{
			autoEnabled = true;
			myInterval = setInterval(function() {
				__next();
			}, options.auto);		
		}
		hiderInterval = setInterval(function() {
			$items.each(function() {
				var thisZ = parseInt( $(this).css("z-index") );
				if(thisZ !== currentZ && !$(this).is(":animated") )
				{
					$(this).hide();
					$(this).data("loaded", false);
					$(this).find("img").remove();
				}
			});
		}, 1000);
	}
	
	function __prev()
	{
		var myEq = current>1 ? current-1 : $items.size();
		
		if( current == 1 && typeof(options.onBeginning) != 'undefined' )
			options.onBeginning();
		else
			__fade(current-1);
	}
	
	function __next()
	{
		var myEq = current<$items.size() ?  current+1 : 1;

		if( typeof(options.onEnd) != 'undefined' && current==$items.size() ) 
			options.onEnd();
		else
			__fade(myEq);
	}
	
	this.fadeTo = __fade;
	this.stop = __stop;
	this.start = __start;
	this.next = __next;
	this.prev = __prev;
	
	if( options.auto )	 __start();
	
	if( options.onInit ) options.onInit(obj);
	
	return this;	
}

	
jQuery.fn.scrollBar = function( o ) {
	/*
		Processing options
	*/
	var options = {
		count: -1,
		start: 1,
		onStart: null,
		onMove: null,
		onEnd: null,
		duration: 400,
		easing: null
	}	
	
	if ( o ) { 
		$.extend( options, o );
	}
	else { 
		alert("No count set! Returning...");
		return false;
	}
	
	/*
		Initiating variables and stuff
	*/
	var $t = $(this); // Track
	var $h = $(".handle", this); // Handle
	
	var enabled = false;
	
	// This obj is going to be used to get the mouse position
	var mouse = {
		x: -1,
		y: -1
	}
	// This obj is going to hold the mouse position on keydown
	var initMouse = {
		x: -1,
		y: -1
	}
	// Setting widths and maximum placements
	var lastCurrent, handleWidth, handleInitLeft = (options.start-1) * handleWidth;
	var minLeft = 0;
	var maxLeft, stepWidth;
	
	$t.disableSelection();
	
	/*
		Methods
	*/	
	function __getCurrent()
	{
		var currentLeft = $h.position().left;
		
		var toReturn = Math.round( currentLeft / stepWidth );
		
		return toReturn + 1;
	}
	
	function getLeft(number)
	{
		return number*stepWidth;
	}
	
	function __align()
	{
		var newItem = __getCurrent();
		$h.stop(true,true).animate({
			left: getLeft(newItem-1)
		}, {
			duration: options.duration,
			easing: options.easing
		});
	}
	
	function __mouseDown()
	{
		initMouse.x = mouse.x;
		initMouse.y = mouse.y;
		handleInitLeft = $h.position().left;
		
		enabled = true;
		
		if( options.onStart ) options.onStart( __getCurrent() );
	}
	
	function __mouseUp()
	{
		enabled = false;
		
		if( options.onEnd ) options.onEnd( __getCurrent() );
		
		__align();
	}
	
	function __mouseMove()
	{
		if(!enabled) return;
		
		var newX = handleInitLeft + (mouse.x - initMouse.x);
		
		if(newX < minLeft) newX = minLeft;
		if(newX > maxLeft) newX = maxLeft;
		
		$h.css({
			left: newX
		});
		
		if( options.onMove ) options.onMove( __getCurrent() );
		
		if( lastCurrent != __getCurrent() && options.onChange ) options.onChange( __getCurrent() );
		
		lastCurrent = __getCurrent();		
	}
	
	this.set = function(idx) {
		if( idx>options.count || enabled || idx == __getCurrent() ) return false;
		
		$h.stop(true,true).animate({
			left: getLeft( idx-1 )
		}, {
			duration: options.duration,
			easing: options.easing
		});
		
		return false;
	}
	this.get = function() {
		return __getCurrent();
	}
	this.setCount = function(number)
	{	
		lastCurrent = 0;
		options.count = number;
		handleWidth = $t.width() / options.count;
		
		$h.stop(true,true).css({
			width: handleWidth,
			left: 0
		});
		
		handleWidth = $h.outerWidth();
		maxLeft = $t.width() - handleWidth;
		stepWidth = maxLeft/(number-1);
	}
	this.isAnimated = function() {
		return $h.is(":animated");
	}
	/*
		After all methods are there, we initiate what 
		happens on mousemove, mousedown and mouseup
	*/
	var IE = document.all ? true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE);
	
	$(document).mousemove(function getMouseXY(e) {
		// Getting values
		if (IE) { 
			mouse.x = event.clientX + document.body.scrollLeft;
			mouse.y = event.clientY + document.body.scrollTop;
		} else {  
			mouse.x = e.pageX;
			mouse.y = e.pageY;
		}  
		
		// Checking values
		if (mouse.x < 0) mouse.x = 0;
		if (mouse.y < 0) mouse.y = 0;
		
		// Processing values
		__mouseMove();
		
		return true;
	});
	
	$h.bind('mousedown', function() {
		__mouseDown();
		return false;
	});
	
	$(document).bind('mouseup', function() {
		__mouseUp();
		return false;
	});
	
	// Returning myself
	if(options.onInit) options.onInit(this);
	this.setCount(options.count);
	
	/*
		End of story
	*/
	return this;
};
