(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($){
	var num = function(el, props) {
		var r = 0;
		$.each(props.split(/\s+/) || [], function(i,v){
			r += parseInt($(el).css(v)) || 0;
		});
		return r;
	}
	
	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()
		});
	}
	
	function linkImageBox(el) {
		var a = $("a:eq(0)", el), 
			href = a.attr("href") || "", 
			title = a.attr("title") || "",
			classes = [],
			attrs = {}, attrs_text;
		
		if ( a.length ) {
			classes.push('imageBoxLink');
			
			$.each(a[0].className.split(/ *, */g), function(i, c) {
				if ( !/^imageBox/.test(c) ) {
					classes.push(c);
				}
			});
			
			classes = classes.join(" ") || "";
			
			//
			for ( var i = 0, k = a[0].attributes, n = k.length; i < n; i++ ) {
				attrs[k[i].nodeName] = k[i].nodeValue;
			}
			
			attrs_text = [];
			
			for ( var at in attrs ) {
				if ( !at.match(/^(class|title|href)$/g) ) {
					attrs_text.push(at + '="' + attrs[at] + '"');
				}
			}
			
			attrs_text = attrs_text.join(" ") || "";
			
			link = $(el).append('<a href="' + a[0].href + '" title="'+ title +'" class="'+ classes +'" '+ attrs_text +'></a>');
		}
	}
	
	var IEShadows = null;
	if ( $.browser.msie && $.browser.version < 10) {
	
		
		
		var IE_SHADOWS = function() {}
		
		$.extend(IE_SHADOWS.prototype, {
			init: function() {
				var arrs = [], selector;
			
				$.each("green, red, recipe_red, purple, black, gold, slate, gray, brown".split(/ *, */g), function(i, n) {
					arrs.push(".shadow-" + n + "-a");
					arrs.push(".shadow-" + n + "-b");
				});
				
				selector = arrs.join(",");
				
				this.elements = $(selector);
				
				for ( var i = 0, n = this.elements.length; i < n; i++ ) {
					this.fix(this.elements[i]);
				}
			},
			
			fix: function(element) {
				var el = $(element), 
					fixed = el.data("IE_SHADOWS_FIXED"),
					regpos = /^(relative|absolute)$/,
					regclass = /^(shadow\-(.+)\-(.+))$/,
					classes = [],
					p, 
					w1, w2, h1, h2;
				
				if ( fixed ) {
					//
				} else {
					p = el.parent();
					
					$.each( el[0].className.split(/\s+/g), function(i, c) {
						if ( regclass.test(c) ) {
							classes.push("ie_filter-" + c);
						} else {
							classes.push(c);
						}
					});
					
					$.each([p, el], function(i, elem) {
						var css = {}, $elem = $(elem);
						
						if ( "inline" == ($elem.css("display") || "inline") ) {
							css.display = "inline-block";
						}
						
						if ( !regpos.test($elem.css("position")) ) {
							css.position = "relative";
						}
						
						$elem.css(css);
					});
					
					position = el.position();
					w1 = el.outerWidth();
					h1 = el.outerHeight();
					
					clone = el.clone(false).insertBefore(el).addClass("ie_filter-shadow-clone").css("position", "absolute");
					clone[0].className = classes.join(" ");
					
					w2 = clone.outerWidth();
					h2 = clone.outerHeight();
					
					position.position = "absolute";
					position.left = position.left - ((w2 - w1) / 2);
					position.top = position.top - ((h2 - h1) / 2);
					
					clone.css(position);

					el.data("IE_SHADOWS_FIXED", true);
					
				}
			},
			
			posChilds: function(el) {
				var pos = el.css("position") || "", position, display = "", self = this;
				
				if ( pos !== "absolute" && pos !== "relative" ) {
					el.css("position", "relative");
				}
				
				/*
				display = el.css("display") || "inline";
				if ( display == "inline" ) {
					el.css("display", "inline-block");
				}
				*/
				
				el.css("zoom",1);
				
				
				el.children().each(function(){
					var child = $(this);
					if ( child.attr("clonethis") == 1 ) {
						position = child.position();
						
						child.css({
							left: position.left + num(child, "marginLeft marginRight"),
							top: position.top + num(child, "marginTop marginBottom")
						});
						
						
						self.posChilds(child);
						
					} else {
						child.remove();
					}
				});
				
				self = null;
			},
			
			_findSuitableParent: function(el) {
				var ret = el.parent();
				
				while ( ret.length && ret[0].nodeName !== "BODY" ) {
					var pos = ret.css("position") || "",
						overflow = ret.css("overflow") || "";
					
					if ( (pos == "relative" || pos == "absollute") && overflow !== "hidden" ) {
						return ret;
					}
					
					ret = ret.parent();
				}
				
				return $("body");
			}
		});
		
		IEShadows = new IE_SHADOWS;
	}

	
	//DOM READY
	$(function(){
		$("input.jDefText").jDefText();
		$("img.frame-image").each(function(){
			applyImageFrame(this);
		});
		
		$(".imageBox, .imageRecipesBox").each(function(){
			linkImageBox(this);
		});
		
		$(".layout2colsRomanticGetaway").each(function(){
			var cols = $(".col", this), maxH = 0, h = 0;
			cols.each(function() {
				h = $(this).height();
				maxH = Math.max(h, maxH);
			});
			
			cols.height(maxH);
		});
		
		$(".form-memory_shoppe").parent().addClass("form-memory_shoppe-container");
		
		if ( $.browser.msie && $.browser.version < 10) {
			IEShadows.init();
		}
		
		 $("#datepicker").datepicker({ 
					 minDate: 0, 
					 showAnim: 'fadeIn',
					 altField: '#zmonth', 
					 altFormat: 'mm',
					 onClose: function(dateText,picker) {
								 $('#zday').val( dateText.split(/\//)[1] );
								 $('#zyear').val( dateText.split(/\//)[2] );
							  }
				});

		$(".tooltip").tipTip();

	});
})(jQuery);
