		
	
	/***************************************************************
	*  Copyright notice
	*
	*  (c) 2007 Reto Kernen <2kk@gmx.ch>
	*  All rights reserved
	*
	*  This script is part of the SSIP project. The SSIP project is
	*  free software; you can redistribute it and/or modify
	*  it under the terms of the GNU General Public License as published by
	*  the Free Software Foundation; either version 2 of the License, or
	*  (at your option) any later version.
	*
	*  The GNU General Public License can be found at
	*  http://www.gnu.org/copyleft/gpl.html.
    *    
	*  !!!!  Important GNU License Exeptions  !!!!!
    *  SSIP musten't be used under the following cases :  
    *  1. If your  Organisation got any Military propose.  
    *  2. If your Organisation violate the human rights.  What is included you
    *  can  read here:
    *  en.wikipedia.org/wiki/European_Convention_on_Human_Rights
	*
	*  This script is distributed in the hope that it will be useful,
	*  but WITHOUT ANY WARRANTY; without even the implied warranty of
	*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	*  GNU General Public License for more details.
	*
	*  This copyright notice MUST APPEAR in all copies of the script!
	***************************************************************/
	
	function SSIP(){
		this.resizeIE;
		this.imageBase;
		this.ssipreadyState = true;
		this.cssCacheArray;
		this.mySSIPFl;
		
		this.initSSIP = function(){
			
			//if (navigator.appName.indexOf("Explorer") != -1){
				//this.properSourceForIE();
			//}
			
			try{
				this.cssCacheArray = new Array();
				this.getLinkedCSSStyle();
			}catch(e){}
		
			//document.getElementById("dumpcontent").innerHTML = document.getElementsByTagName("body")[0].innerHTML;
		}
		
		this.updateSSIP = function(){

			for(var h = 0; h<mySSIP.cssCacheArray.length;h++){
				imageBase = mySSIP.cssCacheArray[h]["imageBase"];
				mySSIP.getSSIPConfig(mySSIP.cssCacheArray[h]["cssCode"]);
			}
			
		}
	
		this.getCssFileContent = function(url){
			http_request = false;
	
			imageBasePath = url.split("/");
			imageBase = "";
			for (c=0; c<imageBasePath.length-1; c++){
				imageBase += imageBasePath[c]+"/";
			}
			
			if(imageBase){
				if (window.XMLHttpRequest) { // Mozilla, Safari,...
					http_request = new XMLHttpRequest();
					if (http_request.overrideMimeType) {
						http_request.overrideMimeType('text/xml');
						// zu dieser Zeile siehe weiter unten
					}
				} else if (window.ActiveXObject) { // IE
					try {
						http_request = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
					}
				}
		
				if (!http_request) {
					alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
					return false;
				}
				http_request.onreadystatechange = this.dumpCssContents;
				http_request.open('POST', url, true);
				http_request.send(null);
			}
		}
	
		this.dumpCssContents = function(){
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
						newCssFile = new Object();
						newCssFile["imageBase"] = imageBase;
						newCssFile["cssCode"]   = http_request.responseText;
						mySSIP.cssCacheArray.push(newCssFile);
						mySSIP.getSSIPConfig(http_request.responseText);	
						mySSIP.ssipreadyState = true;
					 
				} else {
					alert ='Bei dem Request ist ein Problem aufgetreten.';
				}
			}else{
				mySSIP.ssipreadyState = false;
				
			}
		}
		
	
		
		this.getSSIPConfig = function(theCode){
			if(theCode != ""){
				var Ergebnis = new Array();
				var Suchtext = theCode;
				
						//Fetch IDs witch SIPP Config
				Suchtext     = Suchtext.replace(/\s/g, " ");
	
				Suche = /(#)([a-zA-Z0-9\-_]*)(\s*\{)[a-zA-Z0-9;:\-_()\s.,%#"]*ssip-background:[a-zA-Z0-9\-_()\s.\/]*;[a-zA-Z0-9;:\-_()\s.,(#\d\d)%"]*}/gi;
				
				Ergebnis = Suchtext.match(Suche);
				if(Ergebnis){
					 this.parseCssConfig(Ergebnis,"id");
				}
				
				
						//Fetch Classes witch SIPP Config
				Suche = /(\.)([a-zA-Z0-9\-_]*)(\s*\{)[a-zA-Z0-9;:\-_()\s.,%#"]*ssip-background:[a-zA-Z0-9\-_()\s.\/]*;[a-zA-Z0-9;:\-_()\s.,(#\d\d)%"]*}/gi;
		
				Ergebnis = Suchtext.match(Suche);
				if(Ergebnis){
					this.parseCssConfig(Ergebnis,"class");
	
				}
				
				
			}else{
				return;
			}
		}
	
		this.parseCssConfig = function(Ergebnis,Selector){
	
			if(Selector == "id"){
				elementsAreClasses=false;
			}else{
				elementsAreClasses=true;
			}

			for (var i = 0; i < Ergebnis.length; i++) {
			
				//alert(Ergebnis[i]);
				if(elementsAreClasses){
					esearch  = /\.[a-zA-Z0-9;{}:\-_()\s.,]*\{/gi;
				}else{
					esearch  = /#[a-zA-Z0-9;{}:\-_()\s.,]*\{/gi;
				}
				
				esearche = Ergebnis[i].match(esearch);
				ename = esearche[0].substr(1,esearche[0].length-2);			
				ename = this.replaceWhitespaces(ename);
	
				urlsearch     = /url[(]\S*[)]/gi;
				urlmatch      = Ergebnis[i].match(urlsearch);
				imageUrl      = this.replaceWhitespaces(urlmatch[0]); 
				imageUrl      = imageUrl.substr(4,imageUrl.length-5);
				
				hoverUrlsearch= /hoverurl[(]\S*[)]/gi;
				hoverUrlmatch = Ergebnis[i].match(hoverUrlsearch);
				
				
				if(hoverUrlmatch){
					hoverImageUrl  = this.replaceWhitespaces(hoverUrlmatch[0]); 
					hoverImageUrl  = hoverImageUrl.substr(9,hoverImageUrl.length-10);
				}else{
					hoverImageUrl  = false;
				}
				
				fontSizesearch= /font-size:\s?\S*;/gi;
				fontSizematch = Ergebnis[i].match(fontSizesearch);
				
				
				if(fontSizematch){
					cssFontsize  = ""
					cssFontsize  =  fontSizematch[0].split(":");
					cssFontsize  = cssFontsize[1].replace(";","");
				}else{
					cssFontsize  = "";
				}
	
				
				widthsearch   = /w\d*px/gi;
				
				bgimagewidth  = Ergebnis[i].match(widthsearch);
				bgimagewidth  = bgimagewidth[0].substr(1,bgimagewidth[0].length-3);
				
				repeatsearch  = /repeat-\S/gi;
				repeatmatch   = Ergebnis[i].match(repeatsearch);
				
				if(repeatmatch){
					bgrepeat  = repeatmatch[0];
				}else{
					bgrepeat  = true;
				} 
				
				paddingsearch = /padding:[\s\dem.auto]*/gi;
				paddingmatch  = Ergebnis[i].match(paddingsearch);
	
				if(paddingmatch){
					
					layerpadding  = paddingmatch[0];
					layerpadding  = layerpadding.substr(8,layerpadding.length-8);
					layerpadding  = layerpadding.replace(/^\s/,"");
					layerpadding  = layerpadding.replace(/\s$/,"");
					layerpaddingsingle  = layerpadding.split(" ");
					
					for(pcounter=0;layerpaddingsingle.length > pcounter;pcounter++){
						layerpaddingsingle[pcounter] = layerpaddingsingle[pcounter].substr(0,layerpaddingsingle[pcounter].length-2);
						layerpaddingsingle[pcounter] = parseFloat(layerpaddingsingle[pcounter]);
						if(isNaN(layerpaddingsingle[pcounter])){
							layerpaddingsingle[pcounter]=0;
						}
					}
					
					switch(layerpaddingsingle.length){
						case 1:
							widthSubstract = layerpaddingsingle[0]*2;
							break;
						case 2:
							widthSubstract = layerpaddingsingle[1]*2;
							break;
						case 4:
							widthSubstract = layerpaddingsingle[1]+layerpaddingsingle[3];
							break;
					}
	
				}else{
					layerpadding   = "0";
					widthSubstract = 0;
				} 
				
				textalignsearch = /text-align:[\d\s\S]*;/gi;
				textalignmatch  = Ergebnis[i].match(textalignsearch);
				
				if(textalignmatch){
					textalign = "";
					textalign = textalignmatch[0].split(":");
					textalign = textalign[1].replace(";","");
				}else{
					textalign   = "";
				} 
				
				heigthsearch  = /h\d*px/gi;
				bgimageheight = Ergebnis[i].match(heigthsearch);
				bgimageheight = bgimageheight[0].substr(1,bgimageheight[0].length-3);
				
				try{
					if(elementsAreClasses){
						selectedDivArray = this.getElementsByClassName(ename);
						for(counter = 0; counter < selectedDivArray.length; counter++){
							selectedDiv  = selectedDivArray[counter];
							this.drawSSIP(selectedDiv,counter,ename);	
						}
					}else{
						
						selectedDiv  = document.getElementById(ename);
						if(selectedDiv){
							this.drawSSIP(selectedDiv,0,ename,elementsAreClasses);
						}
					}
				}catch(error){}
			}
		}
		
		
		this.drawSSIP = function(selectedDiv,itemNumber,ename){
				
				layerHeight   = selectedDiv.offsetHeight;
				layerWidth    = selectedDiv.offsetWidth;
			
				if(document.getElementById(ename+itemNumber+"_background") == null){
					tempContent = selectedDiv.innerHTML;	
				}else{
					tempContent = document.getElementById(ename+itemNumber+"_content").innerHTML;	
					
				}
	
				selectedDiv.innerHTML = "";
	
				selectedDiv.style.padding = "0";
				selectedDiv.style.textAlign = "left" ;
				selectedDiv.style.fontSize = "1em" ;		
	
				/*var backgroundDiv = document.createElement("div");
				var backgroundDivAttribute = document.createAttribute("id");
				backgroundDivAttribute.nodeValue = ename+itemNumber+'_background';
				backgroundDiv.setAttributeNode(backgroundDivAttribute);*/
				
				//containerDiv = '<div style="position:relative;" id="'+ename+itemNumber+'_container"></div>';
				
				var containerLayer = document.createElement("div");
				containerLayer.setAttribute("id", ename+itemNumber+'_container');
				containerLayer.style.position = "relative";

				backgroundDiv = '<div id="'+ename+itemNumber+'_background" style="height:10em;width:10em;overflow:hidden;position:absolute;left:0;top:0;"></div>';

				
				//alert(selectedDiv.innerHTML);
				//containerLayer = document.getElementById(ename+itemNumber+"_container");
				
				containerLayer.innerHTML = backgroundDiv;
				
				selectedDiv.appendChild(containerLayer);
		
				backgroundLayer = document.getElementById(ename+itemNumber+"_background");
				scaleFaktor = 160/backgroundLayer.offsetHeight;
				oneEm = 16/scaleFaktor;
				
				//backgroundLayer.innerHTML = "adfdasf";
		
				
				bgimagewidthem  = bgimagewidth/16;
				bgimageheightem = bgimageheight/16;
				
				ytimes = parseInt(layerHeight/(bgimageheight/scaleFaktor))+1;
				xtimes = parseInt(layerWidth/(bgimagewidth/scaleFaktor))+1;
	
	
				backgroundLayer.style.width =  "100%" //layerWidth/oneEm+"em";
				backgroundLayer.style.height = layerHeight/oneEm+"em";
				containerLayer.style.height = layerHeight/oneEm+"em";
	
				
				if(hoverImageUrl){
					var mouseOverCode = "onMouseOver=\"mySSIP.replaceImages('"+ename+itemNumber+"','"+imageBase+hoverImageUrl+"');\"";
					var mouseOutCode = "onMouseOut=\"mySSIP.replaceImages('"+ename+itemNumber+"','"+imageBase+imageUrl+"');\"";
				}
							
				
				layerBackground = '<table style="width:'+ bgimagewidthem * (xtimes+1)+'em;" cellspacing="0" cellpadding="0">';
				
				var layerBackgroundContainer = document.createElement("div");
				//layerBackgroundContainer.setAttribute("style", "width:"+ bgimagewidthem * (xtimes+1)+"em;");
				//layerBackgroundContainer.setAttribute("cellspacing", 0);
				//layerBackgroundContainer.setAttribute("cellpadding", 0);
				
				
				//var layerBackground = '';
				for (var y = 0; y < ytimes; y++) {
					layerBackground += '<tr><td>';
					
					for (var x = 0; x < xtimes; x++) {
	
						layerBackground += '<img src="'+imageBase+imageUrl+'" '+mouseOverCode+' '+mouseOutCode+' style="width:'+bgimagewidthem+'em;height:'+bgimageheightem+'em;" name="'+ename+itemNumber+'bgimage" />';
						if(bgrepeat == "repeat-y" || bgrepeat == "repeat-o" ){
							x = xtimes;
						}
					}
					layerBackground += '</td></tr>';
					if(bgrepeat == "repeat-x" || bgrepeat == "repeat-o" ){	
						y = ytimes;
					}
				}
				
				layerBackground += '</table>';
				
				layerBackgroundContainer.innerHTML = layerBackground;
				backgroundLayer.appendChild(layerBackgroundContainer);
	
				contentWidth = (layerWidth/16)*scaleFaktor;
				contentWidth = contentWidth-widthSubstract;
				
				//layerContent = '<div id="'+ename+itemNumber+'_content" '+mouseOverCode+' '+mouseOutCode+'  style="position:absolute;left:0;top:0;padding:'+layerpadding+';'+textalign+'background:transparent;display:block;width:'+contentWidth+'em;'+cssFontsize+'">'+tempContent+'</div>';
				
				var layerContent = document.createElement("div");
				layerContent.setAttribute("id", ename+itemNumber+'_content');
				//layerContent.setAttribute("style", 'position:absolute;left:0;top:0;padding:'+layerpadding+';'+textalign+'background:transparent;display:block;width:'+contentWidth+'em;'+cssFontsize);
				
				layerContent.style.position = "absolute";
				layerContent.style.left = 0;
				layerContent.style.top = 0;
				layerContent.style.padding = layerpadding;
				layerContent.style.background = "transparent";
				layerContent.style.display = "block";
				layerContent.style.width = contentWidth+"em";
				layerContent.style.textAlign = textalign;
				layerContent.style.fontSize = cssFontsize;

				if(hoverImageUrl){
					layerContent.setAttribute("onMouseOver", "mySSIP.replaceImages('"+ename+itemNumber+"','"+imageBase+hoverImageUrl+"');");
					layerContent.setAttribute("onMouseOut","mySSIP.replaceImages('"+ename+itemNumber+"','"+imageBase+imageUrl+"');");
				}
				
				layerContent.innerHTML = tempContent;
				
				//document.getElementById(ename+itemNumber+"_content").innerHTML=tempContent;	
				
				document.getElementById(ename+itemNumber+'_container').appendChild(layerContent);
				
				//backgroundLayer.innerHTML += divContent;
				/*var contentDiv = document.createElement("div");
				var contentDivAttribute = document.createAttribute("id");
				contentDivAttribute.nodeValue = ename+"_content";
				contentDiv.setAttributeNode(contentDivAttribute);
				var cssStyleContent = document.createAttribute("style");
				cssStyleContent.nodeValue = "position:absolute;left:0;top:0;padding:"+layerpadding+";background:transparent;";
				contentDiv.setAttributeNode(cssStyleContent);
				contentDiv.innerHTML = tempContent;
				backgroundLayer.appendChild(contentDiv);*/
				
				return;
		}
	
	
		this.getElementsByClassName = function(classname, node){
			if(!node) node = document.getElementsByTagName("body")[0];
			
			var a = [];
			var re = new RegExp('\\b' + classname + '\\b');
			var els = node.getElementsByTagName("*");
			
			for(var i=0,j=els.length; i<j; i++)
				if(re.test(els[i].className)){
					a.push(els[i]);
				}
			return a;
		}
		
		this.replaceImages = function(elementsName,imageName){
			wichElements = document.getElementsByName(elementsName+"bgimage");
			for (i=0;i<wichElements.length;i++){
				wichElements[i].src = imageName; 
			}
		}
		
		this.replaceWhitespaces = function(theString){
			return theString.replace(/\s/g, "");
		}
		
		
		this.getLinkedCSSStyle = function() {
			var links = document.getElementsByTagName("LINK");

			if (links.length){
				var contents;
				var i, len = links.length;
				for (i=0; i<len; i++){
					
					if (links[i].rel == "stylesheet" && links[i].href){
						if(links[i].href){
							this.execCssRequest(links[i].href);
							
						}
					}
				}
			}
		}
		
		this.execCssRequest = function(url){
			if(this.ssipreadyState){
				this.getCssFileContent(url);
			}else{
				setTimeout("mySSIP.execCssRequest('"+url+"')",2);
			}
		}
		
		this.getFile = function(pURL){
			if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
				xmlhttp=new XMLHttpRequest();
				eval('xmlhttp.onreadystatechange='+pFunc+';');
				xmlhttp.open("GET", pURL, true); // leave true for Gecko
				xmlhttp.send(null);
			} else if (window.ActiveXObject) { //IE 
				xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
				if (xmlhttp) {
					xmlhttp.open('GET', pURL, false);
					xmlhttp.send();
				}
			}
		
		}
		
		
		this.properSourceForIE = function(){
			replaceArea = document.getElementsByTagName("body")[0];
			replaceArea = this.so_getText(replaceArea);
			replaceArea = replaceArea.replace(/<P /g, "<DIV ");
			replaceArea = replaceArea.replace(/<\/P>/g, "</DIV>");
			document.getElementsByTagName("body")[0].innerHTML = replaceArea;
		}
		
		this.so_getText = function (obj) {
			// return the data of obj if its a text node
				if (obj.nodeType == 3) return obj.nodeValue;
				var txt = new Array(),i=0;
				// loop over the children of obj and recursively pass them back to this function
			   while(obj.childNodes[i]) {
					txt[txt.length] = so_getText(obj.childNodes[i]);
					i++;
				}
				// return the array as a string
				return txt.join("");
		}

	
		// IE Resize Hack //
		this.lastResize = function() {
		   this.updateSSIP();
		   window.setTimeout("window.clearTimeout(mySSIP.resizeIE)", 100);
		}
		
	}
	
	mySSIP = new SSIP();
	
	initMySSIP = function(){
		mySSIP.initSSIP();
		initFuntools();
		mySSIPFl.loadFunlibSettings();
	}	
	
	if ( document.addEventListener ){ // for Firefox
	   window.addEventListener("load" , initMySSIP, false);
	   window.addEventListener("resize" , mySSIP.updateSSIP, false);
	} else if (document.attachEvent){ // IE hack
	   window.attachEvent("onload" , initMySSIP);
	   window.attachEvent("onresize" , function () {
	   window.clearTimeout(mySSIP.resizeIE);
	   mySSIP.resizeIE= window.setTimeout("mySSIP.lastResize()", 200)});
	}  	
	


