
//  This file is the sole property of MediaZam.  Any use of of this complete file
//  or use of any incomplete portion of this file MUST be licensed.  Any unlicensed
//  use of this file violates federal law.

var PLAYER_HEIGHT = 404;
var PLAYER_BASE = 'http://www.voicezam.com';

var PLAYER_INITIALIZED = null;
var HOLDING_DIV = null;
var LEGACY_PLAYER = false;
var PLAYER_OWNERID = null;
var PLAYER_PORTAL = null;
var CAN_CLOSE = true;
var TALENT_CONTACT = true;
var LOG_OWNER_EVENT_ONLY = false;

// Preload the images
var PRE_LOADIMAGE = new Image();
PRE_LOADIMAGE.src = PLAYER_BASE + '/demoplayer/images/playerShadow.png';

function initializeLegacyPlayer() { LEGACY_PLAYER = true; initializePlayerDiv(); }
function initializePlayer(ownerId) { PLAYER_OWNERID = ownerId; LEGACY_PLAYER = false; initializePlayerDiv(); }
function setOwner(ownerId) { PLAYER_OWNERID = ownerId; }
function setPortal(portal) { PLAYER_PORTAL = portal; }
function logOwnerEventOnly(logLevel) { LOG_OWNER_EVENT_ONLY = logLevel; }

function initializePlayerDiv()
{
    // Create the elements to hold the player
    HOLDING_DIV = document.createElement("DIV");
    PLAYER_INITIALIZED = document.createElement("iframe");
    var closeButton = document.createElement("DIV");
    var shadowBox = document.createElement("DIV");
    
    // Change the characteristics of the holding box
    HOLDING_DIV.style.position = 'absolute';
    HOLDING_DIV.style.top = '0px';
    HOLDING_DIV.style.left = '0px';
    HOLDING_DIV.style.overflow = 'hidden';
    HOLDING_DIV.style.width = '1px';
    HOLDING_DIV.style.height = '1px';
    HOLDING_DIV.style.visibility = 'hidden';
    HOLDING_DIV.style.zIndex = 32000;
    
    // Dialog box
    PLAYER_INITIALIZED.style.width = '480px';
    PLAYER_INITIALIZED.style.height = PLAYER_HEIGHT + 'px';
    PLAYER_INITIALIZED.style.position = 'absolute';
    PLAYER_INITIALIZED.style.top = '0px';
    PLAYER_INITIALIZED.style.left = '0px';
    PLAYER_INITIALIZED.allowTransparency = !(navigator && navigator.appName && navigator.appName == "Microsoft Internet Explorer");
    PLAYER_INITIALIZED.scrolling = 'no';
    PLAYER_INITIALIZED.frameBorder = 0;
    PLAYER_INITIALIZED.marginWidth = '0px';
    PLAYER_INITIALIZED.marginHeight = '0px';
    PLAYER_INITIALIZED.src = 'about:blank';
    PLAYER_INITIALIZED.zIndex = 5;
    
    // Close button
    if (CAN_CLOSE)
    {
        closeButton.zIndex = 10000;
        closeButton.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
        closeButton.style.position = 'absolute';
        closeButton.style.top = '4px';
        closeButton.style.left = '456px';
        closeButton.style.backgroundColor = 'transparent';
        closeButton.style.width = '20px';
        closeButton.style.height = '20px';
        closeButton.style.overflow = 'hidden';
        closeButton.style.cursor = 'pointer';
        closeButton.onclick = hidePlayer;
    }
        
    // Shadow box
    shadowBox.zIndex = 1;
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7.0)
        shadowBox.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + PLAYER_BASE + "/demoplayer/images/playerShadow.png',sizingMethod='scale')";
    else
        shadowBox.innerHTML = '<img src="' + PLAYER_BASE + '/demoplayer/images/playerShadow.png" style="width:100%;height:100%;" />';

    shadowBox.style.position = 'absolute';
    shadowBox.style.top = (BrowserDetect.browser == "Explorer" ? '0px' : '0px');
    shadowBox.style.left = '0px';
    shadowBox.style.width = '503px';
    shadowBox.style.height = '427px';
    
    // Add the information to the current document
    document.body.appendChild(HOLDING_DIV);
    HOLDING_DIV.appendChild(shadowBox);
    HOLDING_DIV.appendChild(PLAYER_INITIALIZED);
    HOLDING_DIV.appendChild(closeButton);
}

var isValidBox = null;

function portalPlayer(param1, param2, param3)
{
	if (PLAYER_INITIALIZED == null)
	{
		alert('Please initialize the player before using this function.');
		return;
	}

	if (LEGACY_PLAYER)
	{
		// param1 = ZamHandle
		// param2 = (null || defaultTab)
		// param3 = not used
		PLAYER_INITIALIZED.src = PLAYER_BASE + '/demoPlayer/demoPlayer.aspx?k=' + param1 +
			(CAN_CLOSE ? '&cc=1' : '&cc=0') +
			(param2 != null ? '&m=' + param2 : '') +
			(LOG_OWNER_EVENT_ONLY == true ? '&loo=1' : '') +
			'&portal=' + escape( (PLAYER_PORTAL == null ? window.location.hostname : PLAYER_PORTAL) );
		mShowDemoPlayer();
	}
	else
	{
		var portalImage = new Image;
		portalImage.onerror = mErrorShowPortalPlayer;
		portalImage.onload = mErrorNoPortalPlayer;
		isValidBox = null;
		portalImage.src = PLAYER_BASE + '/clientScripts/IsUserValid.aspx?o=' + PLAYER_OWNERID + '&c=' + param1 + '&rand=' + Math.random();
		
		PLAYER_INITIALIZED.src = PLAYER_BASE + '/demoPlayer/demoPlayer.aspx?o=' + (PLAYER_OWNERID == null ? param1 : PLAYER_OWNERID) +
			'&c=' + param1 +
			(CAN_CLOSE ? '&cc=1' : '&cc=0') +
			(TALENT_CONTACT ? '' : '&cf=1') +
			(param2 != null ? '&m=' + param2 : '') +
			(param3 != null ? '&wt=' + escape(param3) : '') +
			(LOG_OWNER_EVENT_ONLY == true ? '&loo=1' : '') +
			'&portal=' + escape( (PLAYER_PORTAL == null ? window.location.hostname : PLAYER_PORTAL) );

		window.setTimeout('mShowDemoPlayerLoop(0);', 50);
	}
}
		
function mErrorShowPortalPlayer() { isValidBox = true; }
function mErrorNoPortalPlayer() { isValidBox = false; }

function mShowDemoPlayerLoop(times)
{
	if (isValidBox == null)
		if (times < 10)
			window.setTimeout('mShowDemoPlayerLoop(' + (times+1) + ');', 50);
		else
			return;
			
	if (isValidBox)
		mShowDemoPlayer();
}

function mShowDemoPlayer()
{
    // Center on the screen
    var frameWidth;
    var frameHeight;
    if (self.innerWidth)
    {
	    frameWidth = self.innerWidth;
	    frameHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
	    frameWidth = document.documentElement.clientWidth;
	    frameHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
	    frameWidth = document.body.clientWidth;
	    frameHeight = document.body.clientHeight;
    }
    else return;
    
    frameWidth = (parseInt('0' + frameWidth, 10) - 480) / 2; 
    frameHeight = (parseInt('0' + frameHeight, 10) - PLAYER_HEIGHT) / 2 - 15; 
    
    if (frameWidth < 0) frameWidth = 0;
    if (frameHeight < 0) frameHeight = 0;
    
    frameHeight += (document.body.scrollTop != 0 ? document.body.scrollTop : document.documentElement.scrollTop );
    frameWidth += (document.body.scrollLeft != 0 ? document.body.scrollLeft : document.documentElement.scrollLeft );
    
    HOLDING_DIV.style.width = '503px';
    HOLDING_DIV.style.height = (PLAYER_HEIGHT + 23) + 'px';
    HOLDING_DIV.style.top = frameHeight + 'px';
    HOLDING_DIV.style.left = frameWidth + 'px';
    HOLDING_DIV.style.visibility = 'visible';
}

function hidePlayer()
{
    HOLDING_DIV.style.width = '1px';
    HOLDING_DIV.style.height = '1px';
    HOLDING_DIV.style.visibility = 'hidden';
    PLAYER_INITIALIZED.src = 'about:blank';
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
		return null;
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return null;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();