
var newsLoader = Class.create();
newsLoader.prototype = {
	
	req:null,
	data:null,
	
	config:{
		url:"./data/newsdata.html",
		handler:null
	},
	initialize:function(config){
		if(config) Object.extend(this.config,config);
		this.load();
	},
	load:function(){
		new Ajax.Request(this.config.url,{
			method:"get",
			onComplete:this.onloaded.bind(this)
		});
	},
	onloaded:function(req){
		var html = req.responseText.replace(/\n|\t|\r/gi,"").replace(/^.+?<body>/gi,"").replace(/<\/body.+?$/gi,"");
		var result = this.validateText(html);
		
		
		if(this.config.handler) this.config.handler(result);
	},
	validateText:function(text){
		if(Prototype.Browser.WebKit){
			var esc = escape(text);
			if(esc.indexOf("%u")<0 && esc.indexOf("%")>-1){
				text = decodeURIComponent(esc);
			}
			
		}
		return text;
	}
}

var newsViewer = Class.create();
newsViewer.prototype = {
	
	loader:null,
	data:null,
	
	config:{
		dataContainer:"newsData",
		newsContainer:"newsContent",
		
		articleContainer:".Article",
		articleTitle:".ArticleTitle",
		articleContent:".ArticleContent"
	},
	
	initialize:function(){
		// setup container
		this.nc = $(this.config.newsContainer);
		this.nc.display = "none";
		
		// selector
		this.sel_con = "#" + this.config.newsContainer;
		this.sel_article = this.sel_con + " " + this.config.acticleContainer;
		this.sel_title = this.sel_con + " " + this.config.articleTitle;
		this.sel_content = this.sel_con + " " + this.config.articleContent;
		
		this.loader = new newsLoader({
			handler:this.onloaded.bind(this)
		});
	},
	onloaded:function(html){
		this.data = html;
		this.nc.innerHTML = this.data;
		this.setupView();
	},
	setupView:function(){
		//handlers
		this.handler_titleOnClick = this.titleOnClick.bindAsEventListener(this);
		
		//setup
		var objs = $$(this.sel_title);
		var i;
		var _this = this;
		
		for(i=0;i<objs.length;i++){
			objs[i].style.cursor = "pointer";
			objs[i].id = "id_"+i;
			Event.observe(objs[i],"click",this.titleOnClick.bindAsEventListener(this));
			Event.observe(objs[i],"mouseover",function(e){
					Element.addClassName(e.target, "Hover");
			});
			Event.observe(objs[i],"mouseout",function(e){
					Element.removeClassName(e.target, "Hover");
			});
		}
		
		//load query
		var id = location.search.parseQuery()["id"] || 0;
		this.toggle(id);
	},
	titleOnClick:function(e){
		var idNum = e.target.id.replace(/id_/,"");
		this.toggle(idNum);
	},
	toggle:function(idNum){
		var activeTitle = $("id_"+idNum);
		var activeObj = activeTitle.parentNode;
		var obj;
		
		var titles = $$(this.sel_title);
		for(i=0;i<titles.length;i++){
			obj = titles[i];
			if(obj==activeTitle){
				this.activateTitle(obj);
			}else{
				this.deactivateTitle(obj);
			}
		}
		
		var contents = $$(this.sel_content);
		for(i=0;i<contents.length;i++){
			obj = contents[i];
			if(obj.parentNode == activeObj){
				obj.style.display = "block";
			}else{
				obj.style.display = "none";
			}
		}
	},
	activateTitle:function(ele){
		Element.addClassName(ele,"Active");
	},
	deactivateTitle:function(ele){
		Element.removeClassName(ele,"Active");
	}
}

var newsPicker = Class.create();
newsPicker.prototype = {
	
	loader:null,
	hiddenContainer:null,
	newsPickerContainer:null,
	
	hiddenContainerID:"hiddenContainer",
	articleTitleClass:"ArticleTitle",
	newsPickerContainerID:"newsPicker",
	
	newsPageURL:"news.html",
	
	
	initialize:function(){
		
		this.hiddenContainer = document.createElement("div");
		this.hiddenContainer.id = this.hiddenContainerID;
		this.hiddenContainer.style.display = "none";
		
		$("newsPicker").parentNode.appendChild(this.hiddenContainer);
		
		this.loader = new newsLoader({
				handler:this.onloaded.bind(this)
		});
	},
	onloaded:function(html){
		this.hiddenContainer.innerHTML = html;
		this.display();
	},
	display:function(){
		var titles = $$("#" + this.hiddenContainerID + " ." + this.articleTitleClass);
		var li;
		var a;
		for(i=0;i<titles.length;i++){
			li = document.createElement("li");
			a = document.createElement("a");
			a.href = this.newsPageURL + "?id=" + i;
			a.innerHTML = titles[i].innerHTML;
			li.appendChild(a);
			$("newsPicker").appendChild(li);
		}
	}
}



Event.observe(window,"load",function(){
		if($$(".Navi-News").length){
			new newsViewer();
		}else if($$(".Index").length){
			new newsPicker();
		}
});