MIT.Player = {
	init: function(videoList, obj){
		$this = this;
		var video_list = $('vidget_list');
		$('loading_viddler').hide();

		var videos = videoList.video_list.video;
		this.videos = videos;

		var autoplay = obj.autoplay ? "y" : "n";
		var widget_width = (obj.autoplay > 0) ? obj.autoplay : 460;
		var delay = (obj.delay > 0) ? obj.delay * 1000 : 0;

		var show_titles = false;
		var show_description = false;
		var hide_thumbs = false;
		var show_player = true;
		var player_type = "simple";
		var height = videos[0].height;
		var width = videos[0].width;
		var videos_per_row = 3;
		var thumbnail_size = Math.floor((widget_width-(videos_per_row-1)*10)/videos_per_row);
		var skinvars = false;
		var code = '';

    var maxwidth = 460; // The maximum with for the player.

    var expressInstallUrl = "../swfs/expressInstall.swf";
    var attributes = {
            allowfullscreen: "true",
            allowscriptaccess: "always",
            wmode: "transparent",
            smoothing: true
    };

    var params = { autoplay: "t", key: videos[0].id };
    swfobject.embedSWF("http://www.viddler.com/" + player_type + "/" + videos[0].id + "/", "vidget_player", maxwidth, oEmbed(player_type,width,height,maxwidth), "9.0.0", expressInstallUrl, params, attributes);

		function viddlerLink(i, inner) {
		  if(show_player) {
		    return '<a href="#" onclick="MIT.Player.setMovie(\'' + obj.id + '\', \'' + videos[i].id + '\', this, \'' + i + '\'); return false;">' + inner + '</a>';
		  } else {
		    return '<a href="' + videos[i].url + '">' + inner + '</a>';
		  }
		}

		function html_entity_decode(str) {
		  var ta=document.createElement("textarea");
		  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
		  return ta.value;
		}

		var count = 1;

		for(i=0; i < videos.length; i++) {
			code += '<li';
			classes = '';
			if(count == 1) classes += 'first';
			if(count % 2 == 0) classes += ' even';
			if(classes != '') code += " class='" + classes + "'";

			code += '>';
			title = '';
			if(!hide_thumbs) {
			  title += '<img src="' + videos[i].thumbnail_url + '" height="' + thumbnail_size + '" title="' + videos[i].title + '" alt="' + videos[i].title + '" />';
		  }
			if(show_titles) title += viddlerLink(i, videos[i].title);
			code += viddlerLink(i, title);

		  if(show_description && videos[i].description) code += html_entity_decode(videos[i].description);
		  code += '</a></li>';
		
			if(count < videos_per_row) {
		  	count++;
		  } else {
		    count = 1;
		  }
		}

		video_list.innerHTML = code;

		$$("div.viddler_widget ul img").each( function(img) {
			new Tooltip(img, {mouseFollow: false});
		});

		var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
		if(playerVersion.major < 7) {
			var vidget = $('vidget');
			vidget.innerHTML = '<p class="error">You need <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player</a> to watch videos on this site.</p>' + vidget.innerHTML;
		}

		if (obj.delay > 0) {
			this.playMovie(obj.id, delay);
		}
	},

  // Checking if flash object method playMovie is available
	playMovie: function(id, delay) {
		$this = this;
		try {
			$('vidget_player').playMovie();
			$this.delay = setTimeout(function(){$this.nextMovie(1, id, delay)}, delay);
			$('vidget_list').down("a").addClassName("playing");
		} catch (ex) {
			setTimeout(function(){
				$this.playMovie(id, delay);
			}, 5000);
		}
	},

	nextMovie: function(i, id, delay) {
		count = this.videos.length;
		$('vidget_player').setMovie(this.videos[i].id, 0);
    $('vidget_player').setStyle({height:oEmbed('simple',this.videos[i].width,this.videos[i].height,'460')+'px'});
		$$('#vidget_list a').invoke("removeClassName", "playing");
		$('vidget_list').down("a", i).addClassName("playing");
		i = (i < count-1)? i+1 : 0;
		if (delay && delay > 0) {
			this.delay = setTimeout(function(){MIT.Player.nextMovie(i, id, delay)}, delay);
		}
	},

	setMovie: function(id, video, link, i) {
		clearTimeout(this.delay);
		$('vidget_player').setMovie(video, 0);
    $('vidget_player').setStyle({height:oEmbed('simple',this.videos[i].width,this.videos[i].height,'460')+'px'});
		$$('#vidget_list a').invoke("removeClassName", "playing");
		$(link).addClassName("playing");
	}
}




 // Returns the proper height based on a maxwidth
 // This is a poor man's oEmbed because it
 // doesn't require another call to the oEmbed service
 function oEmbed(player_type,width,height,maxwidth) {
  
  // Difference between Width and Height
  if (width>height) {
    var diff = height/width;
  } else {
    var diff = width/height;
  }
  
  // 16:9 or 4:3?
  if (diff<.70) {
    var ratio = 'widescreen';
  } else {
    var ratio = 'fullscreen';
  }
  
  // Player type?    
  switch (player_type) {
    case 'player':
      var plusminus = 42;
    break;
    
    case 'simple':
      var plusminus = 20;
    break;
    
    default:
      // Assume simple
      var plusminus = 20;
    break;
  }
  
  // Adjust height
  switch (ratio) {
    case 'fullscreen':
      height = Math.round((maxwidth * (3/4))+plusminus);
    break;
    
    case 'widescreen':
      height = Math.round((maxwidth * (9/16))+plusminus);
    break;
    
    default:
      // Default fullscreen
      height = Math.round((maxwidth * (3/4))+plusminus);
    break;
  }
  
  return height;

 }
