﻿var TabBox = {
	_tabs: [],
	init: function (target, title) {
		if (this._tabs.length > 0) {
			var items = '';
			for (var i = 0; i < this._tabs.length; i++) {
				$('.' + this._tabs[i]).hide();
				items += '<li class="tab_' + this._tabs[i] + '"><a href="#tab_' + this._tabs[i] + '" onclick="return TabBox.load(\'' + this._tabs[i] + '\', \'' + location.href + '\');">' + $('.' + this._tabs[i] + ' ' + title).html() + '</a></li>';
			}
			$('.' + target).after('<div class=\'tab-box\'><ul class=\'tab-list\'>' + items + '</ul></div>');
			TabBox.open(this._tabs[0]);
		}
	},
	add: function (name) {
		if ($('.' + name).length > 0) {
			this._tabs[this._tabs.length] = name;
		}
	},
	open: function (name) {
		for (var i = 0; i < this._tabs.length; i++) {
			if (this._tabs[i] == name) {
				$('.' + this._tabs[i]).show();
				$('.tab_' + this._tabs[i]).addClass('selected');
			} else {
				$('.' + this._tabs[i]).hide();
				$('.tab_' + this._tabs[i]).removeClass('selected');
			}
		}
		return false;
	},
	load: function (name, url) {
		if ($('.' + name).length > 0) {
			TabBox.open(name);
			return false;
		} else {
			location.href = url + '#' + name;
			return false;
		}
	}
};
