(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);
				}
			});
		}
	});
	
	
	//MAIN SLIDES
	$.widget("ui.mainSlides", {
		options: { 
			autoRun: false,
			autoRun_timeout: 5000
		},
		
		_create: function(){
			var 
				slides = $(".slide", this.element);
				
			if ( slides.length > 1 && this.options.autoRun === true && this.options.autoRun_timeout > 0 ) {
				this.slides = slides;
				this.currentIdx = 0;
				this._setupAutoRun();
			}
		},
		
		_clearAutoRun: function() {
			if ( typeof this.autoRun_timeout !== "undefined" && this.autoRun_timeout !== null ) {
				window.clearTimeout(this.autoRun_timeout);
				this.autoRun_timeout = null;
			}
		},
		
		_setupAutoRun: function() {
			var self = this;
			
			this.autoRun_timeout = window.setTimeout(function(){
				self.nextSlide.call(self);
				self._setupAutoRun.call(self);
				self = null;
			}, this.options.autoRun_timeout);
		},
		
		_hide: function(elem) {
			if ( elem.is(":visible") ) {
				elem.stop().animate({opacity:0}, {queue:false, duration:800, complete: function(){
					$(this).css({
						opacity:"",
						display:"none"
					});
				}});
			}
		},
		
		_show: function(elem) {
			var opacity = elem.css("opacity") || 1;
			
			
			if ( !elem.is(":visible") && (opacity == "" || opacity >= 1) ) {
				elem.css("opacity",0);
			}
			
			elem.stop().show().animate({opacity:1}, {queue:false, duration:800, complete: function(){
				$(this).css({
					opacity:"",
					display:"block"
				});
			}});
		},
		
		
		nextSlide: function() {
			var idx = this.currentIdx + 1;
			if ( idx > this.slides.length-1 ) {
				idx = 0;
			}
			
			this._hide($(this.slides[this.currentIdx]));
			this._show($(this.slides[this.currentIdx = idx]));
		},
		
		destroy: function() {
			this._clearAutoRun();
			$.Widget.prototype.destroy.apply( this, arguments );
		}
	});
	
})(jQuery);


(function($){
	//DOM READY
	$(function(){
		$(".mainSlideShows").mainSlides({
			autoRun: true,
			autoRun_timeout: 6000
		});
		
		$("input.jDefText").jDefText();
	});
})(jQuery);
