var Behaviour = {
	list : new Array,

	register : function(sheet){
		Behaviour.list.push(sheet);
	},

	start : function(){
		Behaviour.addLoadEvent(function(){
			Behaviour.apply();
		});
	},

	apply : function(){
		for (h=0;sheet=Behaviour.list[h];h++){
			for (selector in sheet){
				list = document.getElementsBySelector(selector);

				if (!list){
					continue;
				}

				for (i=0;element=list[i];i++){
					sheet[selector](element);
				}
			}
		}
	},

	addLoadEvent : function(func){
		var oldonload = window.onload;

		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
}

Behaviour.start();

/*
   The following code is Copyright (C) Simon Willison 2004.

   document.getElementsBySelector(selector)
   - returns an array of element objects from the current document
     matching the CSS selector. Selectors can contain element names,
     class names and ids and can be nested. For example:

       elements = document.getElementsBySelect('div#main p a.external')

     Will return an array of all 'a' elements with 'external' in their
     class attribute that are contained inside 'p' elements that are
     contained inside the 'div' element which has id="main"

   New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
   See http://www.w3.org/TR/css3-selectors/#attribute-selectors

   Version 0.4 - Simon Willison, March 25th 2003
   -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
   -- Opera 7 fails
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }

    if (!currentContext[0]){
    	return;
    }

    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

/* That revolting regular expression explained
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute
   Tag
*/

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}




var Engine = {

	setStatus: function(message){
		if($('status') != null){
			$('status').parentNode.removeChild($('status'));
			$('overlay').parentNode.removeChild($('overlay'));
		}
		var body = document.getElementsByTagName("body")[0];
		var overlay = document.createElement("div");
		overlay.id = 'overlay';
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		overlay.style.height = (arrayPageSize[1] + 'px');
		overlay.style.width = (arrayPageSize[0] + 'px');
		overlay.style.display = 'block';
		body.appendChild(overlay);
		overlay.innerHTML = "";

		var div = document.createElement("div");

		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20) / 2);

		div.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		div.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		div.style.position = "absolute";
		div.style.top = "50%";
		div.style.left = "50%";
		div.style.width = "300px";
		div.style.margin = "-12px 0 0 -150px";
		div.style.padding = "20px";
		div.style.opacity = "0.85";
		div.style.backgroundColor = "#940000";
		div.style.border = "1px #FFFFFF solid";
		div.style.color = "#FCFFFF";
		div.style.fontSize = "25px";
		div.style.textAlign = "center";
		div.id = 'status';
		body.appendChild(div);
		div.innerHTML = message;
	},

	setValidation: function(message){
		if($('status') != null){
			$('status').parentNode.removeChild($('status'));
			$('overlay').parentNode.removeChild($('overlay'));
		}
		var body = document.getElementsByTagName("body")[0];
		var overlay = document.createElement("div");
		overlay.id = 'overlay';
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		overlay.style.height = (arrayPageSize[1] + 'px');
		overlay.style.width = (arrayPageSize[0] + 'px');
		overlay.style.display = 'block';
		body.appendChild(overlay);
		overlay.innerHTML = "";

		var div = document.createElement("div");

		div.style.position = "absolute";
		div.style.width = "340px";
		div.style.margin = "-100px 0 0 -170px";
		div.style.padding = "20px";
		div.style.opacity = "0.85";
		div.style.backgroundColor = "#FFFFFF";
		div.style.border = "5px #972323 solid";
		div.style.color = "#333333";
		div.style.fontSize = "12px";
		div.style.textAlign = "left";
		div.id = 'status';
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 400) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 150) / 2);

		div.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		div.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		body.appendChild(div);
		div.innerHTML = message;
	},

	setMessage: function (message){
		var btn = '<a id="closebtn" class="button" href="#step1" onclick="Engine.hideStatus();$(' + "'" + 'cntry' + "'" + ').style.display = ' + "'" + 'inline' + "'" + ';$(' + "'" + 'dobc' + "'" + ').style.display = ' + "'" + 'inline' + "'" + ';$(' + "'" + 'doba' + "'" + ').style.display = ' + "'" + 'inline' + "'" + ';$(' + "'" + 'dobb' + "'" + ').style.display = ' + "'" + 'inline'+"'"+';" title="Fenster schließen und fortfahren">X Schließen</a>';
		Engine.setValidation(message + btn);

		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 000) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 150) / 2);

		$('status').style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		$('status').style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	},



	hideStatus: function(){
		$('status').style.display = 'none';
		$('overlay').style.display = 'none';
		//window.onscroll = function (){};
	},

	showMessage: function(message){
		Engine.setStatus(message);
		setTimeout("Engine.hideStatus()",10000);
	}
}




function nextStep(element1,element2){
	Engine.setStatus('<img src="/images/ajax_ind/indicator_medium.gif" alt="Lade" /><br/><small style=\'font-size:12px;\'>Bitte haben Sie einen Moment gedult!</small>');
	$(element1).style.display = "none";
	setTimeout('function bla(){return false};',1000);
	Engine.hideStatus();
	$(element2).style.display = 'block';
	if($(element2)){
		$(element2).style.display = 'block';
		 new Effect.Opacity($(element2),{duration: 0.5, from: 0.0, to: 1.0});
	}else{
		alert("cant find e2");
	}


}

function showhide(id){
	idname = "ph" + id;
	el = $(idname);
	if(el.style.display == 'block')
	{
	el.style.display = 'none';
	}else
	{
	el.style.display = 'block';
	}
}
  function showhideplus(id){
    var imgid = document.getElementById("img" + id);
    var containerid = document.getElementById("container" + id);
    if(containerid.style.display == "none"){
        containerid.style.display = "block";
        imgid.src = "/images/icons/minus.gif"
    }else{
        containerid.style.display = "none";
        imgid.src = "/images/icons/plus.gif"
    }
  }
function CheckFor (expr,el) {
element = document.getElementById(el);

  for(i = 0; i < document.getElementById(el).length; ++i){
    if (document.getElementById(el).options[i].value == expr){
      document.getElementById(el).options[i].selected = true;
    }
  }
}

function Openwin(Adresse,Title) {
  Fenster1 = window.open(Adresse, Title, "width=300,height=400,left=100,top=200,resizable=yes,scrollbars=yes,toolbar=no,dependent=yes;");
  Fenster1.focus();
}


var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
if(window.Ajax){
Ajax.InPlaceEditorWithEmptyText = Class.create(Ajax.InPlaceEditor, {

  initialize : function($super, element, url, options) {

    if (!options.emptyText)        options.emptyText      = "keine Beschriftung - zum editieren klicken!";
    if (!options.emptyClassName)   options.emptyClassName = "inplaceeditor-empty";

    $super(element, url, options);

    this.checkEmpty();
  },

  checkEmpty : function() {

    if (this.element.innerHTML.length == 0 && this.options.emptyText) {

      this.element.appendChild(
          new Element("span", { className : this.options.emptyClassName }).update(this.options.emptyText)
        );
    }

  },

  getText : function($super) {

    if (empty_span = this.element.select("." + this.options.emptyClassName).first()) {
      empty_span.remove();
    }

    return $super();

  },

  onComplete : function($super, transport) {

    this.checkEmpty();
    return $super(transport);

  }

});
}

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}


