/*==========================================================

 @sitename JPAP.JP
 @lastupdate 2009/4/23

==========================================================*/

/* News Feed
----------------------------------------------------------*/
(function(){
	
	var newsfeed;
	var ajax = new KEY.Data.XHRequest();
	var keycode = "?kQbqc=nvoej";
	
	KEY.Module.NewsFeed = function() {
		KEY.User.Size.call(this);
		KEY.HTML.GetElement.call(this);
	}
	
	KEY.Module.NewsFeed.prototype = {
	
		init : function(id) {
			newsfeed = this.$(id);
			newsfeed.innerHTML = "";
		},
		
		load : function(file, max) {
			//if (location.protocol != "http:") return;
			
			ajax.onLoadError = function(){newsfeed.innerHTML = '<p class="mt20 tc">XMLファイルの読み込みに失敗しました。</p>'};
			ajax.onLoading = function(){newsfeed.innerHTML = '<p class="mt20 tc">XMLファイルの読み込み中...</p>'};
			
			ajax.onComplete = function(xml) {
				
				var years = document.createElement("ul");
				years.id = "Years";
				newsfeed.innerHTML = "";
				newsfeed.appendChild(years);
				
				var container;
				var item = xml.getElementsByTagName("更新情報");
				var index = item.length;
				
				for (var i=0; i<index; i++) {
					
					//Definition List
					var dl = document.createElement("dl");
					var category = item[i].getAttribute("掲載");
					if (!category) continue;
					if (!category.match("ポータル")) continue;
					
					//Date Term
					var dt = document.createElement("dt");
					
					if (category.match("医療")) dt.innerHTML = '<img src="cmn/portal/img/icn_news_medical.gif" alt="医療" />';
					if (category.match("一般")) dt.innerHTML = '<img src="cmn/portal/img/icn_news_general.gif" alt="一般" />';
					if (category.match("1020")) dt.innerHTML = '<img src="cmn/portal/img/icn_news_1020.gif" alt="1020" />';
					
					var Dated = item[i].getElementsByTagName("日付")[0];
					if(Dated) {
						var date = Dated.firstChild.nodeValue;
						var current = date.split(".")[0];
					} else {
						var date = "&nbsp;";
					}
					
					dt.innerHTML += date;
					dl.appendChild(dt);
					
					
					//Description
					var dd = document.createElement("dd");
					var Text = item[i].getElementsByTagName("内容")[0];
					if(Text) {
						var text = Text.firstChild.nodeValue;
					} else {
						var text = "&nbsp;";
					}
						
					var Link = item[i].getElementsByTagName("リンク")[0];
					if(Link) {
						var a = document.createElement("a");
						var href = Link.getAttribute("アドレス");
						var target = Link.getAttribute("オプション");
						if(href) a.setAttribute("href", href);
						if(target) a.setAttribute("target", "_blank");
					}
					
					//Description > set content
					if (Link && text) {
						a.innerHTML = text;
						dd.appendChild(a);
					} else if(text) {
						dd.innerHTML = text;
					}
					dl.appendChild(dd);
					
					//Append YearTabs and Container
					if (!container) {
						year = document.createElement("li");
						var a = document.createElement("a");
						a.className = "unscroll";
						a.href = "#"+ current;
						a.onclick = tabs;
						a.innerHTML = '<img src="img/news_year'+ current +'_ov.gif" alt="'+ current +'年度" />';
						year.appendChild(a);
						years.appendChild(year);
						
						container = document.createElement("div");
						container.id = current;
						container.className = "newscontainer show";
						newsfeed.appendChild(container);
					} else if(container.id != current) {
						year = document.createElement("li");
						var a = document.createElement("a");
						a.className = "unscroll";
						a.href = "#"+ current;
						a.onclick = tabs;
						a.innerHTML = '<img src="img/news_year'+ current +'_ot.gif" alt="'+ current +'年度" />'
						year.appendChild(a);
						years.appendChild(year);
						
						container = "";
						container = document.createElement("div");
						container.id = current;
						container.className = "newscontainer hide";
						newsfeed.appendChild(container);
					}
					
					//document.getElementById(current).appendChild(dl);
					container.appendChild(dl);
					
				}
			};
			
			ajax.load(file);
		}
		
	}
	
	var tabs = function() {
		KEY.HTML.GetElement.call(this);
		var years = this.$("#Years").getElementsByTagName("img");
		
		var target = this.$(this.hash);
		var container = this.$("div.newscontainer");
		var index = container.length;
		
		for (var i=0; i<index; i++) {
			if (target.className.match("show")) continue;
			years[i].src = years[i].src.replace("_ov", "_ot");
			var classname = container[i].className;
			if (classname.match("show")) container[i].className = classname.replace("show", "hide");
		}
		
		var tabimage = this.getElementsByTagName("img")[0];
		tabimage.src = tabimage.src.replace("_ot", "_ov");
		// rollover対策
		tabimage.onmouseover = function() {};
		tabimage.onmouseout = function() {};
		
		if (target.className.match("hide")) {
			target.className = target.className.replace("hide", "show");
			if (target.style.filter != undefined) {
				target.style.zoom = 1;
				target.style.filter = "alpha(opacity=0)";
			} else {
				target.style.opacity = 0;
			}
			
			var self = this;
			self.timer = setInterval(function() {
				if(target.style.filter) {
					var opacity = Number(target.filters['alpha'].opacity);
					if (opacity == 100) clearInterval(self.timer);
					var alpha = opacity + 5;
					target.style.filter = "alpha(opacity="+ alpha +")";
				} else {
					var opacity = Number(target.style.opacity);
					if (opacity == 1) clearInterval(self.timer);
					target.style.opacity = opacity + 0.05;
				}
			}, 30);
		}
		return false;
	}
	
	key.news = new KEY.Module.NewsFeed();
	
})();
