﻿(function($) {

$.widget("ui.popOver", {
	_init: function() {
		var self = this;
		this.element
			.mouseover(function(ev) { self._onMouseOver(ev); })
			.mouseout(function(ev) { self._onMouseOut(ev); });
		if (this.options.preload)
			this._beginLoad(this.element, true);
	},

	_showContent: function(container) {
		this.element[this.element.offset().left > $(document.body).width() / 2 ? 'addClass' : 'removeClass'](this.options.classRight);
		$(document).find("select").each(function() {
			$(this).blur();
		});
		this._hideContent();
		if (!this.options.url || container.attr("loaded"))
			this._showOverlay(container.css('zIndex', '999').addClass(this.options.classOver));
		$.ui.popOver._opened = container;
	},

	_hideContent: function() {
		if ($.ui.popOver._opened) {
			$.ui.popOver._opened.css('zIndex', '0').removeClass(this.options.classOver).find('iframe').hide();
			$.ui.popOver._opened._opened = null;
		}
	},

	_showOverlay: function(container) {
		if (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7) {
			var content = container.find("." + this.options.classContent);
			if (content.length) {
				var iframes = container.find("iframe");
				if (iframes.length) {
					iframes.show();
				} else {
					var offset = content.offset();
					$("<iframe />")
						.attr("src", "javascript:'<html></html>';")
						.attr("frameBorder", "0")
						.css({
							left: offset.left + "px",
							top: offset.top + "px",
							width: content.width() + "px",
							height: content.height() + "px",
							position: "absolute",
							zIndex: -999,
							opacity: 0
						})
						.insertBefore(content);
				}
			}
		}
	},

	_beginLoad: function(container, preload) {
		if (!this.options.url || this.element.attr("loading") || this.element.attr("loaded"))
			return;
		var content = $("<div />").addClass(this.options.classContent);
		var loader;
		if (!preload)
			container.ajaxIndicator().addClass(this.options.classInactive);
		container.append(content).attr("loading", true);
		var self = this;
		var template = $.template(this.options.url);
		$.ajax({
			type: "GET",
			url: template.evaluate({context:this.element.attr('context')}),
			cache: false,
			complete: function(request, status) {
				if (!preload)
					container.ajaxIndicator('destroy').removeClass(self.options.classInactive);
				if (request.status == 200) {
					container.attr("loaded", true);
					content.append(request.responseText);
					if (!preload && $.ui.popOver._opened == container)
						self._showContent(container);
				} else
					container.ajaxIndicator({className:'icon-error'}).addClass(self.options.classInactive);
			}
		});
	},

	_onMouseOver: function(ev) {
		var self = this;
		var handler = (function () {
			var container = $(ev.target);
			if (!container.is("." + self.options.classContainer))
				container = container.parents("." + self.options.classContainer + ":first");
			self._beginLoad(container, false);
			if ($.ui.popOver._opened != container)
				self._showContent(container);
		});
		if (this.options.showDelay > 0)
			$.ui.popOver._timerShow = setTimeout(handler, this.options.showDelay);
		else
			handler();
		this._resetHideTimeout();
	},
	
	_onMouseOut: function(event) {
		var self = this;
		$.ui.popOver._timerHide = setTimeout(function() { self._hideContent() }, this.options.hideDelay);
		this._resetShowTimeout();
	},

	_resetShowTimeout: function() {
		if ($.ui.popOver._timerShow) {
			clearTimeout($.ui.popOver._timerShow);
			$.ui.popOver._timerShow = null;
		}
	},

	_resetHideTimeout: function() {
		if ($.ui.popOver._timerHide) {
			clearTimeout($.ui.popOver._timerHide);
			$.ui.popOver._timerHide = null;
		}
	}
});

$.ui.popOver.defaults = {
	preload: false,
	showDelay: 0,
	hideDelay: 300,
	classContainer: "po",
	classContent: "pb",
	classOver: "over",
	classRight: "aright",
	classInactive: "alpha40"
};

})(jQuery);

