(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),
				buttons = slides.find("a:eq(0)"),
				container = $(".slides-contents", this.element).scrollTop(0);
				
			for ( var i = 0, n = buttons.length; i<n; i++ ) {
				var btn = $(buttons[i]),
					href = btn.attr("href").replace("#", "") || "",
					content = (href == "") ? false : $("#" + href);
					
				if ( content && content.length ) {
					btn.data("mainSlides_contenPos", (content.position()).top);
				} else {
					btn.data("mainSlides_contenPos", false);
				}
			}
			
			buttons.bind("click.mainSlides", $.proxy(this._click, this));
			
			this.slides = slides;
			this.container = container;
			this.slides.filter(":eq(0)").addClass("slide-active");
			
			if ( this.options.autoRun === true && this.options.autoRun_timeout > 0 ) {
				this._setupAutoRun();
			}
		},
		
		_clearAutoRun: function() {
			if ( typeof this.autoRun_interval !== "undefined" && this.autoRun_interval !== null ) {
				window.clearInterval(this.autoRun_interval);
				this.autoRun_interval = null;
			}
		},
		
		_setupAutoRun: function() {
			var self = this;
			this.autoRun_interval = window.setInterval(function(){
				self.nextSlide();
			}, this.options.autoRun_timeout);
		},
		
		_click: function(event) {
			var target = $(event.target).closest("a"),
				slide = target.closest(".slide"),
				pos = target.data("mainSlides_contenPos");
			
			if ( pos !== false && !slide.is(".slide-active") ) {
				this._clearAutoRun();
				
				this.slides.removeClass("slide-active");
				slide.addClass("slide-active");
				this.container.stop().animate({scrollTop: pos}, {queue:false, duration:800, easing:"easeOutExpo"});
				
				if ( this.options.autoRun === true && this.options.autoRun_timeout > 0 ) {
					this._setupAutoRun();
				}
			}
			
			target = slide = null;
			$(event.target).blur();
			return false;
		},
		
		nextSlide: function() {
			if (this.slides.filter(".slide-active").next().length) {
				this.slides.filter(".slide-active").next().find("a:eq(0)").trigger("click");
			} else {
				this.slides.filter(":eq(0)").find("a:eq(0)").trigger("click");
			}
		},
		
		destroy: function() {
			$(".slide>a", this.element).removeData("mainSlides_contenPos");
			$.Widget.prototype.destroy.apply( this, arguments );
		}
	});
	
	
	//require jQuery QuickFlip plugin (v2.1.1)
	//http://jonraasch.com/blog/quickflip-2-jquery-plugin
	$.widget("ui.flipper", {
		options: {
			autoRun: false,
			autoRun_timeout: 5000
		},
		
		_create: function() {
			var childs = this.element.children().addClass("flipper-content"), self = this, H = 0;
			
			for ( var i = 0, n = childs.length; i < n; i++ ) {
				H = Math.max($(childs[i]).show().outerHeight(true), H);
				
				$(".flipper-to",  childs[i])
					.each(function() {
						var 
							href = $(this).attr("href").replace("#", "") || "",
							target = (href == "") ? false : $("#" + href),
							targetIdx = childs.index(target);
						
						if ( target && target.length && targetIdx >= 0 ) {
							self._init_trigger(this, i, targetIdx);
							//new _quickflipper_trigger(this.element, $(this), i, targetIdx);
						} else {
							$(this).addClass("flipper-to-disabled");
						}
					})
					.bind("click.flipper", $.proxy(this.flip, this));
				
				$(".flipper-back",  childs[i])
					.each(function() {
						self._init_trigger(this, i, -1);
					})
					.bind("click.flipper", $.proxy(this.flipBack, this));
			}
			
			this.element.css("height", H);
			
			if  ( this.options.autoRun && this.options.autoRun_timeout > 0 ) {
				this._setup_autoRun();
			}
			
			this.element.quickFlip();
			
			self = null;
		},
		
		_setup_autoRun: function() {
			var self = this;
			
			this.currentIndex = 0;
			this.maxIndex = this.element.children().length;
			
			if ( this.maxIndex > 0 ) {
				this.autoRun_interval = window.setInterval( function(){
					var idx = (self.currentIndex + 1) % self.maxIndex;
					
					self.element.quickFlipper({}, idx);
					self.currentIndex = idx;
					
				}, this.options.autoRun_timeout );
			}
		},
		
		_init_trigger: function(elem, idx, targetIdx) {
			var el = $(elem);
			
			el.data("flipper_idx", idx);
			el.data("flipper_targetIdx", targetIdx);
			this.element.children(targetIdx).data("flipper_queue", []);
			
			el = null;
		},
		
		flip: function(event) {
			var el = $(event.target).closest(".flipper-to");
			
			if ( !el.hasClass(".flipper-to-disabled") ) {
				var idx = el.data("flipper_idx"),
					targetIdx = el.data("flipper_targetIdx"),
					queue = this.element.children(targetIdx).data("flipper_queue");
					
				queue.push(idx);
				this.element.quickFlipper({}, targetIdx);
			}
			
			return false;
		},
		
		flipBack: function(event) {
			var el = $(event.target).closest(".flipper-back"),
				idx = el.data("flipper_idx"),
				queue = this.element.children(idx).data("flipper_queue");
				
			if ( $.isArray(queue) && queue.length ) {
				i = queue.pop();
				if ( i >= 0 ) {
					this.element.quickFlipper({}, i);
				}
			}
			
			return false;
		}
	});
	
})(jQuery);


(function($){
	//DOM READY
	$(function(){
		$(".mainFeatures-slides").mainSlides({
			autoRun: true,
			autoRun_timeout: 6000
		});
		
		$(".autoflipper").flipper({
			autoRun: true,
			autoRun_timeout: 6000
		});
		
		$("input.jDefText").jDefText();
	});
})(jQuery);
