/*@cc_on _d=document;eval('var document=_d')@*/


var SortableTable = new Class ({

	// pass in the table element to get this started
	initialize: function (table, options) {

		this.options = Object.extend(
			{
				ascCl: 'asc',
				descCl: 'desc',
				primaryCl: 'primary',
				secondaryCl: 'secondary',
				altCl: 'alt',
				overCl: 'over',
				dateCl: 'date',
				prefCl: 'pref',
				dummyCl : 'dummy',
				noneCl : 'none'
			} , options || {} );

		this.table = $(table);
		this.thead = this.table.getElement('thead');
		this.tbody = this.table.getElement('tbody');
		this.tfoot = this.table.getElement('tfoot');

		this.headers = this.thead.getElements('th');
		this.rows = this.tbody.getElements('tr');
		this.cells = this.tbody.getElements('td');

		this.row_count = this.rows.length;
		this.col_count = this.headers.length;
		this.cell_count = this.cells.length;

		this.sortasc = true;
		this.sortasc2 = true;
		this.sortby;
		this.sortby2;

		this.order = new Array();
		for (i=this.row_count-1;i>=0;i--) {
			this.order[i] = i;
		}

		for (i=(this.col_count-1);i>=0;i--) {
			if(this.headers[i].hasClass(this.options.noneCl)) continue;
			
			this.headers[i].addEvent('click',function(i){this.sort(i)}.pass(i,this));
			this.headers[i].addEvent('mouseover',function(i){this.headers[i].addClass(this.options.overCl)}.pass(i,this));
			this.headers[i].addEvent('mouseout',function(i){this.headers[i].removeClass(this.options.overCl)}.pass(i,this));
			this.headers[i].setStyle('cursor','pointer');
		}

	this.prefarray = {
					  "北海道":1,
					  "青森":10,
					  "岩手":20,
					  "宮城":30,
					  "秋田":40,
					  "山形":50,
					  "福島":60,
					  "茨城":70,
					  "栃木":80,
					  "群馬":90,
					  "埼玉":100,
					  "千葉":110,
					  "東京":120,
					  "神奈川":130,
					  "新潟":140,
					  "富山":150,
					  "石川":160,
					  "福井":170,
					  "山梨":180,
					  "長野":190,
					  "岐阜":200,
					  "静岡":210,
					  "愛知":220,
					  "三重":230,
					  "滋賀":240,
					  "京都":250,
					  "大阪":260,
					  "兵庫":270,
					  "奈良":280,
					  "和歌山":290,
					  "鳥取":300,
					  "島根":310,
					  "岡山":320,
					  "広島":330,
					  "山口":340,
					  "徳島":350,
					  "香川":360,
					  "愛媛":370,
					  "高知":380,
					  "福岡":390,
					  "佐賀":400,
					  "長崎":410,
					  "熊本":420,
					  "大分":430,
					  "宮崎":440,
					  "鹿児島":450,
					  "沖縄":460,
					  "":470,
					  "":480,
					  "":490
					  };					  


		this.getdata();

		

	},

	// put all the table data in arrays
	// special case for dates
	getdata: function () {

		this.data = new Array();

		for (i=(this.row_count-1);i>=0;i--) {
			this.data[i] = new Array();
			for (j=(this.col_count-1);j>=0;j--) {

				var cell = this.cells[((i*this.col_count)+j)];
				var datum = '';

//				var classes = cell.getProperty('class').split(" ");
//				for (k=classes.length-1;k>=0;k--) {
//					if (classes[k].contains('sortby:')) {
//						datum = classes[k].split(":")[1];
//					}
//				}

				if (this.headers[j].hasClass(this.options.dateCl) && !datum) {
					var text = cell.getText().clean();
					if (text=="－"){
						text = "1900年1月1日";

						}	
					if (text.indexOf("施工中",0)>=0){
						text = "3000年12月31日";

					}	
					if (text.indexOf("月",0)<0){text += "1月";} 
					if (text.indexOf("日",0)<0){text += "1日";} 
					text = text.replace("年","/");
					text = text.replace("月","/");
					text = text.replace("日","");

					datum = new Date(text).getTime();

					datum*=(-1);

				} else if (this.headers[j].hasClass(this.options.prefCl) && !datum) {
					var text = cell.getText().clean();
					//debugprint(text);
					if(text=="－"){
						datum=1001;
					}else{
						if(this.prefarray[text]){
							datum=this.prefarray[text]-0;
						}else{
							datum=1000;	
						}
						/*
						var flg=true;
						for(var i2=0; i2<this.prefs.length; i2++){
							if(	text == this.prefs[i2]){
								datum=i2;
								flg=false;
								break;
							}
						}
						
						if(flg){datum=100;}
						*/
					}
					
				} else if (!datum) {
                    var datum = cell.getText().clean();
	                datum = datum.replace(/,\\-/g, '');
                    //datum = datum.replace(/\\/, '');
                    //datum = datum.replace(/-/, '');
                    if (datum.match(/^\d+$/)) {
                        datum = datum-0; // force to integer type
                    } else {

						//外人用
                        //datum = datum.replace(/^(\d\d)\/(\d\d)\/(\d\d\d\d)/, "$3$1$2");
						

                    }
				}

				this.data[i][j] = datum;
			}
		}


	},

	// called by clicking on a header
	sort: function (col) {

		this.headers.each(function(th){
			th.removeClass(this.options.primaryCl);
			th.removeClass(this.options.secondaryCl);
			th.removeClass(this.options.ascCl);
			th.removeClass(this.options.descCl);
		}.bind(this));
		
		//ダミー用に変換
		if(this.headers[col].hasClass(this.options.dummyCl)){
			col++;
		}

		if (this.sortby == col) {
			this.sortasc = this.sortasc ? false : true;
		} else {
			this.sortasc2 = this.sortasc;
			this.sortasc = true;
			this.sortby2 = this.sortby;
			this.sortby = col;
		}

		this.headers[this.sortby].addClass(this.options.primaryCl);
		if (this.sortasc) {
			this.headers[this.sortby].addClass(this.options.ascCl);
		} else {
			this.headers[this.sortby].addClass(this.options.descCl);
		}
		if (this.sortby2 || this.sortby2 === 0) {
			this.headers[this.sortby2].addClass(this.options.secondaryCl);
			if (this.sortasc2) {
				this.headers[this.sortby2].addClass(this.options.ascCl);
			} else {
				this.headers[this.sortby2].addClass(this.options.descCl);
			}
		}

		this.order.sort(this.compare.bind(this));

		this.build();

	},

	// custom sort function
	compare: function (a, b) {

		if (this.data[a][this.sortby] == this.data[b][this.sortby]) {
			if (this.data[a][this.sortby2] == this.data[b][this.sortby2]) return 0;
			order = (this.data[a][this.sortby2] < this.data[b][this.sortby2]) ? -1 : 1;
			return this.sortasc2 ? order : order*-1;
		} else {
			order = (this.data[a][this.sortby] < this.data[b][this.sortby]) ? -1 : 1;
			return this.sortasc ? order : order*-1;
		}

	},

	// reorganize the table rows based on the new order
	build: function () {

		//this.rows.each(function(tr){tr.removeClass(this.options.altCl)}.bind(this));

		for(i=0;i<this.row_count;i++) {
			this.tbody.adopt(this.rows[this.order[i]]);
			if (i % 2 == 0) this.rows[this.order[i]].addClass(this.options.altCl);
			else this.rows[this.order[i]].removeClass(this.options.altCl);
		}

	}

});


function debugprint(str){
	var dbgscr = document.getElementById("debug");
	dbgscr.innerHTML += str + "<br/>";		
}

window.addEvent('domready',function(){new SortableTable($('demo'))});
