if (!this.APPION) {
  APPION = {};
}


APPION.video = {};
(function() {
   var requestObj;

   var vidPlayer;
   var vidTitle;
   var vidDesc;
   var vidLength;
   var movLink;
   var wmvLink;

   /*----------------------------------------------------------------------
    * Callback handler for Ajax request
    *----------------------------------------------------------------------*/
   function handleJSON() {
     if (requestObj && requestObj.readyState == 4) {
       var data = JSON.parse(requestObj.responseText);

       if (data.title && vidTitle) {
	 vidTitle.innerHTML = data.title;
       }
       if (data.desc && vidDesc) {
	 vidDesc.innerHTML = data.desc;
       }
       if (data.vidLink && vidPlayer) {
	 vidPlayer.href = data.vidLink;
	 APPION.video.player(vidPlayer, data.vidLink, 480, 360);
       }
       if (data.movLink && movLink) {
	 movLink.href = data.movLink;
       }
       if (data.wmvLink && wmvLink) {
	 wmvLink.href = data.wmvLink;
       }
       if (data.vidLength && vidLength) {
	 vidLength.innerHTML = data.vidLength;
       }
     }
   }

   /*----------------------------------------------------------------------
    * Function to get a query string containing the video name from the
    * browser location bar and loads the corresponding video.
    *
    * --Arguments:
    *   'playerId': the id of the player element on the page.
    *   'titleId': the id where the video title will be displayed.
    *   'descId': the id where the video description will be displayed.
    *   'lengthId': the id where the video length will be displayed.
    *----------------------------------------------------------------------*/
   APPION.video.fromQueryString = function(playerId, titleId, descId, lengthId, movId, wmvId) {
     vidPlayer = document.getElementById(playerId);
     vidTitle = document.getElementById(titleId);
     vidDesc = document.getElementById(descId);
     vidLength = document.getElementById(lengthId);
     movLink = document.getElementById(movId);
     wmvLink = document.getElementById(wmvId);

     var queryString = window.location.search.substring(1);
     requestObj = APPION.ajax.sendRequest("/cgi-bin/vidsearch.py", handleJSON, queryString);
   };

   /*----------------------------------------------------------------------
    * Function to create a video player instance on the page.
    *
    * --Arguments:
    *   'id': the id for the element that will hold the video player.
    *   'filepath': the path to the video file.
    *   'width': optional width of the player (default is 320).
    *   'height: optional height of the player (default is 240).
    *----------------------------------------------------------------------*/
   APPION.video.player = function(id, filepath, width, height) {
     width = width || 320;
     height = height || 240;

     flowplayer(id, {src:"/media/flowplayer-3.1.1.swf",
		     height: height,
		     width: width,
		     wmode: "opaque"},
		     {canvas: {backgroundColor: "#000004"},
		     plugins: {
		       controls: {
			 backgroundColor: '#000000',
			 bufferGradient: 'none',
			 timeColor: '#f5c366',
			 sliderGradient: 'none',
			 durationColor: '#ffffff',
			 progressColor: '#d09f06',
			 borderRadius: '0px',
			 sliderColor: '#000000',
			 progressGradient: 'medium',
			 bufferColor: '#52556f',
			 buttonColor: '#6f7990',
			 backgroundGradient: [0.6,0.3,0,0,0],
			 buttonOverColor: '#728B94',
			 volumeColor: '#52556f',
			 opacity:1.0
		       }
		     },
		     clip: {
		       url: filepath,
		       autoPlay: false
		     }
		     });
   };
 }());