$(function() {
	
	function init(target) {
		// initialize input 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.zebra tr:even, ul.zebra li:even, div#specifications tr:odd, div#compatibility tr:odd', target).addClass('stripe');
		
		$('.si-product').bind('productchange', function(event, product, productOption) {
			$('span.sku', this).text(productOption.sku);
		});
	}
	
	// home slideshow scriptery
	$('#home-slideshow').cycle({ 
    	fx:     'fade', 
	    speed:  1000, 
	    timeout: 10000, 
	    pager:  '.controls' 
	});
	
	//main nav highlighting
	
	$('#main-nav > ul > li').has('.si-current').addClass('highlight');
	
	//crazy sidebar action
	
	$grandchildren = $('div.sidebar-section li.si-has-children li.si-has-children');
	
	$grandchildren.prepend('<span class="arrow"></span>');
	
	$('ul', $grandchildren).hide();
	
	$('div.sidebar-section span.arrow').click(function() {
		$(this).toggleClass('open');
		$(this).nextAll('ul').slideToggle();
	});
	
	$('div.sidebar-section li.si-has-children li.si-has-children a.si-current')
		.parents().show()
		.end()
		.parent()
			.find('ul:first').show()
			.end()
			.find('span:first').toggleClass('open');
	
	$grandchildren.has('.si-current').find('span:first').toggleClass('open');
	
	$('.si-current', $grandchildren).parent().find('span:first').toggleClass('open');
	
	//product detail page tabs
	$('.prod-descriptions').tabs();
	$('a.tab-link').click(function() {
		href = $(this).attr('href');
		$('.prod-descriptions').tabs('select',href);
		return false;
	});
	
	//product image gallery slider
	$('div.gallery div.carousel img').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').toggleClass('disabled');
    }
    
    //product image switcheroo
    $('div.gallery img').click(function() {
    	$('.mousetrap').remove();
    	var lrg = $(this).attr('src').replace('_thumb', '_large');
    	var src = $(this).attr('src').replace('_thumb', '');
    	$.ajax({
    		url:		lrg,
    		type:		'HEAD',
    		error:		function() { 
    			$('.main-image a').attr('href', src);
    			$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
    			},
    		success:	function() { 
    			$('.main-image a').attr('href', lrg);
    			$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
    			}
    	});
    });
    $('div.gallery img.si-current').trigger('click'); //ensures that zoom img is correct for first thumbnail
    
    //lightbox
    $('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;
    });
   
    // #message stuff 
	$(document).click(function(){
		$('div#message').hide();
	});
	
	// 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
	$('#main-nav > ul').dropDown({arrows:false});
	
});

