var fancyBoxExceptions = [".mp3"];

function FancyBox(nodes, autoscale) {
	$(nodes).each(function() {
		var href = $(this).attr('href');
		if (isFancyboxException(href) != true) {
			if (autoscale) {
				$(this).fancybox({
					'autoScale'     	: true,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
				});	
			} else {
				$(this).fancybox({
					'width'				: width,
					'height'			: '84%',
					'autoScale'     	: false,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'				: 'iframe'
				});					
			}
		}		
	});
}

function isFancyboxException(href) {
	if (href == undefined) throw "href undefined"
	// extract the extension
	var extension = href.slice(href.length - 4, href.length);
	for (var i=0; i<fancyBoxExceptions.length; i++) {
		if (extension == fancyBoxExceptions[i]) {
			return true;
		}
	}
	return false;
}

// Album content getter and updater
function Lock() {
	this.locked = false;
	this.lock = function() {
		this.locked = true;
	}
	this.unlock = function() {
		this.locked = false;
	}
	this.isLocked = function() {
		return this.locked;
	}
}
function embedGallery(gallery) {
	if (gallery == undefined) throw "gallery not defined";
	if (!lock.isLocked()) {
		lock.lock();
		$.getJSON('/?json=get_page&page_slug=press/' + gallery, function(data) {
			var container = $('#content #press div.post_column_1')[0];
			if ($(container).children('div').children('ul').length > 0) {
				$(container).children('div').fadeOut(speed, function() {
					$(container).children('div').children().remove();
					$(container).children('div').hide().append(data.page.content).fadeIn(speed);
					fancyBox = new FancyBox("#press ul li a"); // needs to be here or race condition
				});
			} else {
				if ($(container).children('div').length == 0) {
					$(container).append(document.createElement('div'));
				}
				$(container).children('div').hide().append(data.page.content).fadeIn(speed);
				fancyBox = new FancyBox("#press ul li a");
			}
			lock.unlock();
		});
	}
}

// Objects
var width = screen.width - 120;
if (width > 1020) {
	width = 1020;
}
var speed = 1500;
var lock = new Lock();
var fancyBox;