/* HCI Video Widget Functions */

/* Requires: 
   1. SWFObject to be included in the HTML/PHP file
   2. A container called video_container
   3. A div or span called cur_video_name
   4. A video_array file (can be loaded by separate JS file)

	Usage:
		1. Create video_array in other file
		2. Call generateVideoList() when loading
*/

// from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {width: myWidth, height:myHeight}
}

function generateVideoList(){
    arrLength = video_array.length;
    var container = document.getElementById('video_container');
    container.innerHTML = "<center><div id=\"flashcontent\"><img style=\"cursor:pointer\" width=\"200\" src=\"/templates/swfobject/clicktoplay.png\" id='click_to_play'/></div></center><span id=\"currentlyplaying\">Currently watching <strong><span id=\"cur_video_name\"></span></strong>.</span>";
	// var containerWidth = container.offsetWidth;
	
	var containerWidth = getWindowSize().width / 3;

    //load a random video
	var whichVideo = Math.floor(Math.random()*arrLength);
    document.getElementById('cur_video_name').innerHTML = video_array[whichVideo].title;
	  var clicktoplay = document.getElementById('click_to_play');
	  clicktoplay.width = containerWidth;
	  clicktoplay.onclick = function(){
    loadVideo(null, video_array[whichVideo].title, video_array[whichVideo].video, containerWidth);
  }
    for(var i = 0; i < arrLength; i++){
       var cur = video_array[i];
       var toInsert = document.createElement('span');
       toInsert.className = "left highlight";
       toInsert.innerHTML = "<a class='target videolink' title=\'" + cur.title + "\' onclick=loadVideo(event,\'" + cur.title + "\',\'" + cur.video + "\'," + containerWidth + ")><img class='video_thumbnail' src=\'" + cur.thumb + "\'/><br/>" + cur.title + "</a>";
       container.appendChild(toInsert);
    }
    window.storedWindowWidth = getWindowSize().width;
}

 function loadVideo(event, name, path, width){
    document.getElementById('cur_video_name').innerHTML = name;
    var so = new SWFObject("/templates/swfobject/FLVPlayer_Progressive.swf", "mymovie", String(width), String(width * .6), "8");
    so.addParam("flashvars",
    "&MM_ComponentVersion=1&skinName=/templates/swfobject/Clear_Skin_2&streamName=" + path + "&autoPlay=true&autoRewind=false");
    so.write("flashcontent");
    if(event || window.event){
      pageTracker._trackPageview('/chose_video/' + encodeURIComponent(name));
    }
}