//adds helpers for intput fields: http://github.com/madrobby/prototype_helpers/blob/ef94cadb27699fe947a900e71e6bf229273b1a49/defaultValueActsAsHint/defaultvalueactsashint.js
(function(){
  var methods = {
    defaultValueActsAsHint: function(element){
      element = $(element);
      element._default = element.value;
      
      return element.observe('focus', function(){
        if(element._default != element.value) return;
        element.removeClassName('hint').value = '';
      }).observe('blur', function(){
        if(element.value.strip() != '') return;
        element.addClassName('hint').value = element._default;
      }).addClassName('hint');
    }
  };
   
  $w('input textarea').each(function(tag){ Element.addMethods(tag, methods) });
})();

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


var horizontal_offset="9px" //horizontal offset of hint box from anchor link
/////No further editting needed
var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
//new Effect.Appear('hintbox');
obj.onmouseout=hidetip
}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
//new Effect.Fade('hintbox');
dropmenuobj.style.left="-500px"
}

function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox


//USE Like this:
//<div id="output" style="padding:1em; background:#BECDD4;display:none;"></div>
//
//
//			<div id="lists" style="display:none">
//				<ul id="list_ul_text" class="list">
//					<li class="listitem">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</li>
//					<li class="listitem">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</li>
//					<li class="listitem">It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged</li>
//				</ul>
//			</div>
var Ticker = Class.create();
Ticker.prototype = {
    messages: new Array(),
    counter: 0, interval: 0,
    target: null, source: null,
    initialize: function(target, source, options) 
    {
        this.target = $(target); 
		this.source = $(source);
        this.options = Object.extend({
		updateRate: 2, 
		duration: 0.5,
		beforeStart:function(){ 
			this.counter++;
		}.bind(this) 
	}, options || {});

        Element.cleanWhitespace(this.source);
        $A($(this.source).childNodes).each(function(sel) {
           this.messages.push(sel.innerHTML.strip());
        }.bind(this));  

	this.start();
    },
    start: function()
    {
	this.interval = new PeriodicalExecuter(function() {
		this.target.update(' ').appendChild(Builder.node('span',{style:'opacity:0'}, this.messages[this.counter])); 
		new Effect.Appear(this.target.lastChild, this.options);
		if(this.counter == this.messages.length){ this.counter = 0;}
	}.bind(this), this.options.updateRate);
    },
    stop: function()
    {
	this.interval.stop();
    }
};
Event.observe(window, 'load', function() {
	//ticker = new Ticker('output','list_ul_text');
});



function insertText(a_oEl, a_sText) {
	a_oEl.focus();
	if (window.document.all) {
		 oSelection      = window.document.selection.createRange();
		 oSelection.text = a_sText;
	} else if (navigator.appName == "Netscape") {
		 sOldValue = new String(a_oEl.value);
		 iStart    = a_oEl.selectionStart;
		 iEnd      = a_oEl.selectionEnd;
		 sNewValue = sOldValue.substring(0, iStart) + a_sText + sOldValue.substring(iEnd, sOldValue.length);

		 a_oEl.value = sNewValue;
	} else {
		 a_oEl.value += a_sText;
	}
}

function add_2_tags( tag ) {
	var tags = $('tags').value;
	var tag_arr = tags.split(' ');
	var newtags;
	var deleted = false;

	for(i=0;i<tag_arr.length;i++) {
		if(tag_arr[i] != "") {
			if( tag_arr[i] != tag ) {
				if( newtags )
					newtags = newtags+" "+tag_arr[i];
				else
					newtags = tag_arr[i];
			} else
				deleted = true;
		}
	}

	if( deleted == false )
		newtags = ( newtags ? newtags+" " : "" )+tag;

	if (typeof( newtags ) == 'undefined')
		newtags = '';
		$('tags').value = newtags;
}


// use like this:  onclick="toggle_element( 'options' )"
//refers to: <div id="options" style="display: none">
function toggle_element( id, type ) {
	if (!$(id)) return;
	
	if( !type )
		//$(id).style.display = ($(id).style.display == 'none')?'':'none';
		if ($(id).style.display == 'none')	{
			new Effect.BlindDown(id, {duration:0.5});
		}	else	{
			new Effect.BlindUp(id, {duration:0.5});
		}
	else 
		$(id).style.display = type;
	
}

function appear_element( id, type ) {
	if (!$(id)) return;
	
	if( !type )
		//$(id).style.display = ($(id).style.display == 'none')?'':'none';
		if ($(id).style.display == 'none')	{
			new Effect.Appear(id, {duration:0.5});
		}	else	{
			new Effect.Fade(id, {duration:0.5});
		}
	else 
		$(id).style.display = type;
	
}

function bbInsertTag (textArea, startTag, endTag) {
	textArea.focus ();
	if (typeof endTag == 'undefined')
 	endTag = '';

 	if (typeof document.selection != 'undefined') {
		var range = document.selection.createRange ();
  		var selectedText = range.text;

 		range.text = startTag + selectedText + endTag;
 		range = document.selection.createRange ();

 		if (selectedText.length == 0)
 			range.move ('character', - endTag.length);
 		else
 			range.moveStart ('character',
 		startTag.length + selectedText.length + endTag.length);
		range.select();
 	} else if (typeof textArea.selectionStart != 'undefined') {
		var start = textArea.selectionStart;
 		var end = textArea.selectionEnd;
 		var selectedText = textArea.value.substring (start, end);

		 textArea.value = textArea.value.substr (0, start)
		 + startTag + selectedText + endTag + textArea.value.substr (end);

 		var cursorPos;

 		if (selectedText.length == 0)
 			cursorPos = start + startTag.length;
 		else
 			cursorPos = start + startTag.length + selectedText.length
 			+ endTag.length;

 		textArea.selectionStart = cursorPos;
 		textArea.selectionEnd = cursorPos;
 	} else {
 		var insertPos = textArea.value.length;
 		var insertText = prompt ("Zu formatierenden Text eingeben:");
 		textArea.value = textArea.value.substr (0, insertPos) + startTag
 		+ insertText + endTag + textArea.value.substr (insertPos);
	}
}

function adjust(sender, min) {
  var text = sender.value.replace(/(\015\012)|(\015)|(\012)/g, '\n');
  var text_arr = text.split('\n');
  sender.rows = (text_arr.length > min) ? text_arr.length : min;
}

function showLightbox(url, params, title) {
	$('header').scrollTo();
	new Ajax.Request(url, {  
		method: "get",
		parameters: params,
		evalJS: true,
		onSuccess: function(t) {
			var div = document.createElement("div");
			div.setAttribute("id","lighboxcontainer");
			var bg = document.createElement("div");
			bg.setAttribute("id","fullbg");
			var content = document.createElement("div");
			content.setAttribute("id","lcontent");
			var box = document.createElement("div");
			box.setAttribute("id","lightbox");
			//box.setAttribute("class","loginbox");
			box.innerHTML += '<a href="javascript:void(0);" class="closelink"  onclick="removeLighbox()"><img src="images/icons/cancel.png" border="0" /></a>';
			if (title != undefined) {
				box.innerHTML += '<h4>'+ title +'</h4>';
			}
			content.innerHTML +=  t.responseText;
			box.appendChild(content)
			div.appendChild(box);
			div.appendChild(bg);
			document.body.appendChild(div);
		}
	});
}
function removeLighbox() {
	if ($('lightbox')) $('lightbox').hide();
	if ($('fullbg')) $('fullbg').hide();
	if ($('lighboxcontainer')) document.body.removeChild($('lighboxcontainer'));
}