(function($){
	$.preloadImage = function(image, callback) {
		var self = this;
		this.file = image;
		this.callback = callback;
		this.loaded = false;
		this.aborted = false;
		this.error = false;
		this.image = new Image();
		
		this.cancelEvent = function() {
			if (this.image && this.image.onload) {
				this.image.onload = null;
			}
			if (this.image && this.image.onabort) {
				this.image.onabort = null;
			}
			if (this.image && this.image.onerror) {
				this.image.onerror = null;
			}
			this.image = null;
		};
		
		this.on_load = function() {
			self.loaded = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, [self, "loaded"]);
			}
			self.cancelEvent();
		};
		
		this.on_error = function() {
			self.error = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, [self, "error"]);
			}
			self.cancelEvent();
		};
		
		this.on_abort = function() {
			self.aborted = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, [self, "aborted"]);
			}
			self.cancelEvent();
		};
		
		this.image.src = this.file;
		
		this.autoCheckIntervalId = 0;
		this.autoCheck = function() {
			if (self.loaded || self.aborted || self.error) {
				window.clearInterval(self.autoCheckIntervalId);
				return;
			}
			
			if (self.image && self.image.complete) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, [self, "loaded"]);
				}
				self.cancelEvent();
				return;
			}
			
			if (self.image && (self.image.width || self.image.height)) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, [self,  "loaded"]);
				}
				self.cancelEvent();
				return;
			}
		};
		
		this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
	};
})(jQuery);

(function($){
	
	$.widget("ui.jDefText", {
		options: {
			changedClass: "jDefText_changed"
		},
		_create: function(){
			$.ui.jDefText.globalInit();
			if ( this.element.value === "" ) {
				this.element.value = this.element.defaultValue;
			}
		}
	});
	
	$.extend($.ui.jDefText, {
		updateState: function(elem, event) {
			var el = $(elem),
				obj;
				
			try {
				obj = el.data("jDefText");
			} catch(e) {
				obj = false;
			}
				
			if ( obj ) {
				if ( elem.value == elem.defaultValue ) {
					if ( event.type === "focusin" || event.type === "focus" ) {
						elem.value = "";
					}
					el.removeClass(obj.options.changedClass);
				}
				else if ( elem.value == "" ) {
					if ( event.type === "focusout" || event.type === "blur" ) {
						elem.value = elem.defaultValue;
					}
					el.removeClass(obj.options.changedClass);
				}
				else {
					el.addClass(obj.options.changedClass);
				}
			}
			
			el = obj = null;
		},
		globalInit: function() {
			$.ui.jDefText.globalInit = $.noop;
			$.ui.jDefText._globalInit();
		},
		_globalInit: function() {
			$(".jDefText").live("focus blur keyup", function(event){
				if ( $.nodeName(this, "textarea") || ($.nodeName(this, "input") && (this.type === "text" || this.type === "password")) ) {
					$.ui.jDefText.updateState(this, event);
				}
			});
		}
	});
	
	
	
})(jQuery);


(function($){
	function applyImageFrame(img) {
		var $img = $(img),	p = $img.parent(),
			zIndex = "", offset = $img.offset(), wrapper = $("<div class='frame-image-box'></div>"),
			copyStyles, resetStyles, css, margin, padding, left, top, position, cssFloat, frame = $("<div class='frame'></div>");
			
		position = $img.css("position");
		cssFloat = ($img.css("float") == "left" || $img.css("float") == "right") ? $img.css("float") : "";
			
		wrapper.css({
			width: $img.outerWidth(true),
			height: $img.outerHeight(true),
			position: position,
			display: "inline-block",
			"float": cssFloat
		});
		
		$img.wrap(wrapper).css({
			position: "relative"
		});
		
		wrapper.remove();
		wrapper = $img.parent();
		
		copyStyles = [];
		resetStyles = {
			margin: 0,
			padding: 0,
			border: "none",
			"float": "none",
			position: "relative"
		};
		
		$(["margin", "padding", "border"]).each(function(i, p){
			$(["Top", "Right", "Bottom", "Left"]).each(function(j, k) {
				if ( p == "border" ) {
					$(["Width", "Style", "Color"]).each(function(j, s) {
						copyStyles.push(p + k + s);
					});
				} else {
					copyStyles.push(p + k);
				}
			});
		});
		
		css = {};
		$(copyStyles).each(function(i, p) {
			css[p] = $img.css(p);
		});
		
		css['width'] = $img.width();
		css['height'] = $img.height();
		css['float'] = $img.css("float");
		
		frame.css(css).appendTo(wrapper).append( $img ) ;
		
		$img.css(resetStyles);
		
		
		frame
			.append(
				'<div class="box"><div class="box-inner"><div class="box-top"></div><div class="box-content"></div></div><div class="box-bottom"><div></div></div></div>'
			);
			
		$(".box .box-content", frame).css({
			width: $img.width() - (parseInt($(".box", frame).css("marginLeft"), 10) || 0),
			height: $img.height() - $(".box-bottom", frame).height()
		});
	}
	
	//DOM READY
	$(function(){
		$("input.jDefText").jDefText();
		//$("img.frame-image").each(function(){
		//	applyImageFrame(this);
		//});
		
	});
})(jQuery);
