// JavaScript Document

var blogController = {
	init:function(_path){
		path = _path;
		
		if (this.inited){
			return;
		}
		
		this.inited = true;
		
		var me = this;
		this.element = $("controller");
		
		//コントローラ自体を消しておく
		this.element.hide();
		this.isShow = false;
		
		//innerを配置
		var inner = document.createElement("div");
		inner.id = "controller-inner";
		this.element.appendChild(inner);
		this.innerElement = $("controller-inner");
		
		//データロード開始
		this.load(function(){
			//内部の生成
			me.setup();
		})
	},

	load:function(callback){
        var me = this;
        var feedURL = path+"archive.xml";
        this.wait(true);
        
        var onloadHandler = function(e) {
			
        	//xmlをパース、_rowDataを設定
            me.parseData(e);
            
			//waitを削除
            me.wait(false);
				
            //
            callback();
        };
		
		new Ajax.Request(feedURL, { method: 'get', onComplete: onloadHandler });
    },
	
	
	
	wait:function(b){
        switch (b){
            case true:
				var path = $("controller-inner");
                this.wait_cursor = new WaitCursor(path,"ac-wait");
            break;
            case false:
                this.wait_cursor.kill();
				delete this.wait_cursor;
            break;
        }
    },

    parseData:function(o){
		var res = o.responseXML;

		var date_data = res.documentElement.getElementsByTagName("date");
		var category_data = res.documentElement.getElementsByTagName("category");
		var tag_data = res.documentElement.getElementsByTagName("tag");
		
		//日付
		this.date_array = new Array();
		for (var i=0;i<date_data.length;i++){
			var y = date_data[i].getAttribute("y");
			var m_obj = {month:date_data[i].getAttribute("m"),url:date_data[i].getAttribute("link")};
			//年にわけて登録する
			var isPush = false;
			for (var j=0;j<this.date_array.length;j++){
				if (y == this.date_array[j].year){
					//同じ年が既に登録されている
					this.date_array[j].month_array.push(m_obj);
					isPush = true;
					break;
				}
			}
			if (!isPush){
				//はじめてでてくる年だ
				this.date_array.push({year:y,month_array:[m_obj]});
			}
		}
		
		//カテゴリ
		this.category_array = new Array();
		for (var i=0;i<category_data.length;i++){
			this.category_array[i] = {
				txt:category_data[i].getAttribute("id"),
				url:category_data[i].getAttribute("link")
			};
		}
		
		//タグ
		this.tag_array = new Array();
		for (var i=0;i<tag_data.length;i++){
			this.tag_array[i] = {
				txt:tag_data[i].getAttribute("id"),
				url:tag_data[i].getAttribute("link")
			};
		}
		
	},
	
	setup:function(){
		//コラム1の大枠
		var div1=document.createElement('div');
		div1.id = 'controller-col1';
		div1.className='controller-col-common';
		this.innerElement.appendChild(div1);
		
		var div1a = document.createElement('div');
		div1a.className="controller-separater";
		this.innerElement.appendChild(div1a);
		
		//コラム2の大枠
		var div2=document.createElement('div');
		div2.id = 'controller-col2';
		div2.className='controller-col-common';
		this.innerElement.appendChild(div2);
		
		var div2a = document.createElement('div');
		div2a.className="controller-separater";
		this.innerElement.appendChild(div2a);
		
		//コラム3の大枠
		var div3=document.createElement('div');
		div3.id = 'controller-col3';
		div3.className='controller-col-common';
		this.innerElement.appendChild(div3);
		
		var div3a = document.createElement('div');
		div3a.className="controller-separater";
		this.innerElement.appendChild(div3a);
		
		//================
		//サーチをセット
		searchController.init();
		
		//アーカイブをセット
		archiveController.init(this.date_array);
		
		//カテゴリーアーカイブをセット
		categoryController.init(this.category_array);
		
		//タグアーカイブをセット
		tagController.init(this.tag_array);
		//================
	},
	
	show:function(){
		//scriptaculous使用
		this.element.visualEffect("SlideDown",{duration:0.5,transition:Effect.Transitions.easeOutQuart});
	},
	
	hide:function(){
		//scriptaculous使用
		this.element.visualEffect("SlideUp",{duration:0.4,transition:Effect.Transitions.easeOutQuad});
	},
	
	toggle:function(){
		this.isShow = (this.isShow==true) ? false : true;
		switch (this.isShow){
			case true : 
				this.show();
			break;
			case false : 
				this.hide();
			break;
		}
	}
}

var archiveController = {
	init:function(_data){
		this.path = $("controller-col1");
		this.data = _data;
		this.setup();
		
		this.initYear();
		this.initMonth();
		this.changeYear(this.data[0].year);
	},
	
	setup:function(){
		var me = this;
		
		//h1
		var h12=document.createElement('h1');
		h12.setAttribute('id','controller-archive-title');
		h12.className = 'controller-title-common';
		this.path.appendChild(h12);
		var txt8=document.createTextNode('ARCHIVE');
		h12.appendChild(txt8);
		
		//年選択DIV
		var div2=document.createElement('div');
		div2.setAttribute('id','controller-archive-year');
		this.path.appendChild(div2);
		
		//左ボタン
		var a2=document.createElement('a');
		a2.setAttribute('href','javascript:void(0)');
		a2.id = 'controller-archive-year-lbtn';
		a2.className = 'disabled';
		div2.appendChild(a2);
		var txt11=document.createTextNode('次の年');
		a2.appendChild(txt11);
		this.lbtn = $("controller-archive-year-lbtn");
		
		//年フィールド
		var div3=document.createElement('div');
		div3.setAttribute('id','controller-archive-year-field');
		div2.appendChild(div3);
		this.yearfield = $("controller-archive-year-field");
		
		//年テーブル
		var table1=document.createElement('table');
		table1.id = "controller-archive-year-table";
		table1.setAttribute('border','0');
		table1.setAttribute('cellPadding','0');
		table1.setAttribute('cellSpacing','0');
		div3.appendChild(table1);
		var tbody1=document.createElement('tbody');
		table1.appendChild(tbody1);
		var tr1=document.createElement('tr');
		tr1.setAttribute('id','controller-archive-year-table-tr');
		tbody1.appendChild(tr1);
		this.yeartabletr = $("controller-archive-year-table-tr");
		this.yeartable = $("controller-archive-year-table");
		
		//右ボタン
		var a6=document.createElement('a');
		a6.setAttribute('href','javascript:void(0)');
		a6.setAttribute('id','controller-archive-year-rbtn');
		a6.className='disabled';
		div2.appendChild(a6);
		var txt33=document.createTextNode('前の年');
		a6.appendChild(txt33);
		this.rbtn = $("controller-archive-year-rbtn");
		
		//月選択DIV
		var div4=document.createElement('div');
		div4.setAttribute('id','controller-archive-month');
		this.path.appendChild(div4);
		
		//月ボタンテーブル
		var table2=document.createElement('table');
		table2.setAttribute('border','0');
		table2.setAttribute('cellPadding','0');
		table2.setAttribute('cellSpacing','0');
		div4.appendChild(table2);
		var tbody2=document.createElement('tbody');
		table2.appendChild(tbody2);
		var tr2=document.createElement('tr');
		tr2.id = 'controller-archive-month-table-tr';
		tbody2.appendChild(tr2);
		this.monthtabletr = $("controller-archive-month-table-tr");
		
	},
	
	initMonth:function(){

		var me = this;
		this.monthbtn_array = new Array();
		
		//1〜12月のボタンを生成
		for (var i=0;i<12;i++){
			//td
			var td=document.createElement('td');
			this.monthtabletr.appendChild(td);
			//a
			var a=document.createElement('a');
			a.setAttribute('href','javascript:void(0)');
			a.className = 'disabled';
			a.id = 'controller-archive-monthbtn-'+i;
			
			function clickHandler(n){
				return function(){
					me.changeMonth(n);
					return false;
				}
			}
			a.onclick = clickHandler(i);
			td.appendChild(a);
			
			//btnarrayに追加
			this.monthbtn_array[i] = {id:i,element:$("controller-archive-monthbtn-"+i)};
			//txt
			var txt=document.createTextNode(i+1);
			a.appendChild(txt);
		}	
	},
	
	initYear:function(){
		var me = this;
		this.yearbtn_array = new Array();
		
		//年選択ボタン
		for (var i=0;i<this.data.length;i++){
			var temp_year = this.data[i].year;
			
			var td1=document.createElement('td');
			this.yeartabletr.appendChild(td1);
			
			var a1=document.createElement('a');
			a1.setAttribute('href','javascript:void(0)');
			a1.className='normal';
			a1.id = "controller-archive-yearbtn-"+temp_year;
			
			function clickHandler(n){
				return function(){
					me.changeYear(n);
				};
			};
			a1.onclick = clickHandler(temp_year);
			
			td1.appendChild(a1);
			this.yearbtn_array[i] = {id:this.data[i].year,element:$("controller-archive-yearbtn-"+this.data[i].year)};
			
			var txt1=document.createTextNode(temp_year);
			a1.appendChild(txt1);
		}
		//スペーサTDを追加する
		var td2 = document.createElement('td');
		this.yeartabletr.appendChild(td2);
		td2.style.width = "190px";
		//TD内に空DIVをいれて実体化する
		var div1 = document.createElement("div");
		div1.style.width = "190px";
		td2.appendChild(div1);
		
		//アクティベート
		this.headyear_id = 0;
		this.scrollYear(0);
	},
	
	scrollYear:function(_headyear_id,callback){
		//クリア
		this.clearScrollYear();
		
		//バリデート
		_headyear_id = (_headyear_id<=0) ? 0 : _headyear_id;
		_headyear_id = (_headyear_id>=this.data.length-1-4) ? this.data.length-1-4 : _headyear_id;
		
		//矢印ボタンを更新
		this.updateYearArrow(_headyear_id);
		
		//idの更新
		this.headyear_id = _headyear_id;
		
		//移動
		var dist_y = _headyear_id*38;
		
		//スクロール設定＆開始
		this.tw_scroll = new Tween(this.yearfield,"scrollLeft",Tween.strongEaseOut,this.yearfield.scrollLeft,dist_y,0.5);
		this.tw_scroll.onMotionFinished = function(){
			if (callback){
				callback();
			}
		}
		this.tw_scroll.start();
	},
	
	clearScrollYear:function(){
		if (this.tw_scroll) {
			this.tw_scroll.stop();
			delete this.tw_scroll;
		}
	},
	
	updateYearArrow:function(_headyear_id){
		var me = this;
		
		//左ボタン
		if (_headyear_id <=0){
			//OFF
			this.lbtn.className = 'disabled';
			this.lbtn.onclick = function(){};
		} else {
			//ON
			this.lbtn.className = "";
			this.lbtn.onclick = function(){
				me.scrollYear(me.headyear_id-1);
			}
		}
		//右ボタン
		if (_headyear_id >= this.data.length-1-4){
			//OFF
			this.rbtn.className = 'disabled';
			this.rbtn.onclick = function(){};
		} else {
			//ON
			this.rbtn.className = "";
			this.rbtn.onclick = function(){
				me.scrollYear(me.headyear_id+1);
			}
		}
	},
	
	changeYear:function(_year){
		//yearボタンの再描画
		for (var i=0;i<this.data.length;i++){
			if (this.yearbtn_array[i].id == _year){
				var temp_monthdata = this.data[i].month_array;
				this.yearbtn_array[i].element.className = "selected";
			} else {
				this.yearbtn_array[i].element.className = "normal";
			}
		}
		//monthテーブルのupdate
		this.updateMonthTable(temp_monthdata);
		//
		this.current_year = _year;
	},
	
	updateMonthTable:function(_monthdata){
		for (var i=0;i<12;i++){
			for (var j=0;j<_monthdata.length;j++){
				if ((i+1)==_monthdata[j].month){
					this.monthbtn_array[i].element.className = "m";
					break;
				} else {
					this.monthbtn_array[i].element.className = "disabled";
				}
			}
		}
	},
	
	changeMonth:function(_month_id){
		
		var yeardata;
		var lnk;
		
		for (var i=0;i<this.data.length;i++){
			if (this.current_year == this.data[i].year){
				yeardata = this.data[i].month_array;
				break;
			}
		}
		
		for (var i=0;i<yeardata.length;i++){
			if ((_month_id+1) == yeardata[i].month){
				lnk = yeardata[i].url;
				break;
			}
		}
		
		if (lnk==undefined){
			return;
		}
		//ジャンプ
		window.location.href = lnk;
	}

}

var searchController = {
	init:function(){
		this.path = $("controller-col1");
		this.setup();
	},
	
	exec:function(){
		//サーチ実行
		this.form.submit();
	},
	
	setup:function(){
		var me = this;
		
		//サーチh1
		var h11=document.createElement('h1');
		h11.setAttribute('id','controller-search-title');
		h11.className='controller-title-common';
		this.path.appendChild(h11);
		var txt2=document.createTextNode('SEARCH');
		h11.appendChild(txt2);
		
		//form
		var form1=document.createElement('form');
		form1.setAttribute('id','controller-search-form');
		form1.setAttribute('method','get');
		form1.setAttribute('action','http://localhost:8002/blog2009/mt/mt-search.cgi');
		this.path.appendChild(form1);
		this.form = $('controller-search-form');
		
		//form - textfield
		var input1=document.createElement('input');
		input1.setAttribute('type','text');
		input1.setAttribute('id','controller-search-field');
		input1.setAttribute('name','search');
		form1.appendChild(input1);
		
		//form - hidden(1)
		var input2=document.createElement('input');
		input2.setAttribute('type','hidden');
		input2.setAttribute('name','IncludeBlogs');
		input2.setAttribute('value','1');
		form1.appendChild(input2);
		
		//form - hidden(2)
		var input3=document.createElement('input');
		input3.setAttribute('type','hidden');
		input3.setAttribute('name','limit');
		input3.setAttribute('value','20');
		form1.appendChild(input3);

		//form送信ボタン
		var a1=document.createElement('a');
		a1.setAttribute('href','javascript:void(0)');
		a1.setAttribute('id','controller-search-execbtn');
		a1.onclick = function(){
			me.exec();
		}
		this.path.appendChild(a1);
		var txt6=document.createTextNode('検索');
		a1.appendChild(txt6);
	}
}

var categoryController = {
	init:function(_data){
		this.path = $("controller-col2");
		this.data = _data;
		this.LISTPERPAGE = 6;
		this.FIELD_HEIGHT = 120;
		
		this.setup();
		this.initList();
		this.initPager();
		
		this.changePage(0);
		
	},
	
	setup:function(){
		//h1
		var h11=document.createElement('h1');
		h11.setAttribute('id','controller-category-title');
		h11.className='controller-title-common';
		this.path.appendChild(h11);
		var txt1=document.createTextNode('CATEGORY');
		h11.appendChild(txt1);
		
		//div
		var div1=document.createElement('div');
		div1.setAttribute('id','controller-category-field');
		this.path.appendChild(div1);
		this.listfield = $('controller-category-field');
		
		//ul
		var ul1=document.createElement('ul');
		ul1.setAttribute('id','controller-category-list');
		div1.appendChild(ul1);
		this.list = $('controller-category-list');
				
		//pager
		var div3=document.createElement('div');
		div3.setAttribute('id','controller-category-pager');
		this.path.appendChild(div3);
		
		//ul
		var ul2=document.createElement('ul');
		ul2.id = "controller-category-pager-ul";
		div3.appendChild(ul2);
		this.pagerul = $("controller-category-pager-ul");
	},
	
	initList:function(){
		for (var i=0;i<this.data.length;i++){	
			//li(s)
			var li1=document.createElement('li');
			this.list.appendChild(li1);
			//a
			var a1=document.createElement('a');
			a1.setAttribute('href',this.data[i].url);
			a1.className='normal';
			li1.appendChild(a1);
			var txt8=document.createTextNode("+ "+this.data[i].txt);
			a1.appendChild(txt8);
		}
					
		//spacer
		var div2=document.createElement('div');
		div2.className='controller-list-spacer';
		this.listfield.appendChild(div2);
	},
	
	initPager:function(){
		//ページ数を計算
		this.page_num = Math.ceil(this.data.length/this.LISTPERPAGE);
		
		this.pagebtn_array = new Array();
		//ボタンを配置
		for (var i=0;i<this.page_num;i++){
			//li
			var li20=document.createElement('li');
			this.pagerul.appendChild(li20);
			
			//a
			var a20=document.createElement('a');
			a20.className='normal';
			a20.id = "controller-category-pagebtn" + i;
			li20.appendChild(a20);
			this.pagebtn_array[i] = $("controller-category-pagebtn" + i);
			
			//txt
			var txt92=document.createTextNode(i+1);
			a20.appendChild(txt92);
		}
	},
	
	changePage:function(_page_id){
		this.scrollList(_page_id);
	},
	
	scrollList:function(_headpage_id,callback){
		
		//クリア
		this.clearScrollList();
		
		//バリデート
		_headpage_id = (_headpage_id<=0) ? 0 : _headpage_id;
		_headpage_id = (_headpage_id>=this.page_num) ? this.page_num : _headpage_id;
		
		//ページボタンを更新
		this.updatePager(_headpage_id);
		
		//idの更新
		this.headpage_id = _headpage_id;
		
		//移動
		var dist_y = _headpage_id*this.FIELD_HEIGHT;
		
		//スクロール設定＆開始
		this.tw_scroll = new Tween(this.listfield,"scrollTop",Tween.strongEaseOut,this.listfield.scrollTop,dist_y,0.5);
		this.tw_scroll.onMotionFinished = function(){
			if(callback){
				callback();
			}
		}
		this.tw_scroll.start();
	},
	
	clearScrollList:function(){
		if (this.tw_scroll) {
			this.tw_scroll.stop();
			delete this.tw_scroll;
		}
	},
	
	updatePager:function(_headpage_id){
		var me = this;
		
		for (var i=0;i<this.pagebtn_array.length;i++){
			if (i==_headpage_id){
				this.pagebtn_array[i].className = "selected";
				this.pagebtn_array[i].onclick = null;
			} else {
				this.pagebtn_array[i].className = "normal";
				
				function clickHandler(n){
					return function(){
						me.changePage(n);
					};
				};
				
				this.pagebtn_array[i].onclick = clickHandler(i);
			}
		}
	}
}

var tagController = {
	init:function(_data){
		this.path = $("controller-col3");
		this.data = _data;
		this.LISTPERPAGE = 6;
		this.FIELD_HEIGHT = 120;
		
		this.setup();
		this.initList();
		this.initPager();
		
		this.changePage(0);
		
	},
	
	setup:function(){
		//h1
		var h11=document.createElement('h1');
		h11.setAttribute('id','controller-tag-title');
		h11.className='controller-title-common';
		this.path.appendChild(h11);
		var txt1=document.createTextNode('TAG');
		h11.appendChild(txt1);
		
		//div
		var div1=document.createElement('div');
		div1.setAttribute('id','controller-tag-field');
		this.path.appendChild(div1);
		this.listfield = $('controller-tag-field');
		
		//ul
		var ul1=document.createElement('ul');
		ul1.setAttribute('id','controller-tag-list');
		div1.appendChild(ul1);
		this.list = $('controller-tag-list');
				
		//pager
		var div3=document.createElement('div');
		div3.setAttribute('id','controller-tag-pager');
		this.path.appendChild(div3);
		
		//ul
		var ul2=document.createElement('ul');
		ul2.id = "controller-tag-pager-ul";
		div3.appendChild(ul2);
		this.pagerul = $("controller-tag-pager-ul");
	},
	
	initList:function(){
		for (var i=0;i<this.data.length;i++){	
			//li(s)
			var li1=document.createElement('li');
			this.list.appendChild(li1);
			//a
			var a1=document.createElement('a');
			a1.setAttribute('href',this.data[i].url);
			a1.className='normal';
			li1.appendChild(a1);
			var txt8=document.createTextNode("+ "+this.data[i].txt);
			a1.appendChild(txt8);
		}
					
		//spacer
		var div2=document.createElement('div');
		div2.className='controller-list-spacer';
		this.listfield.appendChild(div2);
	},
	
	initPager:function(){
		//ページ数を計算
		this.page_num = Math.ceil(this.data.length/this.LISTPERPAGE);
		
		this.pagebtn_array = new Array();
		//ボタンを配置
		for (var i=0;i<this.page_num;i++){
			//li
			var li20=document.createElement('li');
			this.pagerul.appendChild(li20);
			
			//a
			var a20=document.createElement('a');
			a20.className='normal';
			a20.id = "controller-tag-pagebtn" + i;
			li20.appendChild(a20);
			this.pagebtn_array[i] = $("controller-tag-pagebtn" + i);
			
			//txt
			var txt92=document.createTextNode(i+1);
			a20.appendChild(txt92);
		}
	},
	
	changePage:function(_page_id){
		this.scrollList(_page_id);
	},
	
	scrollList:function(_headpage_id,callback){
		
		//クリア
		this.clearScrollList();
		
		//バリデート
		_headpage_id = (_headpage_id<=0) ? 0 : _headpage_id;
		_headpage_id = (_headpage_id>=this.page_num) ? this.page_num : _headpage_id;
		
		//ページボタンを更新
		this.updatePager(_headpage_id);
		
		//idの更新
		this.headpage_id = _headpage_id;
		
		//移動
		var dist_y = _headpage_id*this.FIELD_HEIGHT;
		
		//スクロール設定＆開始
		this.tw_scroll = new Tween(this.listfield,"scrollTop",Tween.strongEaseOut,this.listfield.scrollTop,dist_y,0.5);
		this.tw_scroll.onMotionFinished = function(){
			if (callback){
				callback();
			}
		}
		this.tw_scroll.start();
	},
	
	clearScrollList:function(){
		if (this.tw_scroll) {
			this.tw_scroll.stop();
			delete this.tw_scroll;
		}
	},
	
	updatePager:function(_headpage_id){
		var me = this;
		
		for (var i=0;i<this.pagebtn_array.length;i++){
			if (i==_headpage_id){
				this.pagebtn_array[i].className = "selected";
				this.pagebtn_array[i].onclick = null;
			} else {
				this.pagebtn_array[i].className = "normal";
				
				function clickHandler(n){
					return function(){
						me.changePage(n);
					};
				};
				
				this.pagebtn_array[i].onclick = clickHandler(i);
			}
		}
	}
}




/*waitcursorクラス*/
var WaitCursor = function(_path,_name){
	this.waitpath = _path;
	this.name = _name;
	this.init();
}

WaitCursor.prototype = {
	init:function(){
		
		var img = new Image();
		
		img.src= path + "common/img/waitcursor.gif";
		img.id = this.name;
		
		//描画
		this.waitpath.appendChild(img);
		this.element = $(this.name);
	},
	
	kill:function(){
		this.waitpath.removeChild(this.element);
	}
}
/*
window.onload = function(){
	blogController.init();
}*/
