/**
 * @author tothsz
 */


// in_ragne
function in_range( value, range) {
	return( value < range[0] || value > range[1] )?false:true;
}

// later
jQuery.later = function (_o, _s, _m) {
	var that = _o,
		args = Array.prototype.slice.apply(arguments, [3]);
	if (typeof _m === 'string')_m = that[_m];
	time = setTimeout(function () {_m.apply(that, args);}, _s);
	return time;	
};

// cookie
jQuery.cookie = {
	
	set: function(name, value, expires, path, domain, secure) {
		var now = new Date();	
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0)
			date.setTime(date.getTime() - skew);		
		
		now.setTime(now.getTime() + expires);
		var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + now.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	},
	
	get: function(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else
			begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
			end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	},
	
	del: function(name, path, domain) {
		if (this.get(name)) {
			document.cookie = [name,"=",((path) ? "; path=" + path : ""),((domain) ? "; domain=" + domain : ""),"; expires=Thu, 01-Jan-70 00:00:01 GMT"].join('');
		}
	}
};

// events
$.fn.extend({
	target: function(){
		return this[0].target || this[0].srcElement;
	},
	stopPropagation: function() {
		if (this[0].stopPropagation) {
		  this[0].stopPropagation();
		} 
		else {
		  this[0].cancelBubble = true;
		}
	},
	preventDefault: function() {
		if (this[0].preventDefault) {
		  this[0].preventDefault();
		} 
		else {
		  this[0].returnValue = false;
		}
	},
	stopEvent: function() {
		this.stopPropagation(this[0]);
		this.preventDefault(this[0]);
	}, 	
	
	pointerX: function() {
		return this[0].pageX || (this[0].clientX +
		  (document.documentElement.scrollLeft || document.body.scrollLeft));
	},
	pointerY: function() {
		return this[0].pageY || (this[0].clientY +
		  (document.documentElement.scrollTop || document.body.scrollTop));
	}
});

// clone
function clone(object) {
	function F() {}
	F.prototype = object;
	return new F;
}

/* Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * $LastChangedDate: 2007-12-20 09:02:08 -0600 (Thu, 20 Dec 2007) $
 * $Rev: 4265 $
 *
 * Version: 3.0
 * 
 * Requires: $ 1.2.2+
 */

(function($) {

$.event.special.mousewheel = {
	setup: function() {
		var handler = $.event.special.mousewheel.handler;
		
		// Fix pageX, pageY, clientX and clientY for mozilla
		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);
		// Get correct pageX, pageY, clientX and clientY for mozilla
		$.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";
		
		// Add delta to the front of the arguments
		args.unshift(delta);
		// Add event to the front of the arguments
		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);