/**
 * Steuert Karteireiterhandling
 * 
 * @version 1.6
 * @since 	20060808
 */


// Kontroller
tabController = {
	addEvent: function(obj, type, fn, tmp) {
		tmp || (tmp = true);
		if( obj.attachEvent ) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function(){obj["e"+type+fn]( window.event );};
			obj.attachEvent( "on"+type, obj[type+fn] );
		} else {
			obj.addEventListener( type, fn, true );
		};
	},
	
	init: function() {

		tab = new tab();
		tab.find();

		tab.flipToHash();
	},

	change: function() {

		tab.flipToHash();
	}
}

// Eigentliches Karteireiterobjekt
function tab (){

	this.containerName = "karteireiter";
	this.cardName = "karte";
	
	this.flags = new Array();
	this.cards = new Array();
	this.find = function(){
 
		var navUL = document.getElementById(this.containerName);
		if (navUL) {
			var navLIs = navUL.childNodes;
			for (var i = 0; i < navLIs.length; i++) {
				if (navLIs[i].nodeType == 1 && navLIs[i].tagName == "LI") {
					this.flags.push(navLIs[i].id);
					var reiterindex = this.flags.length;
					this.cards[navLIs[i].id] = this.cardName + reiterindex;
				}
			}
		}
	}

	this.flip = function(id){
		var aktuellerReiter = document.getElementById(id);
		if (!aktuellerReiter) {
			// the tab with the given id is not available (or there are no tabs)
			return;
		}
		for (var i = 0; i < this.flags.length; i++) {
			document.getElementById(this.flags[i]).className = "";
			document.getElementById(this.cards[this.flags[i]]).style.display = "none";
		}
		document.getElementById(id).className = "aktiv";
		document.getElementById(this.cards[id]).style.display = "block";
        window.location.hash = "tab_" + id;
	}

	/**
	 * the method evaluates the url-hash and switches to the appropriate tab
	 * format of hash-param: "tab_karte<index>" (e.g. tab_karte1)
	 */
	this.flipToHash = function() {

		if (window.location.hash.length > 0 && window.location.hash.substr(1, 4) == "tab_") {
			var hashId = window.location.hash.substr(5);
			if (hashId.length > 0) {
				this.flip(hashId);
				return;
			}
		}
		// default, if no hash was given
		this.flip('reiter1');
	}
}

// start tab handler
tabController.addEvent(window, 'load', tabController.init);

// change to previous tab when user clicked "back"
tabController.addEvent(window, 'hashchange', tabController.change);
