/*
	(c) 2009 - Dominic Szablewski 
	www.phoboslab.org
*/

/* Whitebox
----------------------------------------------------------------------- */
var Whitebox = {
	overlay: null,
	box: null
};

Whitebox.init = function() {
	$('a[rel=whitebox]').click( Whitebox.open );
	$('body').append(
		'<div id="whiteboxOverlay"></div>' +
		'<div id="whitebox"></div>'
	);
	Whitebox.overlay = $('#whiteboxOverlay');
	Whitebox.box = $('#whitebox');
	
	Whitebox.overlay.click( Whitebox.close );
}

Whitebox.close = function() {
	Whitebox.overlay.fadeOut( 200 );
	Whitebox.box.fadeOut( 200, function(){ Whitebox.box.html( '' ); } );
}

Whitebox.open = function() {
	var target = $(this).attr('href');
	var title = $(this).attr('title');
	Whitebox.box.html( '' );
	
	
	// Image
	if( target.match(/\.(jpe?g|png|gif)$/i) ) {
		var imgPreload = new Image();
		imgPreload.onload = function(){
			Whitebox.box.html( 
				'<img class="whiteboxImage" src="' + target + '" alt=""/>' +
				'<p>' + title + '</p>'
			);
			var left = ($('html').width() - imgPreload.width)/2;
			var top = ($('html').height() - imgPreload.height)/2;
			Whitebox.box.css( 'left', Math.max(0, left) );
			Whitebox.box.css( 'top', Math.max(0, top) );
		}
		
		Whitebox.box.css( 'left', ($('html').width() - 24)/2 );
		Whitebox.box.css( 'top', ($('html').height() - 24)/2 );
		Whitebox.box.click( Whitebox.close );
		imgPreload.src = target;
	}
	
	// Flash
	else if( target.match(/^(.*\.swf)\?width=(\d+).*?height=(\d+)/i) ) {
		var m = target.match(/^(.*\.swf)\?width=(\d+).*?height=(\d+)/i);
		var swf = m[1];
		var width = m[2];
		var height = m[3];

		Whitebox.box.html( 
			'<object id="whiteboxFlash" width="'+width+'" height="'+height+'">' +
				'<param name="movie" value="' + swf + '" />' +
				'<embed ' +
					'src="' + swf + '"' +
					'type="application/x-shockwave-flash" width="'+width+'" height="'+height+'">' +
				'</embed>' +
			'</object>'
		);
		
		$('#whiteboxFlash').click(function(){ ev.stopPropagation(); })
		Whitebox.box.unbind('click', Whitebox.close );
		Whitebox.box.css( 'left', ($('html').width() - width)/2 );
		Whitebox.box.css( 'top', ($('html').height() - height)/2 );
	}
	
	// Vimeo
	else if( target.match(/vimeo.com\/(\d+).*?width=(\d+).*?height=(\d+)/i) ) {
		var m = target.match(/vimeo.com\/(\d+).*?width=(\d+).*?height=(\d+)/i);
		var id = m[1];
		var width = m[2];
		var height = m[3];
		
		Whitebox.box.html( 
			'<object id="whiteboxVimeo" width="'+width+'" height="'+height+'"><param name="allowfullscreen" value="true" />' +
				'<param name="allowscriptaccess" value="always" />' +
				'<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=bcf02c&amp;fullscreen=1" />' +
				'<embed ' +
					'src="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=bcf02c&amp;fullscreen=1" ' +
					'type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+width+'" height="'+height+'">' +
				'</embed>' +
			'</object>'
		);
		
		$('#whiteboxVimeo').click(function(){ ev.stopPropagation(); })
		Whitebox.box.unbind('click', Whitebox.close );
		Whitebox.box.css( 'left', ($('html').width() - width)/2 );
		Whitebox.box.css( 'top', ($('html').height() - height)/2 );
	}
	
	Whitebox.overlay.fadeIn( 200, function(){ $(this).css('filter','alpha(opacity=70)'); } );
	Whitebox.box.fadeIn( 200 );
	return false;
}



/* Document Ready
----------------------------------------------------------------------- */
$(function(){
	if( navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ) {
		// no Fancy stuff for the iPhone/iPod
		return;
	}
	Whitebox.init();
});
