/*
 * SS Library to jQuery (Utils, HTML, DOM)
 * 
 * Version: 1.0 (stable)
 * 
 * Required:
 * 		jquery-1.6.1.js
 * 
 * Notes:
 * 		- Some features of this version are not in the original version
 * 
*/

(function($){
	$.extend({
		Utils: {
			trim: function(value){
			    return (!$.Utils.empty_string(value)) ? value.replace(/^\s*|\s*$/g, "") : "";
			},
			preg_match: function(pattern, subject){
				        /* Cross Browser Valid Pattern */
				return ((pattern != null) && (pattern != "") && !$.Utils.empty_string(subject) && (subject.match(new RegExp(pattern)) != null));
			},
			preg_replace: function(pattern, replacement, subject){
				        /* Cross Browser Valid Pattern */
				return ((pattern != null) && (pattern != "") && !$.Utils.empty_string(replacement) && !$.Utils.empty_string(subject)) ? subject.replace(new RegExp(pattern), replacement) : "";      
			},
			str_startwith: function(content, value){
				return (!$.Utils.empty_string(content) && !$.Utils.empty_string(value) && !content.indexOf(value));
			},
			str_remove_spaces: function(value){
				return (!$.Utils.empty_string(value)) ? value.replace(/\s/g, "") : value;
			},
			isdefined: function(value){
				return (!$.Utils.empty_string(value) && (value in window));
			},
			str_remove_nl: function(value){
				return (!$.Utils.empty_string(value)) ? value.replace(/[\n\r\t]/g, "") : "";
			},
			isfunction: function(value){
				return ((value != null) && (typeof value == "function"));
			},
			isnumber: function(value){
				return ((value != null) && !isNaN(value));
			},
			isrealnumber: function(value){
				return ((value != null) && (typeof value == "number"));
			},
			iselement: function(value){
				return ($.Utils.isobject(value) && ("tagName" in value) && !$.Utils.empty_string(value.tagName));
			},
			isobject: function(value){
				return ((value != null) && (typeof value == "object"));
			},
			isstring: function(value){
				return ((value != null) && (typeof value == "string"));
			},
			isboolean: function(value){
				return ((value != null) && (typeof value == "boolean"));
			},
			isdocument: function(value){
				return ((value != null) && (value == document));
			},
			iswindow: function(value){
				return ((value != null) && (value == window));
			},
			isscreen: function(value){
				return ((value != null) && (value == screen));
			},
			isevent: function(value){
				return ($.Utils.isobject(value) && ((("UIEvent" in window) && (value instanceof UIEvent)) || (("Event" in window) && (value instanceof Event)) || ("bubbles" in value)));
			},
			isdate: function(value){
				return (value instanceof Date);
			},
			empty_number: function(value){
				return (!$.Utils.isnumber(value));
			},
			empty_string: function(value){
				return ($.Utils.isstring(value)) ? (value == "") : true;
			},
			isemail: function(value){
				return (!$.Utils.empty_string(value) && $.Utils.preg_match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, value));
			},
			isrgb: function(value){
				return (!$.Utils.empty_string(value) && $.Utils.preg_match(/^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\,([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){2}$/, value));
			},
			isarray: function(value){   
				return ($.Utils.isobject(value) && !value.propertyIsEnumerable("length") && $.Utils.isnumber(value.length));
			},
			
			/* These functions are only in the jquery's version */
			isjqueryelement: function(value){
				return (value instanceof $);
			}
		},
		HTML: {
			DisableSelection: function(element){
				try{
					if ($.Utils.iselement(element)){
						if (typeof element.onselectstart != "undefined"){
							/* Save Event */
							if ($.Utils.isfunction(element.onselectstart) && (element.onselectstart.toString() != "function(jquerydssTTT990dddd432){return false;}")){
								element.jquery_save_event_onselectstart_TTT990dddd432 = element.onselectstart;
							}
							
							element.onselectstart = function(jquerydssTTT990dddd432){return false;};
						} else if (typeof element.style.MozUserSelect != "undefined"){
							element.style.MozUserSelect = "none";
						} else {
							/* Save Event */
							if ($.Utils.isfunction(element.onmousedown) && (element.onmousedown.toString() != "function(jquerydssTTT990dddd432){return false;}")){
								element.jquery_save_event_onmousedown_TTT990dddd432 = element.onmousedown;
							}
							
							element.onmousedown = function(jquerydssTTT990dddd432){return false;};
						}
					}
				}catch(e){}
			},
			EnableSelection: function(element){
				try{
					if ($.Utils.iselement(element)){
						if (typeof element.onselectstart != "undefined"){
							/* Restore Event */
							element.onselectstart = (("jquery_save_event_onselectstart_TTT990dddd432" in element) && $.Utils.isfunction(element.jquery_save_event_onselectstart_TTT990dddd432)) ? element.jquery_save_event_onselectstart_TTT990dddd432 : null;
							delete element.jquery_save_event_onselectstart_TTT990dddd432;
						} else if (typeof element.style.MozUserSelect != "undefined"){
							element.style.MozUserSelect = "";
						} else {
							/* Restore Event */
							element.onmousedown = (("jquery_save_event_onmousedown_TTT990dddd432" in element) && $.Utils.isfunction(element.jquery_save_event_onmousedown_TTT990dddd432)) ? element.jquery_save_event_onmousedown_TTT990dddd432 : null;
							delete element.jquery_save_event_onmousedown_TTT990dddd432;
						}
					}
				}catch(e){}
			},
			DisablePaste: function(element){
				try{
					if ($.Utils.iselement(element)){
						var oElement = $(element);
						
						oElement.trigger("enablepaste").one("enablepaste", function(){
							oElement.unbind(".paste");
						}).bind("keypress.paste", function(ev){
							if ((ev.ctrlKey == true || ev.metaKey == true) && (ev.which == 118 || ev.which == 86)) return false;
						}).bind("keydown.paste", function(ev){
							if ((ev.ctrlKey == true || ev.metaKey == true) &&  ev.keyCode==86) return false;
						}).bind("click.paste", function(ev){
							if (ev.which != 1) return false;
						});
					}
				}catch(e){}
			},
			EnablePaste: function(element){
				try{
					if ($.Utils.iselement(element)){
						$(element).trigger("enablepaste");
					}
				}catch(e){}
			},
			DisableContextMenu: function(element){
				try{
					if ($.Utils.iselement(element)){
						var oElement = $(element);
						
						oElement.trigger("enablecontextmenu").one("enablecontextmenu", function(){
							oElement.unbind(".contextmenu");
						}).bind("contextmenu.contextmenu", function(ev){
							return false;
						});
					}
				}catch(e){}
			},
			EnableContextMenu: function(element){
				try{
					if ($.Utils.iselement(element)){
						$(element).trigger("enablecontextmenu");
					}
				}catch(e){}
			}
		},
		DOM: {
			IS: {
				position: function(value){
					return (value instanceof $.DOM.Position);
				}
			},
			Position: function(left, top){
				this.top = $.Utils.isrealnumber(top) ? top : 0;
				this.left = $.Utils.isrealnumber(left) ? left : 0;
			}
		}
	});
	
	$.fn.extend({
		DisableSelection: function(){
			return this.each(function(){
				$.HTML.DisableSelection(this);
			});
		},
		EnableSelection: function(){
			return this.each(function(){
				$.HTML.EnableSelection(this);
			});
		},
		ToggleImage: function(source, hover){
			source = $.Utils.trim(source);
			hover = $.Utils.trim(hover);
			
			return this.trigger("removetoggleimage").each(function(){
				var oElement = $(this);
				
				var sSource = (source || $.Utils.trim(oElement.attr("src")));
				var sHover = (hover || $.Utils.trim(oElement.attr("hover")));
				
				if ((sSource != "") && (sHover != "")){
					var sNodeName = this.nodeName.toLowerCase();
					
					if ((sNodeName == "img") ? true : ((sNodeName == "input") && (oElement.attr("type") == "image") ? true : false)){
						oElement.bind("mouseover.toggleimage", function(){
							this.src = sHover;
						}).bind("mouseout.toggleimage", function(){
							this.src = sSource;
						}).one("removetoggleimage", function(){
							oElement.unbind(".toggleimage");
						});
					}
				}
			});
		},
		RemoveToggleImage: function(){
			return this.trigger("removetoggleimage");
		},
		DisablePaste: function(){
			return this.each(function(){
				$.HTML.DisablePaste(this);
			});
		},
		EnablePaste: function(){
			return this.each(function(){
				$.HTML.EnablePaste(this);
			});
		},
		DisableContextMenu: function(){
			return this.each(function(){
				$.HTML.DisableContextMenu(this);
			});
		},
		EnableContextMenu: function(){
			return this.each(function(){
				$.HTML.EnableContextMenu(this);
			});
		}
	});
})(jQuery);
