$(function() {

	//INITIATE CYCLE
	$('.cycle').after('<span id="prev"></span><span id="next"></span>').cycle({
		fx: 'fade',
		timeout: 8000,
		next: '#next',
		prev: '#prev'
	});

	// 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');
	});
	
	// load the review list template via ajax on the product detail page
	$('#review-list-wrapper').load($('#review-page-link').attr('href'), function() {
		$(this)
			.removeClass('loading')
			.trigger('render');
	});
	
	//product gallery asset image swapping
	$('.si-product').not('.si-partial').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()
					.find('a:first img').trigger('click');
			});
		}
	});
	
	function init(target) {
		// initialize input hints
		$('input:text', target).hint();
		
		// initialize generic toggles
		$('a.toggle', target).toggler();
		
		// 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');
		
		// close wishlist context dialogs when a product gets added
		$('.si-wishlist-add-form').bind('addtowishlist.si', function() {
			$(this).parents('.ui-context-dialog').contextDialog('close');
		});
		
		// remove new review form and deactivate button when one is created
		$('.si-review-create-form', target).bind('success', function(event) {
			$('#new-review-toggle')
				.toggler('toggle')
				.toggler('destroy')
				.addClass('disabled');
			
			$('#new-review').remove();
		});
		
		// review edit form swapping
		$('.review-edit', target).swap({ replacementText: '[cancel]' });
		
		// hide review form when it's saved
		$('.si-review-edit-form', target).bind('success', function() {
			$(this).parents('.si-review').find('.review-edit').swap('restore');
		});
		
		// remove review helpful links when clicked
		$('.si-review-helpful-button').click(function() {
			$(this).parents('.review-helpful-wrapper').html('Thank you for voting.');
			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', "adjustX: 10, adjustY:-4, position:'right'");
		$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
  });
  
  	// LIGHTBOX
    $('a.lightbox-link').click(function() {
    	$('#lightbox').empty().append('<img src=""><a href="#" class="close lightbox-link">(X) CLOSE</a>');
    	$('#lightbox img').attr('src', $(this).attr('href'));
    	$('#lightbox').lightbox_me({
    		centered: true
    	});
    	return false;
    });
    
    $('div.main-image').click(function() {
    	$('#lightbox').empty().append('<img src=""><a href="#" class="close lightbox-link">(X) CLOSE</a>');
    	$('#lightbox img').attr('src', $('div.main-image > a').attr('href'));
    	$('#lightbox').lightbox_me({
    		centered: true
    	});
    });
	
	// 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);
	
/*
	Where are circular and navigator plugins? Where is $.fn.scrollable() defined?

	// select #flowplanes and make it scrollable. use circular and navigator plugins
	$("#flowpanes").scrollable({clickable: false,size: 2,speed:800}).circular().navigator({
		// select A tags inside the navigator to work as items (not direct children)
		naviItem: 'a',

		// assign "current" class name for the active A tag inside navigator
		activeClass: 'current'

	}).autoscroll({autoplay: true,interval:6000});
*/
	
	// initialize menu dropdowns (this takes a long time, so do it after the rest of the JS has initialized)
	$('#main-nav > ul').dropDown({ arrows:false });
});

