Event.observe(window, 'load', function(){
	var start = new TabMaker('tab');
	start.create();
});

var TabMaker = Class.create()
TabMaker.prototype = {
	initialize: function(tab) {
		this.tabLnegth = gcn(tab).length;
		this.tabName = tab;
	},
	create: function() {
		var menu = new TabIndex(this.tabName);
		for (var i = 0; i < this.tabLnegth; i++) {
			menu.appendTab(new Tab('Tab' + i, (i==0)));
		}
		menu.setTab();
	}
}

var Tab = Class.create();
Tab.prototype = {
	initialize: function(name, open) {
		this.name = name;
		this.page = name + 'Box';
		this.open = open;
	},
	styleTab: function() {
	if (location.search && (location.search.substr(6,1)-1)>0) {
		if (this.name == 'Tab'+ (location.search.substr(6,1)-1)) {
			this.setStyle('visible', '', 'open');
		} else {
			this.setStyle('hidden', 'absolute', 'close');
		}
	} else {
		if (this.open) {
			this.setStyle('visible', '', 'open');
		} else {
			this.setStyle('hidden', 'absolute', 'close');
		}
	}
		this.open = false;
	},
	setStyle: function(visibility, position, className){
		var page = $(this.page).style;
		var name = $(this.name);
		page.visibility = visibility;
		page.position = position;
		name.className = className;
	}
}

var TabIndex = Class.create();
TabIndex.prototype = {
	initialize : function(tab) {
		this.last = 0;
		this.tabs = new Array();
		this.tabName = tab;
	},
	getTabAt : function(index) {
		return this.tabs[index];
	},
	appendTab : function(tab) {
		this.tabs[this.last] = tab;
		gcn(this.tabName)[this.last].id = tab.name;
		gcn(this.tabName+'Box')[this.last].id = tab.page;
		this.last++;
		var link = document.createElement('a');
		link.innerHTML = $(tab.name).innerHTML;
		if (location.search) {
			link.href = '?kind=' + this.last + '&' + 
					location.search.substr(8);
		} else {
			link.href = '?kind=' + this.last;
		}
		var listTitle = new Array(4);
		listTitle[1] = '新築一戸建';
		listTitle[2] = '中古一戸建';
		listTitle[3] = '中古マンション';
		listTitle[4] = '土地';
		$(tab.name).innerHTML = '';
		$(tab.name).appendChild(link);

		var kind = 1;
		if (location.search && (location.search.substr(6,1)-1)>0) {
			kind = location.search.substr(6,1);
		}
		document.getElementById('listTitle').innerHTML = listTitle[kind];
		document.getElementById('kindHidden').value = kind;
		var elm = new Array(10);
		elm[0] = document.getElementById('houseform');
		elm[1] = document.getElementById('yearsform');
		elm[2] = document.getElementById('buildingform');
		elm[3] = document.getElementById('footprintform');
		elm[4] = document.getElementById('landform');
		elm[5] = document.getElementById('conditionform1');
		elm[6] = document.getElementById('conditionform2');
		elm[7] = document.getElementById('conditionform3');
		elm[8] = document.getElementById('conditionform4');
		elm[9] = document.getElementsByName('condition[]');
		if (kind == 1) {
			elm[0].style.display = 'block';
			elm[1].style.display = 'none';
			elm[2].style.display = 'block';
			elm[3].style.display = 'none';
			elm[4].style.display = 'block';
			elm[5].style.display = 'block';
			elm[6].style.display = 'none';
			elm[7].style.display = 'none';
			elm[8].style.display = 'none';
			for (ci=0; ci<elm[9].length; ci++) {
				if (ci>=32 && ci<=38) {
				if (elm[9][ci].checked == true) {
					elm[9][ci].checked == false;
				}
				}
			}
		} else if (kind == 2) {
			elm[0].style.display = 'block';
			elm[1].style.display = 'block';
			elm[2].style.display = 'block';
			elm[3].style.display = 'none';
			elm[4].style.display = 'block';
			elm[5].style.display = 'none';
			elm[6].style.display = 'block';
			elm[7].style.display = 'none';
			elm[8].style.display = 'none';
		} else if (kind == 3) {
			elm[0].style.display = 'block';
			elm[1].style.display = 'block';
			elm[2].style.display = 'none';
			elm[3].style.display = 'block';
			elm[4].style.display = 'none';
			elm[5].style.display = 'none';
			elm[6].style.display = 'none';
			elm[7].style.display = 'block';
			elm[8].style.display = 'none';
		} else if (kind == 4) {
			elm[0].style.display = 'none';
			elm[1].style.display = 'none';
			elm[2].style.display = 'none';
			elm[3].style.display = 'none';
			elm[4].style.display = 'block';
			elm[5].style.display = 'none';
			elm[6].style.display = 'none';
			elm[7].style.display = 'none';
			elm[8].style.display = 'block';
		}
		$(tab.name).onclick = function(){
			var kind = eval($(tab.name).id.substr(3, 1)) + 1;
			var url = './index.php?kind=' + kind;
        		if (location.search) {
                		str = location.search;
				if (str.slice(7).indexOf('&') == 0) {
					url = url + str.slice(7);
				} else {
					url = url + '&' + str.slice(7);
        			}
			}
			tab.open = true;
			this.setTab();
			location.href = url; 
		}.bind(this);
	},
	getLength : function() {
		return this.last;
	},
	each : function(func) {
		for (var i = 0; i < this.getLength(); i++) {
			func(this.getTabAt(i));
		}
	},
	setTab: function() {
		this.each(function(tab) {
				tab.styleTab();
		});
	}
};
function gcn(id){
	return document.getElementsByClassName(id);
}


