/**
 * mg@powerflasher.de 06.03.2007
 */
function Controller(){
	
	// init Controller (called from HTML)
	this.init = function(){ 
		
		this.table;
		var data;
		var contents;
		
		this.activeNaviId = 0;
		this.idCounter = 0;
		this.activeNaviTopicId = 0;
		
		this.config = new Config();
		var address = this.config.address;
	
		if(address.indexOf(this.config.home) == -1){
			alert("HOME PATH: "+this.config.home+" NOT FOUND IN ADDRESS: "+address+" !!!");
			return;
		}

		this.path = address.split(this.config.home)[1];
		this.locations = this.path.split("/");
		
		this.mainContentLocation = this.locations[0];
		this.contentTopicLocation = this.locations[1];
		this.contentLocation = this.locations[2];
		
		this.storedMainContent = readMainContent();
		//alert("this.storedMainContent: "+this.storedMainContent);
		
		this.isGlobalContent = !arraySearch(this.mainContentLocation,this.config.mainContents);


		// tables
		data = new Data(this);
		
		if(this.isGlobalContent){
			
			 // mainContent by Cookie if content or rather cookie is set within the last 30 minutes!
			if(this.storedMainContent == null){
				//alert("isGlobalContent = true, cookie == null");
				contents = data[this.config.mainContents[0]+"Contents"];
				this.mainContentLocation = this.config.mainContents[0];
			} else {
				//alert("isGlobalContent == true, cookie = "+this.storedMainContent);
				contents = data[this.storedMainContent+"Contents"];
				this.mainContentLocation = this.storedMainContent;
			}
		} else {
			contents = data[this.mainContentLocation+"Contents"];
		}
		

		
		// build list of all contents
		this.contentList = contentListBuilder(contents);
		

		// establish activeNaviTopicId and activeNaviId
		var idKey = this.contentTopicLocation+"/"+this.contentLocation;
		
		for(var f = 0; f < this.contentList.length; f ++) {
			
				if(this.contentTopicLocation == this.contentList[f]){
					this.activeNaviTopicId = f+1;
					//alert("activeNaviId: "+this.activeNaviTopicId+" contentTopicLocation : "+this.contentTopicLocation+" == this.contentList[f] : "+this.contentList[f])
				}
				if(idKey == this.contentList[f]){
					this.activeNaviId = f+1;
					//alert("activeNaviId: "+this.activeNaviId+" idKey : "+idKey+" == this.contentList[f] : "+this.contentList[f])
				}
		}

		// set tables
		if(this.isGlobalContent){
			
			var ct;
			 // mainContent by Cookie if content or rather cookie is set within the last 30 minutes!
			if(this.storedMainContent == null) ct = this.config.mainContents[0];	
			else ct = this.storedMainContent;

		} else {
			ct = this.mainContentLocation;
		}
		
		this.table = eval("data."+ct+"()");
		
		// set cookie
		storeMainContent(this.mainContentLocation);
		
		//alert("ids: "+this.contentList.length+" --> "+this.contentList);

	}

	// mainContentsLinks (called from HTML)
	this.mainContentLinks = function(){
		var linksDIV = document.getElementById("mainContentLinksDIV");
		var pkw = "<a class=\""+this._mainContentLinks("pkw","automobile","automobile-active")+"\" href=\""+this.config.webroot+"pkw\">Automobile</a>";
		var motorrad = "<a class=\""+this._mainContentLinks("motorrad","motorrad","motorrad-active")+"\" href=\""+this.config.webroot+"motorrad\">Motorrad</a>";
		var erlebniswelt = "<a class=\""+this._mainContentLinks("erlebniswelt","erlebniswelt","erlebniswelt-active")+"\" href=\""+this.config.webroot+"erlebniswelt\">Erlebniswelt</a>";
		linksDIV.innerHTML = pkw+"<span style=\"color: #FFFFFF\"> | <\/span>"+motorrad+"<span style=\"color: #FFFFFF\"> | <\/span>"+erlebniswelt;
	}

	// navi (called from HTML)
	this.navi = function(origin){
		
		// build menu
		document.write(this.table);
		
		//alert(this.idCounter);
		this.ddm1 = new DropDownMenu1('naviTable');
		this.ddm1.position.top = -1;
		this.ddm1.init();
	}

	// footer (called from HTML)
	this.footer = function(){
		var foo = document.getElementById("footerPathInfoDIV");
		var home = "<span class=\"footer\">www.kohl.de</span>";
		if(this.isGlobalContent){
			var mainContent = this.locations[0] == undefined || this.locations[0].length < 1 ? "" : "<span class=\"footer\"> | "+this.locations[0]+"</span>";
			foo.innerHTML = "Sie befinden sich hier: "+home+" "+mainContent
		} else {
			var pkw_automobile = this.mainContentLocation == "pkw" ? "automobile" : this.mainContentLocation;
			var mainContent = this.mainContentLocation == undefined || this.mainContentLocation.length < 1 ? "" : " <span class=\"footer\"> | "+pkw_automobile+"</span>";
			var contentTopic = this.contentTopicLocation == undefined || this.contentTopicLocation.length < 1 ? "" : "<span class=\"footer\"> | "+this.contentTopicLocation+"</span>";
			var contentLocationTMP = this.contentLocation == undefined || this.contentLocation.length < 1 ? "" : "<span class=\"footer-selected\"> | "+this.contentLocation+"<\/span>";
			foo.innerHTML = "<span class=\"footer-title\">Sie befinden sich hier:<\/span> "+home+mainContent+" "+contentTopic+" "+contentLocationTMP;
		}
	}

	
	// content topic highlight function (Tables)
	this.__ = function(value){
		this.idCounter++;
		if(this.idCounter == this.activeNaviTopicId){
			//alert("highlight__ activeNaviTopicId: "+this.idCounter+" value: "+value);
			return "<strong>"+value+"</strong>";
		} else if(this.idCounter == this.activeNaviId){
			//alert("highlight__ activeNaviId: "+this.idCounter+" value: "+value);
			return "<strong>"+value+"</strong>";
		} else {
			//alert("no highlight__ : "+this.idCounter+" != "+this.activeNaviId);
			return value;
		}
	}

	// content highlight function (Tables)
	this._ = function(value){
		this.idCounter++;
		if(this.idCounter == this.activeNaviId){
			//alert("highlight_ activeNaviId: "+this.idCounter+" value: "+value);
			return "<strong>"+value+"</strong>";
		} else {
			//alert("no highlight_ : "+this.idCounter+" != "+this.activeNaviId);
			return value;
		}
	}
	
	// mainContentLinks highlight function
	this._mainContentLinks = function(contentId,cssInactive,cssActive){
		if(contentId == this.mainContentLocation){
			//alert("mainContentLink ACTIVE : "+contentId+" value: "+value+" mainContentLocation: "+this.mainContentLocation);
			return cssActive;
		} else {
			//alert("mainContentLink INACTIVE : "+contentId+" value: "+value+" mainContentLocation: "+this.mainContentLocation);
			return cssInactive;
		}
	}

	
	// utils
	
	function getTable(table,mainContent){
		if(mainContent == "motorrad") return table.motorrad();
		else if(mainContent == "erlebniswelt") return table.erlebniswelt();
		else return table.pkw();
	}
	
	function contentListBuilder(contents){

		var contents_tmp;
		var content_tmp;
		
		var contentList = new Array();
		
		for(var cT in contents){
			
			contents_tmp = contents[cT];
			contentList.push(cT);

			for(var c in contents_tmp){
				content_tmp = contents_tmp[c];
				if(typeof content_tmp == "string"){
					contentList.push(cT+"/"+content_tmp);
				}
			}
		}
	
		return contentList;
	}

	function storeMainContent(mainContent) {
		var date = new Date();
		date.setTime(date.getTime()+ (30*60*1000)); //(days*hours*minutes*seconds*milliseconds));
		var expires = "; expires="+date.toGMTString();
		document.cookie = "mainContent="+mainContent+expires+"; path=/";
	}
	
	function readMainContent() {
		var nameEQ = "mainContent=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function arraySearch(needle,haystackArray){
		for(var i in haystackArray) if(needle === haystackArray[i]) return true;
		return false;
	}

}

