(function($) {

     var SKU_PHOTOS;

     $(function() {
	 SKU_PHOTOS = $('table.skus td.photo a');
	 zoom('div.product-detail div.photo a', product_zoom(first_sku_photo));
	 zoom(SKU_PHOTOS, product_zoom(clicked_sku_photo));

	 info($('div.product-detail div.info'));

	 $('div.related div.slider').each(function() {
	     related_slider($(this), 1);
	 });
     });

     function info($info) {
	 $info.tabs();
	 $info.find('a.designed-by').click(function(e) {
	     e.preventDefault();
	     $info.tabs('select', 1);
	 });
     }

     function first_sku_photo() {
	 return SKU_PHOTOS.get(0).href;
     }

     function clicked_sku_photo() {
	 return this.href;
     }

     function product_zoom(get_big_href) {
	 var w = 800, h = 630;
	 return on_zoom(modal('#modal', w, h, 'product-detail'), vertical_slider, get_big_href);
     }

     function vertical_slider(items, index) {
	 return slider(make_slider(items), 3, 139, index, 'marginTop', 'height');
     }

     function horizontal_slider(items, index) {
	 return slider(make_slider(items), 3, 139, index);
     }

     function related_slider(items, index) {
	 return slider(items, 4, 84, index);
     }

     function on_zoom(modal, slider, get_big_href) {
	 return function() {
	     var big_href = get_big_href.call(this);

	     if (modal.data('dialog').isOpen) {
		 replace_image(modal.find('div.big-image img'), big_href);
	     }
	     else {
		 var item_list = $('table.skus td.photo a'),
		     items = item_list.clone(true).addClass('photo-item'),
 		     big = $('<img src="' + big_href + '" />').wrap('<div class="big-image"/>').parent(),
		     wrap = big.wrap('<div class="wrap"/>').parent().append(items);

		 modal.html(wrap);
		 items = modal.find('a.photo-item').wrap('<div class="item"/>').parent().wrapAll('<div class="item-list"/>').parent();
		 slider(items, item_list.index(this) + 1);
		 modal.dialog('open');
	     }
	 };
     }

})(jQuery);