﻿
(function() {
   
   function Video() { 
      this.VideoId = "";
      this.Author = "";
      this.Email = "";
      this.PreviewImage = "";
      this.JobTitle = "";
      this.Flv = "";
      this.Index = -1;
   }
   var _videoList;
   var _videoMap;
   var _playlistTitles;
   var _playlist;
   Video.Count = 0;
   var options;
   var _player;
   
   function initVideoList(xml) { 
      _videoList = new Array();
      _videoMap = new Array();
      $(xml).find('video').each(function() { 
         var vid = new Video();
         vid.VideoId = $(this).attr('id');
         vid.Author = $(this).find("author").text();
         vid.Email = $(this).find("email").text();
         vid.Flv = $(this).find("flv").text();
         vid.PreviewImage = $(this).find("preview-image").text();
         vid.JobTitle = $(this).find("title").text();
         vid.Num = _videoList.length;
         _videoList[vid.Num] = vid;
         _videoMap[vid.VideoId] = vid;
      });
      if (_playlistTitles != null && _playlistTitles.length != 0) { 
         processPlaylist();
      }
   }
   
   function processPlaylist() { 
      _playlist = new Array();
      for(var i = 0; i < _playlistTitles.length; i++) { 
         var id = _playlistTitles[i];
         if (_videoMap[id] != null) { 
            _playlist[_playlist.length] = _videoMap[id];
         }
         else { 
            alert(id + " not found");
         }
      }
   }
   jQuery.vc_videoInit = function(settings) { 
       options = jQuery.extend({
             id: "flv-player",
             divId: "flash-content",
             swf: "player.swf",
             autoStart: true,
             loop: true,
             videoXmlPath: '/_res/videos.xml',
             dontLoad: false,
             /* swfobject params */
             width: "558",
             height: "334",
             version: "9",
             background: "#FFFFFF"
          }, settings);

          $.ajax(
          { 
             url: options.videoXmlPath,
             dataType: "xml",
             success: function(xml) { 
                initVideoList(xml); 
                initVideo();
             }
          }
          );
   };
   function loadFirstVideo() { 
      var ndx = Math.floor(Math.random() * _playlist.length);
      playVideo(_playlist[ndx], options.autoStart);
   }
   jQuery.vc_setVideo = function(name, play) { 
      var doPlay = play;
      if (doPlay == null) { 
         doPlay = false;
      }
      if (_player == null) { 
         window.setTimeout(function() { jQuery.vc_setVideo(name, doPlay); }, 1000);
         return;
      }
      var v = _videoMap[name];
      if (v != null) { 
         playVideo(v, play);
      }
   };
   
   function initVideo() { 
      var so = new SWFObject(options.swf, options.id, options.width, options.height, options.version, options.background);
      so.addParam("quality", "high");
      so.addParam("wmode", "transparent");
      so.addParam('allowfullscreen','true');
      so.write(options.divId);
   }
   function playVideo(video, start) {
     if (start == null)
        start = true;
      
     var preview = "";
      
     preview = video.PreviewImage;

     var obj = {
        file: video.Flv,
        image: preview
      };
      _player.sendEvent("LOAD",obj);
      if (start) {
         _player.sendEvent("PLAY");
      }
      
      if (options.newVideoCallback) { 
         options.newVideoCallback(video);
      }
   }

   jQuery.vc_playerReady = function(player) { 
      _player = player;
      
      if ( !options.dontLoad) {
         loadFirstVideo();
      }
   };
   
   
   jQuery.fn.vc_videoPlaylist = function(bind) { 
      if (bind == null) { 
         bind = true;
      }
      _playlistTitles = new Array();
      $(this).each(function() { 
         var index = $(this).attr('title');

         _playlistTitles[_playlistTitles.length] = index;
         if (bind) {
             $(this).bind('click',function() { 
                jQuery.vc_setVideo(index);
             });
         }
      });
   };
   
   jQuery.vc_videoClear = function() { 
      _player = null;
   }
   
   
   
})();


function playerReady(thePlayer) {
    player = document.getElementById(thePlayer.id);
	jQuery.vc_playerReady(player);
}


