// init cursor
var cursor = {"posx":0,"posy":0};
var cursor_isOpera = (navigator.userAgent.indexOf('Opera') != -1);
var cursor_isIE = (!cursor_isOpera && navigator.userAgent.indexOf('MSIE') != -1);

// popup
function close_popup() {
	var obj = document.getElementById("popup");
	if(obj) {
		obj.style.visibility="hidden";
	};
	document.getElementById("popup_content").innerHTML = "";
};
function open_popup(code) {
    code = code.replace(/&/g,"&amp;");
	code = code.replace(/</g,"&lt;");
	code = code.replace(/>/g,"&gt;");
	document.getElementById("popup_content").innerHTML = code;
	//document.getElementById("popup_content").innerHTML = "hello world";
	var obj = document.getElementById("popup");
	if(obj) {
		// cursor must be defined in onclick handler
		obj.style.top = cursor.posy;
		obj.style.left = cursor.posx - 400;
		obj.style.visibility="visible";
	};
};


// change production setting
function toggleProduction(customer, project, visbyte, check) {
	document.location = "production_toggle.php?customer=" + customer + "&project=" + project + "&visbyte=" + visbyte + "&check=" + check;
};
// change autoplay setting
function toggleAutoplay(project, visbyte, check) {
	document.location = "dashboard_project.php?project=" + project + "&autoplay=" + visbyte + "&check=" + check;
};
// create link code
function genLinkCode(file) {
	// this be some bad obfuscation! :-)
	var thefile = 123456 * parseInt(file);
	var code = "http://www.visstream.com/view.html?file=" + thefile;
	open_popup(code);
	//document.getElementById("link").value = code;
};
// create hd link code
function genHDLinkCode(id) {
	var code = "http://visstream.com/previewHD.php?id=" + id;
	open_popup(code);
};


// create embed code
function genRawLinkCode(code) {
	document.getElementById("link").value = code;
};
// create embed code
function genEmbedCode(file,width,height,autoplay) {
	if (width=="" || width==0) {
		alert("You must provide a width for this Visbyte");
		return;
	};
	if (height=="" || height==0) {
		alert("You must provide a height for this Visbyte");
		return;
	};
	// add 28 pixels for the menu bar
	height = height + 28;
	
	if (autoplay == 1) {
		autoplay = "true";
	} else {
		autoplay = "false";
	};
	var code;
	code = "<script \n"+
	"	type='text/javascript' \n"+
	"	src='http://visstream.com/visplayer.js'> \n"+
	"</script> \n"+
	"<script>  \n"+
	"var path = escape('http://visstream.com/video.php?file=" + file + "'); \n"+
	"flashembed('visbyte', \n"+
	"	{src:'http://visstream.com/visplayer.swf', \n"+
	"	width: " + width + ", \n"+
	"	height: " + height + "}, \n"+
	"	{config: \n"+
	"	{autoPlay: " + autoplay + ", \n"+
	"	 autoRewind: false, \n"+
	"	 loop: false, \n"+
	"	 showMenu: false, \n" +
	"    initialVolumePercentage: 100, \n" +
	"    videoFile: path, \n"+
	"    initialScale: 'scale', \n"+ 
	"    useNativeFullScreen: true} \n"+
	"    } \n"+
	"); \n"+
	"</script> \n"+
	"<div id='visbyte'></div>";

	open_popup(code);
};
// create HD embed code
function genHDEmbedCode(visbyte,autoplay) {
	var path = escape('http://visstream.com/video.php?t=mp4&v=');
	
	if (autoplay == 1) {
		autoplay = "true";
	} else {
		autoplay = "false";
	};
	var code = "";
	code += "<script src='http://visstream.com/videojs/video.js' type='text/javascript' charset='utf-8'></script>\n";
	code += "<link rel='stylesheet' href='http://visstream.com/videojs/video-js.css' type='text/css' media='screen' title='Video JS' charset='utf-8'>\n";
	code += "<div class='video-js-box'>\n";
	code += "<video class='video-js' width='512' height='288' controls preload poster='http://visstream.com/images/poster.png'>\n";
	code += "<source src='http://visstream.com/video.php?t=mp4&v=" + visbyte + "' type=\"video/mp4; codecs='avc1.42E01E, mp4a.40.2'\" />\n";
	code += "<source src='http://visstream.com/video.php?t=webm&v=" + visbyte + "' type=\"video/webm; codecs='vp8, vorbis'\" />\n";
	code += "<source src='http://visstream.com/video.php?t=ogg&v=" + visbyte + "' type=\"application/ogg; codecs='theora, vorbis'\" />\n";
	code += "<object class='vjs-flash-fallback' width='512' height='288' type='application/x-shockwave-flash' data='http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf'>\n";
	code += "<param name='movie' value='http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf' />\n";
	code += "<param name='allowfullscreen' value='true' />\n";
	code += "<param name='flashvars' value=\"config={'playlist':['http://visstream.com/images/poster.png', {'url': '" + path + visbyte + "','autoPlay':false,'autoBuffering':true}]}\" />\n";
	code += "<img src='http://visstream.com/images/poster.png' width='512' height='288' alt='Poster Image' title='No video playback capabilities.' />\n";
	code += "</object>\n";
	code += "</video>\n";
	code += "</div>\n";
	code += "<script type='text/javascript' charset='utf-8'>VideoJS.setupAllWhenReady();</script>";
	code += "<!-- NOTE: poster and swf must be an absolute url to work in firefox -->";
	open_popup(code);
};

function show(obj) {
	if (document.getElementById(obj)) {
		document.getElementById(obj).style.display="inline";	
	};
};

function mouseFocus(obj) {
	obj.style.backgroundColor = "#ffc";
};
function mouseBlur(obj) {
	obj.style.backgroundColor = "";
};
function getCursorPos(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)	{
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX;
		posy = e.clientY;
		if (cursor_isIE) {
			posx += document.body.scrollLeft;
			posy += document.body.scrollTop;
		};
	};
	return {"posx":posx,"posy":posy};
};
