(function( $, undefined ) {
	$.fn.noSelection = function() {
		
		var noSelObj = {
			'user-select':'none',
			'-o-user-select':'none',
			'-moz-user-select':'none',
			'-khtml-user-select':'none',
			'-moz-user-select':'none'
		},
		selObj = {
				'user-select':'',
				'-o-user-select':'',
				'-moz-user-select':'',
				'-khtml-user-select':'',
				'-moz-user-select':''
		}
		
		return function(s) {
			
			s = s === undefined ? true : s;
			
			if (s) {
				$.fn.clearSelection();
				return $(this).each(function(){
					t = $(this);
					this.onselectstart = function() { return false; };
					this.unselectable = "on";
					t.css(noSelObj);
				});
			} else {
				return $(this).each(function(){
					t = $(this);
					delete this.onselectstrt;
					delete this.unselectable;
					t.css(selObj);
				});
			}
		}
	}();
	
	$.fn.clearSelection = function() {
		var sel = window.getSelection ? window.getSelection() : document.selection;
		var empt = sel.removeAllRanges ? sel.removeAllRanges : sel.empty;
		
		return function() {
			empt.apply(sel);
		};
	}();
	
	$.fn.contentTransition = function(html, speed, callback, beforeFadeInCallback, skipResize) {
		var self = this;
		return this.each(function() {
			var el = $(this);
			var finish = {width: this.style.width, height: this.style.height};
			var cur = {width: el.width()+'px', height: el.height()+'px'};
			el.animate({opacity:0.01}, 1000, function() {
				el.html(html);
				if ( $.isFunction(beforeFadeInCallback) ) beforeFadeInCallback.call(self);
				var next = {width: el.width()+'px', height: el.height()+'px'};
				
				if (!skipResize) {
					el.css(cur).animate(next, speed, function() {
						el.css(finish);
						if ( $.isFunction(callback) ) callback();
					});
				}
				
				el.animate({opacity:1}, speed);
			});
		});
	};
	
	$.fn.hasScrollbar = function() {
        return this.get(0).scrollHeight > this.height();
    };
    
    $.fn.scrollbarWidth = function() {
    	var width;
    	
    	return function() {
	    	var parent, child;
	    	if (width === undefined) {
	    		parent = $('<div style="width:50px;height:50px;overflow:auto;"><div/></div>').appendTo('body');
	    		child = parent.children();
	    		width = child.innerWidth() - child.height(99).innerWidth();
	    		parent.remove();
	    	}
    		return width;
    	}();
    };
    
    var pmFocus = function() {
    	
    	var jqFocus = $.fn.focus;
    	
    	return function() {
	    	if (this.data()._pm && this.data()._pm.noFocus) {
	    		return this;
	    	} else {
	    		return jqFocus.apply(this, arguments);
	    	}
    	}
    }();
    
    $.fn.noFocus = function(f) {
    	f = f === undefined ? true : !!f;
    	var pm = this.data('_pm');
    	if (!pm) {
    		this.data('_pm', {noFocus:f});
    	} else {
    		pm.noFocus = f;
    	}
    };
    
    $.fn.focus = pmFocus;

}( jQuery ));
