$(function() {
	// set toggler defaults
	$.extend($.ui.toggler.defaults, {
		activeClass	: 'active',
		closeClass	: 'close'
	});

	//randomize testimonials
	randomize_testimonials();
	
	//Image Zoom
	$('.main-image .si-image').outlawZoom();

	//Initialize Accordion
	$('.ui-accordion').accordion({ autoHeight: false, clearStyle: true });
	
	// initialize Cycle
	$(".cycle").cycle({ pager: '#topPager', cleartype: true, cleartypeNoBg: true, timeout: 10000 });
	$("#topToggle a").click(function(){
		$("#topCycle").cycle('toggle');
		$(this).toggleClass("paused");
	});
	
	// 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()
					//trigger click on first image link to load new main image
					.find('a:first img').trigger('click');
			});
		}
	});
	
	$('.si-cart-add-form').bind('addtocart', function(event, message) {
		$('#message').hide();
		$(event.target)
			.siblings('.cartBubble')
			.css('zIndex', 99)
			.fadeIn('fast', function() {
				var t = $(this);
				$('.cart-msg', t).show();
				setTimeout(function() {
					t.fadeOut('fast');
					$('.cart-msg', t).hide();
					}, 2000
				);
			})
	});



		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', target).bind('addtowishlist', function(event) {
				$('#message').hide();
				$(this).parents('.ui-context-dialog').contextDialog('close');
				$(event.target)
					.parents('.prod-purchase')
					.find('.cartBubble')
					.css('zIndex', 99)
					.fadeIn('fast', function() {
						var t = $(this);
						$('.cart-msg', t).show();
						setTimeout(function() {
							t.fadeOut('fast');
							$('.cart-msg', t).hide();
							}, 2000
						);
					});
			});
	
			// remove new review form and deactivate button when one is created
			$('.si-review-create-form', target).bind('success', function(event) {
				$('#new-review-toggle').remove();
				$('#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;
			});
			//Stock Notifications
			$('.si-product').bind('productchange', function(event, product, productOption) {
				$('.stock-wrapper', event.target).empty();
				
				if (productOption) {
					if(productOption.stock == 0) {
						$('.stock-wrapper', event.target).append('Out of stock');
					} else if(productOption.stock < 26) {
						$('.stock-wrapper', event.target).append(productOption.stock + ' in stock');
					} else {
						$('.stock-wrapper', event.target).append('In stock');
					}
				}
			});		
		}
	
	
	// 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);
	
	// initialize menu dropdowns (this takes a long time, so do it after the rest of the JS has initialized)
	$('#main-nav > ul, #subNav > ul').dropDown({ arrows:false });

});






