
//  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 ZAMPLAYER_IFRAME_NAME = 'ZAMPLAYER_IFRAME_MEDIAZAM';

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; }

var HIDE_WINDOW_TIMER = null;

function initializePlayerDiv()
{
    // Create the elements to hold the player
    HOLDING_DIV = document.createElement("DIV");
    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
    createNewPlayerObject(false);
    
    // 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);
    
    // Make sure the player doesn't play in the background
    HIDE_WINDOW_TIMER = window.setTimeout('hidePlayer();', 1);
}

function createNewPlayerObject(replacePlayer)
{
    var newChild = document.createElement("iframe");
    
    // Dialog box
    newChild.id = ZAMPLAYER_IFRAME_NAME;
    newChild.name = ZAMPLAYER_IFRAME_NAME;
    newChild.style.width = '480px';
    newChild.style.height = PLAYER_HEIGHT + 'px';
    newChild.style.position = 'absolute';
    newChild.style.top = '0px';
    newChild.style.left = '0px';
    newChild.allowTransparency = !(navigator && navigator.appName && navigator.appName == "Microsoft Internet Explorer");
    newChild.scrolling = 'no';
    newChild.frameBorder = 0;
    newChild.marginWidth = '0px';
    newChild.marginHeight = '0px';
    newChild.src = 'about:blank';
    newChild.zIndex = 5;
    
    if (replacePlayer)
        HOLDING_DIV.replaceChild(newChild, PLAYER_INITIALIZED);

    PLAYER_INITIALIZED = newChild;
}

var isValidBox = null;

function portalPlayer(param1, param2, param3)
{
	window.clearTimeout(HIDE_WINDOW_TIMER);

	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
		frames[ZAMPLAYER_IFRAME_NAME].window.location.replace(
		    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();
		
		var newPlayerObject = 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 ? '&st=' + escape(param3) : '') +
			(LOG_OWNER_EVENT_ONLY == true ? '&loo=1' : '') +
			'&portal=' + escape( (PLAYER_PORTAL == null ? window.location.hostname : PLAYER_PORTAL));

        try
        {
		    frames[ZAMPLAYER_IFRAME_NAME].window.location.replace(newPlayerObject);
		}
		catch(e)
		{
		    createNewPlayerObject(true);
		    frames[ZAMPLAYER_IFRAME_NAME].window.location.replace(newPlayerObject);
		}

		window.setTimeout('mShowDemoPlayerLoop(0);', 50);
	}
}

function portalPlayer2(clientId, mediaType, showContact)
{
	window.clearTimeout(HIDE_WINDOW_TIMER);

	if (PLAYER_INITIALIZED == null)
	{
		alert('Please initialize the player before using this function.');
		return;
	}
	
	var portalImage = new Image;
	portalImage.onerror = mErrorShowPortalPlayer;
	portalImage.onload = mErrorNoPortalPlayer;
	isValidBox = null;
	portalImage.src = PLAYER_BASE + '/clientScripts/IsUserValid.aspx?o=' + PLAYER_OWNERID + '&c=' + clientId + '&rand=' + Math.random();

	var newPlayerObject = PLAYER_BASE + '/demoPlayer/demoPlayer2.aspx?o=' + (PLAYER_OWNERID == null ? clientId : PLAYER_OWNERID) +
		'&c=' + clientId +
		(CAN_CLOSE ? '&cc=1' : '&cc=0') +
		(TALENT_CONTACT ? '' : '&cf=1') +
		(mediaType != null ? '&m=' + mediaType : '') +
		(showContact != null && showContact != false ? '&ShowContact=1' : '') +
		(LOG_OWNER_EVENT_ONLY == true ? '&loo=1' : '') +
		'&portal=' + escape( (PLAYER_PORTAL == null ? window.location.hostname : PLAYER_PORTAL));

    try
    {
	    frames[ZAMPLAYER_IFRAME_NAME].window.location.replace(newPlayerObject);
	}
	catch(e)
	{
	    createNewPlayerObject(true);
	    frames[ZAMPLAYER_IFRAME_NAME].window.location.replace(newPlayerObject);
	}

	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
            OnCloseNotify();

		return;
	}
			
	if (isValidBox)
		mShowDemoPlayer();
	else
	{
	    //alert('The player requested could not be found.');  
        OnCloseNotify();
    }
}

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';

    try
    {
        frames[ZAMPLAYER_IFRAME_NAME].window.location.replace('about:blank');
    }
    catch(e) { createNewPlayerObject(true); }
    
    OnCloseNotify();
}

function OnCloseNotify() {
    if (window.OnPortalPlayerClose)
        window.OnPortalPlayerClose();
}

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();
