/*!************************** rbmWindow.js *******************************
**** Version 1.02.0003
**** last changed 07.05.2008
**** (c) 2007, 2008 Bernd Rudolf, rbm digitaltechnik, www.rbm.de
************************************************************************/
//
//**** requires rbmCore.js
//
// 04.03.2008	extendCombo.adjustSize must not effect rbmComboMaster
// 04.03.2008	added Params for fader: color, speed, maxOpac
// 25.03.2008	Mousepointer added
// 07.05.2008	Fader: disVis setting changed from 0 to 1 to get display:none
// 			Param added for FadeBG to avoid unexpected toggling
// 25.07.2008  Check for missing close-Button on init (Line 158)
//
//**********************************************************************

var rbmWindow = {		//z-index-Range 1000-2000; 1000 = BGFader
		version: "1.01.0002",
		fader: null,
		color: "#000000",
		zIndex: 1090,
		toolTipzIndex: 2100,
		allWindows: [],
		allCombos: [],
		allTooltips: [],
		tooltipCorner: [
			"tltpnnw",
			"tltpnne",
			"tltpwnw",
			"tltpene",
			"tltpssw",
			"tltpsse"
		],
		comboSetText: true		//set to false for HC
	}                   
	                    
rbmWindow.init = function()	{
	this.initWindows(); 
	this.initCombos();  
	this.initTooltips();
	this.initTabstrips();
	this.createFader();
	this.initbgHiLighter();

}


rbmWindow.initWindows = function()	{
		this.allWindows = $(document.body).getElementsByClassName("window");
		for(this.i = 0; this.i < this.allWindows.length; this.i++)	{
			this.extendWindow(this.allWindows[this.i]);
		}
	}
	

rbmWindow.extendWindow = function(w)	{
		w = $fx($(w));
		w.caption = $fx(w.getElementByClassName("wndcaption"));
		if(w.caption)
			w.caption.displayed = 1;
			
		w.shadow= $fx(w.getElementByClassName("windowShadow"));
//		w.inner = $fx(w.getElementByClassName("windowinner"));
		w.content = $fx(w.getElementByClassName("wndcontentinner"));
		w.content.parent = w;
		
		w.effects = [];

		this.initW = 80;		//für den Reset auf den Ausgangspunkt bei close()
		this.initH = 80;

		
		w.open = function(x, y, w, h, bCaption, bScroll, bFade, transition, bShadow)	{
			this.type = "window";
			this.bScroll = (bScroll == "undefined") ? true : bScroll;
			this.bShadow = (bShadow == "undefined") ? true : bShadow;
			this.bCaption = (bCaption == "undefined") ? true : bCaption;
			if(!this.bScroll)	
				this.getElementByClassName("wndcontent").style["overflow"] = "hidden";

			if(!this.bShadow && w.shadow)	
				this.shadow.setDisplay(false);

			this.bFade = !!bFade;
			if(this.bFade) rbmWindow.fadeBG();
			if(!transition)	{
				this.setDisplay(true);
				this.size(w, h);
				this.move(x, y, transition);
				this.show(true);
				var content = $fx(this.getElementByClassName("wndcontentinner"))
				content.style["visibility"] = "visible";
				content.setOpacity(100);
				this.onLoad();
			}
			else	{
				this.move(x, y);
				this.setDisplay(true);
				this.size(this.initW, this.initH, true);		
				this.adjustSize();	
				this.show(true);

				var newFx2 = new rbmFX.size(this, w, h, null, null, 100)
				newFx2.onStart = function()	{
					var content = $fx(this.parent.getElementByClassName("wndcontentinner"))
					content.style["visibility"] = "hidden";
					content.setOpacity(0);
				}		
				newFx2.onEnd = function()	{
					var content = $fx(this.parent.getElementsByClassName("wndcontentinner")[0]);
//					content.setOpacity(0);
					content.show(true);
					content.fx = new rbmFX.fade(content, 1, 100, 4, 4, 500);
					content.fx.onEnd = function()	{
						this.parent.parent.onLoad();
					}	
					content.fx.go();

				}		
				this.effects.push(newFx2);

			}
			if(!this.bCaption)	{
				$fx(this.caption).setDisplay(0);
				this.getElementByClassName("wndcontent").style["borderTop"] = "1px solid #666666";
				
			}
			else	{
				$fx(this.caption).setDisplay(1);
				this.getElementByClassName("wndcontent").style["borderTop"] = "";
			}	


			this.style["zIndex"] = rbmWindow.zIndex++;
			
			for(this.i = 0; this.i < this.effects.length; this.i++)	{
				this.effects[this.i].go();
			}
		}
		
		w.adjustSize = function (w, h)	{
			w = w || this.initW;
			h = h || this.initH;

			var captionHeight = 0;
			if(this.bCaption)	{
				this.getElementByClassName("wndcpt").style["width"] = (w - 17) + "px";
				captionHeight = this.caption.offsetHeight - 3;
			}
			this.getElementByClassName("wndbody").style["height"] = (h - captionHeight - 5) + "px"; //subtract 5 for bottom


			this.getElementByClassName("wndcontent").style["width"] = (w - 7) + "px";
			this.getElementByClassName("wndbtm").style["width"] = (w - 7) + "px";

			if(this.bShadow)	{
				this.style["width"] = this.offsetWidth + 10;
				this.style["height"] = this.offsetHeight + 10;
				this.shadow.style["width"] = (w + 2) + "px";
				this.shadow.style["height"] = (h + 3) + "px";
			}
		}
		
		
		w.close = function()	{

			this.size(this.initW, this.initH, true);		

			if(this.bFade) rbmWindow.fadeBG();

//			this.fade();	

			this.setDisplay(false);
			
			if(this.onClose) this.onClose();
		}

		w.toFront = function()	{
			this.style.zIndex = rbmWindow.zIndex++;
		}

		w.onclick = w.toFront.bind(w);
		
		
		if(w.caption)
			w.getElementById("wndclose").onclick = w.close.bind(w);
		
		w.onLoad = function()	{ return null }

	}

rbmWindow.onFaderStart = function()	{ return null }

rbmWindow.onFaderEnd = function()	{ return null }


rbmWindow.createFader = function()	{

		this.fader = $("mainfader");
		if(!this.fader)	{
			this.fader = $fx(document.createElement("div"));
			this.fader.style["display"] = "none";
			document.body.appendChild(rbmWindow.fader)
			this.initFader();
		}
}

rbmWindow.initFader = function(color, speed, maxOpac)	{

		this.fader.fadecolor = color || '#000000';
		this.fader.maxOpac = maxOpac || 20;
		this.fader.speed = speed || 1;	//XXX param2 ist nicht speed sondern direction!!!
		this.fader.fx = new rbmFX.fade(this.fader, this.fader.speed , this.fader.maxOpac, 10);	//element, d, max, s, transition, duration
		this.fader.fx.disVis = 1; //use display = none
		this.fader.id = "mainfader";
		this.fader.style["display"] = "none";
		this.fader.style["position"] = "absolute";
		this.fader.style["top"] = "0px";
		this.fader.style["left"] = "0px";
		this.fader.style["width"] = "100%";
//auskommentiert04.03.2008
//testen wenn scrollbar ggf. mit scrollTop in fadeBG()
//			if(window.scrollMaxY)
//				this.fader.style["height"] = (window.innerHeight + window.scrollMaxY) + "px"
//			else
//				this.fader.style["height"] = $("main").offsetHeight + "px";

		this.fader.style["height"] = "100%";
//Height wird über bodyResize() ermittelt;
		this.fader.setOpacity(0);
		this.fader.style["zIndex"] = "1000";
		this.fader.style["top"] = "0px";
		this.fader.style["left"] = "0px";
		this.fader.style["backgroundColor"] = this.fader.fadecolor;
	}
	
rbmWindow.fadeBG = function(n)	{ //n=1 on, n=0 off, n=2 toggle

		var dir;
		switch(n)	{
			case 0:
				dir = "none";
				break;
			case 1:
				dir = "block";
				break;
			default:
				dir = "toggle";
				break
		}

		this.createFader();

		if(this.fader.style["display"] != dir)	{
			if(this.fader.op <= 0)	{
				this.onFaderStart();
	//testen:		this.fader.style["top"] = document.body.scrollTop; 
				this.fader.style["display"] = "block";		//visibility is not sufficient because it can overlap MediaPlayer etc.
				if(dir == "toggle")
					this.fader.fx.toggle();
				else
					this.fader.fx.go(n);
			}	
			else	{
				if(dir == "toggle")
					this.fader.fx.toggle();
				else
					this.fader.fx.go(n);
				//this.fader.fx.toggle();
				this.onFaderEnd();
			}
		}
					
			
	}
	

rbmWindow.initCombos = function()	{
	
		var oldCombos = $(document.body).getElementsByClassName("rbmCombo");
		var i;
		for(i = 0; i < oldCombos.length; i++)	{
			var newCombo = $($("rbmComboMaster").cloneNode(true));
			newCombo.style.width = oldCombos[i].offsetWidth + "px";
			newCombo.style.left = Coordinates.northwestOffset(oldCombos[i], true).x + "px";
			newCombo.style.top = Coordinates.northwestOffset(oldCombos[i], true).y + "px";
			newCombo.style.display = "block";
			newCombo.id = oldCombos[i].id;
			newCombo.name = oldCombos[i].name;
			var oldListEntries = oldCombos[i].getElementsByTagName("option");
			var j;
			var newFirstEntry = $(newCombo.getElementsByTagName("li")[0]);
			var newDropList = newCombo.getElementByClassName("droplist");
			for(j = 0; j < oldListEntries.length; j++)	{
				if(j==0)
					newFirstEntry.getElementByClassName("droplistitem").innerHTML =  oldListEntries[j].innerHTML;
				else {
					var newListEntry = $(newFirstEntry.cloneNode(true));
					newListEntry.getElementByClassName("droplistitem").innerHTML = oldListEntries[j].innerHTML;
					newDropList.appendChild(newListEntry);
				}
			}
			oldCombos[i].parentNode.replaceChild(newCombo, oldCombos[i])
		}
	
		this.allCombos = $(document.body).getElementsByClassName("combo");
		for(i = 0; i < this.allCombos.length; i++)	{
				this.extendCombo(this.allCombos[i], i);
		}
	}
	
	
rbmWindow.extendCombo = function(c, zIndex)	{

		
	c.drop = false;
	c.setText = rbmWindow.comboSetText;	
	c.dropdownbox = $fx($(c).getElementByClassName("dropdownbox"));
	c.dropbutton = c.getElementByClassName("dropdownLink");
	c.formelement = c.getElementByClassName("dropformelement");
//	c.formelement.name = c.name;
	c.textelement = c.getElementsByClassName("combotextbox")[0];
	c.dropboxMaxW = 200;		//set Max Width of dropbox here, 0 for no limit, 1 for width of Combo
	c.style["zIndex"] = 255 - zIndex;
		
	c.dropdown = function(ud)	{
		if(ud === null)
			this.drop = !this.drop
		else	
			this.drop = ud;
		var i;
		if(this.drop)	{ 	//runter
			for(i=0; i<rbmWindow.allCombos.length; i++)	{
				if(rbmWindow.allCombos[i] != this)	{
					rbmWindow.allCombos[i].drop = false;
					rbmWindow.allCombos[i].dropdownbox.setDisplay(false);
				}
			}
		} 
		//this.dropdownbox.style["z-index"] = "90";
		this.dropdownbox.setDisplay(this.drop);
	}
	
	c.dropSelect = function(id, n)	{
		c.dropdown(false);
		if(typeof(hcDropSelect) != "undefined" )	{
			hcDropSelect(id, n);
		}
	}
	
	c.clickSelect = function(n)	{
		c.value = n;
		c.formelement.value = n;
		if(c.setText)
			c.textelement.innerHTML = c.droplistItems[n - 1].innerHTML;		//inkl. <nobr>

		c.dropSelect(c.id, n);
	}

	c.droplistItems = c.getElementsByClassName("droplistitem");
	var i;
	for(i=0; i<c.droplistItems.length; i++)	{
		c.droplistItems[i].id = i+1;
		c.droplistItems[i].onclick = c.clickSelect.bind(c.droplistItems[i], i+1);
	}


	c.dropbutton.onclick = c.dropdown.bind(c, null);

	c.adjustSize = function (w, h)	{
		if(c.id == "rbmComboMaster")		//do not adjust Master!!
			return false;
			
		w = w || this.offsetWidth;
		h = h || this.offsetHeight;
//w = 200; nur für huntclick!
		this.getElementsByClassName("combotextbox")[0].style["width"] = (w - 30) + "px";
		this.getElementsByClassName("comboinner")[0].style["width"] = (w - 6) + "px";
		this.getElementsByClassName("dropdownbox")[0].style["width"] = (this.dropboxMaxW) + "px";
		this.getElementsByClassName("dropdownbox")[0].style["width"] = (this.dropboxMaxW) + "px";
//XXX dropdownbox anpassen!!!
	}

	c.adjustSize();
}


rbmWindow.initTooltips = function()	{
		this.allToolTips = $(document.body).getElementsByClassName("tooltip");
		for(this.i = 0; this.i < this.allToolTips.length; this.i++)	{
			this.extendToolTips(this.allToolTips[this.i]);
		}
	}
	

rbmWindow.extendToolTips = function(t)	{
		t = $fx(t);
		t.content = t.getElementByClassName("tltpcontentinner");
		t.FX = new rbmFX.fade(t, 1, 100, 10, 4, 500);
		t.setOpacity(0);
		t.tipType = "nnw";
		
		
		t.showTip = function(dir)	{

			this.getElementById("tltpnnw").style.display = "none";
			this.getElementById("tltpnne").style.display = "none"; 
			this.getElementById("tltpwnw").style.display = "none"; 
			this.getElementById("tltpene").style.display = "none"; 
			this.getElementById("tltpssw").style.display = "none"; 
			this.getElementById("tltpsse").style.display = "none";
				
			this.getElementById("tltp" + dir).style.display = "block";
			this.tipType = dir;
			

		}
		
		t.showToolTip = function(dir, element, w, h, offsetX, offsetY, text)	{
			this.showTip(dir);
			var o = $(element);
			var pos = Coordinates.northwestOffset(o, true);
			this.posTooltip(pos.x + offsetX, pos.y + offsetY, w, h, o);
			this.load(text);
			this.FX.go(1);

		}

		t.hideToolTip = function()	{
			this.FX.go(-1)
		}

		
		t.posTooltip = function(x, y, w, h, o)	{
			this.style["zIndex"] = rbmWindow.toolTipzIndex++;
			
//			alert(x + " - " + y);

			switch(this.tipType)	{
				case "sse":
					var tippos = new Coordinate(w - 34, h);
					break;
				case "ene":
					var tippos = new Coordinate(w, 34);
					break;
				case "nne":
					var tippos = new Coordinate(w - 34, 0-o.offsetHeight);
					break;
				case "nnw":
					//var tippos = Coordinates.northwestOffset(this.getElementById("tltp" + this.tipType), true);
					var tippos = new Coordinate(34-o.offsetWidth, 0-o.offsetHeight);
					break;
				case "wnw":
					var tippos = new Coordinate(0-o.offsetWidth, 34);
					break;
				case "ssw":
					var tippos = new Coordinate(34-o.offsetWidth, h);
					break;
				default:
					break;
			}

			var newPos = new Coordinate(x - tippos.x, y - tippos.y);
			this.size(w, h);
			this.move(newPos.x, newPos.y);
		}
		
		t.adjustSize = function (w, h)	{
			w = w || this.offsetWidth;
			h = h || this.offsetHeight;

//XXX store these Elements on init for better performance!
			this.getElementByClassName("tltptop").style["width"] = (w - 46) + "px";
			this.getElementByClassName("tltpcontent").style["width"] = (w - 48) + "px";
			this.getElementByClassName("tltpbottom").style["width"] = (w - 46) + "px";

			this.getElementByClassName("tooltipcenter").style["height"] = (h - 48) + "px"; //

//			if(this.bShadow)	{
//				this.shadow.style["width"] = (w + 2) + "px";
//				this.shadow.style["height"] = (h + 3) + "px";
//			}
		}
		
		
		t.load = function(element)	{
			var oElement = $(element);
			if(oElement)	{
				this.content.innerHTML = oElement.innerHTML;
			}
		}

	}


rbmWindow.initMousePointer = function()	{
		this.mousePointer = $fx("mouse");

		var t = this.mousePointer;
		t.show = function(element, offsetX, offsetY, text, color)	{	//Position from southeast!!!
			if(text)	{
				var x = this.getElementById("mouseText")
				x.innerHTML = text;
				x.style.display = "block";
				x.style.color = color ? color : "#000000";
			}
			else	{
				var x = this.getElementById("mouseText")
				x.style.display = "none";
			}
			var o = $(element);
			var pos = Coordinates.northwestOffset(o, true, -1);	//subtract scrolled content
			this.move(pos.x - offsetX + o.offsetWidth, pos.y - offsetY + o.offsetHeight - 10);

			this.setDisplay(true);
			//this.FX.go(1);
		
		}
	}

// ******************************** Tabstrips **********************************************

rbmWindow.initTabstrips = function()	{
		this.allTabStrips = $(document.body).getElementsByClassName("rbmTabstrip");
		for(this.i = 0; this.i < this.allTabStrips.length; this.i++)	{
			this.extendTabstrips(this.allTabStrips[this.i]);
		}
	}

rbmWindow.extendTabstrips = function (t)	{


	t.selectTab = function(sel)	{
		//alert(sel.id);
		
		for(var i=0; i<this.aTabs.length; i++)	{
			//alert( this.aTabs[i].className.split(" ")[0]);
			var cName = this.aTabs[i].className.split(" "); //take all classNames except the last one which is the activation
			var newClass = ""
			for(var j=0; j==0 || j<cName.length - 1; j++)	{
				newClass += cName[j];
			}
			this.aTabs[i].className  = newClass;
		}
	
		for(var i=0; i<this.aTabContainer.length; i++)	{
			$fx(this.aTabContainer[i]).setDisplay(false);
		}
		sel.className = sel.className + " " + sel.className + "_a";
		this.aTabContainer[sel.tIndex].setDisplay(true);
	}

	t.aTabs = $(t).getElementByClassName("tabA").getElementsByTagName("li");
	for(var i=0; i<t.aTabs.length; i++)	{
		if(t.aTabs[i].id != "blank")	{
			t.aTabs[i].id = t.id + "_" + i;
			t.aTabs[i].tIndex = i;
			t.aTabs[i].onclick = function()	{
				t.selectTab(this);
			}
			
		}
	}
	

	t.aTabContainer = $(t).getElementsByClassName("tabContainer");
	for(var i=0; i<t.aTabContainer.length; i++)	{
		t.aTabContainer[i].id = t.id + "C_" + i;
	}
}



// ******************************** HiLighter **********************************************

	rbmWindow.initbgHiLighter = function()	{

		this.bgHiLighter = $fx("bgHiLighter");
		if(this.bgHiLighter)	{
			this.bgHiLighter.fx = new rbmFX.fade(this.bgHiLighter, 1, 100, 4, 4, 300);
			this.bgHiLighter.fx.disVis = 1;	//do not turn off to avoid clicking!
		}
	}

	rbmWindow.setHiLight = function (x, y, w, h)	{
		sw = document.body.offsetWidth;
		sh = document.body.offsetHeight;
		
		this.bgHiLighter.setDisplay(true);

		//sw = mecoVariables["SCREEN_WIDTH"];
		//sh = mecoVariables["SCREEN_HEIGHT"];
		
		$fx("bgHiLighter").size(sw, sh);
		$fx("bgHiLighterTop").size(sw, y);
		$fx("bgHiLighterLeft").size(x, h);
		$fx("bgHiLight").size(w, h);
		$fx("bgHiLighterRight").size(sw - x - w, h);
		$fx("bgHiLighterBottom").size(sw, sh - y - h);
		
		$fx("bgHiLighter").fx.go(1);
	}
	
	rbmWindow.resetHiLight = function ()	{
		$fx("bgHiLighter").fx.go(-1);
	}
	