var UserSwf = Class.create();
UserSwf.prototype = {
	movie_obj : '',
	repeat : 1,
	videoid : "",
	base_url : "http://www.remotetube.net/msg/",
	width : 480,
	height :380,
	status : -1,
	volume : 50,
	videotitle : '',
	initialize : function(lang){
		if(lang == "jp"){
			this.base_url = "http://jp.remotetube.net/msg/";
		}
		var timer = new PeriodicalExecuter(this.check.bindAsEventListener(this), this.repeat);
	},
	 loadClickedMovie: function(id){
		var url = this.base_url + "AjaxSetMovie";
		var postBody = "video_id=" + id;
		this.req(url, postBody,this.clickedMovie);
	},
	clickedMovie:function(req){
//		this.loadMovie();
	},
	check : function(){
		var seconds = 0;
		var duration = 0;
		if(this.movie_obj){
			seconds = this.movie_obj.getCurrentTime();
			this.status = this.movie_obj.getPlayerState();
			this.volume = this.movie_obj.getVolume();
			duration = this.movie_obj.getDuration();
		}
		var url = this.base_url + "GetCommand";
		var postBody = "second=" + seconds + "&status=" + this.status + "&volume=" + this.volume + "&duration=" + duration;
		this.req(url, postBody, this.setCommand.bindAsEventListener(this));
	},
	setCommand : function(req){

		var xml = req.responseXML;
		var com = xml.getElementsByTagName('command')[0].firstChild.nodeValue;

		if(com == 1){
			this.loadMovie();
		}else if(com == 2){
			this.stopMovie();
		}else if(com == 3){
			this.startMovie();
		}else if(com == 4){
			var thisSecond = xml.getElementsByTagName('second')[0].firstChild.nodeValue;
			this.seek(thisSecond);
		}else if(com == 5){
			var thisVolume= xml.getElementsByTagName('volume')[0].firstChild.nodeValue;
			this.setVolume(thisVolume);
		}else if(com == 6){
            this.fullScreen();
        }else if(com == 7){
            this.normalSize();
        }
		return true;
	},
	seek : function(thisSecond){
		if(this.movie_obj){
			this.movie_obj.seekTo(thisSecond);
		}
	},
	setVolume : function(thisVolume){
		if(this.movie_obj){
			this.movie_obj.setVolume(thisVolume);
		}
	},
	loadMovie : function(){
		var url = this.base_url + "GetAjaxWatchingMovie";
		var postBody = "";
		this.req(url, postBody, this.loadSwf.bindAsEventListener(this));
	},
	
	req: function(url, body, func){

		var myAjax = new Ajax.Request(
			url, 
			{
			method : 'post',
			postBody : body,
			onSuccess: func.bindAsEventListener(this)
			}
		);
	},
	loadSwf: function(req){

		var xml = req.responseXML;
		var new_id = xml.getElementsByTagName('movie_id')[0].firstChild.nodeValue;
		var videotitle = xml.getElementsByTagName('video_title')[0].firstChild.nodeValue;
		if (this.videoid != new_id) {
			this.videoid = new_id;
		$('video_title').innerHTML = videotitle;


			this.startVideo();
		}
	},
	startVideoOn: function(){
		this.movie_obj = $(this.videoid);
	},
	startVideo : function(){
		var params = {
			allowScriptAccess: "always"
		};
		var atts = {
			id: this.videoid
		};
		if (this.createHtml()) {
			swfobject.embedSWF("http://www.youtube.com/v/" + this.videoid + "&enablejsapi=1&playerapiid=ytplayer&rel=0&autoplay=1&disablekb=1&fmt=22", this.videoid, this.width, this.height, "8", null, null, params, atts);
		}
	},
    fullScreen : function(){
			this.movie_obj.style.position="absolute";
			this.movie_obj.style.top = 0;
			this.movie_obj.style.left = 0;
            this.movie_obj.style.height = "100%";
            this.movie_obj.style.width = "100%";
    },
    normalSize : function(){
			this.movie_obj.style.position="static";
            this.movie_obj.style.width = "480px";
            this.movie_obj.style.height = "380px";

    },
	createHtml: function(){
		var parent = $('player');
		parent.innerHTML = "";
		var divs = document.createElement("div");
		divs.id = this.videoid;
		divs.innerHTML = '<object width="'+this.width+'" height="'+this.height+'"><param name="movie" value="http://www.youtube.com/v/'+this.videoid+'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+this.videoid+'" type="application/x-shockwave-flash" wmode="transparent" width="'+this.width+'" height="'+this.height+'"></embed></object>';
		parent.appendChild(divs);
		return true;
	},
	stopMovie : function(){
		if (this.movie_obj) {
			this.movie_obj.pauseVideo();
		}
	},
	startMovie: function(){
		if (this.movie_obj) {
			this.movie_obj.playVideo();
		}else{
			this.loadMovie();
		}
	},
	debug : function(str){
		alert(str);
	}
}
function loadVideo(vid){
	var parent = $('player');
	parent.innerHTML = "";
	var div = document.createElement("div");
	div.id = vid;
	div.innerHTML = '<object width="480" height="38-"><param name="movie" value="http://www.youtube.com/v/'+vid+'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+vid+'" type="application/x-shockwave-flash" wmode="transparent" width="480" height="380"></embed></object>';

	parent.appendChild(div);
	var params = {
		allowScriptAccess: "always"
	};
	var atts = {
		id: vid
	};
	swfobject.embedSWF("http://www.youtube.com/v/" + vid + "&enablejsapi=1&playerapiid=ytplayer&rel=0&autoplay=1&disablekb=1&fmt=22", vid, 480, 380, "8", null, null, params, atts);
}
function getVideoList(id, lang){
	var url = "http://www.remotetube.net/list/";
	if(lang == "jp"){
		url = "http://jp.remotetube.net/list/";
	}
	var pars = 'id='+id+'&lang='+lang;
	colorChange(id);
	var myAjax = new Ajax.Updater(
		'item_box', 
		url, 
		{
			method: 'POST', 
			postBody: pars
		});
}
function colorChange(id){
	var gray = "#c0c0c0";
	var black = "#000000";
	var h_color = gray;
	var p_color = gray;
	var pl_color = gray;
	switch(id){
		case 1:
			pl_color = black;
			break;
		case 2:
			h_color = black;
			break;
		case 3:
			p_color = black;

			break;
	}
	$("history_list").style.background = h_color;
	$("popular_video").style.background = p_color;
//	$("playlist").style.background = pl_color;

}
