function openCart() {

    $("#cart").overlay({

        mask: {
            color: '#000',
            opacity: .5
        },
        top: '10'
    });

    $("#cart").data("overlay").load();
}

function closeCart() {
    $("#cart").data("overlay").close();
}

function closeItem() {
    $("#itemDetail").data("overlay").close();
}


$(document).ready(function() {


/************************
	Modal Overlay (using jQuery Tools)
**************************/
$(".openModal[rel]").overlay({
	mask: {
		color: '#000',
		opacity: .5
	},
	top: '10'
});


//$('a.loadModal[rel]').overlay({
//	mask: {
//		color: '#000',
//		opacity: .5
//	},
//	top: '8',
//	onBeforeLoad: function() {
//		var wrap = this.getOverlay().find('.loadWrap');
//		wrap.load(this.getTrigger().attr('href')+' .loadThis');
//	}
//});




/**************************
	SLIDERS 
**************************/

$('.slider').each(function() {
	var id = '#'+$(this).attr('id');
	sliderSetup(id);
	hideShowArrows(id);
});

// Add a slide number to each dd
function sliderSetup(id) {
	var i = 1;
	$(id+' dd').each(function() {
		$(this).attr('data-slide', i);
		i++;
	});
	
	// put a first and last class on the first and last dd
	$(id+' dd:eq(0)').addClass('first active');
	$(id+' dd:last-child').addClass('last');
	
	// set the width of the dl 
	var ddWidth = $(id+' dd').width();
	var dlCount = $(id+' dd').length;
	$(id).css('width', dlCount * ddWidth);
	
	// Flicker seems to happen on first animation no matter what,
	// so this makes the fliker happen on loading, rather than use. 
	if(Modernizr.csstransitions) {
		$(id).css('transform','translate3d(0, 0, 0)');
	}
//	
	swipeSlider(id);
	
}

// hide next or previous buttons if at end or beginning of slides
function hideShowArrows(sliderID) {
	if ( $(sliderID+' dd.first').hasClass('active') ) {
		$(sliderID+'Wrap .prevArrow').hide();	
	} else {
		$(sliderID+'Wrap .prevArrow').show();	
	}
	
	if ( $(sliderID+' .last').hasClass('active') ) {
		$(sliderID+'Wrap .nextArrow').hide();
	} else {
		$(sliderID+'Wrap .nextArrow').show();	
	}
}




// add universal slider control to the next and previous buttons
$('.sliderWrap .nextArrow').click(function() {
	var id = '#'+$(this).siblings('.slider').attr('id');
	moveSlide('next', id);					
});

$('.sliderWrap .prevArrow').click(function() {
	var id = '#'+$(this).siblings('.slider').attr('id');
	moveSlide('prev', id);											
});


    // SWIPING - makes use of touchSwipe plugin
    function swipeSlider(id) {

        if (Modernizr.touch) { //wb      

            $(id).swipe({
                swipeLeft: function () { moveSlide('next', id, true) },
                swipeRight: function () { moveSlide('prev', id, true) },
                allowPageScroll: 'auto'
            });
        }; //wb
    }




function moveSlide(direction, sliderID, swipe) {
	// get the width of the slider to see how far to move
	// the left position of the dl.slider
	var slideWidth = $(sliderID+' dd').width();

	if(direction == 'next') { // not last slide
		
		var activeSlide = $(sliderID+' .active').attr('data-slide');
		var newActive = $(sliderID+' .active').next().next().attr('data-slide');
		
		if( !$(sliderID+' .active').hasClass('last')) { // not last slide
			
			var Xpos = (newActive - 1) * -slideWidth;
			
			if (Modernizr.csstransitions) {
				$(sliderID).css('transform','translate3d('+Xpos+'px, 0, 0)');
			} else {
				$(sliderID).animate({left:Xpos}, 500, 'swing');
			} // end mondernizer if/else
			
			$(sliderID+' .active').removeClass('active').next().next().addClass('active');
			
		} /* remove comment to allow for swipe cycle
			else { // last slide
			if(Modernizr.csstransitions) {
				$(sliderID).animate({'transform':'translate3d(0, 0, 0)'});
			} else {
				$(sliderID).animate({left:0}, 500, 'swing');
			}
			
			$(sliderID+' .active').removeClass('active');
			$(sliderID+' .first').addClass('active');
		} 
		*/
	} // end if 'next'
	
	if(direction == 'prev') {
		if( !$(sliderID+' .active').hasClass('first')) { // not first slide
			
			var newActive = $(sliderID+' .active').prev().prev().attr('data-slide');
			var Xpos = (newActive - 1) * -slideWidth;
			
			if(Modernizr.csstransitions) {
				$(sliderID).css('transform','translate3d('+Xpos+'px, 0, 0)');
			} else { 
				$(sliderID).animate({left:Xpos}, 500, 'swing');
			}
			
			$(sliderID+' .active').removeClass('active').prev().prev().addClass('active');
		} /* remove comment to allow for swipe cycle
		   else { // first slide
			var lastSlideNum = $(sliderID+' dd:last-child').attr('data-slide');
			var lastXpos = (lastSlideNum - 1) * slideWidth;
			
			if(Modernizr.csstransitions) {
				$(sliderID).css('transform','translate3d(0, 0, 0)');
			} else {
				$(sliderID).animate({left:lastXpos}, 500, 'swing');
			}
			
			$(sliderID+' .active').removeClass('active');
			$(sliderID+' .last').addClass('active');
		}
		*/
	} // end if 'next'
	
	hideShowArrows(sliderID);
}




}); // close document.ready
