﻿//
// Methods and events used by Lenovo AMP
//

function AMP()
{
}

//
// Public properties
//
AMP.liveUrl = "http://www.lenovovision.com/amp3/mediaplayer.htm";
AMP.testUrl = "http://test.lenovovision.com/amp3/mediaplayer.htm";
//AMP.liveUrl = "http://www.jeff.com/d/projects/jobs/amp3/source/website/htdocs/mediaplayer.htm";
//AMP.testUrl = "http://test.jeff.com/d/projects/jobs/amp3/source/website/htdocs/mediaplayer.htm";
AMP.localUrl = "mediaplayer.htm";
AMP.base = "mediaplayer/";
AMP.media="mediaplayer/demo.swf";
AMP.flashVersion = "8.0.0";
AMP.locale = "en-us";				// Specific locale, like "en-us"
AMP.localeKey = "";					// Locale specifier, like "en-xx"
AMP.fid = "";
AMP.shareUrl = "http://www.lenovo.com/share/us/";

//
// Localization data
//
AMP.aIdString = new Object();
AMP.aIdString[ "cs-cz" ] =
{
	idsRequires: "Tento obsah vyžaduje:",
	ACGTB_closeWindowButton: "Zavřít [x]",
	ACGTB_closeWindowButtonTooltip: "Zavřít"
};
AMP.aIdString[ "de-de" ] =
{
	idsRequires: "Diese Anwendung benötigt:",
	ACGTB_closeWindowButton: "Schließen [x]",
	ACGTB_closeWindowButtonTooltip: "Schließen"
};
AMP.aIdString[ "en-xx" ] =
{
	idsRequires: "This content requires:",
	ACGTB_closeWindowButton: "Close [x]",
	ACGTB_closeWindowButtonTooltip: "Close"
};
AMP.aIdString[ "es-xx" ] =
{
	idsRequires: "Este contenido requiere:",
	ACGTB_closeWindowButton: "Cerrar [x]",
	ACGTB_closeWindowButtonTooltip: "Cerrar"
};
AMP.aIdString[ "fr-fr" ] =
{
	idsRequires: "Cette vidéo nécessite :",
	ACGTB_closeWindowButton: "Fermer [x]",
	ACGTB_closeWindowButtonTooltip: "Fermer"
};
AMP.aIdString[ "it-it" ] =
{
	idsRequires: "Questo contenuto richiede:",
	ACGTB_closeWindowButton: "Chiudere [x]",
	ACGTB_closeWindowButtonTooltip: "Chiudere"
};
AMP.aIdString[ "ja-jp" ] =
{
	idsRequires: "必要システム環境：",
	ACGTB_closeWindowButton: "閉じる [x]",
	ACGTB_closeWindowButtonTooltip: "閉じる"
};
AMP.aIdString[ "ko-kr" ] =
{
	idsRequires: "이 내용을 보시기 위해서는",
	ACGTB_closeWindowButton: "닫기 [x]",
	ACGTB_closeWindowButtonTooltip: "닫기"
};
AMP.aIdString[ "pl-pl" ] =
{
	idsRequires: "Ta strona wymaga",
	ACGTB_closeWindowButton: "Zamknij [x]",
	ACGTB_closeWindowButtonTooltip: "Zamknij"
};
AMP.aIdString[ "pt-br" ] =
{
	idsRequires: "Este conteúdo requer:",
	ACGTB_closeWindowButton: "Fechar [x]",
	ACGTB_closeWindowButtonTooltip: "Fechar"
};
AMP.aIdString[ "ru-ru" ] =
{
	idsRequires: "Для запуска требуется:",
	ACGTB_closeWindowButton: "Закрыть [x]",
	ACGTB_closeWindowButtonTooltip: "Закрыть"
};
AMP.aIdString[ "tr-tr" ] =
{
	idsRequires: "Bu içerik şunu gerektirir:",
	ACGTB_closeWindowButton: "Kapat [x]",
	ACGTB_closeWindowButtonTooltip: "Kapat"
};
AMP.aIdString[ "zh-cn" ] =
{
	idsRequires: "展示内容需要：",
	ACGTB_closeWindowButton: "关闭 [x]",
	ACGTB_closeWindowButtonTooltip: "关闭"
};
AMP.aIdString[ "zh-tw" ] =
{
	idsRequires: "閱覽此份文件需要",
	ACGTB_closeWindowButton: "關閉 [x]",
	ACGTB_closeWindowButtonTooltip: "關閉"
};

//
// Fired from Flash
//
AMP.onFlashEvent = function()
{
	var sEvent = arguments[ 0 ];
	
//alert( "onFlashEvent:" + sEvent );
	switch ( sEvent )
	{
		case "browseAll" :
			AMP.popUpUrl( arguments[ 1 ], "width=930,height=620,scrollbars=no,toolbar=0,resizable=0,status=1" );
		break;
		
		case "shareAsset" :
			AMP.shareAsset( arguments[ 1 ], arguments[ 2 ], arguments[ 3 ] );
		break;
		
		default :
//alert( "Unknown event:" + sEvent );
		break;
	}
}

//
//
//
AMP.shareAsset = function( sUrl, sTitle, sTrackingId )
{
	var sPopupUrl = AMP.shareUrl;
	
	sPopupUrl += "?";
	sPopupUrl += "url=" + encodeURIComponent( sUrl ) + "&";
	sPopupUrl += "title=" + encodeURIComponent( sTitle ) + "&";
	sPopupUrl += "cid=" + sTrackingId + "&";
	
	AMP.popUpUrl( sPopupUrl, "width=550,height=350" );
}

//
//
//
AMP.popUpUrl = function( sUrl, sOptions )
{
	window.open( sUrl, "amppopup", sOptions );
}

//
// Shows the AMP HTML file in a ThickBox "popup"
//
AMP.show = function( oOptions )
{
	var sLocale = oOptions[ "locale" ];
	if ( ( sLocale == undefined ) || ( sLocale == null ) )
		sLocale = getParamFromLocation( 'locale' );
	var sFid = oOptions[ "fid" ];
	if ( ( sFid == undefined ) || ( sFid == null ) )
		sFid = getParamFromLocation( 'fid' );
	var sStartId = oOptions[ "sid" ];
	if ( ( sStartId == undefined ) || ( sStartId == null ) )
		sStartId = getParamFromLocation( 'sid' );
	var sEnv = oOptions[ "env" ];
	
	AMP.locale = sLocale;
	AMP.localeKey = AMP.getLocaleKeyFromLocale( AMP.locale );
	AMP.fid = sFid;
	AMP.sid = sStartId;
	
	// if running on localhost or lenovovision, use local HTML
	var sUrl = AMP.liveUrl;
	
	if ( ( sEnv != undefined ) && ( sEnv != "live" ) )
	{
		switch ( sEnv )
		{
			case "test" :
				sUrl = AMP.testUrl;
			break;
			case "dev" :
				sUrl = AMP.localUrl;
			break;
			default :
				alert( "AMP Fatal Error: Unknown environment:" + sEnv );
			break;
		}
	}
	
	sUrl += "?";
	sUrl += "locale=" + sLocale + "&";
	sUrl += "fid=" + sFid + "&";
	if ( sStartId != null )
		sUrl += "sid=" + sStartId + "&";
	
	if ( getParamFromLocation( "noflash" ) )
	{
		sUrl += "noflash=1&";
	}
	
	sUrl += "ACGTB_iframe=true&width=632&height=538&modal=false";
	
//alert( sUrl );
	acg_tb_show( null, sUrl, null );
	
	AMP.setLocaleString( "ACGTB_closeWindowButton" );
	var oElement = document.getElementById( "ACGTB_closeWindowButton" );
	var oStringTable = AMP.aIdString[ AMP.localeKey ];
	oElement.setAttribute( "title", oStringTable[ "ACGTB_closeWindowButtonTooltip" ] );
}

//
// Sets the string data for element [sId] to use the localized data
//
AMP.setLocaleString = function( sId )
{
	var oElement = document.getElementById( sId );
	if ( oElement != null )
	{
		var oStringTable = AMP.aIdString[ AMP.localeKey ];
		if ( oStringTable != undefined )
		{
			oElement.innerHTML = oStringTable[ sId ];
		}
	}
}

//
// Sets the "requirements" page appropriate for the locale
//
AMP.establishRequirements = function()
{
	//
	// Establish requirements
	//
	AMP.setLocaleString( "idsRequires" );
	
	var oRequirements = document.getElementById( "idsRequirements" );
	var sRequirements = "<li>" + "Adobe Flash " + AMP.flashVersion + "</li>";
	oRequirements.innerHTML = sRequirements;
}

//
// Called by the AMP HTML.
// Sets the "requirements" page and creates the Flash object.
//
AMP.init = function()
{
	//
	// Set the locale
	//
	if ( getParamFromLocation( 'locale' ) )
	{
		AMP.locale = getParamFromLocation( 'locale' );
	}
	AMP.localeKey = AMP.getLocaleKeyFromLocale( AMP.locale );
	if ( getParamFromLocation( "fid" ) )
		AMP.fid = getParamFromLocation( "fid" );
	if ( getParamFromLocation( "sid" ) )
		AMP.sid = getParamFromLocation( "sid" );
		
	//
	// Establish the requirements.
	// Note: We are piggybacking on the "addDomLoadEvent" functionality provided by SWFObject
	//
	swfobject.addDomLoadEvent( AMP.establishRequirements );
	
	//
	// Attempt to create Flash player
	//
	var flashvars =
	{
		fid: AMP.fid,
		locale: AMP.locale,
		sid: AMP.sid
	};
	if ( getParamFromLocation( 'debug' ) )
	{
		flashvars.g_iDebugLevel = getParamFromLocation( 'debug' );
	}
		
	//
	// Note: We CANNOT use the top level URL as the referrer. It is a security violation to access
	// the DOM if the top level is outside the LV domain (which it would be in normal use). So,
	// we can only utilize the 'referrer' option if one is passed in.
	//
	//var sReferrer = top.location.href;
	var sReferrer = "";
	if ( getParamFromLocation( 'referrer' ) )
	{
		sReferrer = getParamFromLocation( 'referrer' );
	}
		
	flashvars.referrer = escape( sReferrer );
	var params =
	{
		wmode: "transparent",
		base: AMP.base,
		menu: "false",
		allowScriptAccess: "always"
	};
	var attributes = {};

	if ( !getParamFromLocation( 'noflash' ) )
	{
		swfobject.embedSWF( AMP.media, "flashid", "632", "538", AMP.flashVersion, "", flashvars, params, attributes );
	}
}

AMP.getLocaleKeyFromLocale = function( sLocale )
{
	for ( var prop in AMP.aIdString )
	{
		// replace "xx" with "*"
		var oRegExp = new RegExp( "-xx" );
		var sMatch = prop.replace( oRegExp, "-*" );
		oRegExp = new RegExp( sMatch );
		if ( oRegExp.test( sLocale ) )
		{
			return prop;
		}
	}
	return "";
}
