/** arrow */
function arrow(x, y, width, height)
{
   if (typeof x == "undefined") { this.x = 164; } else { this.x = x; }
   if (typeof y == "undefined") { this.y = 116; } else { this.y = y; }

   if (typeof width == "undefined") { this.width = 43; } else { this.width = width; }
   if (typeof height == "undefined") { this.height = 44; } else { this.height = height; }

   this.halfwidth = this.width / 2;
   this.halfheight = this.height / 2;
}

/** board */
function board(x, y, width, height, min, max)
{
   if (typeof x == "undefined") { this.x = 164; } else { this.x = x; }
   if (typeof y == "undefined") { this.y = 100; } else { this.y = y; }

   if (typeof width == "undefined") { this.width = 620; } else { this.width = width; }
   if (typeof height == "undefined") { this.height = 72; } else { this.height = height; }

   if (typeof min == "undefined") { this.min = 3; } else { this.min = min; }
   if (typeof max == "undefined") { this.max = 617; } else { this.max = max; }

   this.size = this.max - this.min;
}

/** trace */
function trace(x, y, min, max, reference)
{
   if (typeof x == "undefined") { this.x = 0; } else { this.x = x; }
   if (typeof y == "undefined") { this.y = 0; } else { this.y = y; }

   if (typeof min == "undefined") { this.min = 0; } else { this.min = min; }
   if (typeof max == "undefined") { this.max = 255; } else { this.max = max; }

   this.mx = null;
   this.my = null;

   this.reference = reference
   this.plugin = null;

   this.value = 0;

   this.samplehd = null;
   this.samplerate = 500;

   this.data = new Array(120)
   this.dataptr = 0;

   this.board = new board(x, y);
   this.arrow = new arrow();

   this.boardpicture = "../../Images/Questionnaire/Viewer/Trace/traceboard.jpg";
   this.arrowpicture = "../../Images/Questionnaire/Viewer/Trace/arrow.gif";
   this.cursor = "../../Images/Questionnaire/Viewer/Trace/blank.cur";

   this.ratio = (this.max - this.min) / (this.board.size - this.arrow.width);
}

trace.prototype.init = function()
{
   this.arrow.x = (this.board.size / 2) - this.arrow.halfwidth + this.board.min;
   this.arrow.y = (this.board.height / 2) - this.arrow.halfheight;

   if (navigator.appName.indexOf("Netscape") != -1)
   {
      document.captureEvents(Event.KEYDOWN);
      document.captureEvents(Event.MOUSEMOVE);
   }
   else
   {
      document.getElementById("board").style.left = this.board.x - (this.board.size / 2);
      document.getElementById("board").style.top = this.board.y;

      document.getElementById("arrow").style.left = this.arrow.x;
      document.getElementById("arrow").style.top = this.arrow.y;
   }
}

trace.prototype.onmousemove = function(e)
{
   var x, y;

   if (navigator.appName.indexOf("Microsoft") != -1)
   {
      e = window.event;

      if (e.clientX || e.clientY)
      {
         if (this.mx == null)
            this.mx = e.clientX;

         if (this.my == null)
            this.my == e.clientY;

         x = this.mx - e.clientX;
         y = this.my - e.clientY;

         this.mx = e.clientX;
         this.my = e.clientY;
      }
   }
   else
   {
      if (e.x || e.y)
      {
         if (this.mx == null)
            this.mx = e.x;

         if (this.my == null)
            this.my == e.y;

         x = this.mx - e.x;
         y = this.my - e.y;

         this.mx = e.x;
         this.my = e.y;
      }
   }

   quest.mm.trace.arrow.x = quest.mm.trace.arrow.x - x;

   if (quest.mm.trace.arrow.x < quest.mm.trace.board.min)
   {
      quest.mm.trace.arrow.x = quest.mm.trace.board.min;
   } else if (quest.mm.trace.arrow.x > quest.mm.trace.board.max - quest.mm.trace.arrow.width)
   {
      quest.mm.trace.arrow.x = quest.mm.trace.board.max - quest.mm.trace.arrow.width;
   }

   document.getElementById("arrow").style.left = quest.mm.trace.arrow.x;
}

trace.prototype.onkeydown = function(e)
{
   if (navigator.appName.indexOf("Microsoft") != -1)
   {
      e = window.event;

      switch (e.keyCode)
      {
         case 37: 						// left arrow
            quest.mm.trace.arrow.x -= 10;
            break;
         case 38: 						// up arrow
            break;
         case 39: 						// right arrow
            quest.mm.trace.arrow.x += 10;
            break;
         case 40: 						// down arrow
            break;
         default:
            break;
      }

      if (quest.mm.trace.arrow.x < quest.mm.trace.board.min)
      {
         quest.mm.trace.arrow.x = quest.mm.trace.board.min;
      } else if (quest.mm.trace.arrow.x > quest.mm.trace.board.max - quest.mm.trace.arrow.width)
      {
         quest.mm.trace.arrow.x = quest.mm.trace.board.max - quest.mm.trace.arrow.width;
      }

      document.getElementById("arrow").style.left = quest.mm.trace.arrow.x;
   }
   else
   {
   }
}

trace.prototype.sample = function()
{
   var curPos = this.plugin.controls.currentPosition;

   this.value = Math.round(this.ratio * (this.arrow.x - (this.board.x + this.board.min)));
   this.data[this.dataptr] = this.value + ";";
   this.data[this.dataptr] += (0.5 * this.dataptr) + ";";
   this.data[this.dataptr] += toFixed(curPos, 4) + ";";
   this.data[this.dataptr] += toFixed(Math.abs((0.5 * this.dataptr) - curPos), 4) + ";";
   this.dataptr++;
}

trace.prototype.reset = function()
{
   this.dataptr = 0;
   this.data.length = null;
}

trace.prototype.start = function()
{
   document.onkeydown = this.onkeydown;
   document.onmousemove = this.onmousemove;

   if (this.plugin != null)
   {
      this.sample();
      this.samplehd = window.setInterval(this.reference + ".sample();", this.samplerate);
   }
}

trace.prototype.stop = function()
{
   document.onkeydown = null;
   document.onmousemove = null;
   window.clearInterval(this.samplehd);
}

trace.prototype.getData = function()
{
   var i;
   var sdata = "";

   for (i = 0; i < this.dataptr; i++)
   {
      sdata += this.data[i] + ",";
   }

   return sdata;
}

trace.prototype.getHTML = function()
{
   var sres = "";

   sres += "<style type='text/css'>";
   sres += "   body {cursor: url(" + this.cursor + "),default;}";
   sres += "   #trace {position:relative; border: solid #000000 4px;}";
   sres += "	#board {position:absolute;}";
   sres += "	#arrow {position:absolute;}";
   sres += "</style>";

   sres += "<div>";
   sres += "   <div id='trace'>";
   sres += "      <div id='board'>";
   sres += "         <img src='" + this.boardpicture + "'>";
   sres += "         <div id='arrow'><img src='" + this.arrowpicture + "'></div>";
   sres += "      </div>";
   sres += "   </div>";
   sres += "</div>";

   return sres;
}

/** ------------------------------------------------------ */

function sequence()
{
   this.value = null;
   this.items = new Array();
}

sequence.prototype.toString = function()
{
   var i, res;

   res = this.items.toString();

   return res;
}

/** ------------------------------------------------------ */

//
// Constructor
//
function multimedia(reference)
{
   // Only XP, when playing this property can be set
   //mm.plugin.fullScreen = true;

   this.domain = "";
   this.reference = reference;

   this.playcount = 0;
   this.playtype = 2; 			// 0 all, 1 rotate, 2 random

   this.autoplay = true;
   this.volume = 100;
   this.width = 350;
   this.height = 350;
   this.contextMenu = false;

   this.path = "";

   this.sequences = new Array();
   this.sequenceindex = 0;
   this.sequencecount = 0;
   this.sequencevalue = 0;

   this.plugin = null;
   this.validwmp = true;

   this.showcontrols = false
   this.showtrace = false;
   this.trace = new trace(0, 0, 0, 255, this.reference + ".trace");

   this.callback = this.onDone;
}

//
// Initialize the object
//
multimedia.prototype.init = function()
{
   this.plugin = document.MediaPlayer;

   if (typeof this.plugin.controls == "undefined")
   {
      this.plugin = null;
      this.validwmp = false;
   }

   // we are unable to show the movie: show error and move on to next question
   if (this.plugin == null || !this.validwmp)
   {
      alert(quest.nowmp);
      gonext();
      document["query"].submit(); // gonext doesn't submit automatically
   }

   if (this.showtrace)
   {
      this.trace.init();
      this.trace.plugin = this.plugin;
   }

   if (this.autoplay)
   {
      switch (this.playType)
      {
         case 0:
            this.playAll();
            break;
         case 1:
            this.playRotate();
            break;
         default:
         case 2:
            this.playRandom();
            break;
      }
   }
}

/* Utility functions */

//
// getNumber
//
multimedia.prototype.getNumber = function()
{
   if (this.sequences.length == 0)
      return 0;
   return Math.round(Math.random() * 1000) % this.sequences.length;
}

/* Management of sequences */

//
// addSequence
//
multimedia.prototype.addSequence = function(v, s)
{
   var seq = new sequence;

   seq.value = v;
   seq.items = s;

   this.sequences[this.sequences.length] = seq;
}

//
// nextSequence
//
multimedia.prototype.nextSequence = function()
{
   if (this.sequences.length > 0)
   {
      this.sequencecount = this.sequences[this.sequenceindex].items.length;

      switch (this.playtype)
      {
         case 0: 		// all
            if (this.sequenceindex < this.sequences.length)
            {
               this.sequencevalue = this.sequences[this.sequenceindex].value;
               this.play();

               this.sequenceindex++;
            } else
            {
               this.stop();

               if (this.callback)
               {
                  this.callback();
               }
            }

            break;
         case 1: 		// rotate
            if (this.playcount > 0)
            {
               this.playcount--;

               this.sequencevalue = this.sequences[this.sequenceindex].value;
               this.play();

               this.sequenceindex++;
               this.sequenceindex %= this.sequences.length;
            } else
            {
               this.stop();

               if (this.callback)
               {
                  this.callback();
               }
            }

            break;
         case 2: 		// random
            if (this.playcount > 0)
            {
               this.playcount--;

               this.sequencevalue = this.sequences[this.sequenceindex].value;
               this.play();

               this.sequenceindex = this.getNumber();
            } else
            {
               this.stop();

               if (this.callback)
               {
                  this.callback();
               }
            }

            break;
         case 3: 		// breakout
            break;
      }
   }
   else
   {
      /* no-op */
   }
}

/* Sequence player controls */

multimedia.prototype.playAll = function()
{
   this.playcount = this.sequences.lenght;
   this.playtype = 0;

   this.sequenceindex = 0;
   this.nextSequence();
}

multimedia.prototype.playRotate = function(val, cnt)
{
   var i;

   if (typeof cnt == "undefined")
   {
      cnt = 1;
   }

   this.playcount = cnt;
   this.playtype = 1;

   if (typeof val == "undefined" || val == null)
   {
      this.sequenceindex = this.getNumber();
   }
   else
   {
      for (i = 0; i < this.sequences.length; i++)
      {
         if (this.sequences[i].value == val)
         {
            this.sequenceindex = val;
            break;
         }
      }
   }

   this.nextSequence();
}

multimedia.prototype.playRandom = function(val, cnt)
{
   var i;

   if (typeof cnt == "undefined")
   {
      cnt = 1;
   }

   this.playcount = cnt;
   this.playtype = 2;

   this.sequenceindex = this.getNumber();

   if (typeof val == "undefined" || val == null)
   {
      this.sequenceindex = this.getNumber();
   }
   else
   {
      for (i = 0; i < this.sequences.length; i++)
      {
         if (this.sequences[i].value == val)
         {
            this.sequenceindex = i;
            break;
         }
      }
   }

   this.nextSequence();
}

/* Simple player controls */

//
// play
//
multimedia.prototype.play = function()
{
   if (this.path == "")
   {
      this.path = "file://c:/movieclips/";
   }

   for (var i = 0; i < this.sequences[this.sequenceindex].items.length; i++)
   {
      var s = this.sequences[this.sequenceindex].items[i];
      this.sequences[this.sequenceindex].items[i] = s.replace(/&/ig, "%26");
   }

   var path = this.path.replace(/&/ig, "%26");
   var filename = this.domain + "/QuestionnaireModule/Viewer/MovieReels.aspx?pth=" + path + "&pllst=" + this.sequences[this.sequenceindex];

   this.plugin.url = filename;

   this.plugin.controls.play();
}

//
// pause
//
multimedia.prototype.pause = function()
{
   this.plugin.controls.pause();
}

//
// stop
//
multimedia.prototype.stop = function()
{
   this.plugin.controls.stop();
}

/* Event handlers */

multimedia.prototype.onPlayStateChanges = function(newState)
{
   // other states: fast forward = 4, fast rewind = 3, buffering = 6 etc.

   switch (newState)
   {
      case 1: 	// Stop
         this.onStop();
         break;
      case 2: 	// Pause
         this.onPause();
         break;
      case 3: 	// Play
         this.onPlay();
         break;
      case 8: 	// Playback ended
         this.onPlayEnded();
         break;
      case 9: 	// Preparing new media
         this.onPreparingNewMedia();
         break;
      case 10: // Ready to play
         this.onReadyToPlay();
         break;
      default:
         break;
   }
}

multimedia.prototype.onPreparingNewMedia = function()
{
}

multimedia.prototype.onReadyToPlay = function()
{
   if (this.sequencecount == 0)
   {
      this.nextSequence();
   }
}

multimedia.prototype.onPlay = function()
{
   if (this.showtrace)
   {
      this.trace.stop();
      this.trace.reset();
      this.trace.start();
   }
}

multimedia.prototype.onPlayEnded = function()
{
   this.sequencecount--;
}

multimedia.prototype.onPause = function()
{
}

multimedia.prototype.onStop = function()
{
   if (this.showtrace)
   {
      this.trace.stop();
   }
}

multimedia.prototype.onDone = function()
{
   alert("DONE!");
}

/* Player display */

//
// Show the player
//
multimedia.prototype.getHTML = function()
{
   var sres = "";

   sres += "<script for='MediaPlayer' event='PlayStateChange(newState)' language='jscript'>";
   sres += "	" + this.reference + ".onPlayStateChanges(newState);";
   sres += "</script>";

   sres += "<table border='0'>";
   sres += "	<tr><td align='center'>";
   sres += "      <div align='center' id='bufferingDiv'></div>";
   sres += "		<object id='MediaPlayer' width='" + this.width + "' height='" + this.height + "' align='center'";
   sres += "			classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'";
   sres += "			codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'";
   sres += "			standby='Loading Microsoft Windows Media Player components...'";
   sres += "			type='application/x-oleobject'>";

   sres += "			<param name='autoStart' value='false'>";
   sres += "			<param name='uiMode' value='none'>";
   sres += "			<param name='stretchToFit' value='true'>";
   sres += "			<param name='volume' value='" + this.volume + "'>";
   sres += "			<param name='enableContextMenu' value='" + this.contextMenu + "'>";

   sres += "			<embed type='application/x-mplayer2'";
   sres += "				pluginspage='http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/'";
   sres += "				src=''";
   sres += "				name='MediaPlayer'";
   sres += "				width='" + this.width + "'";
   sres += "				height='" + this.height + "'";
   sres += "				autostart='0'";
   sres += "				showcontrols='0'";
   sres += "				enablecontextmenu='0'";
   sres += "				clicktoplay='0'>";
   sres += "			</embed>";
   sres += "		</object>";

   sres += "      <script for='MediaPlayer' event='Buffering(begin)'>";
   sres += "         var idBuffer;";
   sres += "         if (begin)";
   sres += "         {";
   sres += "           UpdateDIV();";
   sres += "           idBuffer = window.setInterval('UpdateDIV()', 1000);";
   sres += "         }";
   sres += "         else";
   sres += "         {";
   sres += "            window.clearInterval(idBuffer);";
   sres += "            bufferingDiv.innerHTML = \"\";";
   sres += "         }";
   sres += "      </script>";
   sres += "      <script>";
   sres += "      function UpdateDIV(){";
   sres += "         var progress = document.MediaPlayer.network.bufferingProgress;";
   sres += "         bufferingDiv.innerHTML = \"\";";
   sres += "         if (progress != 100) {";
   sres += "           bufferingDiv.innerHTML = quest.bufferingvideo;";
   sres += "           bufferingDiv.innerHTML += progress + '%';";
   sres += "         }";
   sres += "      }";
   sres += "      </script>";

   sres += "	</td></tr>";

   if (this.showcontrols)
   {
      sres += "	<tr><td align='center'>";
      sres += "		<table border=0>";
      sres += "			<tr>";
      sres += "				<td align='center'><a href='javascript:" + this.reference + ".stop();'><img src='multimedia/stop.gif' border='0'></a>";
      sres += "				<td align='center'><a href='javascript:" + this.reference + ".play();'><img src='multimedia/play.gif' border='0'></a>";
      sres += "				<td align='center'><a href='javascript:" + this.reference + ".pause()'><img src='multimedia/pause.gif' border='0'></a>";
      sres += "			</tr>";
      sres += "		</table>";
      sres += "	</td></tr>";
   }

   if (this.showtrace)
   {
      sres += "	<tr><td align='center' height='" + (this.trace.board.height) + "' valign='top'>";
      sres += this.trace.getHTML();
      sres += "	</td></tr>";
   }

   // For debugging
   //	sres += "<tr><td><textarea name='status_values' cols='80' rows='15'></textarea></td></tr>";

   sres += "</table>";

   return sres;
}

/* Netscape eventhandling */

//
// OnDSPlayStateChangeEvt
//
function OnDSPlayStateChangeEvt(oldState, newState)
{
   doPlayStateChanges(oldState, newState);
}

