$(function() {
	// set toggler defaults
	$.extend($.ui.toggler.defaults, {
		activeClass	: 'active',
		closeClass	: 'close'
	});
	
	// the product container needs a class for different view types
	$('.SI-partial-target').bind('viewchange', function(event, viewType, oldViewType) {
		$(this)
			.removeClass(oldViewType + '-view')
			.addClass(viewType + '-view');
	});
	
	
	
	function init(target) {
		// initialize input hints
		$('input:text', target).hint();
		
		//initialize quickviews
		$('a.quickview', target).quickview();
		
		//meta hovers
		$('div.fade-in .item-meta-hover', target).hide();
		
		$('div.fade-in', target).hover(
			function() {
				$('.item-meta-hover', this).stop(true, true).fadeIn();
			}, 
			function() {
				$('.item-meta-hover', this).fadeOut();
			});
		
		// initialize generic toggles
		$('a.toggle', target).toggler();
		
		// initialize context dialogs
		$('.context-dialog', target).contextDialog(); 
		
		// initialize zebra striping on tables and lists
		$('table tr:even', target).addClass('stripe');
		
		// close wishlist context dialogs when a product gets added
		$('.si-wishlist-add-form', target).bind('addtowishlist', function() {
			$(this).parents('.ui-context-dialog').contextDialog('close');
		});
		
		//product gallery asset image swapping and option name swapping
		$('.si-product', target).bind('optionchange', function(event, option) {
			var $product = $(this),
				$target = $product.find('#opt' + option.id);
			//only replace gallery if new one exists
			if($target.length > 0) {
				$product
				.find('.active-gallery')
				.removeClass('active-gallery')
				.fadeOut(function(){
					$target
						.addClass('active-gallery')
						.fadeIn()
						//trigger click on first image link to load new main image
						.find('a:first img').trigger('click');
				});
			 } 
			 //swap option name
			 if($(event.target).is(':first-child')) {
				$product
					.find('span.swatch-name')
					.empty()
					.html(option.name);
			 }
		});
		
		//KLUDGE: add name of swatch if there is only one color since optionchange event fires too soon
		var $swatches = $('.si-option-swatches a');
		
		if ($swatches.length == 1) {
			$swatches.parents('.prod-purchase')
					.find('span.swatch-name')
					.text($swatches.attr('title'));
		}
		
	
		
		//in-stock/out-of-stock / sale price messaging
		$('div.prod-add-cart.out-of-stock form.si-cart-add-form input').attr('disabled','disabled');
		
		$('.si-product').bind('productchange', function(event, product, productOption) {
			$('div.final-sale', event.target).remove();
			if (productOption.stock <= 0) {
				$('ul.out-of-stock', event.target).show();
				$('ul.in-stock', event.target).hide();
				$('form.si-cart-add-form input', event.target).attr('disabled','disabled');
				$('.prod-purchase', event.target).addClass('out-of-stock');
			} else {
				$('ul.in-stock', event.target).show();
				$('ul.out-of-stock', event.target).hide();
				$('form.si-cart-add-form input', event.target).removeAttr('disabled');
				$('.prod-purchase', event.target).removeClass('out-of-stock');
				
				if(+product.sale_price) {
					$('div.prod-add-cart', event.target).append('<div class="final-sale">All sales items are final sale</div>');
				}
			}
		});
		
		//init supply
		svpply_api.construct();
	}
	
	$('li.mailchimp-dialog').contextDialog();
	$('li.mailchimp-dialog a.btn-close').click(function() {
		$('li.mailchimp-dialog').contextDialog('close');
		return false;
	});
	
    //product image switcheroo
    $('div.item-photos div.gallery img').click(function() {
			var href = $(this).attr('src').replace('_thumb', '');
			$('.main-image a').attr('href', href).attr('rel', "position: 'right', adjustX: 25, zoomWidth: '510'");
			$('.mousetrap').remove();
			$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
    });
	
	//close the messenger
	$(document).click(function() {
		$('div#message').hide();
	});
	
	// make sure ui widgets get initialized when elements get added to the dom
	$(document).bind('render.si', function(event) {
		init(event.target);
	});
	
	// initialize ui widgets on the initial page load
	init(document);	 
});


