/**
 * Logo Lite Video Player
 */

jQuery(document).ready(function(){
	if (jQuery("div.player-placeholder").length > 0) {
		LOGOLitePlayer.init();
		LOGOLitePlayer.applyClick();
	}
});

LOGOLitePlayer = new function() {
	this.baseUrl = "http://www.logotv.com";
	
	this.imageServer = "logoonline.mtvnimages.com";
	this.videoUriPrefix = "mgid:uma:video:logotv.com:";
	this.playlistUriPrefix = "mgid:uma:videolist:logotv.com:";
	this.sid = "logo";

	this.affilatedHost = "";

	this.active = false;
	this.playerUrl = "http://media.mtvnservices.com/";
	this.configUrl = "CONFIG_URL_NOT_SET";
	this.width = "240";
	this.height = "211";
	this.playerArgs = "";

	var _this = this;
	var initPlayer = function() {
		jQuery("div.player-placeholder")
				.each(
						function(i) {
							$this = jQuery(this);

							var playerArgs = _this.playerArgs;
							var flashVars;
							var configParams = "";

							if (this.id) {
								configParams = this.id.replace(/\./g, "&")
										.replace(/:/g, "=");
								if ($this.attr("flashVars")) {
									configParams += '&'
											+ $this.attr("flashVars");
								}
							} else {
								if ($this.attr("flashVars").length > 0) {
									configParams = $this.attr("flashVars");
								}
							}
							if (playerArgs.length > 0) {
								playerArgs += "&";
							}

							playerArgs += configParams;

//							flashVars = "sid=MTV_Videos&configParams="
//									+ escape(playerArgs);
							flashVars = "sid="+_this.sid;//+
									//"&configParams="
								//+ escape(playerArgs);

							var paramsFromId = {};
							var params = configParams.split("&");
							var uri;
							jQuery.each(params, function() {
								var pair = this.split("=");
								paramsFromId[pair[0]] = pair[1];
							});

							if (typeof paramsFromId.id != "undefined") {
								uri = _this.playlistUriPrefix + paramsFromId.id;
							} else {
								if (typeof paramsFromId.vid != "undefined") {
									uri = _this.videoUriPrefix
											+ paramsFromId.vid;
								}
							}
							var width = (typeof $this.attr("width") == 'undefined') ? _this.width
									: $this.attr("width");
							var height = (typeof $this.attr("height") == 'undefined') ? _this.height
									: $this.attr("height");
							
							imageServerUri = uri.replace("videolist", "content");
							imageServerUri = imageServerUri.replace(/:[^:]+.com:/, ":logoonline.com:");
							
							this.innerHTML = _this.generatePreloadHTML(
									"playerPlaceholder", width, height, uri,
									flashVars, imageServerUri, playerArgs);

							// Fix for IE breaking when leaving a page where a
							// player has been loaded with litePlayer
							(function() {
								var s = function() {
									__flash__removeCallback = function(i, n) {
										if (i)
											i[n] = null;
									};
									window.setTimeout(s, 10);
								};
								s();
							})();
						});
		this.init = function() {
		};
	}

	this.init = initPlayer;

	this.generatePreloadHTML = function(className, width, height, playerUri,
			flashVars, imageServerUri, playerArgs) {

		var playButtonCenterX = Math.floor(width / 2) - 46;
		var playButtonCenterY = Math.floor(height / 2) - 36;

		var controlBarTop = height - 31;

		var html = '<div style="position: relative; width: ' + width
				+ 'px; height: ' + height + 'px; cursor: pointer;" class="'
				+ className + '" width="' + width + '" height="' + height
				+ '" id="' + playerUri + '" flashVars="' + flashVars + '" playerArgs="' + escape(playerArgs) + '">';

		html += '<img style="position: absolute; top: ' + playButtonCenterY
				+ 'px; left: ' + playButtonCenterX + 'px;" sr' + 'c="'
				+ LOGOLitePlayer.affilatedHost
				+ LOGOLitePlayer.baseUrl + '/global/music/player/images/bttn_play-big.png" />';

		html += '<img src="http://' + _this.imageServer + '/uri/'
//				+ imageServerUri + '?height=' + controlBarTop + '&width='
//				+ width + '" />';
				+ imageServerUri + '?width=' + width + '&height=' + height + '" width=' + width + ' height=' + height + ' />';

		html += '<img style="position: absolute; left: 0px; bottom:0px;" sr'
				+ 'c="' + LOGOLitePlayer.affilatedHost
				+ LOGOLitePlayer.baseUrl + '/global/music/player/images/bttn_play.gif" />';

		html += '<div style="position: absolute; left: 31px; bottom: 0px; width: '
				+ (width - 31)
				+ 'px; height: 31px; background: url('
				+ LOGOLitePlayer.affilatedHost
				+ LOGOLitePlayer.baseUrl + '/global/music/player/images/control-bar.gif) repeat-x top left"> </div>';

		html += '</div>';

		return html;
	}

	this.applyClick = function() {
		var h = 0;
		var w = 0;
		var bgcolor = "#000000";

		jQuery(".playerPlaceholder").one("click", function() {
			var $this = jQuery(this);
			var id = this.id;
			var flashVars = $this.attr("flashVars");
			var playerArgs = $this.attr("playerArgs");

			if (h == 0) {
				h = $this.attr("height");
				w = $this.attr("width");
			}

			var embed = "";
			embed += '<embed src="'+LOGOLitePlayer.playerUrl+''+id+'/cp~autoPlay%3Dtrue%26'+playerArgs+'"';
			embed += 'type="application/x-shockwave-flash"';
			embed += 'wmode="transparent"';
			embed += 'name="' + id + '"';
			embed += 'id="' + id + '"';
			embed += 'width="' + w + '"';
			embed += 'height="' + h + '"';
			embed += 'bgcolor="' + bgcolor + '"';
			embed += 'allowFullscreen="true"';
			embed += 'flashVars="autoPlay=true&' + flashVars + '"';
			//embed += 'flashVars="' + flashVars + '"';
			embed += 'allowScriptAccess="always">';
			embed += '</embed>';

			$this.html(embed);

		});
	}
}
