/**
 * Catalog.js
 * eFashionSolutions Meteor Library
 * 2008.05.19
 *
 * Description: Handles the JS for the catalog pages
 * TJ Eastmond <tj.eastmond@gmail.com>
 */

EFS.Quicklook = {
	open : function(element, parent) {
			var left = ((parent.getWidth() / 2) - (element.getWidth() / 2));
			if (left < 0) left = 0;
			var bottom = parent.getHeight() - element.getHeight();
			element.setStyle({ display : '', left : left + 'px', top : bottom + 'px' });
	},

	close : function(qid) { $(qid).setStyle({ display : 'none' }); },

	purchase : function(productid, color, size, quantity, optional) {
		var vars = 'productid=' + productid + '&color=' + color + '&size=' + size + '&quantity=' + quantity + '&optional=' + optional;
		new Ajax.Request(minicart.options.ajaxURL, {
			method: 'post',
			parameters : vars,
			onSuccess: function(t) {
				ModalIFrame.close();
				minicart.content(t.responseText);
				minicart.openClose.delay(.6);
				pars = {};
				EFS.doAjax('/updateminicart', $H(pars).toJSON(), minicart.updateWidget);

			}
		});
	}
};

EFS.Catalog = {

	init : function() {

		$$('.detailSwatch').each(function(element) {
			var parts = element.id.split("_");
			if (jsonPage[parts[0]]) {
				var productData = jsonPage[parts[0]]['colors'][parts[1]];
				element.onmouseover = function() {
					$(parts[0]).src = productData['images']['tx'];
					$("link_" + parts[0]).href = productData['link'];
					element.style.cursor = 'pointer';
				};
				element.onclick = function() { window.location = productData['link']; };
			}
		});
if (true) {
		//if (!Prototype.Browser.IE6) {
			$$('.catalogPage_thumb').each(function(element) {
				var pid = element.id.replace('container_', '');
				var qid = $('quick_' + pid);
				var aid = $('quick_' + pid + '_link');
				//qid.setStyle({ width : $(pid).getWidth() + 'px' });
				var parent = qid.getOffsetParent();
				Event.observe(element, 'mouseover', function(e) {
					if (mouseLeaveEnter(e, this)) {
						qid.setStyle({ width : $(pid).getWidth() + 'px' });
						if (Prototype.Browser.IE6) {
							aid.setStyle({ width : ($(pid).getWidth() - 20) + 'px' });
							if (element.getWidth() > $(pid).getWidth()) {
								qid.setStyle({ marginLeft : '-' + ((element.getWidth() - $(pid).getWidth())/2) + 'px' });
							}
						}
						EFS.Quicklook.open(qid, parent);
					}
				});
				Event.observe(element, 'mouseout', function(e) { if (mouseLeaveEnter(e, this)) { EFS.Quicklook.close(qid); } });
			});
		}

		$$('select.catalogPageNumbers_select').each(function(element) {
			element.onchange = function() { window.location = element.value;	};
		});

		if (typeof(Shadowbox) != 'undefined') {
			Shadowbox.init({'enableKeys': false, 'modal': false});
		}
	}
};

EFS.onDomReady(EFS.Catalog.init);