(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);
				}
			});
		}
	});
	
	
	//MAIN SLIDES
	$.widget("ui.mainSlides", {
		options: { 
			autoRun: false,
			autoRun_timeout: 5000
		},
		
		_create: function(){
			
			this.slidesHolder = $(".imageHolder", this.element);
			this.thumbsHolder = $(".thumbsHolder", this.element);
			
			this._initCarousel();
			this._initSlides();
			
			this.showFirstSlide();
		},
		
		_initCarousel: function() {
			this.thumbsClipper = $(".thumbs", this.thumbsHolder);
			this.thumbsContainer = $("ul", this.thumbsClipper);
			this.thumbs = $("li", this.thumbsContainer);
			
			if ( this.thumbs.length ) {
			
				this.thumbW = $(this.thumbs.get(0)).outerWidth(true);
				
				this.thumbsContainer.css({
					width: this.thumbW * this.thumbs.length
				});
				
				this.thumbsClipperW = this.thumbsClipper.width();
				this.thumbsStep = Math.floor(this.thumbsClipperW / this.thumbW);
				
				this.curIdx = 0;
				
				this._initArrows();
				this._initArrowsState();
			}
		},
		
		_initArrowsState: function() {
			$("a.prev", this.element).css({
				display: ( this.curIdx > 0 ) ? "block" : "none"
			});
			
			$("a.next", this.element).css({
				display: ( this.curIdx < this.thumbs.length - this.thumbsStep ) ? "block" : "none"
			});
		},
		
		_initArrows: function() {
			$("a.prev, a.next", this.element).bind("click", $.proxy(this._onArrowClick, this));
		},
		
		_onArrowClick: function(event) {
			var target = $(event.target).closest("a");
			
			if ( target.is(".prev") ) {
				this._movePrev();
			} else {
				this._moveNext();
			}
			
			return false;
		},
		
		_movePrev: function() {
			this.moveTo(this.curIdx - this.thumbsStep);
		},
		
		_moveNext: function() {
			this.moveTo(this.curIdx + this.thumbsStep);
		},
		
		moveTo: function(idx) {
			if ( idx < 0 ) {
				idx = 0;
			}
			
			if ( idx > this.thumbs.length - this.thumbsStep ) {
				idx = this.thumbs.length - this.thumbsStep;
			}
			
			if ( idx !== this.curIdx ) {
				this.curIdx = idx;
				this._initArrowsState();
				this.thumbsContainer.stop().animate({left: -idx * this.thumbW}, {queue:false, duration:1200, easing:"easeOutExpo"});
			}
		},
		
		
		_hide: function(el) {
			if ( el.is(":visible") ) {
				el.stop().animate({opacity:0}, {queue:false, duration:1200, complete:function(){
					$(this).hide();
				}});
			}
		},
		
		_show: function(el) {
			var opacity = el.css("opacity") || "";
			
			if ( opacity == "" || opacity > 0.99 ) {
				el.css("opacity", 0);
			}
			
			if ( !el.is(":visible") ) {
				el.show();
			}
			
			el.stop().animate({opacity:1}, {queue:false, duration:1200, complete:function(){
				$(this).css("opacity","");
				if ( $.browser.msie ) {
					$(this).find("div").css("opacity","");
				}
			}});
			
		},
		
		_initSlides: function() {
			if ( this.thumbs.length ) {
				this.curSlide = this.oldSlide = -1;
				this.loadingSlide = false;
				this.loader = $(".loading", this.slidesHolder);
				this.slides = new Array(this.thumbs.length);
				
				this.loader.hide();
				
				$("a", this.thumbs).bind("click", $.proxy(this._onThumbClick, this));
			}
		},
		
		_onThumbClick: function(event) {
			var target = $(event.target).closest("a"),
				li = target.closest("li"),
				idx = this.thumbs.index(li), href;
				
			href = target.get(0).href || "";
			
			if ( href && href.match(/\.(jpg|png|gif)$/i) ) {
				this.showSlide(idx);
				return false;
			}
		},
		
		showFirstSlide: function() {
			for ( var i = 0; i < this.thumbs.length; i++ ) {
				var href = $("a", this.thumbs[i]).get(0).href || "";
				
				if ( href && href.match(/\.(jpg|png|gif)$/i) ) {
					this.showSlide(i);
					break;
				}
			}
		},
		
		showSlide: function(idx) {
			var oldIdx, a;
			
			if ( idx < 0 ) {
				idx = 0;
			}
			
			if ( idx > this.thumbs.length - 1 ) {
				idx = this.thumbs.length - 1;
			}
			
			if ( idx !== this.curSlide ) {
				oldSlide =  this.curSlide;
				
				if ( typeof this.slides[idx] !== "undefined" && this.slides[idx].length ) {
					this.curSlide = idx;
					this.oldSlide = idx;
					
					this._hide(this.loader);
					
					if ( typeof this.slides[oldSlide] !== "undefined" && this.slides[oldSlide].length ) {
						this._hide(this.slides[oldSlide]);
					}
					
					this._show(this.slides[idx]);
					
					this.thumbs.removeClass("current");
					$(this.thumbs[idx]).addClass("current");
					
				} else {
					if ( typeof this.slides[oldSlide] !== "undefined" && this.slides[oldSlide].length ) {
						this.oldSlide = oldSlide;
					}
					this.curSlide = idx;
				
					a = $("a", this.thumbs[idx]).get(0).href || "";
					
					if ( a && a.match(/\.(jpg|png|gif)$/i) ) {
						this._loadSlide(a, idx);
						
					}
					
				}
			}
		},
		
		_loadSlide: function(src, idx) {
			var self = this;
			this._show(this.loader);
			
			new $.preloadImage(src, function(obj, status) {
				if (status == "loaded") {
					self._slideLoaded.call(self, obj, idx);
					self = null;
				}
			});
		},
		
		_slideLoaded: function(obj, idx) {
			var newSlide = $("<div class='slide'><img src='" + obj.file + "' alt='' /></div>").prependTo(this.slidesHolder).hide(),
				thumb = $(this.thumbs[idx]),
				rel = $("a", thumb).attr("rel") || "";
				
			if ( rel !== "" ) {
				newSlide.append("<a href='" + rel + "' class='slideLink'></a>");
			}
			this.slides[idx] = newSlide;
			
			if ( idx == this.curSlide ) {
				this._hide(this.loader);
				
				if ( typeof this.slides[this.oldSlide] !== "undefined" && this.slides[this.oldSlide].length ) {
					this._hide(this.slides[this.oldSlide]);
				}
				
				this._show(this.slides[this.curSlide]);
				
				this.thumbs.removeClass("current");
				$(this.thumbs[this.curSlide]).addClass("current");
			}
		},
		
		destroy: function() {
			$(".slide>a", this.element).removeData("mainSlides_contenPos");
			$.Widget.prototype.destroy.apply( this, arguments );
		}
	});
})(jQuery);


(function($){
	
	function IE_CreateShadow(img) {
		var $img = $(img),	p = $img.parent(),
			zIndex = "", offset = $img.offset(), wrapper = $("<div class='IEShadowWrapper'></div>"),
			margin;
			
		wrapper.css({
			width: $img.outerWidth(true),
			height: $img.outerHeight(true),
			position: "relative",
			display: "inline-block",
			styleFloat: ($img.css("styleFloat") == "left" || $img.css("styleFloat") == "right") ? $img.css("styleFloat") : ""
		});
		
		$img.wrap(wrapper).css({
			position: "relative"
		});
		
		margin = $img.css("marginTop") + " " || "0px ";
		margin += $img.css("marginRight") + " " || "0px ";
		margin += $img.css("marginBottom") + " " || "0px ";
		margin += $img.css("marginLeft") || "0px";
		//console.log($img.css("margin"));
		
		//console.log(margin);
		
		$img.parent().prepend("<div class='IEShadow' style='width:" + $img.outerWidth() + "px;height:" + $img.outerHeight() + "px;margin:" + margin + "'></div>");
	}
	
	//DOM READY
	$(function(){
		$("input.jDefText").jDefText();
		
		$("#slideShow").mainSlides();
		
		if ( $.browser.msie && $.browser.version < 9 ) {
			$(".entry-content img").each(function(){
		//		IE_CreateShadow(this);
			});
			
			$(".widget_banner_box .wrapper > img").each(function(){
				IE_CreateShadow(this);
			});
		}
	});
})(jQuery);
