var jBox = {
	// global vars
	animating: false,
	iFrame: [],
	nGal: [],
	curGalSel: 0,
	// Initilize
	init: function(options) {
		//jBox.options = options;
		switch(options.type) {
			case 'iframe':
				var thisIndex = options.id;
				jBox.iFrame[options.id] = options; // this is to hold multiple iframes
				jBox.createIFrame(thisIndex);
				jBox.initIFrame();
				if (options.buttons) {
					$(options.buttons).each(function() {
						$(this).click(function() {
							jBox.showIFrame(thisIndex);
							return false;
						});
					});
				}
			break;
			case 'gallery':
				var thisIndex = jBox.nGal.length;
				jBox.nGal.push({'options': options, 'curIndex': 0});
				
				jBox.createGallery(thisIndex);
				jBox.preloadImages(thisIndex);
				//jBox.initGallery(thisIndex);
				$(jBox.nGal[thisIndex].options.buttons).each(function(i) {
					$(this).click(function() {
						jBox.showGallery(thisIndex, i);
						return false;
					});
				});
			break;

		}
	},
	preloadImages: function(index) {
		for (var i = 0; i < jBox.nGal[index].options.images.length; i++) {
			var myImage = new Image();
			myImage.src = jBox.nGal[index].options.images[i];
			if (jBox.nGal[index].options.loadedImages == undefined) jBox.nGal[index].options.loadedImages = [];
			jBox.nGal[index].options.loadedImages.push(myImage);	
		}
	},
	// showGallery is called from the page thumbs
	showGallery: function(index, imgIndex) {
		if (!jBox.animating) { // to be made into animation effects
			$(".jBoxOverlay").css({display: "block", opacity: '0'}).animate({opacity: '0.8'}, 500, function() {
				$(".jGal").css({ top: $(($.browser.safari?'body':'html')).scrollTop() + 30 }).animate({ opacity: 'show' }, 500);
				jBox.nGal[index].curIndex = imgIndex;
				jBox.initGallery(index);
				jBox.showGalleryByIndex(index, imgIndex);
			});
		}
		
	},
	showGalleryByIndex: function(index, imgIndex) {
		if (imgIndex == (jBox.nGal[index].options.loadedImages.length-1)) {
			$('.jGalNext').css({ display: 'none' });
		} else {
			$('.jGalNext').css({ display: 'block' });
		}
		if (imgIndex == 0) {
			$('.jGalPrev').css({ display: 'none' });
		} else {
			$('.jGalPrev').css({ display: 'block' });
		}
		if (!jBox.animating) { // to be made into animation effects
			var heightDuration = ($(".jGal .jGalHolder").height() == jBox.nGal[index].options.loadedImages[imgIndex].height) ? 1 : 250;
			var widthDuration = ($(".jGal .jGalHolder").width() == jBox.nGal[index].options.loadedImages[imgIndex].width) ? 1 : 250;
			$(".jGal .jGalHolder").animate({ height: jBox.nGal[index].options.loadedImages[imgIndex].height + "px" }, heightDuration, function() {
				$(".jGal").animate({ width: jBox.nGal[index].options.loadedImages[imgIndex].width + "px", marginLeft: '-' + (jBox.nGal[index].options.loadedImages[imgIndex].width / 2) + 'px' }, widthDuration, function() {
					$(".jGal .jGalHolder").html(jBox.nGal[index].options.loadedImages[imgIndex]);
					$(".jGal .jGalHolder img").css({ display: 'none' }).animate({opacity: 'show'}, 250);
				});	
			});
		}
	},
	createGallery: function(index) {
		// creates the overlay, and binds the click
		jBox.createOverlay();
		// creates the main structure
		if (!$('.jGal').exists()) {
			$('body').createAppend('div', { className: 'jBox jGal', style: 'width: ' + jBox.nGal[index].options.width + 'px; margin-left: -' + (jBox.nGal[index].options.width / 2) + 'px; display: none'}, [
				'div', {className: 'jGalHolder', style: 'height: ' + jBox.nGal[index].options.height + 'px'}, '',
				'div', {className: 'jGalCaption'}, [
					'a', {className: 'close', href: '#'}, '',
					'a', {className: 'jGalNext', href: '#'}, '',
					'a', {className: 'jGalPrev', href: '#'}, ''
				]
			]);
		}
	},
	closeFnc: function() {
		jBox.hideLightBox();
		return false;
	},
	prevFunc: function() {
		if (jBox.nGal[curGalSel].curIndex > 0) jBox.nGal[curGalSel].curIndex--;
		//console.log('prev << ' + jBox.nGal[curGalSel].curIndex);
		jBox.showGalleryByIndex(curGalSel, jBox.nGal[curGalSel].curIndex);
		return false;
	},
	nextFunc: function() {
		if (jBox.nGal[curGalSel].curIndex < (jBox.nGal[curGalSel].options.images.length - 1)) jBox.nGal[curGalSel].curIndex++;
	//	console.log('next >> ' + jBox.nGal[curGalSel].curIndex);
		jBox.showGalleryByIndex(curGalSel, jBox.nGal[curGalSel].curIndex);
		return false;
	},
	initGallery: function(index) {
		curGalSel = index;
		
		$('.jGal .close').unbind('click', jBox.closeFnc).click(jBox.closeFnc);
		$('.jGal .jGalPrev').unbind('click', jBox.prevFunc).click(jBox.prevFunc);
		$('.jGal .jGalNext').unbind('click', jBox.nextFunc).click(jBox.nextFunc);
	},
	showIFrame: function(indexIFrame) {
		if (!jBox.animating) {
			$('.jBoxOverlay').css({ opacity: '0', display:'block' }).animate({opacity: '0.8'}, 500, function() {
				$('.jBox.' + indexIFrame).find('iframe').attr('src', jBox.iFrame[indexIFrame].src);
				$('.jBox.' + indexIFrame).css({ display:'block', top: $(($.browser.safari?'body':'html')).scrollTop() + 10 }).animate({ opacity: 'show' }, 500);
			});
		}
	},
	initIFrame: function() {
		$('.jBox .close').click(function() {
			jBox.hideLightBox();
			return false;
		});
	},
	createIFrame: function(indexIFrame) {
		// creates the overlay, and binds the click
		jBox.createOverlay();
		// creates the main structure
		$('body').createAppend('div', {className: 'jBox ' + jBox.iFrame[indexIFrame].id, id: jBox.iFrame[indexIFrame].id, style: 'width: ' + jBox.iFrame[indexIFrame].width + 'px; margin-left: -' + (jBox.iFrame[indexIFrame].width / 2) + 'px; display: none'}, [
			'div', {className: 'iFrameHolder'}, '<iframe name="' + jBox.iFrame[indexIFrame].id + '" src="'+jBox.iFrame[indexIFrame].src+'" height="' + jBox.iFrame[indexIFrame].height + '" width="' + jBox.iFrame[indexIFrame].width + '" frameborder="0" allowTransparency="true"></iframe>',
			'div', {className: 'iFrameCaption'}, [
				'a', {className: 'close', href: '#'}, ''
			]
		]);
	},
	createOverlay: function() {
		if (!$('.jBoxOverlay').exists()) {
			$('body').createAppend('div', {className: 'jBoxOverlay',  style: 'display: none'}, '');
			$('.jBoxOverlay').click(function() {
				jBox.hideLightBox();
			});
		}
	},
	hideLightBox: function() {
		$('.jBox').animate({ opacity: 'hide' }, 500, function() {
			$('.jBoxOverlay').animate({ opacity: 'hide' }, 500);
		});
		if ($('.jBox iframe').exists()) { $('.jBox iframe').attr({ src: '' }); }
		if ($('.jGal .jGalHolder').exists()) { $('.jGal .jGalHolder').html(''); }
	}
}

jQuery.fn.exists = function() { return jQuery(this).length>0; }

var path = '/';

$(window).load(function() {
		$('.video_player').each(function() {
			jBox.init({
				type: 'iframe',
				id: this.rel.replace('video|', ''),
				src: path + 'video/' + this.rel.replace('video|', ''),
				height: 385,
				width: 480,
				buttons: this
			});
		});
		
		// below filters images into their gallery options, then we loop through and init the gal.
		var jGals = {};		
		$("a[rel^='lightbox']").each(function() {
			var btnTarget = "a[rel='" + this.rel + "']";
			var galID = this.rel.replace(/lightbox\[([a-z0-9]+)\]/i, '$1');
			if (jGals[galID] == undefined) {
					jGals[galID] = {
						type: 'gallery',
						width: 200, // initial width
						height: 200, // initial height
						buttons: btnTarget,
						images: []
					};
			}
			jGals[galID].images.push(this.href);
		});
		
		for (var i in jGals) {
			jBox.init(jGals[i]); // init for lightbox[<gal name here>]
		}


});

