function nicoEmbed(){
    var self = arguments.callee;
    if(self.instance == null){
	this.initialize();
	self.instance = this;
    }
    return self.instance;
}

nicoEmbed.prototype.initialize = function(){
    this.playerWidth = 480;
    this.playerHeight = 405;
    this.nicoPluginURL = 'http://www.nicovide.jp/wp-content/plugins/base/';
    this.scriptFiles = [this.nicoPluginURL + 'swfobject.js',
			this.nicoPluginURL + 'browserinfo.js'];
    this.launchStack = new Array();;
    for(var i in this.scriptFiles){
	var url = this.scriptFiles[i];
	var element = document.createElement('script');
	element.src = url;
	document.body.appendChild(element);
	//element.addEventListener('onreadystatechange', 
    }
    this.scriptLoaded.callTimeout(5000, this);
};

nicoEmbed.prototype.scriptLoaded = function(){
    this.nicoSwfFile = this.nicoPluginURL + 'playerSWF.swf';
    this.initialized = true;
    this.callLaunchStack();
};

nicoEmbed.prototype.launch = function(element, purveyor){
    if(this.initialized != true){
	//this.launchStack[this.launchStack.length] = function(){this.launch.apply(this,arguments);};
	this.addLaunchStack(element, purveyor);
    }else{
	var params = {};
	params.allowfullscreen = 'true';
	var flashvars = {
	    numid: this.getNumid(element, purveyor),
	    purveyor: purveyor,
	    nicoid: 0,
	    wait: 1,
	    useragent: navigator.userAgent
	};
	element.id = Math.floor(Math.random()*256);
	swfobject.embedSWF(this.nicoSwfFile, element.id, this.playerWidth, this.playerHeight, '9.0.0', "expressInstall.swf", flashvars, params);
    }
};

nicoEmbed.prototype.getNumid = function(element, purveyor){
    switch(purveyor){
    case 2:
    var url = element.src;
    var numid = url.substring(url.indexOf('sid/')+4,url.indexOf('/v.swf'));
    break;
    }
    return numid;
};

nicoEmbed.prototype.searchElements = function(purveyor){
    var ret = new Array();
    switch(purveyor){
    case 2:
	var elements = document.getElementsByTagName('embed');
	for(var i=0; i<elements.length; i++){
	    if(elements[i].src.indexOf('player.youku.com') != -1){
		ret.push(elements[i]);
	    }
	}
    } // end switch
    return ret;
};

nicoEmbed.prototype.replaceEmbed = function(purveyor){
    var elements = this.searchElements(purveyor);
    for(var i in elements){
	//this.launchStack[this.launchStack.length] = this.launch(elements[i], purveyor);
	this.launch(elements[i], purveyor);
    }
};

nicoEmbed.prototype.callLaunchStack = function(){
    for(var i=0; i<this.launchStack.length; i++){
	this.launchStack[i].func.apply(this, this.launchStack[i].args);
    }
};

nicoEmbed.prototype.addLaunchStack = function(element, purveyor){
    this.launchStack[this.launchStack.length] = {};
    this.launchStack[this.launchStack.length-1].func = function(){this.launch.apply(this, arguments);};
    this.launchStack[this.launchStack.length-1].args = arguments;
}

Function.prototype.applyTimeout = function (ms, self, args) {
    var f = this;
    return setTimeout(
		      function () {
			  f.apply(self, args);
		      },
		      ms);
};

Function.prototype.callTimeout = function (ms, self) {
    return this.applyTimeout(
			     ms,
			     self,
			     Array.prototype.slice.call(arguments, 2));
};

Function.prototype.applyInterval = function (ms, self, args) {
    var f = this;
    return setInterval(
		       function () {
			   f.apply(self, args);
		       },
		       ms);
};

Function.prototype.callInterval = function (ms, self) {
    return this.applyInterval(
			      ms,
			      self,
			      Array.prototype.slice.call(arguments, 2));
};

try{
    window.addEventListener('load', loadNico,false);
}catch(e){
    window.attachEvent('onload', loadNico);
}

function loadNico(){
    var nico = new nicoEmbed();
    nico.replaceEmbed(2);
}

/* bookmarklet
javascript:(function(){
  var%20launch=function(){
    var%20nico=new%20nicoEmbed();
    nico.replaceEmbed(2);
  };
  var%20crs=function(sc){
    switch(sc.readyState){
    case 'complete':
    case 'loaded':
      launch();
    }
  };
  if(typeof(lf)=='undefined'){
    var%20sc=document.createElement('script');
    sc.src='http://www.nicovide.jp/wp-content/plugins/nico/nicoEmbed.js';
    if(document.all){
      sc.onreadystatechange=function(){crs(sc);};
    }else{
      sc.onload=launch;
    }
    document.body.appendChild(sc);
    lf=true;
  }
})();
*/