/*
jQuery functions for creating a simple light box
Copyright (c) 2009 Ylab, http://creativecommons.org/licenses/by-sa/3.0/
Author: Yohan Creemers

requires: jquery.min.js
*/
var objYOverlay, objYLightbox;

function showViewer(urlForm, sCaption, fCallback){
	if(!objYOverlay){
		objYOverlay = new YOverlay('yOverlay');
	}
	objYOverlay.show();
	objYLightbox = new YLightBox('yLightbox', urlForm, sCaption);
	objYLightbox.show(fCallback);
	objYLightbox.$dom.click(function(event){
		objYLightbox.hide();
		objYOverlay.hide();
	});
	return false;
}

function YOverlay(id){
	//styling properties
	this.YOverlayBgColor = '#000';
	this.YOverlayOpacity = 0.5;

	//public methods
	this.show = methodShow;
	this.hide = methodHide;
	this.resize = methodResize;
	this.hideOnClick = methodHideOnClick;

	//internal functions
	function methodShow(){
		//hide some elements to avoid these elements to appear above the YOverlay in IE
		this.$visible = $('embed:visible, object:visible, select:visible').css({ 'visibility' : 'hidden' });
		//apply styling and show YOverlay
		this.$YOverlay.css({
			backgroundColor: this.YOverlayBgColor,
			opacity: this.YOverlayOpacity
		});
		this.resize();
		this.$YOverlay.fadeIn();
	}

	function methodHide(){
		if(this.$visible){
			this.$visible.css({ 'visibility' : 'visible' });
			this.$visible = null;
			this.$YOverlay.fadeOut();
		}
	}

	function methodResize(){
		this.$YOverlay.css({
			width: $(window).width() + 'px',
			height: $(document).height() + 'px'
		});
	}

	function methodHideOnClick(){
		$(this.$YOverlay).click(function(){obj.hide();});
	}

	//init, create dom element, set initial styling
	var obj = this;
	this.$visible;
	$('body').append('<div id="' + id + '"></div>');
	this.$YOverlay = $('#' + id);
	this.$YOverlay.css({
		position: 'absolute',
		top: 0,
		left: 0,
		zIndex: 90
	});
	$(window).resize(function(){obj.resize();});
}

function YLightBox(id, urlForm, caption){
	//styling properties
	this.initialTop = 50; //integer in pixels

	//public methods
	this.show = _methodShow;
	this.hide = _methodHide;

	//internal functions
	function _methodShow(fCallback){
		//apply styling and load html
		this.$dom.css({
			left: (($(window).width() - this.$dom.width()) / 2) + 'px',
			top: this.initialTop + 'px'
		});

		this.$domBody[0].fCallback = fCallback;
		var q = urlForm.split('v=');
		if(q.length == 2){
			var videourl = 'http://www.youtube-nocookie.com/v/' + q[1] + '&amp;hl=nl&amp;fs=0&amp;rel=0&amp;hq=0&amp;color1=0xaaaaaa&amp;color2=0xcccccc';
			var videohtm = '<object class="video"><param name="movie" value="' + videourl + '"></param><param name="allowscriptaccess" value="always"></param><embed class="video" src="' + videourl + '" type="application/x-shockwave-flash" allowscriptaccess="always" width="560" height="340"></embed></object>';
			this.$domBody.html(videohtm);
		}else{
			this.$domBody.load(urlForm);
		}
		this.$dom.css({
			left: (($(window).width() - this.$dom.width()) / 2) + 'px',
			top:  (($(window).height() - this.$dom.height()) / 2) + 'px'
		}).show();
	}

	function _methodHide(){
		this.$dom.hide();
	}

	function _displayOnStage(responseText, textStatus, XMLHttpRequest){
		//scroll to the center of the viewport
		var scrollTop = $(document).scrollTop();
		$(this).parent().css({top: scrollTop}).show().animate({
			top: scrollTop + (($(window).height() - $(this).height()) / 2) + 'px'
		});
		if(this.fCallback && $.isFunction(this.fCallback)){
			this.fCallback();
		}
	}

	var obj = this;
	if($('#' + id).length == 0){
		//init, create dom element, set initial styling
		$('body').append(
			'<div id="' + id + '">' +
				'<div id="' + id + '-header"></div>' +
				'<div id="' + id + '-body"></div>' +
			'</div>');
	}
	this.$dom = $('#' + id);
	this.$domHeader = $('#' + id + '-header');
	this.$domBody = $('#' + id + '-body');
	this.$dom.css({
		position:'absolute',
		zIndex: 91
	});
	if(caption){
		this.$domHeader.html(caption);
	}
}

