Nicovideo = new Object();

Nicovideo.MiniPlayer = function (video, vars, width, height) {
	this.id          = Nicovideo.MiniPlayer.allocateId(this);
	this.video       = video;
	this.attributes  = {};
	this.parameters  = {};
	if (navigator.platform === 'Nintendo Wii') delete vars.bgms;
	vars.noAdSense = '1';
	this.variables   = vars || {};
	if (Prototype.Browser.WebKit) {
		this.width       = width  || '100%';
		this.height      = height || '100%';
	} else {
		this.width       = width  || document.documentElement.clientWidth  || document.body.clientWidth  || window.innerWidth  || '100%';
		this.height      = height || document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight || '100%';
	}
	document.title = video.title;
};
Nicovideo.MiniPlayer.prototype = {
	defaultAttributes: {},
	defaultParameters: {
		allowScriptAccess: 'always',
		bgcolor: '#000000',
		quality: 'high'
	},
	defaultVariables: {
		thumbWatch: 1,
		playerTimestamp: '1279158237',
		player_version_xml: '1279158243',
		player_info_xml: '1279266096'
	},


	_merge: function () {
		var o = {};
		for (var i = 0, len = arguments.length; i < len; i++) {
			var arg = arguments[i];
			if (typeof arg != "object") continue;
			for (var key in arg) {
				o[key] = arg[key];
			}
		}
		return o;
	},
	getHTML: function () {
		var date = new Date();
		var day = date.getDay();
		var hours = date.getHours();
		var fpv = swfobject.getFlashPlayerVersion();
		if (navigator.platform === 'Nintendo Wii' && this.video.movieType === 'mp4' && (day === 0 || day === 6 ? hours >= 2 && hours < 10 : hours >= 2 && hours < 12)) {
			return '<p style="font-size:x-large;">この動画はH.264なのでWiiでは再生する事が出来ません<br />平日12時～26時の間と土・日・祝の10時～26時の間になる<br />エコノミーモードの時間帯に再生する事が出来るかもしれません</p>';
		} else if (navigator.platform === 'Nintendo Wii' || fpv.major === 8) {
			Nicovideo.playerUrl = 'http://ext.nicovideo.jp/swf/thumbwatch.swf?ts=1269925820';
		} else if (fpv.major < 8) {
			return '<p>最新の <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player</a> にアップデートしてください</p>';
		} else {
			Nicovideo.playerUrl = 'http://ext.nicovideo.jp/swf/player/thumbwatch.swf?ts=1269925820';
		}

		var id = this.id,
			video = this.video,
			attrs = this._merge(this.defaultAttributes, this.attributes),
			params = this._merge(this.defaultParameters, this.parameters),
			vars = this._merge(
				this.defaultVariables,
				{
					v: video.v,
					videoId: video.id,
					thumbTitle: video.getShortTitle(),
					thumbDescription: video.getShortDescription(),
					thumbImage: video.thumbnail,
					thumbWidth: this.width,
					thumbHeight: this.height,
					mylist_counter: video.mylistCount.toString()
						.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,"),
					movie_type: video.movieType,
					wv_id: video.id
				},
				this.variables
			);

		var flashvars = [];
		for (var key in vars) {
			flashvars.push(key + '=' + encodeURIComponent(vars[key]));
		}
		params.flashvars = flashvars.join("&");

		var html;
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
			html =
				'<embed type="application/x-shockwave-flash"'
				+ ' id="' + id + '"'
				+ ' name="' + id + '"'
				+ ' src="' + Nicovideo.playerUrl + '"'
				+ ' width="' + this.width  + '"'
				+ ' height="' + this.height + '"'
				;
			for (var key in params) {
				html += ' ' + key + '="' + params[key] + '"';
			}
			for (var key in attrs) {
				html += ' ' + key + '="' + attrs[key] + '"';
			}
			html += ' />';
		} else {
			html =
				'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
				+ ' id="' + id + '"'
				+ ' name="' + id + '"'
				+ ' width="' + this.width + '"'
				+ ' height="' + this.height + '"'
				;
			for (var key in attrs) {
				html += ' ' + key + '="' + attrs[key] + '"';
			}
	 		html += '>';
			html += '<param name="movie" value="' + Nicovideo.playerUrl + '" />';
			for (var key in params) {
				html += '<param name="' + key + '" value="' + params[key] + '" />';
			}
			html += '</object>';
		}

		return html;
	},
	write: function (id) {
		if (id === undefined) {
			document.write(this.getHTML());
		} else if (typeof id == "string") {
			var el = document.all ? document.all[id] : document.getElementById(id);
			if (el) el.innerHTML = this.getHTML();
		} else if (id) {
			id.innerHTML = this.getHTML();
		}
	}
};

Nicovideo.MiniPlayer.players = {};
Nicovideo.MiniPlayer.playerCount = 0;
Nicovideo.MiniPlayer.allocateId = function (player) {
	var id = 'external_nico_' + (this.playerCount++);
	this.players[id] = player;
	return id;
};

if (document.all && !window.opera) {
	window.attachEvent("onbeforeunload", function () {
		window.attachEvent("onunload", function () {
			for (var i = 0, l = Nicovideo.MiniPlayer.playerCount; i < l; i++) {
				var id = 'external_nico_' + i, el = document.all ? document.all[id] : document.getElementById(id);
				if (el && el.parentNode) {
					for (var key in el) {
						if (typeof el[key] == 'function')
							el[key] = null;
					}
					el.parentNode.removeChild(el);
				}
			}
		});
	});
}

var q = document.location.search.substring(1).toQueryParams();
if (q.video_id) {
	document.write('<script type="text/javascript" src="http://ext.nicovideo.jp/thumb_watch/' + q.video_id + '"><\/script>');
}
