if (typeof(GameSalad) == "undefined") GameSalad = new Object();	

GameSalad.Tab = Class.create({
	currentId:'',
	resetContent: function() {
		var _this = this;
		this.tabs.each(function(link) {
			var url = window.location.href.split('#')[0];
			var id = link.href.replace(url,'').replace("#",'')
			var t = $(id)
			if (t) {
				if (t.id == _this.currentId) {
					t.show()
				} else {
					t.hide()
				}
			}
		})
	},
	switchTab: function(link, id, fade, clicked) {
		var _this = this;
		var i = 0;
		this.tabs.each(function(l) {
			if (l.href.endsWith(id)) {
				_this.currentId = id;
				if (_this.transitionTo) {
					_this.transitionTo(l,i,clicked);
				} else {
					$(l.parentNode).addClassName("selected")
				}
			} else {
				if (_this.transitionFrom) {
					_this.transitionFrom(l,i,clicked);
				} else {
					$(l.parentNode).removeClassName("selected")
				}
			}
			i++;
		})
		this.content.each(function(t) {
			var t = $(t)
			if (t) {
				if (t.id == id) {
					if (fade) {
						new Effect.Appear(t,{ queue: { position: 'end', scope: t.id } });
					} else {
						t.show()
					}
				} else {
					if (t.visible()) {
						if (fade) {
							new Effect.Fade(t,{ queue: { position: 'end', scope: t.id } });
						} else {
							t.hide()
						}
					}
				}
			}
		})
		if (this.onSwitchTab) {
			this.onSwitchTab(link, id)
		}
	},
	initialize: function(tab_list_container,p_options){

		var _this = this;
		var firstTab;
		var firstContent;
		var options = (p_options != null) ? p_options : {};
		var url = window.location.href.split('#')[0];
		_this.transitionTo = options.transitionTo;
		_this.transitionFrom = options.transitionFrom;
		_this.onSwitchTab = options.onSwitchTab;
		_this.content = new Array();
		_this.tabs = new Array();
		var currentId = window.location.href.replace(url,'').replace("#",'');
		if ($(currentId) == null) {
			currentId = null;
		} else {
			_this.currentId = currentId;
		}
		$(tab_list_container).select("li a").each(function(link) {
			
			var id = link.href.replace(url,'').replace("#",'')
			if (!link.hasClassName('not-tab')) {
				if (currentId == null) {
					if (!firstTab) firstTab = link;
					if (!firstContent) firstContent = id;
				} else {
					if (currentId == id) {
						firstContent = id;
						firstTab = link;
					}
				}

				link.onclick = function() { return false; }
				Event.observe(link, "click", function() {
					if (options.title) {
						$(options.title).innerHTML = id.substr(0,1).toUpperCase() + id.substr(1,id.length-1);
					}
					_this.switchTab(link, id, options.fade, true);
				});

				_this.tabs.push(link)
				_this.content.push(id)
			}

		})
		if (firstTab && firstContent) {
			_this.switchTab(firstTab,firstContent);
		}
	}	
})
