/*
 * Light Gear Box
 * The lightbox for gearBox
 * Author: Paul Sayre
 * Version: 1.0
 * Created: 2008-11-24
 * Updated: 2008-11-24
 */
var LightGearBox = function(id, speed) {
	var $ = window.jQuery;
	var boxID = '#'+id;
	var backID = '';
	var inTrans = false;
	var index = 0;
	var isIE6 = $.browser.msie && $.browser.version == '6.0';
	speed = speed || 500;

	function init() {
		$(boxID+' .item:eq('+index+')').show();
		$(boxID+' .close').click(hideBox);
		$(boxID+' .prev').click(transPrev);
		$(boxID+' .next').click(transNext);
		$(boxID+' .item').click(transNext);
		
		if($(backID).length < 1) {
			$(boxID).before($('<div id="' + backID.substr(1) + '"/>'));
			var css = {background: '#000', cursor: 'pointer', display: 'none', left: 0, opacity: .4, top: 0};
			$.extend(css, isIE6 ? {height: $('#container').height() + 'px', position: 'absolute', width: $('body').width() + 'px'} : {bottom: 0, right: 0, position: 'fixed'});
			$(backID).click(hideBox).css(css);
		}
	}
	$(init);

	function showBox() {
		$(boxID).fadeIn(speed);
		$(backID).fadeIn(speed);
	}

	function hideBox() {
		$(boxID).fadeOut(speed);
		$(backID).fadeOut(speed);
	}

	function transNext() {
		var length = $(boxID+' .item').length;
		trans((index + 1) % length)
	}

	function transPrev() {
		var length = $(boxID+' .item').length;
		trans((length + index - 1) % length);
	}

	function trans(newIndex) {
		if(inTrans) return;
		inTrans = true;
		$(boxID+' .item:eq('+newIndex+')').fadeIn(speed);
		$(boxID+' .item:eq('+index+')').fadeOut(speed, function(){inTrans = false;});
		index = newIndex;
	}

	return {
		prev: transPrev,
		next: transNext,
		show: showBox,
		hide: hideBox
	};
};
