


var PATH_news_xml   = "/_xml/news.xml";
var INT_repeat = 15; // minute



function fade(obj){
	
	
	function Fade(obj){ // class
		
		this.obj = obj;
		this.style = obj.currentStyle || document.defaultView.getComputedStyle(obj,'');
		this.alpha = 100;
		this.timerID = null;
		
		this.getAlpha();
	}
	Fade.prototype = {
		
		setAlpha : function(value){
			
			if(browser.nav == "MSIE"){
				this.obj.style.filter = "alpha(opacity=" + value + ")";
			}
			else{
				this.obj.style.opacity = value / 100;
			}
			this.alpha = value;
		},
		
		getAlpha : function(){
			
			if(browser.nav == "MSIE"){
				var tmp = this.style.filter.match(/alpha\(opacity=([0-9]+)\)/);
				this.alpha = (tmp) ? tmp[1] : 100;
			}
			else{
				this.alpha = this.style.opacity * 100;
			}
			
		},
		
		In : function(){
			
			var a = (arguments.length > 0) ? arguments[0] : 5 ;
			var callback = (arguments.length > 1) ? arguments[1] : false ;
			this.To(100,a,callback);
			
		},
		
		Out : function(){
			
			var a = (arguments.length > 0) ? arguments[0] : 5 ;
			var callback = (arguments.length > 1) ? arguments[1] : false ;
			this.To(0,a,callback);
			
		},
		
		To : function(value){
			
			var F = this;
			
			var a = (arguments.length > 1) ? arguments[1] : 5 ;
			var callback = (arguments.length > 2 && typeof arguments[2] == "function") ? arguments[2] : false ;
			
			if(a > 1) a = 1 / a;
			clearInterval(this.timerID);
			this.p = this.alpha;
			var initial = this.alpha;
			this.timerID = setInterval(function(){
				
				F.p += (value - F.p) * a;
				F.setAlpha(F.p);
				
				if(
					(initial >= value && F.p <= value+1) || 
					(initial <  value && F.p >= value-1)
				){
					clearInterval(F.timerID);
					F.setAlpha(value);
					
					if(callback != false) callback();
				}
				
			},0.01 * 1000);
			
		}
	}
	
	return new Fade(obj);
}



function ie_getOffsetLeft(obj){
	/*
	this returns offsetLeft of obj on MSIE
	*/
	var return_value = 0;
	(function tmp(obj){
		return_value += obj.offsetLeft;
		if(obj.offsetParent && obj.offsetParent.nodeName != "BODY" && obj.offsetParent.nodeName != "HTML"){
			tmp(obj.offsetParent);
		}
	})(obj);
	return return_value;
}




function ie_getOffsetTop(obj){
	/*
	this returns offsetTop of obj on MSIE
	*/
	var return_value = 0;
	(function tmp(obj){
		return_value += obj.offsetTop;
		if(obj.offsetParent && obj.offsetParent.nodeName != "BODY" && obj.offsetParent.nodeName != "HTML"){
			tmp(obj.offsetParent);
		}
	})(obj);
	return return_value;
}





function newsLoader(){
	
	if(!document.getElementById("news")) return;
	
	var STR_page_domain = window.location.href.split("/")[2];
	
	function couple(date,flag,comment,link){
		
		this.date = date;
		this.flag = flag;
		this.comment = comment;
		this.link = link;
		
		this.dt = document.createElement("dt");
		this.dd = document.createElement("dd");
		
		if(this.flag == "0") this.dt.className = "new" ;
		
		if(this.comment){
			this.dt.appendChild(document.createTextNode(this.date));
			if(this.link != false){
				this.a  = document.createElement("a");
				this.a.href = this.link;
				if(this.link.indexOf("http") != -1 && this.link.indexOf(STR_page_domain) == -1){
					this.a.target = "_blank";
				}
				this.a.appendChild(document.createTextNode(this.comment));
				this.dd.appendChild(this.a);
			}
			else{
				this.dd.appendChild(document.createTextNode(this.comment));
			}
		}
	}
	
	
	function Loading(main){
		
		var L = this;
		
		this.main = main;
		this.lod = document.createElement("img");
		this.lod.src = "_img/ind_bod_lod_img.gif";
		this.but = document.createElement("img");
		this.but.src = "_img/ind_bod_lod_but.gif";
		
		this.obj = document.createElement("img");
		this.obj.src = this.lod.src;
		this.obj.className = "load-image";
		this.obj.style.position = "absolute";
		this.Init();
		this.visibility = false;
		this.fade = null;
		
		onresize_items.push(function(){
			if(L.visibility === false) return;
			L.Init();
		});
	}
	Loading.prototype = {
		
		Init : function(){
			
			if(browser.nav == "MSIE"){
				this.obj.style.posLeft = ie_getOffsetLeft(this.main.obj.parentNode) + (this.main.obj.parentNode.offsetWidth  - 45) / 2;
				this.obj.style.posTop  = ie_getOffsetTop(this.main.obj.parentNode)  + (this.main.obj.parentNode.offsetHeight - 45) / 2;
			}
			else{
				this.obj.style.left = this.main.obj.parentNode.offsetLeft + (this.main.obj.parentNode.offsetWidth  - 45) / 2 + "px";
				this.obj.style.top  = this.main.obj.parentNode.offsetTop  + (this.main.obj.parentNode.offsetHeight - 45) / 2 + "px";
			}
			
		},
		
		Show : function(){
			if(this.visibility === true) return;
			
			this.main.obj.parentNode.insertBefore(this.obj,this.main.obj);
			this.fade = new fade(this.obj);
			this.fade.setAlpha(100);
			this.visibility = true;
		},
		
		Hide : function(){
			if(this.visibility === false) return;
			
			var L = this;
			var callback = (arguments.length > 0 && typeof arguments[0] == "function") ? arguments[0] : false ;
			
			this.fade.Out(20,function(){
				L.obj.parentNode.removeChild(L.obj);
				L.visibility = false;
				L.fade = null;
				if(callback) callback();
			});
			
		},
		
		ButtonOn : function(){
			
			var L = this;
			this.obj.src = this.but.src;
			this.obj.style.cursor = "pointer";
			this.obj.onclick = function(){
				L.main.update();
			}
			
		},
		
		ButtonOff : function(){
			this.obj.src = this.lod.src;
			this.obj.style.cursor = "normal";
			this.obj.onclick = null;
		}
		
	}
	
	
	function main(){
		
		var M = this;
		
		this.data = [];
		this.couple = [];
		this.url = PATH_news_xml;
		this.XHR = createXMLHttpRequest();
		this.obj = document.getElementById("news").getElementsByTagName("div")[0];
		this.dl = null;
		
		this.span = document.createElement("span");
		this.span.className = "button";
		this.button = document.createElement("img");
		this.button.src = "_img/ind_bod_cap_01_but_01.gif";
		this.span.appendChild(this.button);
		this.h2 = document.getElementById("news").getElementsByTagName("h2")[0];
		this.h2.getElementsByTagName("span")[0].style.width = "434px";
		//this.h2.getElementsByTagName("span")[0].style.width = "392px";
		//this.h2.insertBefore(this.span,this.h2.getElementsByTagName("a")[0]);
		
		this.button.onclick = function(){
			M.update();
		}
		
		this.loading = new Loading(this);
		this.loading.Show();
		
		this.timerID = null;
	}
	main.prototype = {
		init : function(){
			if(this.obj.getElementsByTagName("dl").length > 0){
				this.dl = this.obj.getElementsByTagName("dl")[0];
				clearChildNodes(this.dl);
			}
			else{
				clearChildNodes(this.obj);
				this.dl = document.createElement("dl");
				this.obj.appendChild(this.dl);
				this.br = document.createElement("br");
				this.br.className = "clearance";
				this.obj.appendChild(this.br);
			}
		},
		
		update : function(){
			
			var M = this;
			
			this.loading.ButtonOff();
			this.loading.Show();
			
			this.XHR.open("GET",this.url.untiCache(),true);
			this.XHR.send("null");
			
			this.timerID = setTimeout(function(){
				M.XHR.abort();
				M.loading.ButtonOn();
			},5 * 1000);
			
			this.XHR.onreadystatechange = function(){
				if(M.XHR.readyState == 4 && M.XHR.responseText){
					clearTimeout(M.timerID);
					
					M.data = [];
					var xml = M.XHR.responseXML;
					
					var tmp = xml.getElementsByTagName("list");
					for(var i=0 ; i<tmp.length ; i++){
						M.data[i] = {
							date     : (tmp[i].getElementsByTagName("date").length > 0 && tmp[i].getElementsByTagName("date")[0].childNodes.length > 0) ? tmp[i].getElementsByTagName("date")[0].childNodes[0].nodeValue : "0000.00.00",
							flag     : (tmp[i].getElementsByTagName("flag").length > 0 && tmp[i].getElementsByTagName("flag")[0].childNodes.length > 0 && tmp[i].getElementsByTagName("flag")[0].childNodes[0].nodeValue == "0") ? 0 : 1,
							comment  : (tmp[i].getElementsByTagName("comment").length > 0 && tmp[i].getElementsByTagName("comment")[0].childNodes.length > 0) ? tmp[i].getElementsByTagName("comment")[0].childNodes[0].nodeValue : false,
							link     : (tmp[i].getElementsByTagName("link").length > 0 && tmp[i].getElementsByTagName("link")[0].childNodes.length > 0) ? tmp[i].getElementsByTagName("link")[0].childNodes[0].nodeValue : false
						};
					}
					
					M.init();
					M.loading.Hide(function(){
						M.render();
					});
				}
				else if(M.XHR.readyState == 4 && !M.XHR.responseText){
					clearTimeout(M.timerID);
					
					clearChildNodes(M.obj);
					M.loading.ButtonOn();
					setTimeout(function(){ M.update(); },INT_repeat * 60 * 1000);
					
				}
			}
		},
		
		render : function(){
			if(this.data.length < 1) return;
			
			clearChildNodes(this.obj);
			this.init();
			
			for(var i=0 ; i<this.data.length ; i++){
				
				var n = this.couple.length;
				this.couple[n] = new couple(
					this.data[i].date,
					this.data[i].flag,
					this.data[i].comment,
					this.data[i].link
				);
				
				this.dl.appendChild(this.couple[n].dt);
				this.dl.appendChild(this.couple[n].dd);
				
			}
		}
	}
	
	
	var myMain = new main();
	myMain.update();
	
	
}
startup_items[startup_items.length] = newsLoader;









