/*
 * MES Videoconsole embed
 */

var playerURL = "http://mediaeventservices.com/videoconsole/player.html";

/**
 * Gets the console URL for a given configuration
 *
 * @param	identifierOrObject		Clip identifier, e.g. "security-demo"
 *									or object such as {id: "security-demo", title: "(title)"} etc.
 */
function getConsoleURL(identifierOrObject) {
    var url = playerURL;
    
    if (typeof(identifierOrObject) == 'object') {
    	/* Serialize object into querystring */
   		for (key in identifierOrObject) {
    		url += (url == playerURL ? "?" : "&") + key + "=" + encodeURIComponent(identifierOrObject[key]);
    	}
    }
    else {
    	/* Simplified invocation */
		url = playerURL + "?" + identifierOrObject + ".xml";
    }
	
	return url;
}

/**
 * Launches the MES video console
 * 
 * @param	identifierOrObject		Clip identifier, e.g. "security-demo"
 *									or object such as {id: "security-demo", title: "(title)"} etc.
 * @param	smallOrWidth (optional)	Set to true to open a smaller version for players without Slideflow.
 *									Provide width and height as integers.
 * @param	height (optional)		Target window height
 */
function openConsole(identifierOrObject, smallOrWidth, height) {
		
	var winName = "Popup";
	var winWidth;
	var winHeight;
	
	if (height === undefined) {	
		winWidth = 800;
		winHeight = (smallOrWidth ? 525 : 580);
	}
	else {
		winWidth = smallOrWidth;
		winHeight = height;
	}
	
    var scrWidth = screen.width;
    var scrHeight = screen.height;
    var pos_x = (scrWidth/2)-(winWidth/2);
    var pos_y = (scrHeight/2)-(winHeight/2);
    
    var popup = window.open(getConsoleURL(identifierOrObject), winName ,"width="+winWidth+",height="+winHeight+",left="+pos_x+",top="+pos_y+",status=no,location=no,scrollbars=no,resizable=yes");
    
    if (popup==null || typeof(popup)=="undefined") {
    	alert("Video player popup was blocked - please deactivate your popup blocker or use another browser!");
 	} else {
    	popup.focus();
 	}
 	
    return false;
}

/**
 * Embeds the MES video console in an iframe
 * 
 * @param	identifierOrObject		Clip identifier, e.g. "security-demo"
 *									or object such as {id: "security-demo", title: "(title)"} etc.
 * @param	width (optional)		Embed width
 * @param	height (optional)		Embed height
 */
function embedConsole(identifierOrObject, width, height) {
	document.write('<iframe src="' + getConsoleURL(identifierOrObject) + '" style="width: ' + width + 'px; height: ' + height + 'px; border: none;" scrolling="no" frameborder="0"><!-- Video window --></iframe>');}
