/*
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);
	// close by click on header
	$("#yLightbox-header", objYLightbox.$dom).click(function(event){
		objYLightbox.hide();
		objYOverlay.hide();
	});
	// close by click on overlay
	$("#yOverlay").click(function(event){
		objYLightbox.hide();
		objYOverlay.hide();
	});
}

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
		var scrollTop = $(document).scrollTop();
		var cssProps;

		this.$dom.css({
			left: (($(window).width() - this.$dom.width()) / 2) + 'px',
			top: this.initialTop + 'px'
		});

		this.$domBody[0].fCallback = fCallback;
		
		if (urlForm.indexOf("vimeo") == -1){
			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);
			}
		}else{
			var lastslash = urlForm.lastIndexOf('/');
			var vimeoString = urlForm.substring(lastslash+1);
			this.$domBody.html('<iframe src="http://player.vimeo.com/video/'+vimeoString+'?title=0&amp;byline=0&amp;portrait=0" width="560" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
			this.$domBody.css("height", 315);
		}
		
		var cssProps = {
			left: Math.floor(($(window).width() - this.$dom.width()) / 2) + 'px',
			top: scrollTop + Math.max(0, ( $(window).height() - this.$dom.height() ) / 2) + 'px'
		};
		this.$dom.css(cssProps).show();
	}

	function _methodHide(){
		this.$dom.hide();
	}

	function _displayOnStage(responseText, textStatus, XMLHttpRequest){
		//scroll to the center of the viewport
		var scrollTop = $(document).scrollTop();
		//make sure titlebar is visible, no negative offset
		$(this).parent().show();
		var cssProps = {top: scrollTop + Math.max(0, ( $(window).height() - $(this).height() ) / 2) + 'px'};
		//$(this).parent().css({top: scrollTop}).show().animate(cssProps);
		$(this).parent().css(cssProps);

		//call callback function
		if(this.fCallback && $.isFunction(this.fCallback)){
			this.fCallback();
			this.fCallback = null;
		}
	}

	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);
	}
}


