(function($) {
	//
	// quickview plugin definition
	//
	$.fn.quickview = function(options) {
		//debug(this);
		// build main options before element iteration
		var opts = $.extend({}, $.fn.quickview.defaults, options);
		// iterate and reformat each matched element
		return this.each(function() {
			if (opts.progressiveParam != null && opts.progressiveParam != '') {
				// add parameter to make it behave differently if javascript isn't enabled
				var quickviewLinkElement = $(this).is('a') ? $(this) : $(this).find('a').eq(0);
				quickviewLinkElement.attr('href',quickviewLinkElement.attr('href')+'&'+opts.progressiveParam+'=true');
			}
			$(this).click(function(event) {
				event.preventDefault();
				var quickviewUrl = $(this).is('a') ? $(this).attr('href') : $(this).find('a').eq(0).attr('href');
				$.ajax({
					url: quickviewUrl,
					success: function(html) {
						// Destroy any previous quick view instances.
						$(opts.selector).dialog('destroy').remove();
						// Add the response to the body, hidden so that it's not shown until the dialog pops up.
						$(html).css('display','none').appendTo(document.body);
						// Initialize the event handlers and then create and popup the dialog.
						$('#tabs').tabs();
						if ($('#product-images ul li').size() > 1 || $('#add-to-cart .item-price').size() > 1) {
							$('#add-to-cart select[name=attrValue]').change(function() {
								$('#product-images ul li').hide();
								$(opts.selector).find('span.info').hide();
								var selectedCatentryId = '#'+$(this).find('option:selected').eq(0).attr('class');
								$(selectedCatentryId + '-price').show();
								if ($(selectedCatentryId).size() > 0) {
									$(selectedCatentryId).fadeIn();
								}
								else if ($('#product-images ul li').size() > 1) {
									$('#product-image').fadeIn();
								}
								else {
									$('#product-image').show();
								}
							});
							$('#add-to-cart select[name=attrValue]').change();
						}
						$('#add-to-cart').submit(function(event) {
							event.preventDefault();
							var $this = $(this);
							$this.find('.return-message, .success-links').hide();
							$this.find('.loading-message').fadeIn();
							$this.find('#addtocart').css('visibility','hidden');
							$.post($this.attr('action'), $this.serialize(), function(html) {
								$this.find('.loading-message').hide();
								$this.find('.return-message').html(html);
								var containsErrors = $this.find('.error').size() > 0;
								if (containsErrors && opts.refresh) {
									$this.find('.return-message, .success-links').fadeIn();
									var closeFunction = function(event) {
										event.preventDefault();
										$(this).parents(opts.selector).dialog('destroy').remove();
										location.reload();
									};
									$this.find('.success-links a.quickview-close').click(closeFunction);
									$(opts.selector).bind('dialogclose',closeFunction);
								}
								else if (opts.refresh) {
									location.reload();
								}
								else {
									$this.find('.return-message, .success-links').fadeIn();
									$this.find('.success-links a.quickview-close').click(function(event) {
										event.preventDefault();
										$(this).parents(opts.selector).dialog('destroy').remove();
									});
									$this.find('#addtocart').css('visibility','visible');
									// try to do an ajax refresh on the minicart
									try {
										miniCart.loadCart(miniCart)();
									}
									catch(ex) {}
								}
							});
						});
						$(opts.selector).dialog({maxHeight: opts.height, maxWidth: opts.width, height: opts.height, width: opts.width, resizable: false, modal:true});
					}
				});
			});
		});
	};
	//
	// private function for debugging
	//
	function debug($obj) {
		if (window.console && window.console.log)
		window.console.log('quickview selection count: ' + $obj.size());
	};
	//
	// plugin defaults
	//
	$.fn.quickview.defaults = {
		width: 535,
		progressiveParam: 'ajax',
		selector: 'div.quickview',
		refresh: false
	};
})(jQuery);
