// JavaScript Document

var cur_lb_item = '';

function initLightBox()
{
	$$('body').each(function(e){
		e.insert(new Element('div',{'class':'blackout', 'id':'lb_blackout', 'style':'display: none;'}));
		e.insert(new Element('div',{'class':'lightBoxContent', 'id':'lb_content', 'style':'display: none;'}));
		e.insert(new Element('div',{'class':'lightBoxCloseButton', 'id':'lb_closeButton', 'style':'display: none;'}));
		$('lb_closeButton').observe('click', hideLightBox);
		e.style.height = '100%';
	});
}

function showLightBox(element, img, width, height)
{
	$('youtube').style.visibility = 'hidden';
	
	transitionLightBox(element, img, width, height);
	
	$('lb_blackout').appear({
		duration: 0.4,
		to: 0.7,
		afterFinish: function(){
			$('lb_content').appear({
				duration: 0.2,
				to: 1,
				afterFinish: function(){
					$('lb_closeButton').show();
					$('lb_blackout').hideFx = hideLightBox.bindAsEventListener();
					$('lb_blackout').observe('click', $('lb_blackout').hideFx);
				}
			});
		}
	});
}

function transitionLightBox(element, img, width, height) {
	cur_lb_item = $(element).up().id;
	
	var content = '';
	
	if ($(element).up().previous() != null)
		if ($(element).up().previous().hasClassName('lightBoxItem') || $(element).up().previous().previous().hasClassName('lightBoxItem'))
			content += '<a href="#" id="lb_prev"><img style="position: absolute; top: 0px; left: 0px;" src="assets\/prev.gif" \/><\/a>';
	
	content += '<img src="assets/u/' + img + '" />';
	
	if ($(element).up().next() != null)
		content += '<a href="#" id="lb_next"><img style="position: absolute; top: 0px; left: 0px;" src="assets\/next.gif" \/><\/a>';

	$('lb_content').innerHTML = content;
	$('lb_content').style.marginTop = (-100-(height/2))+'px';
	$('lb_content').style.marginLeft = (-100-(width/2))+'px';
	$('lb_closeButton').style.marginTop = ((height/2))+'px';
	$('lb_closeButton').style.marginLeft = (-112-(width/2))+'px';
	var nextLeftWidth = parseInt(width) + 200 - 63;
	if ($('lb_next'))
		$('lb_next').down().style.left = nextLeftWidth.toString()+'px';

	if ($('lb_prev'))
		Event.observe('lb_prev', 'click', prevImg.bindAsEventListener('lb_prev'));
	if ($('lb_next'))
		Event.observe('lb_next', 'click', nextImg.bindAsEventListener('lb_next'));
}

function hideLightBox()
{
	$('youtube').style.visibility = 'visible';
	$('lb_closeButton').hide();
	$('lb_blackout').stopObserving('click', $('lb_blackout').hideFx);
	$('lb_content').fade({
		duration: 0.2,
		to: 0,
		afterFinish: function(){
			$('lb_blackout').fade({
				duration: 0.4,
				to: 0
			});
		}
	});

	if ($('lb_prev'))
		Event.stopObserving('lb_prev');
	if ($('lb_next'))
		Event.stopObserving('lb_next');
}

function prevImg(e) {
	var prev = $(cur_lb_item).previous();
	while (prev.down() == null)
		prev = prev.previous();

	var regexp = /'(.*)', '(.*)', '(.*)'/;
	var matches = regexp.exec(prev.down().attributes["onclick"].value);
	var src = matches[1];
	var width = matches[2];
	var height = matches[3];
	
	transitionLightBox(prev.down(), src, width, height);
}

function nextImg(e) {
	var next = $(cur_lb_item).next();
	while (next.down() == null)
		next = next.next();

	var regexp = /'(.*)', '(.*)', '(.*)'/;
	var matches = regexp.exec(next.down().attributes["onclick"].value);
	var src = matches[1];
	var width = matches[2];
	var height = matches[3];
	
	transitionLightBox(next.down(), src, width, height);
}
