jQuery(function($) {
	// 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');
	});

	// close wishlist context dialogs when a product gets added
	$(document).delegate('.si-wishlist-add-form', 'addtowishlist', function() {
		$(this).parents('.ui-context-dialog').contextDialog('close');
	});
	
	//Image Zoom
	$('.main-image .si-image').prodZoom();
	$('.zoom-link').click(function() {
		$('.main-image .si-image').trigger('click');
	});

	function init(target) {
		// initialize all input with hints
		$('input:text', target).hint();
		
		// initialize generic toggles
		$('a.toggle', target).toggler({
			activeClass	: 'active',
			closeClass	: 'close'
		});
		
		// initialize context dialogs
		$('.context-dialog', target).contextDialog();
		
		// initialize sliders
		$('.callout-products', target).itemSlider();
		
		// initialize zebra striping on tables and lists
		$('table tr:even', target).addClass('stripe');
	}
	
	// make sure ui widgets get initialized when elements get added to the dom
	$(document).bind('render', function(event) {
		init(event.target);
	});
	
	// initialize ui widgets on the initial page load
	init(document);
	
	// initialize the sidebar accordion
	$('#accordion li.si-has-children li.si-has-children > a.si-current-branch').addClass('open');
	
	$('#accordion li.si-has-children li.si-has-children > a').click(function() {
		if($(this).hasClass('open')) {
			$(this).removeClass('open').next().slideUp();
		} else {
			$('.open').removeClass('open').next().slideUp();
			$(this).addClass('open').next().slideDown();
		}
		return false;
	})
	
	// initialize menu dropdowns (this takes a long time, so do it after the rest of the JS has initialized)
	$('#main-nav > ul').dropDown({ arrows: true });
	
	
});

(function($) {
// Product image zoom
$.fn.prodZoom = function() {

	var zi = this,
		ziW = $(zi).width(),
		ipW = $('.item-photos').width(),
		lgSrc = '',
		zbW = $('#product-detail').width();

	$(this).bind('click', function(e) {
		lgSrc = SI.image.getSrc(this, 'source')
		if ($('.item-info').is('.zoomed')) {
			close($(this));
		} else {
			zoom($(this));
		};
		e.preventDefault();
	});

	$('.zoom-close').live('click', function(e) {
		close(zi);
	});

	function zoom(zi) {

		$('.item-info, .active-gallery')
			.addClass('zoomed')
			.fadeOut('fast');

		$('.item-info')
			.addClass('zoomed')
			.fadeOut('slow', function(){
				$('.item-photos').animate({width:zbW}, 'fast');
				$(zi).css('height', 'auto');
				$(zi).animate({width:zbW}, 'fast', function(){
					$('.main-image').append('<a class="si-center zoom-close">close zoom</a>');
					$('.zoom-close').css('cursor', 'pointer');
					$(zi).attr('src', lgSrc);
				});
			});
		$('.zoom-link').fadeOut('fast');
	}
	
	function close(zi) {

		$('.item-info, .active-gallery').removeClass('zoomed');

		$(zi).animate({width:ziW}, 'fast');

		$('.zoom-close').remove();

		$('.item-photos').animate({width:ipW}, 'fast', function(){
			$('.item-info, .active-gallery').fadeIn('fast');
		});
		
		$('.zoom-link').fadeIn('fast');

	}

};


})(jQuery);
