		
	
	/***************************************************************
	*  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!
	***************************************************************/
	
	var resizeIE;
	var imageBase;
	var readyState = true;
	var cssCacheArray = new Array();
	
	function initSSIP(){
		if (navigator.appName.indexOf("Explorer") != -1){
			//properSourceForIE();
		}
		
		getLinkedCSSStyle();

		try{
			initFuntools();	
			loadFunlibSettings();
		}catch(e){}
	
		//document.getElementById("dumpcontent").innerHTML = document.getElementsByTagName("body")[0].innerHTML;
	}
	
	function updateSSIP(){
		
		for(var h = 0; h<cssCacheArray.length;h++){
			imageBase = cssCacheArray[h]["imageBase"];
			getSSIPConfig(cssCacheArray[h]["cssCode"]);
		}
		
	}

    function getCssFileContent(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 = dumpCssContents;
			http_request.open('POST', url, true);
			http_request.send(null);
		}
    }

    function dumpCssContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
					newCssFile = new Object();
					newCssFile["imageBase"] = imageBase;
					newCssFile["cssCode"]   = http_request.responseText;
					cssCacheArray.push(newCssFile);
                 	getSSIPConfig(http_request.responseText);	
					readyState = true;
				 
            } else {
                alert ='Bei dem Request ist ein Problem aufgetreten.';
            }
        }else{
			readyState = false;
		}
    }
	

	
	function getSSIPConfig(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){
				 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){
				parseCssConfig(Ergebnis,"class");

			}
			
			
		}else{
			return;
		}
	}

	function parseCssConfig(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 = replaceWhitespaces(ename);

			urlsearch     = /url[(]\S*[)]/gi;
			urlmatch      = Ergebnis[i].match(urlsearch);
			imageUrl      = replaceWhitespaces(urlmatch[0]); 
			imageUrl      = imageUrl.substr(4,imageUrl.length-5);
			
			hoverUrlsearch= /hoverurl[(]\S*[)]/gi;
			hoverUrlmatch = Ergebnis[i].match(hoverUrlsearch);
			
			
			if(hoverUrlmatch){
				hoverImageUrl  = 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  =  fontSizematch[0];
			}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 = textalignmatch[0];
			}else{
				textalign   = "";
			} 
			
			heigthsearch  = /h\d*px/gi;
			bgimageheight = Ergebnis[i].match(heigthsearch);
			bgimageheight = bgimageheight[0].substr(1,bgimageheight[0].length-3);
			
			if(elementsAreClasses){
				selectedDivArray = getElementsByClassName(ename);
				for(counter = 0; counter < selectedDivArray.length; counter++){
					selectedDiv  = selectedDivArray[counter];
					drawSSIP(selectedDiv,counter,ename);			
				}
			}else{
				
				selectedDiv  = document.getElementById(ename);
				if(selectedDiv){
					drawSSIP(selectedDiv,0,ename,elementsAreClasses);
				}
			}
		}
	}
	
	
	function drawSSIP(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>';
			backgroundDiv = '<div id="'+ename+itemNumber+'_background" style="height:10em;width:10em;overflow:hidden;position:absolute;left:0;top:0;"></div>';
			
			selectedDiv.innerHTML = containerDiv;
			containerLayer = document.getElementById(ename+itemNumber+"_container");
			
			containerLayer.innerHTML = backgroundDiv;

	
			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=\"replaceImages('"+ename+itemNumber+"','"+imageBase+hoverImageUrl+"');\"";
				var mouseOutCode = "onMouseOut=\"replaceImages('"+ename+itemNumber+"','"+imageBase+imageUrl+"');\"";
			}
						
			
			layerBackground = '<table style="width:'+ bgimagewidthem * (xtimes+1)+'em;" cellspacing="0" cellpadding="0">';
			
			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>';
			
			backgroundLayer.innerHTML = layerBackground;

			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>';
			
			containerLayer.innerHTML += 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);*/
			document.getElementById(ename+itemNumber+"_content").innerHTML=tempContent;	
			return;
	}


	function getElementsByClassName(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;
	}
	
	function replaceImages(elementsName,imageName){
		wichElements = document.getElementsByName(elementsName+"bgimage");
		for (i=0;i<wichElements.length;i++){
			wichElements[i].src = imageName; 
		}
	}
	
	function replaceWhitespaces(theString){
		return theString.replace(/\s/g, "");
	}
	
	
	function getLinkedCSSStyle (){
		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){
						execCssRequest(links[i].href);
						
					}
				}
			}
		}
	}
	
	function execCssRequest(url){
		if(readyState){
			
			getCssFileContent(url);
		}else{
			setTimeout("execCssRequest('"+url+"')",2);
		}
	}
	
	function getFile(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();
			}
		}
	
	}
	
	
	function properSourceForIE(){
		replaceArea = document.getElementsByTagName("body")[0].innerHTML;
		replaceArea = replaceArea.replace(/<P/g, "<DIV ");
		replaceArea = replaceArea.replace(/<\/P>/g, "</DIV>");
		document.getElementsByTagName("body")[0].innerHTML = replaceArea;
	}
	
	

	// IE Resize Hack //

		if ( document.addEventListener ){ // for Firefox
		   window.addEventListener("load" , initSSIP, false);
		   window.addEventListener("resize" , updateSSIP, false);
		} else if (document.attachEvent){ // IE hack
		   window.attachEvent("onload" , initSSIP);
		   window.attachEvent("onresize" , function () {
		   window.clearTimeout(resizeIE);
		   resizeIE= window.setTimeout("lastResize()", 200)});
		}  	

	
	function lastResize() {
	   updateSSIP();
	   window.setTimeout("window.clearTimeout(resizeIE)", 100);
	}
	
	function loadFunlibSettings(){
		
		ssipCrumbs = getSsipCrumbs();
		
		if(!ssipCrumbs){
			return;	
		}else{
			for(c=0;c<ssipCrumbs.length;c++){
				document.getElementById(ssipCrumbs[c]["name"]).style.fontSize = ssipCrumbs[c]["value"]/100 + "em";
			}
		}
	}
