(function(){
	var newEntryXML = "/atom.xml";
	var videoXML = "http://blog.ks-product.com/crossdomain-proxy.php?url=http://ks-product.com/common/xml/videolist.xml";
	
	drawBaseHTML();
	createXMLHttp(newEntryXML);
	createXMLHttp(videoXML);
	
	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);
			}
		};
		
		xmlHttp.open("GET",url,true); 
		xmlHttp.send(null);
	}
	
	function onComplete(xmlHttp,url){
		if(url == newEntryXML){
			var container = document.getElementById("newEntryList").getElementsByTagName("ul")[0];
			var count = 10;
			var html = "";
			
			if(xmlHttp.responseXML.getElementsByTagName("entry").length < count){
				count = xmlHttp.responseXML.getElementsByTagName("entry").length;
			}
			
			for(var i = 0; i < count; i++){
				var date = xmlHttp.responseXML.getElementsByTagName("entry")[i].getElementsByTagName("published")[0].firstChild.nodeValue.slice(0,10);
				var title = xmlHttp.responseXML.getElementsByTagName("entry")[i].getElementsByTagName("title")[0].firstChild.nodeValue;
				var url = xmlHttp.responseXML.getElementsByTagName("entry")[i].getElementsByTagName("link")[0].getAttribute("href");
				html += "<li><span class='date'>"+date+"</span> ： <a href='"+url+"'>"+title+"</a></li>";
			}
			container.innerHTML = html;
		}else if(url == videoXML){
			var titleContainer = document.getElementById("newVideoVideoTitle");
			var watchByNico2Container = document.getElementById("newVideoWatchByNico2");
			var thumbContainer = document.getElementById("newVideoVideoThumb");
			var item;
			var items = xmlHttp.responseXML.getElementsByTagName("item");
			var itemLen = items.length;
			for(var j=0; j<itemLen; j++){
				if ( -1 != String(items[j].getAttribute("category")).indexOf("pickup"))	{
					item = items[j];
					break;
				}
			}
			var title = item.getAttribute("title");
			var img = item.getElementsByTagName("thumbnail")[0].getAttribute("src");
			var url = item.getElementsByTagName("path")[0].getAttribute("href");
			var urlNico2 = item.getElementsByTagName("path")[0].getAttribute("nicohref");
			
			titleContainer.innerHTML = '<a href="'+url+'" onmouseover="over(this);" onmouseout="out(this);"><span>'+title+'</span><img class="openNewWindowIcon" src="/common/img/icon_new_window.gif" alt="" width="10" height="9" /></a>'; 
			watchByNico2Container.innerHTML = '<a href="'+urlNico2+'" target="_blank"><img src="/common/img/btn_watch_by_nico2.gif" alt="ニコニコ動画で見る" width="117" height="20" /></a>';
			thumbContainer.setAttribute("href",url);
			thumbContainer.innerHTML = '<img src="'+img+'" alt="" width="160" height="90" />';
		}else{
			return;	
		}
	}
	
	function drawBaseHTML(){
		document.write(
			'<div id="newEntryListWrapper">'
		+'	<div id="newEntryList">'
		+'		<div class="title"><img src="/common/img/txt_h_newentry.gif" alt="最近投稿された記事" width="146" height="28" /></div>'
		+'		<ul>'
		+'			<li>ロード中…</li>'
		+'		</ul>'
		+'		<div class="archivesButton"><a href="/archives.html"><img src="/common/img/btn_newentry_archive.gif" alt="記事一覧を表示" width="125" height="28" /></a></div>'
		+'	</div>'
		+''	
		+'	<div id="newVideo">'
		+'		<div class="title"><img src="/common/img/txt_h_newvideo.gif" alt="最近投稿された作品" width="147" height="28" /></div>'
		+'		<div id="newVideoVideoTitle">ロード中…</div>'
		+'		<div id="newVideoWatchByNico2"><a href="#"><img src="/common/img/btn_watch_by_nico2.gif" alt="ニコニコ動画で見る" width="103" height="20" /></a></div>'
		+'		<a id="newVideoVideoThumb" href="#"></a>'
		+'	</div>'
		+'</div>');
	}
})();
