$(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');
	});
	
	$('#leftProdList > li.si-has-children > a').click(function() {
		if (!$(this).hasClass('active')) {
			$('#leftProdList > li > a.active').next().stop(true,true).slideToggle();
			$('#leftProdList > li > a.active').removeClass('active');
			$(this).addClass('active');
			$(this).next().stop(true,true).slideToggle('slow');
		} else {
			$(this).removeClass('active').next().stop(true,true).slideToggle('slow');
		}
		return false;
	});
	
	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');
	}
	
	// product image switcheroo // TODO: make this better
    $('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: 20");
			$('.mousetrap').remove();
			$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
    });
	
	// 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 menu dropdowns (this takes a long time, so do it after the rest of the JS has initialized)
	$('#main-nav > ul').dropDown({ arrows:true });
	
	//3 steps to beauty pop-up
	$('area').click(function() {
		window.open($(this).attr('href'), '3StepsInstructions', 'scrollbars=yes, width=550, height=800');
		return false;
	});
});


