// ========================================
//
// 		  Dynamo Javascript Library
//
// ========================================
// Copyright by tW net (www.dynamo-system.ch)
// ========================================

// ========================================
//
// 			Class CMSRequest
//
// ========================================
// Konstruktor
// ========================================

function CMSRequest()
{
	this.cmsRequest	= null;
	this.cmsXml		= null;
	this.cmsClient	= "unsupported";

// ========================================
// Setup
// ========================================

	this.setup = function()
	{
		if(window.XMLHttpRequest)
		{
			this.cmsRequest = new XMLHttpRequest();
			this.cmsClient  = "GECKO";
		}
		else
		{
			this.cmsRequest = new ActiveXObject("Microsoft.XMLHTTP");
			this.cmsClient  = "IE";
		}
	}

// ========================================
// Meldung an Server senden
// ========================================

	this.sendEvent = function(url,listener)
	{
		this.cmsRequest.open("GET",url,true);

		if(listener)
		{
			this.cmsRequest.onreadystatechange = listener;
		}
		
		this.cmsRequest.send(null);
	}

// ========================================
// Request senden
// ========================================

	this.request = function(method,url,params,listener)
	{
		this.cmsRequest.open(method,url,true);
		this.cmsRequest.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");

		if(listener)
		{
			this.cmsRequest.onreadystatechange = listener;
		}

		this.cmsRequest.send(params);
	}

// ========================================
// Uebergibt die Message als Text
// ========================================

	this.getText = function()
	{
		return(this.cmsRequest.responseText);
	}

// ========================================
// Prueft ob die Daten geladen sind
// ========================================

	this.isReady = function()
	{
		try
		{
			if(this.dspRequest.readyState == 4) 
			{
				if(this.dspRequest.status == 200)
				{
					return(true);
				}
				else alert("Error: Loading Data failed \"" + this.dspRequest.statusText + "\"");
			}
			
			return(false);
		}
		catch(e) {return(false);}
	}	
}

// ========================================
//
// 			Class CMSElement
//
// ========================================
// Konstruktor
// ========================================

function CMSElement(name,left,top,right,bottom,hcenter,vcenter)
{
	this.cmsName		= name;					// Element-id
	this.cmsLeft		= left;					// Abstand links
	this.cmsRight		= right;				// Abstand rechts
	this.cmsTop			= top;					// Abstand oben
	this.cmsBottom		= bottom;				// Abstand unten
	this.cmsVCenter		= vcenter;				// Vertikal zentriert
	this.cmsHCenter		= hcenter;				// Horizontal zentriert
	this.cmsContainer	= new Array();			// Unterelemente
	this.cmsElement		= null;					// Referenz
	
// ========================================
// Prueft ob dies ein Container ist.
// ========================================

	this.isContainer = function()
	{
		if(this.cmsContainer.length > 0)
		{
			return(true);
		}
		else return(false);
	}
	
// ========================================
// Fuegt ein Element hinzu
// ========================================

	this.addElement = function(element)
	{
		this.cmsContainer.push(element);
	}
		
// ========================================
// Fuegt ein Element hinzu
// ========================================

	this.firstElement = function(element)
	{
		return(this.cmsContainer[0]);
	}
	
// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isLeft = function()
	{
		if(this.cmsLeft != null) return(true);
			else return(false);
	}

// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isRight = function()
	{
		if(this.cmsRight != null) return(true);
			else return(false);
	}	

// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isTop = function()
	{
		if(this.cmsTop != null) return(true);
			else return(false);
	}
	
// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isBottom = function()
	{
		if(this.cmsBottom != null) return(true);
			else return(false);
	}	
	
// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isHCenter = function()
	{
		if(this.cmsHCenter != null) return(true);
			else return(false);
	}
	
// ========================================
// Prueft ob dieser Wert gesetzt ist.
// ========================================
		
	this.isVCenter = function()
	{
		if(this.cmsVCenter != null) return(true);
			else return(false);
	}
		
// ========================================
// Uebergibt einElement
// ========================================
		
	this.getElement = function(name)
	{			
		for(var i=0; i < this.cmsContainer.length; i++)
		{
			if(this.cmsContainer[i].cmsName == name)
			{
				return(this.cmsContainer[i]);		
			}
		}
		
		return(null);
	}
	
// ========================================
// Sucht ein Element
// ========================================

	this.findElement = function(name)
	{
		var container,path;
		
		path	  = name.split(".");
		container = this;
		
		for(var i=0; i < path.length; i++)
		{	
			element = container.getElement(path[i]);
			
			if(element == null)
			{
				break;
			}
			else container = element;
		}
		
		return(element);
	}
	
// ========================================
// Sucht ein Element
// ========================================

	this.initList = function(conf)
	{
		var count,element;
		var n=0;
		
		count = conf.length / 8;
		
		for(var i=0; i < count; i++,n+=8)
		{
			element	= new CMSElement(conf[n+1],conf[n+2],conf[n+3],conf[n+4],conf[n+5],conf[n+6],conf[n+7]);
			
			if(conf[n] != "")
			{
				container = this.findElement(conf[n]);
				container.addElement(element);
			}
			else this.addElement(element);
		}
	}
	
// ========================================
// Uebergibt das HTML-Element
// ========================================
		
	this.getHTMLElement = function()
	{
		if(this.cmsElement == null) 
		{
			this.cmsElement = document.getElementById(this.cmsName);
		}
		
		return(this.cmsElement);
	}
	
// ========================================
// Berechnet die Position neu
// ========================================

	this.layout = function(container) 
	{
		var	constraint,element;
		var	boxwidth,boxheight;	
		var max,x,y,width,height;
		
		element	  = container.getHTMLElement();
		boxwidth  = element.clientWidth;
		boxheight = element.clientHeight;
		max		  = container.cmsContainer.length;
		
		for(var i=0; i < max; i++)
		{
			constraint = container.cmsContainer[i];
			element   = container.cmsContainer[i].getHTMLElement();
			x		  = parseInt(element.style.left);
			y		  = parseInt(element.style.top);
			width	  = parseInt(element.style.width);
			height	  = parseInt(element.style.height);
			
			if(constraint.isHCenter())
			{
				x = (boxwidth - width) / 2 + constraint.cmsHCenter;
			}
			else
			{
				if(constraint.isLeft() && constraint.isRight())
				{
					x	  = constraint.cmsLeft;
					width = boxwidth - constraint.cmsRight - x;
				}
				else
				{
					if(constraint.isLeft())  x = constraint.cmsLeft;
					if(constraint.isRight()) x = boxwidth - width - constraint.cmsRight;
				}
			}
			
			if(constraint.isVCenter())
			{
				y = (boxheight - height) / 2 + constraint.cmsVCenter;
			}
			else
			{
				if(constraint.isTop() && constraint.isBottom())
				{
					y	   = constraint.cmsTop;
					height = boxheight - constraint.cmsBottom - y;
				}
				else
				{
					if(constraint.isTop())	 y = constraint.cmsTop;
					if(constraint.isBottom()) y = boxheight - height - constraint.cmsBottom;
				}
			}
			
			if(isNaN(x) == false) element.style.left = x;
			if(isNaN(y) == false) element.style.top	 = y;
			element.style.width  = width;
			element.style.height = height;
			
			if(constraint.isContainer())
			{
				constraint.layout(constraint);
			}
		}
	}
	
	return(this);
}

// ========================================
//
// 			Class CMSTemplate
//
// ========================================
// Konstruktor
// ========================================

function CMSTemplate()
{
	this.xml	= null;
	this.xsl	= null;
	this.output = null;

// ========================================
//	XML aus Dokument setzen
// ========================================

	this.setDocument = function(doc)
	{
		this.xml = this.loadXMLFile(doc);
		this.createOutput();
    }

// ========================================
//	XML aus String setzen
// ========================================

	this.generateDocument = function(str)
	{
		this.xml = this.loadXMLString(str);
    }

// ========================================
//	Template aus Dokument
// ========================================

	this.setTemplate = function(template)
	{
		this.xsl = this.loadXMLFile("cmspages/templates/" + template);
    }

// ========================================
//	XML aus String laden
// ========================================

	this.loadXMLString = function(xmlString)
	{
		var xmlDoc;
		
		if(window.ActiveXObject)
		{
			xmlDoc		 = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.loadXML(xmlString);
		}
		else if (document.implementation && document.implementation.createDocument)
		{
			var parser	= new DOMParser();
			xmlDoc		= parser.parseFromString(xmlString,"text/xml");
		}
		else alert('Your Browser cannot handle XSLT-Scripts');
		
		return(xmlDoc);
    }

// ========================================
//	XML aus Dokument laden
// ========================================

	this.loadXMLFile = function(xmlFile)
	{
		var xmlDoc;
		if (window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}
		else if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc = document.implementation.createDocument("","",null);
		}
		else
		{
			alert('Your browser cannot handle this script');
		}
		xmlDoc.async = false;
		xmlDoc.load(xmlFile);
		return(xmlDoc);
    }

// ========================================
//	XSLT-String erzeugen
// ========================================

	this.createOutput = function()
	{
		if(window.ActiveXObject)
		{
			this.output = this.xml.transformNode(this.xsl);
        }
		else if(document.implementation && document.implementation.createDocument)
		{
			var xsltProcessor = new XSLTProcessor();

			xsltProcessor.importStylesheet(this.xsl);

			this.output	= xsltProcessor.transformToFragment(this.xml,document);
			this.output = CMSXmlToString(this.output);
        }
    }

// ========================================
//	XSLT schreiben
// ========================================
// @param id Opt. id des Zielelements
// ========================================

	this.process = function(id)
	{
		this.createOutput();

		if(id != undefined)
		{
			var target		 = document.getElementById(id);
			target.innerHTML = this.output;
        }
		else document.write(this.output);
    }
}

// ========================================
//
// 			Class CMSRuntime
//
// ========================================

function CMSRuntime()
{
	this.cmsDocuments = new Array();
	
// ========================================
// Document hinzufuegen.
// ========================================

	this.addDocument = function(doc)
	{
		this.cmsDocuments.push(doc);
	}
	
// ========================================
// Ubergibt Bildergruppe.
// ========================================

	this.getDocument = function(name)
	{
		for(var i=0; i < this.cmsDocuments.length; i++)
		{
			if(name == this.cmsDocuments[i].cmsName)
			{
				return(this.cmsDocuments[i]);
			}
		}
		
		return(null);
	}	
}

// ========================================
//
// 			Class CMSDocument
//
// ========================================

function CMSDocument(name)
{
	this.cmsImageGroups = new Array();
	this.cmsName		= name;
	
// ========================================
// Fuegt eine Bildergruppe hinzu.
// ========================================

	this.addImageGroup = function(group)
	{
		this.cmsImageGroups.push(group);
	}	
	
// ========================================
// Ubergibt Bildergruppe.
// ========================================

	this.getImageGroup = function(name)
	{
		for(var i=0; i < this.cmsImageGroups.length; i++)
		{
			if(name == this.cmsImageGroups[i].cmsName)
			{
				return(this.cmsImageGroups[i]);
			}
		}
		
		return(null);
	}
}

// ========================================
//
//		Globale Funktionen
//
// ========================================
//	ServerAction mit refresh
// ========================================

function CMSAction(handler,action)
{
	var count,url;
	    
	url = "http://" + location.hostname + "/index.php?class=" + handler + "&method=" + action;

	if(arguments != undefined)
	{
		count = arguments.length - 2;

		for(var i=0; i < count; i++)
		{
			url += "&param" + i + "=" + encodeURIComponent(encodeURIComponent(arguments[i + 2]));//doppelt wegen utf
		}
	}
	
	location = url;
}

// ========================================
//	ServerAction ohne Feedback
// ========================================

function CMSEvent(handler,action)
{
	var count,url;
	var request;

	url = "http://" + location.hostname + "/index.php?class=" + handler + "&method=" + action;

	if(arguments != undefined)
	{
		count = arguments.length - 2;

		for(var i=0; i < count; i++)
		{
			url += "&param" + i + "=" + encodeURIComponent(encodeURIComponent(arguments[i + 2]));//doppelt wegen utf
		}
	}

	request = new CMSRequest();
	request.setup();
	request.sendEvent(url);
}

// ========================================
// Absolute Positionsermittlung
// ========================================

function CMSPosition(element)
{
	var pos = {x:0,y:0};

	while(element)
	{
		pos.x  += element.offsetLeft;
		pos.y  += element.offsetTop;
		element = element.offsetParent;
	}

	return(pos);
}

// ========================================
// Klappt das Menu ein
// ========================================

function CMSMenuClose()
{
	var path,menuitems;

	if(document.cmsmenupath)
	{
		if(document.cmsmenupath != "")
		{
			path = document.cmsmenupath.split("_");

			for(var i=0; i < path.length; i++)
			{
				menuitems = document.getElementById(path[i] + "_content");
				menuitems.style.visibility = "hidden";
			}

			document.cmsmenupath = "";
		}
	}
}

// ========================================
// Klappt das Menu aus
// ========================================

function CMSMenuOver(name)
{
	var menupath,menuitems;
	var path,pos;
	var menu;
	var xoffset,yoffset

	menuitems = document.getElementById(name + "_content");
	menupath  = menuitems.getAttribute("cmsmenu");
	path	  = menupath.split("_");

	CMSMenuClose();

	for(var i=0; i < path.length; i++)
	{
		menuitems = document.getElementById(path[i] + "_content");

		if(menuitems != null)
		{
			menu				= document.getElementById(path[i]);
			pos				= CMSPosition(menu);
			xoffset				= parseInt(menuitems.attributes.menuxoffset.value);
			yoffset				= parseInt(menuitems.attributes.menuyoffset.value);

			menuitems.style.top		= pos.y + yoffset;
			menuitems.style.visibility	= "visible";

			if((pos.x + menuitems.clientWidth) > document.body.clientWidth)
			{
				menuitems.style.left = pos.x - menuitems.clientWidth + xoffset;
			}
			else menuitems.style.left = pos.x + menuitems.clientWidth + xoffset;;
		}
	}

	document.cmsmenupath = menupath;
}

// ========================================
// Setzt eine Liste von CSS-Attributen
// ========================================

function CMSStyles(element)
{
	var name,value,count;

	if(arguments != undefined)
	{
		count = arguments.length - 1;

		for(var i=1; i < count; i+=2)
		{
			name  = arguments[i];
			value = arguments[i + 1];
			element.style[name] = value;
		}
	}
}

// ========================================
// Setzt eine Liste von HTML-Attributen
// ========================================

function CMSAttributes(element)
{
	var name,value,count;

	if(arguments != undefined)
	{
		count = arguments.length - 1;

		for(var i=1; i < count; i+=2)
		{
			name  = arguments[i];
			value = arguments[i + 1];
			element[name] = value;
		}
	}
}

