if (typeof Nicovideo == 'undefined') {
	Nicovideo = new Object();
}

Nicovideo.version = '1.4.2';
Nicovideo.url = 'http://www.nicovideo.jp/';
Nicovideo.playerUrl = 'http://ext.nicovideo.jp/swf/player/thumbwatch.swf?ts=1264842453';

if (typeof Nicovideo.Video == 'undefined') {
	Nicovideo.Video = function (video) {
		if (video) {
			for (var key in video) {
				this[key] = video[key];
			}
		}
	};
	Nicovideo.Video.create = function (video) {
		if (typeof this.__cache == "undefined") this.__cache = {};
		var v = this.__cache[video.v];
		if (!v) v = this.__cache[video.v] = new this(video);
		return v;
	};
	Nicovideo.Video.prototype = {
		getPlayURL: function () {
			return Nicovideo.url + 'watch/' + this.v;
		},
		getShortTitle: function () {
			return this.title.substring(0, 60) + 
				(this.title.length > 60 ? "..." : "");
		},
		getShortDescription: function () {
			return this.description.substring(0, 60) + 
				(this.description.length > 60 ? "..." : "");
		}
	};
}
if (typeof Nicovideo.MiniPlayer == 'undefined') {
	Nicovideo.MiniPlayer = function (video, vars, width, height) {
		this.id          = Nicovideo.MiniPlayer.allocateId(this);
		this.video       = video;
		this.attributes  = {};
		this.parameters  = {};
		this.variables   = vars || {};
		this.width       = width  || '485';
		this.height      = height || '385';
	};
	Nicovideo.MiniPlayer.prototype = {
		defaultAttributes: {},
		defaultParameters: {
			allowScriptAccess: 'always',
			bgcolor: '#000000',
			quality: 'high'
		},
		defaultVariables: {
			thumbWatch: 1,
			playerTimestamp: '1265781359',
			player_version_xml: '1265777808'
		},


		_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 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,
						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);
					}
				}
			});
		});
	}


}

(function () {
	var b = true;
	try {
		var w = window.parent, self = window.self;
		while (w && w != self) {
			if (w.location.host != self.location.host) {
				b = false;
				break;
			}
			if (w === w.parent) break;
			w = w.parent;
		}
	} catch (e) {
		b = false;
	}

	if (!b) {
		Nicovideo = undefined;
		document.write(
			'<p><a href="http://www.nicovideo.jp/watch/sm8313864" target="_blank">[MAD] \u8d85\u5f29\u7d1a\u5c11\u59734946\u300c\u3042\u306a\u305f\u306e\u3053\u3068\u304c\u3001\u597d\u304d\u306b\u306a\u308a\u307e\u3072\u305f\u300d</a></p>'
		);
		return;
	}

	var video = Nicovideo.Video.create({
		v: 'sm8313864'
	,	id: 'sm8313864'
	,	title: '[MAD] \u8d85\u5f29\u7d1a\u5c11\u59734946\u300c\u3042\u306a\u305f\u306e\u3053\u3068\u304c\u3001\u597d\u304d\u306b\u306a\u308a\u307e\u3072\u305f\u300d'
	,	description: '\u300c\u8d85\u5f29\u7d1a\u5c11\u59734946\u300d\u306e\u5358\u884c\u672c\u767a\u58f2\u8a18\u5ff5\u3063\u3066\u3053\u3068\u3067\u3001\u52dd\u624b\u306a\u304c\u3089\u5fdc\u63f4MAD\u3092\u4f5c\u3089\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002\u3000by\u85e9\u7530\u5fd7\u4fe1\u3000mylist\/8319140 '
	,	thumbnail: 'http:\/\/tn-skr1.smilevideo.jp\/smile?i=8313864'
	,	postedAt: new Date('2009\/09\/23 14:09:32 +0900')
	,	length: 131
	,	viewCount: 6532
	,	mylistCount: 105
	,	commentCount: 51
	,	movieType: 'mp4'
	,	isDeleted: false	,	isMymemory: false	});
	var player = new Nicovideo.MiniPlayer(video, {
		thumbPlayKey: '1265892131.0.4jKLCswDjTPncrPRWKhlNkaQ0Tc.'
	,	wv_title: '[MAD] \u8d85\u5f29\u7d1a\u5c11\u59734946\u300c\u3042\u306a\u305f\u306e\u3053\u3068\u304c\u3001\u597d\u304d\u306b\u306a\u308a\u307e\u3072\u305f\u300d'
	,	iee: '1'
	}, '485', '386');
	player.write();
})();

