var YoutubePlaylist = new function(){
	//publicメソッド
	this.getIndex = function(){
		if(document.getElementById("youtubePlaylistRoot").childNodes.length <= 2){
			var _container = document.getElementById("youtubePlaylistRoot");
			var container = _container.appendChild(document.createElement("p"));
			container.setAttribute("id","yotubeRootProgress");
			container.innerHTML = "ロード中…";
			var icon = document.getElementById("youtubeFolder_root");
			icon.src = "/common/img/icon_folder_o.gif";
			createXMLHttp(playlistIndexXML);
		}else{
			var lists = document.getElementById("PlaylistAll");
			var icon = document.getElementById("youtubeFolder_root");
			if(lists.style.display == "block"){
				lists.style.display = "none";
				icon.src = "/common/img/icon_folder_c.gif";
			}else{
				lists.style.display = "block";
				icon.src = "/common/img/icon_folder_o.gif";
			}
		}
	}
	
	this.getPlaylist = function(PlaylistId){
		if(document.getElementById("YoutubePlaylistId_"+PlaylistId).childNodes.length <= 2){
			var _container = document.getElementById("YoutubePlaylistId_"+PlaylistId);
			var container = _container.appendChild(document.createElement("p"));
			container.setAttribute("id",PlaylistId+"Progress");
			container.innerHTML = "ロード中…";
			var icon = document.getElementById("folder_"+PlaylistId);
			icon.src = "/common/img/icon_folder_o.gif";
			createXMLHttp("http://blog.ks-product.com/crossdomain-proxy.php?url=http://gdata.youtube.com/feeds/api/playlists/"+PlaylistId);
		}else if(document.getElementById("Playlist"+PlaylistId)){
			var Playlist = document.getElementById("Playlist"+PlaylistId);
			var icon = document.getElementById("folder_"+PlaylistId);
			if(Playlist.style.display == "block"){
				Playlist.style.display = "none";
				icon.src = "/common/img/icon_folder_c.gif";
			}else{
				Playlist.style.display = "block";
				icon.src = "/common/img/icon_folder_o.gif";
			}
		}
	}
	
	this.showVideoDetail = function(uniqueId){
		clearInterval(timer);
		
		if(videoInfoArray[uniqueId]){
			var e;
			if(!(e = document.getElementById("youtubePlaylistPopup"))){
				e = document.createElement("div");
				e.setAttribute("id","youtubePlaylistPopup");
				e.style.left = "-730px";
				e.style.top = "-100px";
			}
			e.innerHTML = "<p class=\"videoTitle\">"+videoInfoArray[uniqueId]["title"]+"</p><div class=\"youtube-desc-wrapper\"><div class=\"youtube-thumbnail\"><img src=\""+videoInfoArray[uniqueId]["thumb"]+"\" alt=\"\" width=\"320\" height=\"240\" /></div><div class=\"youtube-description\">"+videoInfoArray[uniqueId]["description"]+"</div></div>";
			
			var container = document.getElementById("YoutubeVideoId_"+uniqueId);
			container.appendChild(e);
			container.style.position = "relative";
			document.getElementById("youtubePlaylistPopup").style.display = "block";
		}
	}
	
	this.hiddenVideoDetal = function(){
		timer = setInterval(function(){
			document.getElementById("youtubePlaylistPopup").style.display = "none";
			clearInterval(timer);
		},500);
	}

	//privateメソッド
	function createXMLHttp(url){
		var xmlHttp;
		
		try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
				xmlHttp=new XMLHttpRequest();
		}
		
		xmlHttp.onreadystatechange = function() {
		　　　　if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		　　　　　onComplete(xmlHttp,url);
		　　　　}else if(xmlHttp.readyState == 4 && (xmlHttp.status != 200 || xmlHttp.responseXML == null)){
							createXMLHttp(url);
						}
		　　};
		xmlHttp.open("GET",url,true); 
		xmlHttp.send(null);
	}
	
	function onComplete(xmlHttp,url){
		if(url == playlistIndexXML){
			var _container = document.getElementById("youtubePlaylistRoot");
			var count = xmlHttp.responseXML.getElementsByTagName("item").length;
			var html = "";
			var container = _container.appendChild(document.createElement("ul"));
			container.setAttribute("id","PlaylistAll");
			container.style.display = "block";
			_container.removeChild(document.getElementById("yotubeRootProgress"));
			
			for(var i = 0; i < count; i++){
				var PlaylistId = xmlHttp.responseXML.getElementsByTagName("item")[i].getAttribute("href");
				var title = xmlHttp.responseXML.getElementsByTagName("item")[i].getAttribute("title");
				html += '<li id="YoutubePlaylistId_'+PlaylistId+'"><a href="#" onclick="YoutubePlaylist.getPlaylist(\''+PlaylistId+'\'); return false;"><img id="folder_'+PlaylistId+'" src="/common/img/icon_folder_c.gif" alt="" width="16" height="14" style="vertical-align:middle; margin-right:5px;" /></a><a href="#" onclick="YoutubePlaylist.getPlaylist(\''+PlaylistId+'\'); return false;">'+title+'</a></li>';
			}
			container.innerHTML = html;
		}else{
			var count = xmlHttp.responseXML.getElementsByTagName("entry").length;
			var html = "";
			var PlaylistIdFull = xmlHttp.responseXML.getElementsByTagName("id")[0].firstChild.nodeValue;
			var PlaylistId = PlaylistIdFull.slice(PlaylistIdFull.lastIndexOf("/")+1);
			var _container = document.getElementById("YoutubePlaylistId_"+PlaylistId);
			var container = _container.appendChild(document.createElement("ul"));
			container.setAttribute("id","Playlist"+PlaylistId);
			container.style.display = "block";
			container.style.marginBottom = "15px";
			_container.removeChild(document.getElementById(PlaylistId+"Progress"));

			for(var i = 0; i < count; i++){
				var item = xmlHttp.responseXML.getElementsByTagName("entry")[i];
				if(item.getElementsByTagName("app:control").length || item.getElementsByTagName("control").length) continue; //削除済みは除外
				var title = item.getElementsByTagName("title")[0].firstChild.nodeValue;
				var link = item.getElementsByTagName("link")[0].getAttribute("href");
				var videoId = link.slice(link.indexOf("?v=")+3,link.indexOf("&"));
				var uniqueId = "y_"+videoId+"_"+PlaylistId;
				var description;
				try{
					description = item.getElementsByTagName("content")[0].firstChild.nodeValue;
					if(description.length >= 700) description = description.slice(0,700) + " ...";
				}catch(e){
					description = "";
				}
				var thumb = (item.getElementsByTagName("media:thumbnail").length)?item.getElementsByTagName("media:thumbnail")[3].getAttribute("url"):item.getElementsByTagName("thumbnail")[3].getAttribute("url");
				videoInfoArray[uniqueId]= {title:title,link:link,videoId:videoId,description:description,thumb:thumb};
				html += '<li id="YoutubeVideoId_'+uniqueId+'" class="videoItem"><a href="#" onclick="openYoutubeVideoWindow(\''+videoId+'\'); return false;" onmouseover="YoutubePlaylist.showVideoDetail(\''+uniqueId+'\'); over(this);" onmouseout="YoutubePlaylist.hiddenVideoDetal(); out(this);"><span>video '+(i+1)+'</span><img class="openNewWindowIcon" src="/common/img/icon_new_window.gif" alt="" width="10" height="9" /></a><span style="color:#999999;">（<a href="http://www.youtube.com/watch?v='+videoId+'" target="_blank" onmouseover="YoutubePlaylist.showVideoDetail(\''+uniqueId+'\'); over(this);" onmouseout="YoutubePlaylist.hiddenVideoDetal(); out(this);"><span>on Youtube</span><img class="openNewWindowIcon" src="/common/img/icon_new_window.gif" alt="" width="10" height="9" /></a>）</span></li>';
			}
			container.innerHTML = html;
		}
	}
	
	function drawBaseHTML(){
		document.write(
				 '<div class="youtubePlaylist">'
				+'	<dl>'
				+'		<dt class="naviHeading"><img src="/common/img/txt_h_youtubeplaylist.gif" alt="Youtubevideo Playlist" width="121" height="13" /></dt>'
				+'		<dd>'
				+'			<ul id="youtubePlaylistContainer" class="folder"><li id="youtubePlaylistRoot"><a href="#" onclick="YoutubePlaylist.getIndex(); return false;"><img id="youtubeFolder_root" src="/common/img/icon_folder_c.gif" alt="" width="16" height="14" style="vertical-align:middle; margin-right:5px;" /></a><a href="#" onclick="YoutubePlaylist.getIndex(); return false;">YouTube再生リスト</a></li></ul>'
				+'		</dd>'
				+'	</dl>'
				+'</div>');
	}
	
	//エントリーポイント
	var videoInfoArray = new Array();
	var timer;
	var playlistIndexXML = "/common/xml/youtubePlaylist.xml";
	
	drawBaseHTML();
}
