
var RequiredFlashVersion = "9.0.21";

// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 21;

var MOVIE_NAME  = "flashcontent";
//the secure url must always be fully qualified and never relative
var SWF_FILE	= "http://cdns.goldmail.com/Shared/slideShowPlayer.swf?ver=315&h=44800b28b5a833634faf22c6bbbfe0b7";

//document referrer
var FVReferrer;
var _gmdebug = false;


///////////////flash version/////////////////
// Version check based upon the values defined in globals
var hasReqestedVersion;
// Get flash version
var FlashVer;

/////////////query string//////////////
var FVGMID;
var FVGMServer; 
var FVViewerName; 
var FVPin; 
var FVSplash; 
var FVSplashSound;
var FVFullScreen;

///////////browser/os info///////////////
var Browser;
var OSName;
var BrowserVer;
var FVScreenHeight;
var FVScreenWidth;
var FVBrowserHeight;
var FVBrowserWidth;

//various stuff that gets passed to the slideshow
var GoldMailFlashVars;

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isAOL = (navigator.userAgent.indexOf("AOL") != -1 || navigator.userAgent.indexOf("America Online") != -1) ? true : false;
var FVCPath;

////////////////////entry point to playerloader///////////////
function runGM(){
  var bodyBackgroundColor;
  var x = document.getElementById("body");
  document.getElementById('noscriptblock').style.display = 'none';

  if (window.getComputedStyle){
    bodyBackgroundColor = window.getComputedStyle(x,null).getPropertyValue("background-color");
    bodyBackgroundColor = rgbToString(bodyBackgroundColor);
  } else {
    bodyBackgroundColor = x.currentStyle["backgroundColor"];
  }
  document.bgColor = bodyBackgroundColor;

  try{
    FVCPath = document.getElementById('player_url').innerHTML;
  }
  catch(e){
    FVCPath = "./";
  }

  try{
    FVReferrer = document.referrer;   
    getFlashInfo();
    if(FlashVer == -1){
      document.getElementById('noflashblock').style.display = 'block';    
    } else if(!hasReqestedVersion){
      document.getElementById('notenoughflashblock').style.display = 'block';    
    } else{
      getParms();
      getBrowserInfo();
      createGmFlashVars();
      runSlideShow();
    }
  }
  catch(e){
    if(_gmdebug)
      alert("Error" + e.toString());
  }

}

function runSlideShow(){
  var flashHeight = "100%";

  if (navigator.userAgent.indexOf("Safari")!=-1) {
     flashHeight = "89%";
  } 
	AC_FL_RunContent(
			"src", SWF_FILE,
			"FlashVars", GoldMailFlashVars,
			"width", "100%",
			"height", flashHeight,
			"align", "middle",
			"id", "flashcontent",
			"quality", "high",
			"bgcolor", document.bgColor,
			"name", MOVIE_NAME,
			'allowFullScreen', 'true',
			"allowScriptAccess","always",
			"wmode","opaque",
			"type", "application/x-shockwave-flash",
			'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
			);
}

function createGmFlashVars(){


  GoldMailFlashVars = "FVGMID=" + FVGMID + "&" +
	"FVCPath="		+ FVCPath + "&" +
	"FVOS="			+ OSName + "&" +
	"FVBrowser="		+ Browser + "&" +
	"FVBrowserVer="		+ BrowserVer + "&" +
	"FVFlashVer="		+ FlashVer + "&" +
	"FVScreenHeight="	+ FVScreenHeight + "&" +
	"FVScreenWidth="	+ FVScreenWidth + "&" +
	"FVBrowserHeight="	+ FVBrowserHeight + "&" +
	"FVBrowserWidth="	+ FVBrowserWidth + "&" +
	"FVReferrer="		+ FVReferrer + "&" +
	"FVGMServer="		+ FVGMServer + "&" +
	"FVViewerName="		+ FVViewerName + "&" +
	"FVPin="			+ FVPin + "&" +
	"FVSplash="			+ FVSplash + "&" +
	"FVSplashSound="	+ FVSplashSound + "&" +
	"FVVisitorID=" 		+ getCookie( "VisitorID" ); 
}

function getBrowserInfo(){

  var navigatorVersion = navigator.appVersion;
  var navigatorAgent = navigator.userAgent;

  // Detect Operating System
  OSName="Unknown OS";
  if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
  if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
  if (navigator.appVersion.indexOf("Unix")!=-1) OSName="UNIX";
  if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

  // Detect Browser
  Browser="Unknown Browser";
  if (navigator.userAgent.indexOf("Firefox")!=-1) Browser="Firefox";
  if (navigator.userAgent.indexOf("Safari")!=-1) Browser="Safari";
  if (navigator.userAgent.indexOf("IE")!=-1) Browser="IE";
  if (navigator.userAgent.indexOf("Opera")!=-1) Browser="Opera";

  // Get Browser Version
  var BrowserVer = "Unknown";
  if ( (versionOffset=navigatorAgent.indexOf("MSIE"))!=-1) {
	  //Get Internet Explorer Version
	  BrowserVer = parseFloat(navigatorAgent.substring(versionOffset+5));
  } else if ( (versionOffset=navigatorAgent.indexOf("Opera"))!=-1 ) {
	  //Get Opera version
	  BrowserVer = parseFloat(navigatorAgent.substring(versionOffset+6));
  } else if ( (versionOffset=navigatorAgent.lastIndexOf('/')) ) {
	  //Get version of Firefox, safari etc
	  BrowserVer = parseFloat(navigatorAgent.substring(versionOffset+1));
  }


  // Get screen height and width
  FVScreenHeight = screen.height;
  FVScreenWidth = screen.width;

  // Get browser height and width
  if (parseInt(navigator.appVersion)>3) {
	   if (navigator.appName=="Netscape") {
		  FVBrowserHeight = window.outerHeight;
		  FVBrowserWidth = window.outerWidth;
	   }
	   if (navigator.appName.indexOf("Microsoft")!=-1) {
	 	  FVBrowserHeight = window.screen.availHeight;
		  FVBrowserWidth = window.screen.availWidth;
	   }
  }
}

// get parameters from the URL
function getParms(){
  FVGMID 		 = urlparams("GMID");
  if (FVGMID == "undefined") {
    FVGMID 		 = urlparams("gmid");   
  }  
  if (FVGMID == "undefined") {
    FVGMID = document.getElementById("player_gmid").innerHTML;
  }  

  FVGMServer 	 = urlparams("SRVR"); 
  FVViewerName = urlparams("NAME"); 
  FVPin 		 = urlparams("PIN");   
  FVSplash 		 = urlparams("splash"); 
  FVSplashSound		 = urlparams("splash_sound"); 
  FVFullScreen		 = urlparams("full_screen"); 

}

function getFlashInfo(){
  hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  FlashVer = GetSwfVer();
}


function urlparams( id )
{
  id = id.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexp = "[\\?&]"+id+"=([^&#]*)";
  var regexO = new RegExp( regexp );
  var result = regexO.exec( window.location.href );
  if( result == null )
    return "undefined";
  else
    return result[1];
}

// -----------------------------------------------------------------------------

function extractLinksFromIframe(){

var links = frames['linkloader'].document.body.innerHTML;
document.getElementById("GMLinks").innerHTML+= frames['linkloader'].document.body.innerHTML;

}

// The color format is "rgb(rrr, ggg, bbb)" or "#rrggbb"
function rgbToString(color)
{
	var rgb = new stringArray(3, 0);
	
	if( color.charAt(0)=='#' )
	{
		rgb[0] = parseInt(color.substring(1, 3), 16);
		rgb[1] = parseInt(color.substring(3, 5), 16);
		rgb[2] = parseInt(color.substring(5, 7), 16);
	}
	else
	{
		var first_comma = color.indexOf(",");
		var second_comma = color.substring(first_comma+1).indexOf(",") + first_comma + 1;

		rgb[0] = parseInt(color.substring(4, first_comma));
		rgb[1] = parseInt(color.substring(first_comma+1, second_comma));
		rgb[2] = parseInt(color.substring(second_comma+1, color.indexOf(")")));
	}

	return '#' + toHex(rgb[0]) + toHex(rgb[1]) + toHex(rgb[2]);
}

function stringArray(n, init)
{
	this.length = n;
	for( var i=0; i<n; i++ )
		this[i] = init;
	return this;
}

var hexchars = '0123456789ABCDEF';

function toHex(num)
{
	return hexchars.charAt(num >> 4) + hexchars.charAt(num & 0xF);
}

// ----------------- Audio Player commands ---------------- //

function getGMPlayer()
{
        if (isIE && isWin && !isOpera)
	   var fp = document.getElementById('flashcontent');
	else
	   var fp = document.getElementsByTagName("embed")[0];

	return fp;
}

function ap_loadSound(msg)
{
	getGMPlayer().ap_loadSound(msg);
}

function ap_playSound(msg)
{
	getGMPlayer().ap_playSound(msg);
}

function ap_stopSound(msg)
{
	getGMPlayer().ap_stopSound(msg);
}

function ap_setVolume(msg)
{
	getGMPlayer().ap_setVolume(msg);
}

function ap_getVolume()
{
	return getGMPlayer().ap_getVolume();
}

function ap_getBytesLoaded()
{
	return getGMPlayer().ap_getBytesLoaded();
}

function ap_getBytesTotal()
{
	return getGMPlayer().ap_getBytesTotal();
}

function ap_getPosition()
{
	return getGMPlayer().ap_getPosition();
}

function ap_getDuration()
{
	return getGMPlayer().ap_getDuration();
}

// ----------------- Main Player commands ---------------- //

function onAudioPlayerLoaded()
{
	if (!isAOL) {
		getGMPlayer().onAudioPlayerLoaded();
	}else{
		getGMPlayer().aolDetected();
	}
}

function onSoundLoaded(msg)
{
	getGMPlayer().onSoundLoaded(msg);
}

function onSoundFailed(msg)
{
	getGMPlayer().onSoundFailed(msg);
}

function onSoundComplete(msg)
{
	getGMPlayer().onSoundComplete(msg);
}
