var videoOverlay = null;
var videoPlayer = null;
var videoDivs = null;
var videoQuestion = null;
var videoMx = null;

function onVideoResize(width, height, olid)
{
	if( videoDivs == null ) return;
	if( videoMx == null ) return;

	while( true ) {
		var horz = width - videoMx.w;
		var vert = (height - (videoDivs.nav.offsetHeight + videoDivs.title.offsetHeight + 20)) - videoMx.h;
		
		if( (horz > 0) && (vert > 0) && (videoMx.fw > videoMx.w) ) {
			videoMx.w = videoMx.fw;
			videoMx.h = videoMx.fh;			
			var vid = document.getElementById( "videoShow" );
			
			if( vid ) {
				vid.setAttribute( "width", videoMx.w );
				vid.setAttribute( "height", videoMx.h );
			} else {
				break;
			}
			continue;
		} else if( (horz >= 0) && (vert >= 0) ) {
			if( horz > 0 ) {
				videoDivs.videoDiv.style.marginLeft = Math.floor(horz/2) + "px";
				videoDivs.videoDiv.style.marginRight = Math.floor(horz/2) + "px";
			} else {
				videoDivs.videoDiv.style.marginLeft = "0px";
				videoDivs.videoDiv.style.marginRight = "0px";
			}
		
			if( vert > 0 ) {
				videoDivs.videoDiv.style.marginTop = Math.floor(vert/2) + "px";
				videoDivs.videoDiv.style.marginBottom = Math.floor(vert/2) + "px";
			} else {
				videoDivs.videoDiv.style.marginTop = "0px";
				videoDivs.videoDiv.style.marginBottom = "0px";
			}
			
			break;
		}
		
		if( horz < vert ) {
			videoMx.w = width;
			videoMx.h = Math.floor( width * (videoMx.fh / videoMx.fw) );
		} else {
			videoMx.h = (height - (videoDivs.nav.offsetHeight + videoDivs.title.offsetHeight + 20));
			videoMx.w = Math.floor( (height - (videoDivs.nav.offsetHeight + videoDivs.title.offsetHeight + 20)) * (videoMx.fw / videoMx.fh) );
		}
		
		var vid = document.getElementById( "videoShow" );
		
		if( vid ) {
			vid.setAttribute( "width", videoMx.w );
			vid.setAttribute( "height", videoMx.h );
		} else {
			break;
		}
	}
}

function onVideoClose()
{
	destroyVideoSpace();	
}

function createVideoSpace()
{
	videoDivs = {
		main : document.createElement( "DIV" ),
		title : document.createElement( "DIV" ),
		heading : document.createElement( "h1" ),		
		videoDiv : document.createElement( "DIV" ),
		nav : document.createElement( "DIV" ),
		btnExit : document.createElement( "A" )
	};
	
	videoDivs.main.id = "galleryView";
	
	setClassName( videoDivs.title, "title" );
	setClassName( videoDivs.nav, "nav" );
	setClassName( videoDivs.videoDiv, "content" );

	videoDivs.main.appendChild( videoDivs.title );
	videoDivs.title.appendChild( videoDivs.heading );
	videoDivs.main.appendChild( videoDivs.videoDiv );
	videoDivs.main.appendChild( videoDivs.nav );
	
	setClassName( videoDivs.btnExit, "exit" );
	var h = "javascript:onVideoClose()";
	
	if( videoDivs.btnExit.href !== undefined )
		videoDivs.btnExit.href = h;
	else
		videoDivs.btnExit.setAttribute( "href", h );

	videoDivs.nav.appendChild( videoDivs.btnExit );
}

function destroyVideoSpace()
{
	videoOverlay.removeChild( videoDivs.main );
	videoDivs.main.removeChild( videoDivs.nav );
	videoDivs.main.removeChild( videoDivs.videoDiv );
	videoDivs.title.removeChild( videoDivs.heading );
	videoDivs.main.removeChild( videoDivs.title );
	videoOverlay.destroy();
	videoQuestion = null;
	videoOverlay = null;
	videoDivs = null;
	videoPlayer = null;
	videoMx = null;
}

function onVideoOpen(width, height, video, title)
{
	if( videoOverlay != null ) return;

	videoMx = {
		w : 640,
		h : 385,
		fw : 640,
		fh : 385,
		vw : width,
		vh : height
	};
	
	createVideoSpace();

	videoOverlay = new Overlay( "video", -50, -50, "fog", "videoView", onVideoResize );
	videoOverlay.create();
	
	videoOverlay.addChild( videoDivs.main );
	
	videoDivs.heading.innerHTML = title;
	
	videoPlayer = new SWFObject( video + "/play", "videoShow", 640, 385, "9", "#000000" );
	videoPlayer.addParam("quality", "high");
	videoPlayer.addParam("wmode", "transparent");
	videoPlayer.addParam("salign", "t");
	videoPlayer.addParam("style", "z-Index: 1;");
	videoPlayer.addParam("allowFullScreen", "true");
	videoPlayer.addParam("FlashVars", "v=" + video );
	videoPlayer.write( videoDivs.videoDiv );

	videoOverlay.positionElements();

	return false;
}

function onVideoInfoLoaded(id, ok, response)
{
	if( ok ) {
		videoQuestion = null;
		onVideoOpen( response.width, response.height, response.video, response.title );
	}
}

function onVideoOpenSimple(video)
{
	if( videoQuestion != null ) return;

	var url = video + "/view(info).jo";
	
	videoQuestion = new Question( video, url, null, onVideoInfoLoaded );
	videoQuestion.ask();
}