/******************************************************************************
  File: playerControls.js
  Desc: Contains JavaScript support functions for interacting with
          the CourseAvenue player

  Vers: 1.0 ; 04/30/2007; Initial Creation.
  Vers: 1.1 ; 07/08/2008; Updated iframe routines for dynamic iframes
******************************************************************************/

//*****************************************************************************
//* WINDOW METHODS
//*****************************************************************************

// This function resizes the inner area of the browser to a certain size
function setWindowSize(pWidth, pHeight, pRelocate) {
  try {
  	var inWidth   = ((window.innerWidth  != null) ? window.innerWidth  : document.body.clientWidth);
  	var inHeight  = ((window.innerHeight != null) ? window.innerHeight : document.body.clientHeight);
  	var outWidth  = ((window.outerWidth  != null) ? window.outerWidth  : document.body.clientWidth + 12);
  	var outHeight = ((window.outerHeight != null) ? window.outerHeight : document.body.clientHeight + 38);	
  	var crWidth   = outWidth  - inWidth;
  	var crHeight  = outHeight - inHeight;
  
    if (pRelocate)
      window.moveTo(screen.availWidth/2 - (pWidth + crWidth)/2, screen.availHeight/2 - (pHeight + crHeight)/2);
    else {
      checkWindowPosition(pWidth, pHeight, crWidth, crHeight);
  	}

    // Check if resizing is even necessary)
    if ((inWidth != pWidth) || (inHeight != pHeight)) {
      window.resizeTo(pWidth + crWidth, pHeight + crHeight);
  
      // If we're in IE, we only guess at the correct size ... and therefore we need to check if
      //   we didn't end up there ... then resize again
      if (document.all) {
        var w = pWidth  - document.body.clientWidth;
        var h = pHeight - document.body.clientHeight;
        if (w != 0 || h != 0) {
          if (pRelocate)
            window.moveTo(screen.availWidth/2 - (pWidth + crWidth + w)/2, screen.availHeight/2 - (pHeight + crHeight + h)/2);
          else {
            checkWindowPosition(pWidth + w, pHeight + h, crWidth, crHeight);
        	}
          window.resizeBy(w, h);
        }
      }
    }
  }
  catch (ex) {
    // If the user is resizing the browser themselves,
    //   this can result in an access denied error
  }

  flashSetJavaScriptResult("setWindowSize", 0, "");
}

// This function checks to ensure that resizing to the specified dimensions
//   will not cause the window to go off screen
function checkWindowPosition(pWidth, pHeight, pCrWidth, pCrHeight) {
  try {
  	var top  = ((window.screenY != null) ? window.screenY : window.screenTop);
  	var left = ((window.screenX != null) ? window.screenX : window.screenLeft);
    posX = screen.availWidth  - (left + pWidth  + pCrWidth);
    posY = screen.availHeight - (top  + pHeight + pCrHeight);
    if (posX < 0 || posY < 0) {
      if (posX < 0) left += posX;
      if (posY < 0) top  += posY;
      window.moveTo(left, top);
    }
  }
  catch (ex) {
  }
}

// This function will create a new browser window and load the secified URL
function createWindow(pURL, pTarget, pShowClutter, pWidth, pHeight, pPosition) {
  var winProps = "directories=0,scrollbars=1,resizable=1";

  if (pWidth && pHeight) winProps += ",width=" + pWidth + ",height=" + pHeight;
  if (pShowClutter) winProps += ",toolbar=1,location=1,status=1,menubar=1";

  var winTop   = (document.all ? window.screenTop  : window.screenY + 20);
  var winLeft  = (document.all ? window.screenLeft : window.screenX);
	var clHeight = ((window.innerHeight != null) ? window.innerHeight - 4 : document.body.clientHeight);
	var clWidth  = ((window.innerWidth  != null) ? window.innerWidth  - 4 : document.body.clientWidth);

  switch (pPosition) {
    case "controlBar" : var top      = winTop  + 50;
                        var left     = winLeft + (clWidth - pWidth) - 5;
                        winProps += (document.all ? ",top=" + top + ",left=" + left : ",screenY=" + top + ",screenX=" + left);
                        break;
    case "center"     : var top      = winTop  + ((clHeight - pHeight) / 2);
                        var left     = winLeft + ((clWidth  - pWidth)  / 2);
                        winProps += (document.all ? ",top=" + top + ",left=" + left : ",screenY=" + top + ",screenX=" + left);
                        break;
  }

  var win = window.open(pURL, pTarget, winProps);
  win.focus();
}

// This function will position the div area and load the secified URL
function showDivLayer(pName, pURL, pX, pY, pWidth, pHeight, pRootWidth, pRootHeight, pShowBorder) {
	var fm = document.createElement("iframe");
	fm.id = "media";
	fm.style.position = "absolute";
	fm.style.zIndex = 2;

	fm.frameBorder = (pShowBorder ? 1 : 0);
	fm.scrolling   = "auto";
	fm._size = {rootWidth: pRootWidth, rootHeight: pRootHeight, width: pWidth, height: pHeight, X: pX, Y: pY};
	sizeDivLayer(fm);
	if (!document.all) pURL = unescape(pURL);
	fm.src = pURL;
	document.body.appendChild(fm);
}

function sizeDivLayer(iframe) {
	var fm = (iframe == undefined ? document.getElementById("media") : iframe);
	if (fm == undefined) return;	
	
	if (!document.all) { fm.style.width = 0; fm.style.height = 0; }
	var size = fm._size;
	var clHeight = ((window.innerHeight != null) ? window.innerHeight - 4 : document.body.clientHeight);
	var clWidth  = ((window.innerWidth  != null) ? window.innerWidth  - 4 : document.body.clientWidth);
	var scale = Math.min(clHeight / size.rootHeight, clWidth / size.rootWidth);
	var xoffset = ((clWidth  - (size.rootWidth  * scale)) / 2);
	var yoffset = ((clHeight - (size.rootHeight * scale)) / 2);

	//alert("sdl:"+size.width+":"+size.rootWidth+":"+clWidth+":"+scale+":"+xoffset+":"+yoffset);
	fm.style.left   = (size.X * scale + xoffset) + "px";
	fm.style.top    = (size.Y * scale + yoffset) + "px";
	fm.style.width  = (size.width  * scale) + "px";
	fm.style.height = (size.height * scale) + "px";
}

function hideDivLayers() {
	var fm  = document.getElementById("media");
	if (fm != undefined) document.body.removeChild(fm);
}

//*****************************************************************************
//* PLAYER COMMUNICATION METHODS
//*****************************************************************************

// This function executes script within the specified frame in the embedded flash object
function flashNotify(pLabel) {
  //alert("Notify: " + pLabel);
  //var cwPlayerObj = document.all ? cwPlayer : document.cwPlayer;
  document.cwPlayer.TCallLabel("/mcClassDefinitions/callbackAPI", pLabel);
}

// This function gets a variable by name from the embedded flash object
function flashGetVariable(psVarName, pIsRoot) {
  //var cwPlayerObj = document.all ? cwPlayer : document.cwPlayer;
  psVarName = (pIsRoot ? "_root:" : "/mcClassDefinitions/callbackAPI:") + psVarName;
  return document.cwPlayer.GetVariable(psVarName);
}

// This function sets a variable by name in the embedded flash object
function flashSetVariable(psVarName, pIsRoot, pValue) {
  //var cwPlayerObj = document.all ? cwPlayer : document.cwPlayer;
  psVarName = (pIsRoot ? "_root:" : "/mcClassDefinitions/callbackAPI:") + psVarName;
  document.cwPlayer.SetVariable(psVarName, pValue);
}

function flashSetJavaScriptResult(psMethod, pResult, pErrDesc) {
	//trace("flashSetJavaScriptResult:" + psMethod);
	flashSetVariable("jsMethodName", false, psMethod);
  flashSetVariable("jsMethodResult", false, pResult);
  flashSetVariable("jsMethodError", false, pErrDesc);
  flashNotify("jsCommandComplete");
}

//*****************************************************************************
//* PLAYER JSAPI METHODS
//*****************************************************************************
var playerAPI = new Object();

// This method will enable/disable the navigation buttons
playerAPI.enableNavigation = function(enabled) {
	flashSetVariable("jsapiMethodName", false, "enableNavigation");
  flashSetVariable("jsapiMethodParams", false, enabled);
  flashNotify("jsapiCallback");
}

// This method will navigate to the next page in a course
playerAPI.displayNextPage = function() {
	flashSetVariable("jsapiMethodName", false, "displayNextPage");
  flashNotify("jsapiCallback");
}

// This method will navigate to the next page in a course
playerAPI.displayPreviousPage = function() {
  alert("Not yet implemented");
}

// This method will invoke a custom method defined in the loaded player skin
playerAPI.invokeCustomMethod = function(command, params) {
	flashSetVariable("jsapiMethodName", false, "invokeCustomMethod");
  flashSetVariable("jsapiMethodParams", false, command+"|"+params);
  flashNotify("jsapiCallback");
}
