
function videoView(identifier, path, width, height) {
    if (dialogActive) return false;
    dialogActive = true;

	//content div
	var content = '<div id="video" style="padding: 2px;"><div id="videoCell" style="z-index: 999; width: ' + width + 'px; height: ' + height + 'px;">&nbsp;</div>' +  videoGetClose() + '</div>';	
	
	// FF + Mac can't handle video on DOM created divs...
	if ( /mac/.test(navigator.userAgent.toLowerCase()) && /firefox/.test(navigator.userAgent.toLowerCase()) ) {
		dialogActive = false;
		content = content.replace('&nbsp;', videoStart(identifier, path, width, height)).replace(videoGetClose(), '');
		videoPopup(content, width, height);
		//trackVideo(path);
		return;
	}
	
    // draw the window in the center
    windowDraw(50);
	
	if ( !(/ppc/.test(navigator.userAgent.toLowerCase()) ) && !(/firefox\/1\.0/.test(navigator.userAgent.toLowerCase())) ) {
		$('centerTable').style.background = "#fff url(/i/icons/progress.gif) no-repeat";
		$('centerTable').style.backgroundPosition = parseInt(width/2) + 'px ' + parseInt(height/2) + 'px';
	}

	var pageWidth = Math.min(document.documentElement.clientWidth, window.innerWidth ? window.innerWidth : Infinity);
    var newLeft = ((pageWidth / 2) - Math.round(width / 2)).toString();

	$('centerTable').style.position = 'absolute';
	$('centerTable').style.zIndex = "998";
	$('centerTable').style.left = (newLeft) + 'px';
	$('centerTable').style.width = (width+4) + 'px';
	$('centerTable').style.padding = '0';
	$('centerTable').innerHTML = content;
	
	var disableScrollEvent = true;
    windowAppear(disableScrollEvent);
	
	setTimeout(function() {
		$('videoCell').innerHTML = videoStart(identifier, path, width, height);
	},1200);
	trackVideo(path);
}

function videoPopup(html, imageWidth, imageHeight) {
   	var left = Math.floor( (screen.width - imageWidth) / 2);
    var top = Math.floor( (screen.height - imageHeight) / 2);
	newWindow = window.open("","newWindow","resizable=1,width="+(imageWidth+10)+",height="+(imageHeight+10)+",left="+left+",top="+top);
	newWindow.document.open();
	newWindow.document.write('<html><title>Getting started with Intervals</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onload="top.focus()">'); 
	newWindow.document.write(html); 
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}

function videoStart(identifier, path, width, height) {

	var w = new SWFObject("/flash/flvplayer.swf","v"+identifier,width,height,"9");
	w.addVariable("file",path);
	w.addVariable("autostart","true");
	w.addVariable("backcolor","0xffffff");
	w.addVariable("frontcolor","0x000000");
	w.addVariable("lightcolor","0x000000");	
	//w.addVariable("image", path + ".jpg");
	w.addVariable('overstretch', 'fit');
	w.addVariable('displayheight', 480);
	w.addVariable('bufferlength', 30); 
	//w.addParam("WMode", "transparent");
	//w.addParam("menu", "false");
	//w.addParam("quality", "high");
	w.addParam('scale','exactfit');
	w.addParam("allowfullscreen", "true");
	return w.getSWFHTML();
}

function videoGetClose() {
	var s = '';
	s += '<div style="text-align: right;"><input id="videoCloseButton" type="button" name="f_submit" value="Close Window" onclick="videoDestroy();" class="submit" /></div>';
	return s;
}

function videoDestroy() {
	$('videoCell').innerHTML = '';
	windowDestroy();
    setTimeout('videoDestroyComplete()',1500);
}

function videoDestroyComplete() {
    dialogActive = false;
}

function trackVideo(path) {
	var uri = '/index.php';
	var jx = new Ajax.Request(
		uri,
		{
			method: 'get',
			parameters: 'module=system&action=trackvideo&f_video=' + encodeURIComponent(path),
			onSuccess: videoTrackEffect,
			onFailure: videoTrackError
		});
}
function videoTrackEffect(response) {
	//do nothing
}
function videoTrackError(response) {
	var xmlObj = response.responseXML;
	alert(processNode(xmlObj.getElementsByTagName('error')).toString().unescapeHTML());
}

