﻿(function($) {

$.widget("ui.pricebox", {
	_timer: null,

	_init: function() {
		var self = this;
		this.element.parents('dl:first')
			.mouseout(function(ev) { self._onMouseOut(ev); })
			.mouseover(function(ev) { self._onMouseOver(ev); });
	},

	show: function() {
		this.element.removeClass(this.options.classHidden);
	},

	hide: function() {
		this.element.addClass(this.options.classHidden);
	},

	_onMouseOut: function(e) {
		var self = this;
		if (!this.element.is('.' + this.options.classHidden))
			this._timer = setTimeout(function() { self.hide() }, this.options.hideDelay);
	},

	_onMouseOver: function(e) {
		if (this._timer) {
			clearTimeout(this._timer);
			this._timer = null;
		}
	}
});

$.ui.pricebox.defaults = {
	classHidden: "hidden",
	hideDelay: 3000
};

$.widget("ui.gallery", {
	_timer: null,

	_init: function(element) {
		var self = this;
		this.element
			.mouseout(function(ev) { self._onMouseOut(ev); })
			.mouseover(function(ev) { self._onMouseOver(ev); });
	},

	_setImage: function(element) {
		var url = $(element || this.element).attr('url');
		if (url)
			$(this.options.productImgElement).attr('src', url);
	},

	_onMouseOut: function(ev) {
		var self = this;
		this._timer = setTimeout(function() { self._setImage(null); }, this.options.hideDelay);
	},

	_onMouseOver: function(ev) {
		this._setImage(ev.target);
		if (this._timer) {
			clearTimeout(this._timer);
			this._timer = null;
		}
	}
});

$.ui.gallery.defaults = {
	productImgElement: "#imgProduct",
	hideDelay: 3000
};

$.widget("ui.ajaxUpdater", {
	_init: function() {
		this._template = $.template(this.options.url);
	},

	invoke: function(parameters) {
		if (!this.options.hideLoader)
			this.element.addClass(this.options.classAlpha).ajaxIndicator({className: this.options.classLoader});
		var self = this;
		$.ajax({
			type: "GET",
			url: this._template.evaluate(parameters),
			cache: false,
			complete: function(request, status) {
				if (!self.options.hideLoader)
					self.element.removeClass(self.options.classAlpha).ajaxIndicator('destroy');
				if (request.status == 200)
					self.element.html(request.responseText);
				else
					self.element.addClass(self.options.classAlpha).ajaxIndicator({className: self.options.classError});
			}
		});
	}
});

$.ui.ajaxUpdater.defaults = {
	url: null,
	hideLoader: false,
	classAlpha: "alpha40",
	classLoader: "icon-loader",
	classError: "icon-error"
};

var TabManager = function(tabs) {
	this.tabs = tabs;
	this.switchTab();
	var self = this;
	setInterval(function() {
		if (self._tabId != document.location.hash)
			self.switchTab();
	}, 20);
}

TabManager.prototype = {
	_tabId: null,
	_prefix: "div",
	_classWrite: "write",
	_classActive: "active",
	_attributeId: "tabid",

	switchTab: function() {
		var id = (this._tabId = document.location.hash).toLowerCase();
		var tabReview = $('#' + this._prefix + "TabReviews");
		if (id == "#writereview") {
			id = "#tabreviews";
			tabReview.addClass(this._classWrite);
		} else
			tabReview.removeClass(this._classWrite);
		var defaultTab = null;
		var hasSelectedTab = false;
		for (var i = 0; i < this.tabs.length; i++) {
			var tab = $(this.tabs[i]);
			with (this.configureTab(id, tab)) {
				hasSelectedTab |= selected;
				if (isDefault) defaultTab = tab;
			}
		}
		if (!hasSelectedTab && defaultTab)
			this.configureTab(null, defaultTab);
	},

	configureTab: function(id, tab) {
		var isDefault = tab.attr("default");
		var tabPage = $('#' + this._prefix + tab.attr(this._attributeId));
		var result = {
			selected: tabPage.attr('id').replace(this._prefix, "#").toLowerCase() == id || !id && isDefault,
			isDefault: isDefault
		};
		if (result.selected) {
			var updater = tabPage.attr('updater');
			if (updater) {
				$('#' + updater).ajaxUpdater('invoke', {});
				tabPage.removeAttr('updater');
			}
			tabPage.show();
			tab.addClass(this._classActive);
		} else {
			tabPage.hide();
			tab.removeClass(this._classActive);
		}
		return result;
	}
};

$.extend({
	tabManager: function(tabs) {
		return new TabManager(tabs);
	}
});

})(jQuery);

function setQtyBox(cb) {
	$('#qty_' + $(cb).val()).disabled(!cb.checked).val(cb.checked ? '1' : '0');
}
