var flashPlayerYouTubeVersion = '3';
//var flashPlayerLocal = 'templates/rescuetec/flash/flowplayer-3.2.7.swf';
var flashPlayerLocal = 'templates/rescuetec/flash/toobplayer_gray.swf';
var flashPlayerLocalDir = '../../../media/videos/';
var dur = 0.3;
var opacity = 0.65;
var file = '';
var local = '';
var body = '';
var offsetTop = 0;


function flashLightbox() {	
	file = $("products_video").innerHTML;
	local = $("products_video_is_local").innerHTML;
	
	eval($("products_video_etracker").innerHTML);
	
	var content = getFlashLightboxContent();
	body = document.getElementsByTagName('body')[0];
	new Insertion.Top(body, content);
	
	offsetTop = getScroll();
	$('productVideoWrapper').setStyle({
		'position': 'absolute',
		'top': offsetTop + "px",
		'left': "0px"
	});
	$('productVideoWrapper').show();
	
	new Effect.Appear('productVideoWrapper',{from: 0, to: opacity, duration: dur, fps:100});//, afterFinish: setFlashVideo});
	setCloseButton();
	
	body.setStyle({overflow:'hidden'});
	$('productVideoWrapper').show();
	$("productVideoWrapper").observe('click', hideFlashVideoWrapper);
	Event.observe(window, 'keypress', hideFlashVideoWrapper);
	return true;
}

function setCloseButton() {
	var closeButton = new Element('a', {'id': 'closeFlashVideoButton', 'href': '#'});
	closeButton.appendChild(new Element('img', {'src': $("products_video_button_close").innerHTML}));
	var margin = offsetTop - 172;
	closeButton.setStyle({
		marginTop: margin + 'px'
	});
	body.insert(closeButton, {'position': 'top'});
	$("closeFlashVideoButton").observe('click', hideFlashVideoWrapper);
}

function getFlashLightboxContent() {
	if( local == 'true' ) {
		var content = getLightboxContentLocal();
	} else {
		var content = getLightboxContentYouTube();
	}
	return content;
}

function getLightboxContentYouTube() {
	return '<div id="productVideoWrapper">'
		 + '<div id="productVideo">'
	     + '<object>'
		 + '<param name="movie" value="'
		 + file
		 + '?version='
		 + flashPlayerYouTubeVersion
		 + '&autoplay=1" />'
		 + '<param name="allowFullScreen" value="true" />'
		 + '<param name="allowScriptAccess" value="always" />'
		 + '<embed src="'
		 + file
		 + '?version='
		 + flashPlayerYouTubeVersion
		 + '&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="100%" height="100%" />'
		 + '</object>'
		 + '</div>'
		 + '</div>';
}

function getLightboxContentLocal() {
	return '<div id="productVideoWrapper">' 
	     + '<div id="productVideo">'
	     + '<object style="height: 100%; width: 100%">'
	     + '<param name="flashvars" value="url='
	     + flashPlayerLocalDir + file
	     + '&autoPlay=true&showFullScreenButton=true&volume=50" />'
	     + '<param name="autoplay" value="true" />'
	     + '<param name="showFullScreenButton" value="true" />'
	     + '<param name="volume" value="50" />'
	     + '<param name="scale" value="noscale" />'
	     + '<param name="quality" value="high" />'
	     + '<param name="bgcolor" value="#FFFFFF" />'
	     + '<param name="src=" value="' + flashPlayerLocal + '" />'
	     + '<param name="name" value="productVideo" />'
	     + '<embed height="100%" width="100%" flashvars="url='
	     + flashPlayerLocalDir + file
	     + '&autoPlay=true&showFullScreenButton=true&volume=50"'
	     + 'wmode="window" menu="false" allowscriptaccess="always" scale="noscale" allowfullscreen="true" quality="high" bgcolor="#FFFFFF" name="productVideo" id="productVideo" src="'
	     + flashPlayerLocal
	     + '" type="application/x-shockwave-flash">'
	     + '</object></div>'
		 + '</div>';
}

function hideFlashVideoWrapper(event) {
	Effect.Fade('productVideoWrapper', {duration: dur, fps:100, afterFinish: removeFlashVideoWrapper});
	body.setStyle({overflow:'auto'});
	event.stop();
	return false;
}

function removeFlashVideoWrapper() {
	$("productVideoWrapper").innerHTML = '';
	$("productVideoWrapper").remove();
	$("closeFlashVideoButton").remove();
	
}
