marbet = function()
{	
	// current navi and content urls
	var navi = '';
	var cont = '';
	var uvar = '';
	
	// current quote/visual rotation
	var quotes;
	var visuals;
	var names;
	var iid;
	var headerPeriod = 5000;
	var qSel = -1;
	var vSel = -1;
	var streamQuotes = false;
	
	// private functions
	var pick = function(array,current)
	{
		if(array==undefined || array.length<2) return 0;
		var newPick;
		do{
			newPick = Math.floor(Math.random()*array.length);
		} while(current==newPick);
		return newPick;
	}
	
	var setvisual = function(url)
	{
		document.getElementById('flash23').visual(url)
	};
	
	var setquote = function(str)
	{
		document.getElementById('flash23').quote(str)
	};
	
	var setmarked = function(str)
	{
		document.getElementById('flash23').marked(str);
	};
	
	var headerSwitch = function()
	{
		if(!streamQuotes) {
			var vNew = pick(visuals,vSel);
		} 
		else {
			var vNew = (vSel<visuals.length-1) ? vSel+1 : 0;
		}
				
		if(vNew != vSel)
		{
			setvisual(visuals.length>0 ? visuals[vNew] : '');
			vSel = vNew;
		}
		
		// wenn genausoviele visuals wie quotes vorhanden sind, diese jeweils syncron anzeigen
		var qNew = (quotes.length==visuals.length) ? vSel : pick(quotes,qSel);
		if(qNew != qSel)
		{
			var theName = (names[qNew]) ? '<p align="right">'+names[qNew]+'</p>' : '';
			
			setquote(quotes.length>0 ? '<p>'+quotes[qNew]+'</p>' + theName : '');
			qSel = qNew;
		}
	};
		
	// flash embed
	var flashmenu = function()
	{
		if(swfobject.hasFlashPlayerVersion("9.0.0") && window.location.href.indexOf('noflash') == -1 )
		{
			var vars = {'xmlfile':''};
			var params = { scale:'noScale', salign:'lt', menu:'false', wmode:'transparent'};
			var attributes = { id: "flash23", name: "flash23" };
			swfobject.embedSWF("marbet.swf", "flash", "100%", "100%", "9.0.0", "js/expressInstall.swf",vars,params,attributes);
			
			// hide Iframe-divs, so FF3 doesn't gets distracted by them
			document.getElementById('subnavi').style.display = "none";
			document.getElementById('content').style.display = "none";
		}
		else
		{
			document.getElementById('flash').style.visibility = "visible";
		}
	};
	
	// linkcheck > the first page to load after intro
	var linkcheck = function()
	{
		if(window.location.href.indexOf('#') > -1)
		{
			var query = window.location.href.substr(window.location.href.indexOf('#')+1);
			var n = query.substr(0,query.indexOf('__'));
			var c = query.substr(query.indexOf('__')+2);
		}
		else
		{
			var n = 'home';
			var c = 'home';
			
			if(window.location.href.indexOf('\?w') > -1)
			{
				n = 'wopu';
				c = 'wopu';
			}
		}
		
		setmarked(n);
		marbet.content(n,c);
		
	};
	
	// #url in 
	var setQuickUrl = function()
	{
		if(window.location.href.indexOf('#')==-1)
			window.location.href = window.location.href+'#'+navi+'__'+cont;
		else
			window.location.href = window.location.href.substr(0,window.location.href.indexOf('#'))+'#'+navi+'__'+cont;
			
		document.title = "Marbet Immobilien AG Luzern, Verkauf und Verwaltung von Immobilien";
	};
	
	// checks the naviframe for links to highlight
	var linkHighlight = function(doc)
	{
		var elements = doc.getElementsByTagName("a");
		for( i = 0; i < elements.length; i++ ) {
			if( elements[i].className == 'active' ) elements[i].className = '';
			if( elements[i].href == parent.cframe.location.href) elements[i].className = "active";
		}
	}
	
	// random bg
	/*
	var randomBg = function()
	{
		var imgnr = Math.floor(Math.random()*4);
		document.getElementById('bgimg').src = "layout/bg"+imgnr+".jpg";
		
		// setup resize listener + init once
		window.onresize = backgroundresize;
		backgroundresize();
	};
	
	var backgroundresize = function(height)
	{
		var ih = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
		var fh = parent.cframe.document.body.clientHeight;
		var offset = navigator.appName=="Microsoft Internet Explorer" ? 0 : 50;
		ih = (ih < fh+offset+172+80) ? fh+offset+172+80 : ih;
	
		var bgH = ih+ 'px';
		if (height && height>ih){
			bgH = height+"px";
		}
		
		var bgW = document.documentElement.clientWidth+"px";
		document.getElementById('bg').style.height = bgH;		
		document.getElementById('bg').style.width = bgW;
	};
	*/
	
	var frameHeight = function()
	{	
		var ih = parent.cframe.document.body.clientHeight;
		//var offset = navigator.appName=="Microsoft Internet Explorer" ? 0 : 50;
		var offset = 50;
		if(ih > 378-offset)
		{
			document.getElementById('content').style.height = (ih+offset) + "px";
			document.getElementById('cframe').style.height = (ih+offset) + "px";
			document.getElementById('flashfooter23').style.top = (ih+172+offset) + "px";
			
			//backgroundresize(ih+offset+172+80);
		}
		else
		{
			document.getElementById('content').style.height = "378px";
			document.getElementById('cframe').style.height = "378px";
			document.getElementById('flashfooter23').style.top = "550px";
			
			//backgroundresize(630);
		}		
	};
	
	// helper by http://www.bloggingdeveloper.com/
	var getQuerystring = function(key, default_,from)
	{
		if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(from ? from : window.location.href);
		if(qs == null)
			return default_;
		else
			return qs[1];
	} 
	
	// public functions
	return {
		init:function()
		{			
			// flash setup
			flashmenu();
			
			// bg
			//randomBg();
			var imgnr = Math.floor(Math.random()*4);
			document.body.style.backgroundImage = "url(layout/bg"+imgnr+".jpg)";
			document.body.style.backgroundRepeat = "no-repeat";
			document.body.style.backgroundPosition = "top left";
		},
		
		introdone:function()
		{	
			var params = { scale:'noScale', salign:'lt', menu:'false', wmode:'transparent'};
			var attributes = { id: "flashfooter23", name: "flashfooter23" };
			swfobject.embedSWF("marbet-footer.swf", "flashfooter", "1000", "80", "9.0.0", "js/expressInstall.swf",{},params,attributes);
			
			document.getElementById('subnavi').style.display = "block";
			document.getElementById('content').style.display = "block";
			
			// first content
			linkcheck();
		},
		
		shrinkflash:function()
		{
			document.getElementById('flash23').style.height = '172px';
		},
		
		header:function (vArray, qArray, nArray, stream)
		{
			visuals = (vArray==undefined) ? new Array() : vArray;
			quotes = (qArray==undefined) ? new Array() : qArray;
			names = (nArray==undefined) ? new Array() : nArray;
			qSel = -1;
			vSel = -1;
			streamQuotes = (stream=='stream') ? true : false;
			
			// no parameters, just clean up
			if(visuals.length==0)	setvisual('');
			if(quotes.length==0)	setquote('');
			
			headerSwitch();
			clearInterval(iid);
			
			// slideshow
			if(quotes.length>1 || visuals.length>1)
			{
				iid = setInterval("marbet.headerSwitch()", headerPeriod);
			}
		},
		
		headerSwitch:function()
		{
			headerSwitch();
		},
		
		content:function (n,c)
		{
			if(n=='port')
			{
				return;
			}		
			parent.nframe.location.href = "navi.php?p=" + n + ".php";
			parent.cframe.location.href = "content.php?p=" + c + ".php&frontpage";
			
			navi = n;
			cont = c;
			
			// show visual
			marbet.header([n],[]);
			
			setQuickUrl();
		},
		
		// this is called from content.php onload
		newContent:function()
		{
			if(parent.cframe.location.href.indexOf("?p=") > -1)
			{
				var p1 = parent.cframe.location.href.indexOf("?p=")+3;
				var p2 = parent.cframe.location.href.lastIndexOf(".php") - p1;			
				cont = parent.cframe.location.href.substr(p1,p2);
				
				setQuickUrl();
				linkHighlight(parent.nframe.document);
				linkHighlight(parent.cframe.document);
				frameHeight();
			}
		}
	}
}();



