/**********************************************************************/
//! Variables

// Options passed into modal windows
/*
var modalOptions = {
	'opacity':45,
	'overlayCss':{backgroundColor:'#000'},
	'autoResize':true,
	'overlayClose': true
};
*/

// Options passed into the lightbox
var lightboxOptions = {
	'overlayOpacity':0.4,

	// Set up image paths, as it doesn't seem to find these outherwise
	'imageBtnClose':'http://jbwebresources.com/js/lightbox/images/lightbox-btn-close.gif',
	'imageBtnPrev':'http://jbwebresources.com/js/lightbox/images/lightbox-btn-prev.gif',
	'imageBtnNext':'http://jbwebresources.com/js/lightbox/images/lightbox-btn-next.gif',
	'imageLoading':'http://jbwebresources.com/js/lightbox/images/lightbox-ico-loading.gif'
};


/**********************************************************************/
//! AJAX modal window
/*
// Set up AJAX
function modal(url, data) {
	if (data==null) data={};
	data.ajax = 1;
	$.post(url, data, modalResponse);
} // End function modal()

// Process response
function modalResponse(data) {
	if (data) {
		// Close existing modal, if any
		$.modal.close();

		// Set up new modal
		$('#modal-content').html(data);
		$('#modal').modal(modalOptions);
	}
} // End function modalResponse()

// Close modal window
function closeModal() {
	$.modal.close();
} // End function closeModal()
*/

/**********************************************************************/
//! Products slider

productSlider = {
	'max':null, // Max margin
	'numItems':null, // Number of items present
	'item':null, // First item in list (setting left margin moves slider)
	'itemWidth':null, // Width of a single item
	'maxItems':6, // Total number of items that can be on the "screen" at a time

	'setup':function() {
		// Initial setup:  Get needed variables (see comments above)
		var slider = $('#products-slider');
		this.numItems = $('li',slider).length;
		this.item = $('li:first-child', slider);
		this.itemWidth = this.item.width();

		this.max = -(this.itemWidth * (this.numItems-this.maxItems));

		// Set the handler
		$('#slider-prev, #slider-next').click(this.handler);

		// Show navigation items
		this.showNav(0);

	}, // End setup()

	// Should we show the navigation?
	'showNav':function(itemMargin) {
		if (this.numItems <=this.maxItems) { // No need
			$('#slider-next, #slider-prev').hide();
			return;
		}

		$('#slider-next').toggle(itemMargin > this.max); // Reached max?
		$('#slider-prev').toggle(itemMargin < 0); // Reached min?

	}, // End showNav()

	// Click handler
	'handler':function() {

		// Which direction? (Returns -1 if prev, index pos. if next)
		var dir = $(this).attr('id').indexOf('next');

		// What's the current margin?
		var itemMargin = parseInt(productSlider.item.css('marginLeft')); // Current margin
		if (isNaN(itemMargin)) itemMargin=0; // Needed for IE
//alert(itemMargin+'\n'+productSlider.numItems+'\n'+productSlider.max);

		// Which direction?
		if (dir > 0) { // Forward
			if (itemMargin > productSlider.max) {
				productSlider.animate(itemMargin, dir);
			}
		}
		else { // Backward
			// Zero is our limit here
			if (itemMargin < 0) {
				productSlider.animate(itemMargin, dir);

				/*var newMargin = itemMargin + productSlider.itemWidth;
				if ($.browser.msie && parseInt(jQuery.browser.version)==7) {
					productSlider.item.css('marginLeft',newMargin); // Move it over
					$('#products-slider li:eq('+((-newMargin / productSlider.itemWidth))+')').css({
						'marginLeft':0
					});
				}
				else {
					productSlider.item.animate({'marginLeft':newMargin}, 400, function() {
						$('#slider-prev, #slider-next').click(productSlider.handler); // Restore handler
					});
					$('#slider-prev, #slider-next').unbind('click').click(function() { return false; }); // Prevent rapid clicking
				}

				productSlider.showNav(newMargin);*/
			}
		}


		return false;
	}, // End handler()

	// Carry out the animation
	'animate':function(itemMargin, dir) {
		var newMargin = (dir>0)? itemMargin - productSlider.itemWidth : itemMargin + productSlider.itemWidth; // Which way?

		// IE7 does NOT play nice, as usual
		if ($.browser.msie && parseInt(jQuery.browser.version)==7) {
			productSlider.item.css('marginLeft',newMargin); // Move it over

			// In addition to the first item, we also need to adjust the first visible item
			// And we have to do the adjustment differently based on the direction
			var mLeft = newMargin;
			var idx = -newMargin / productSlider.itemWidth;
			if (dir>0) $('#products-slider li:eq('+(idx-1)+')').css('marginLeft',newMargin);
			else $('#products-slider li:eq('+idx+')').css('marginLeft',0);

		} // End if (IE7 fixes)
		else {
			// All other browsers animate JUST FINE.
			productSlider.item.animate({'marginLeft':newMargin}, 400, function() {
				$('#slider-prev, #slider-next').click(productSlider.handler); // Restore handler
			});
			$('#slider-prev, #slider-next').unbind('click').click(function() { return false; }); // Prevent rapid clicking
		}

		productSlider.showNav(newMargin);

	} // End animate()


}; // End object productSlider



/**********************************************************************/
//! Other functions

