$(function() {

	// Home page slider
  $('#slides').cycle({
		fx:     'fade',
		speed:   900,
		timeout: 5000,
		pause:   true,
		next:   '#next',
		prev:   '#prev'
  });

	// 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');
	});

	function init(target) {
		// initialize all input with hints
		$('input:text', target).hint();
		
		// 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);
	
	// swatches custom hover
	$('.si-option-swatches a img').mouseenter(function() {
		var src = $(this).attr('src'),
			name = $(this).parent().attr('title');
		src=src.replace('_swatch','');
		$(this).parent().parent().append('<div class="swatch-hover"><img src="' + src + '" alt="' + name + '"></div>');
	}).mouseleave(function() {
		$('div', $(this).parent().parent()).remove();
	});
	
	//product image gallery slider
	$('div.gallery div.carousel ul a').wrap('<li />');
	
    $('div.gallery div.carousel').jCarouselLite({
        btnNext: '.gallery .next',
        btnPrev: '.gallery .prev',
        visible: 5,
        circular: false
    });
    
    if($('div.gallery div.carousel img').length <= 5) {
    	$('div.gallery span.next, div.gallery span.prev').hide();
    }
	
	// zoom
	$('.SI-image-gallery-target a').attr("id", "zoom").addClass('cloud-zoom').attr('rel', 'position: "inside", smoothMove: 5');

	var $elemsR = $(".SI-image-gallery a");
	var $arrR = $.makeArray($elemsR);
	
	$.each($arrR, function(){
		var url = $(this).attr('href');
		var newurl = url.replace(".jpg","_medium.jpg");
		
		$(this).addClass('cloud-zoom-gallery').attr('rel', 'useZoom: "zoom", smallImage: "'+ newurl +'"');
	});
	
	//lightbox
	$('#lightbox a').attr('rel', 'fancy-gallery').addClass('fancybox');
	$('#lightbox a:first').show();
	$('#lightbox a:first').html('view larger photos');
	$("a.fancybox").fancybox();
	    /*$('a[rel="lightbox"]').click(function() {
	    	$('#lightbox').empty().append('<img src="">').append('<a href="#" class="close si-invisitext">Close</a>');
	    	$('body').append('<div class="lightbox-loading"></div>');
	    	$('#lightbox img').load(function(){
	    		$('#lightbox').lightbox_me({
	    			centered: true
	    		});
	    		$('.lightbox-loading').remove();
	    	}).attr('src', $(this).attr('href'));
	    	return false;
	    });
	    
	    $('div.item-photos').click(function() {
	    	$('body').append('<div class="lightbox-loading"></div>');
	    	var src = $('.main-image a img').attr('src').replace('_medium', '');
	    	$('#lightbox').empty().append('<img src="">').append('<a href="#" class="close si-invisitext">Close</a>');
	    	$('#lightbox img').load(function(){
	    		$('#lightbox').lightbox_me({
	    			centered: true
	    		});
	    		$('.lightbox-loading').remove();
	    	}).attr('src', src);
	    	return false;
	    });*/
	
	
	// THE SIDEBAR
	$('ul.store-menu > li.si-has-children > a').click(function() { 
		if ( $(this).parent('li.si-has-children').is(':first-child') ) { return true; }
		return false; 
	});
	$('ul.store-menu li.si-has-children  li.si-has-children > a').click(function(e) {
		e.preventDefault();
		$(this).siblings('ul').stop(true,true).toggle().toggleClass('open');
	});
	
	/*$('ul.store-menu > li.si-has-children > ul > li.si-has-children').bind({
		'mouseenter' : function(e) {
			e.stopImmediatePropagation();
			$('ul', this).stop(true,true).show('slow');
		},
		'mouseleave' : function(e) {
			e.stopImmediatePropagation();
			var $blockUL = $('ul', this);
			if ( $('ul', this).siblings('a').is('.si-current-branch') ) { return false; }
			setTimeout( function(){$blockUL.stop(true,true).hide('slow');} , 10);
		}
	});*/
	
	// 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 });
	
	//hides the brands list in the sidebar
	$(".si-has-children:last").hide();
	
	//for the subnavs, when active, show the subnav list
	$(".si-current").parents().show();
	
	//hide the "no image available" icon in top description area
	$(".area-description img[alt='no image available']").hide();
	
	//$(".prod-descriptions .prod-description-wrapper").eq(1).remove();
	$(".prod-descriptions").accordion({ 
		header: 'h3',
		autoHeight: false 
	});
	
	
});

