var browser = "FF";
if(navigator.userAgent.indexOf ("MSIE") != -1)
{ browser = "IE";
}
else if(navigator.userAgent.indexOf("Mozilla") != -1)
{ browser = "FF";
}
else if(navigator.userAgent.indexOf("firefox") != -1)
{ browser = "FF";
}

var videoTimer;

// 1/velocityQuotient ~= % of movement each iteration
// lower numbers mean faster movement
var velocityQuotient = 10;

// speedQuotient ~= milliseconds between iterations
// lower numbers mean faster movement
// if a monitor operates at 60 cycles / sec, given that 1000 ms / sec .: 1 cycle / 16.7 ms
var speedQuotient = 16;

function showPopout2() {
    insertVideo(videoFile); //
}

function showPopout()
{
  clearTimeout(videoTimer);
  createShadow();
  if ((screen.width > 1024) && (screen.height > 768)) {
      adjustVideo(950, 700, -750, -190, speedQuotient);
  }
  else {
      adjustVideo(800, 570, -680, -190, speedQuotient);
  }
}

function hidePopout() {
  clearTimeout(videoTimer);
  document.getElementById("flshMenu").innerHTML = "";
  removeShadow();
  adjustVideo(200, 145, 0, 0, speedQuotient);
}

function adjustVideo(sizeX, sizeY, offsetX, offsetY, millisec) {
  clearTimeout(videoTimer);
  var somethingChanged = "";

  var theObject = document.getElementById("videoObject");
  if(!theObject)
  { return;
  }
  var theContainer = document.getElementById("videoContainer2");
  if(!theContainer)
  { return;
  }

  var units = "";
  if(browser == "FF")
  { units = "px";
  }

  var theObjectW = myParseInt(theObject.offsetWidth);
  var theObjectH = myParseInt(theObject.offsetHeight);

  var theObjectdW = Math.abs(sizeX - theObjectW) / velocityQuotient + 1;
  var theObjectdH = Math.abs(sizeY - theObjectH) / velocityQuotient + 1;

  var theContainerW = myParseInt(theContainer.offsetWidth);
  var theContainerH = myParseInt(theContainer.offsetHeight);

  var theContainerdW = Math.abs(sizeX - theContainerW) / velocityQuotient + 1;
  var theContainerdH = Math.abs(sizeY - theContainerH) / velocityQuotient + 1;


  var theContainerX = myParseInt(theContainer.style.left);
  var theContainerY = myParseInt(theContainer.style.top);

  var theContainerdX = Math.abs(offsetX - theContainerX) / velocityQuotient + 1;
  var theContainerdY = Math.abs(offsetY - theContainerY) / velocityQuotient + 1;

  //resize theObject's width
  if(theObjectW > sizeX)
  { theObject.style.width = (theObjectW - theObjectdW) + units;
    somethingChanged += "objectW-";
  }
  else if(theObjectW < sizeX)
  { theObject.style.width = (theObjectW + theObjectdW) + units;
    somethingChanged += "objectW+";
  }

  //resize theObject's height
  if(theObjectH > sizeY)
  { theObject.style.height = (theObjectH - theObjectdH) + units;
    somethingChanged += "objectH-";
  }
  else if(theObjectH < sizeY)
  { theObject.style.height = (theObjectH + theObjectdH) + units;
    somethingChanged += "objectH+";
  }

  //resize theContainer's width
  if(theContainerW > sizeX)
  { theContainer.style.width = (theContainerW - theContainerdW) + units;
    somethingChanged += "containerW-";
  }
  else if(theContainerW < sizeX)
  { theContainer.style.width = (theContainerW + theContainerdW) + units;
    somethingChanged += "containerW+";
  }

  //resize theContainer's height
  if(theContainerH > sizeY)
  { theContainer.style.height = (theContainerH - theContainerdH) + units;
    somethingChanged += "containerH-";
  }
  else if(theContainerH < sizeY)
  { theContainer.style.height = (theContainerH + theContainerdH) + units;
    somethingChanged += "containerH+";
  }

  //move theContainer along the X axis
  if(theContainerX > offsetX)
  { theContainer.style.left = (theContainerX - theContainerdX) + units;
    somethingChanged += "containerX-";
  }
  else if(theContainerX < offsetX)
  { theContainer.style.left = (theContainerX + theContainerdX) + units;
    somethingChanged += "containerX+";
  }

  //move theContainer along the Y axis
  if(theContainerY > offsetY)
  { theContainer.style.top = (theContainerY - theContainerdY) + units;
    somethingChanged += "containerY-";
  }
  else if(theContainerY < offsetY)
  { theContainer.style.top = (theContainerY + theContainerdY) + units;
    somethingChanged += "containerY+";
  }

  if (somethingChanged != "") {
      videoTimer = setTimeout("adjustVideo(" + sizeX + ", " + sizeY + ", " + offsetX + ", " + offsetY + ", " + millisec + ");", millisec);
  }
  else {
      if (Math.abs(offsetX) == 0) {
        var theDiv = document.getElementById("videoContainer2");
        if (!theDiv) {
            return;
        }
        theDiv.innerHTML = "<img src='../video2.jpg' id='vidImg' alt='Click to Play Video'/>";
      }
  }

}

function createShadow()
{ var theShadowBox = document.getElementById("shadowBox");
  if(!theShadowBox)
  { var theBody = document.getElementsByTagName("body")[0];
    var theNode = document.createElement("div");
    theNode.style.position = "absolute";
    theNode.style.top = "0px";
    theNode.style.left = "0px";
    theNode.style.overflow = "hidden";
    theNode.style.display = "none";
    theNode.id = "shadowBox";
    theBody.appendChild(theNode);
    theShadowBox = document.getElementById("shadowBox");
  }

  var pageWidth, pageHeight;
  if(document.body && (document.body.scrollWidth || document.body.scrollHeight))
  { pageWidth = document.body.scrollWidth + "px";
    pageHeight = document.body.scrollHeight + "px";
  }
  else if(document.body.offsetWidth)
  { pageWidth = document.body.offsetWidth + "px";
    pageHeight = document.body.offsetHeight + "px";
  }
  else
  { pageWidth = "100%";
    pageHeight = "100%";
  }

  theShadowBox.style.opacity = 0.40;
  theShadowBox.style.MozOpacity = 0.40;
  theShadowBox.style.filter = "alpha(opacity='40')";
  theShadowBox.style.backgroundColor = "#000000";
  theShadowBox.style.width = pageWidth;
  theShadowBox.style.height = pageHeight;
  theShadowBox.style.display = "block";
  theShadowBox.onclick = hidePopout;
  window.onresize = "resizeShadow()";
}

function resizeShadow()
{ alert("resizing the shadow to match");
  var theShadowBox = document.getElementById("shadowBox");
  if(!theShadowBox)
  { return;
  }
  theShadowBox.style.top = "0px";
  theShadowBox.style.left = "0px";
  var pageWidth, pageHeight;
  if(document.body && (document.body.scrollWidth || document.body.scrollHeight))
  { pageWidth = document.body.scrollWidth + "px";
    pageHeight = document.body.scrollHeight + "px";
  }
  else if(document.body.offsetWidth)
  { pageWidth = document.body.offsetWidth + "px";
    pageHeight = document.body.offsetHeight + "px";
  }
  else
  { pageWidth = "100%";
    pageHeight = "100%";
  }
  theShadowBox.style.width = pageWidth;
  theShadowBox.style.height = pageHeight;
}

function removeShadow()
{ var theShadowBox = document.getElementById("shadowBox");
  if(theShadowBox)
  { theShadowBox.parentNode.removeChild(theShadowBox);
  }
  window.onresize = "";
}

function insertVideo(pathToVideoFile)
{ insertVideoIntoDiv(pathToVideoFile, 145, 200, "videoContainer2");
}

function insertVideoIntoDiv(videoFile, height, width, divId)
{ var theDiv = document.getElementById(divId);
  if(!theDiv) { //no div found, exit the function
      //alert("no div");
    return;
  }
  //alert("insertVideoIntoDiv");
  var theContent, dFlash, bIsFireFox;

  var agid = navigator.userAgent.toLowerCase();
  bIsFireFox = (agid.indexOf("firefox") != -1);
  dFlash = detectFlash();
  //  * 2: Flash installed
  //  * 1: Flash not installed
  //  * 0: Unknown if Flash is installed

  if ( dFlash == 1 ) {
      theContent = "Did not detect a Flash Player. Flash Player is required to view the video. <a href='http://get.adobe.com/flashplayer/'>Click Here to Get the Flash Player </a>";
 }
 else {
     if (divId == "videoContainer2") {
         theContent = "<div id='flshMenu' style='background-color:#0033FF;width:100%;position:relative;cursor:pointer;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1,StartColorStr='#B09900',EndColorStr='#FFF192');' onclick='hidePopout();' align='right'>Click to Close <img src='/images/X.jpg' alt='Click to Close Window' style='cursor;pointer;margin:8px;' align='middle' /></div>";
     }
     else {
        theContent = ""
     }
   if(bIsFireFox) {
      theContent += "<embed width=\"" + width + "\" height=\"" + height + "\" src=\"" + videoFile + "\"";
      theContent += " quality=\"high\" pluginspage=\"https://www.macromedia.com/go/getflashplayer\"";
      theContent += " id=\"videoObject\" bgcolor=\"#ffffff\" type=\"application/x-shockwave-flash\"";
      if (divId == "videoContainer2")
          theContent += " onfocus=\"showPopout();\" style=\"position:absolute;\">";
      else
          theContent += " style=\"position:absolute;\">";
      theContent += "</embed>";
    }
    else
    { theContent += '<object codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
      theContent += ' width="' + width + '" height="' + height + '" id="videoObject"';
      theContent += ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
      if (divId == "videoContainer2")
          theContent += ' onfocus="showPopout();"';
      theContent += '><param name="movie" value="' + videoFile + '" />';
      theContent += '<param name="quality" value="high" />';
      theContent += '<param name="play" value="true" />';
      theContent += '<param name="loop" value="true" />';
      theContent += '<param name="scale" value="showall" />';
      theContent += '<param name="devicefont" value="false" />';
      theContent += '<param name="bgcolor" value="#ffffff" />';
      theContent += '<param name="menu" value="true" />';
      theContent += '<param name="allowScriptAccess" value="sameDomain" />';
      theContent += '<param name="swliveconnect" value="true" />';
      theContent += '<param name="salign" value="" />';
      theContent += '</object>';
    }
}

theDiv.innerHTML = theContent;
  //alert(theContent)
}

function detectFlash()
{ //ripped: http://www.quirksmode.org/js/flash.html
  //  * 2: Flash installed
  //  * 1: Flash not installed
  //  * 0: Unknown if Flash is installed

  var x;
  if (navigator.plugins && navigator.plugins.length)
  { x = navigator.plugins["Shockwave Flash"];
    if(x)
    { return 2;
    }
    else
    { return 1;
    }
    if(navigator.plugins["Shockwave Flash 2.0"])
    { return 2;
    }
  }
  else if(navigator.mimeTypes && navigator.mimeTypes.length)
  { x = navigator.mimeTypes['application/x-shockwave-flash'];
    if(x && x.enabledPlugin)
    { return 2;
    }
    else
    { return 1;
    }
  }

  return 0;
}
