var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
		return false;
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		var returnValue;
		returnValue =  parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		if (returnValue>0)
			return returnValue;
		else 
			return false;
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var Popup = {
                open: function(options)
                {
                  this.options = {
                    url: '#',
                    width: 600,
                    height: 500,
                    name:'_blank',
                    location:'no',
                    menubar:'no',
                    toolbar:'no',
                    status:'yes',
                    scrollbars:'yes',
                    resizable:'yes',
                    left:'',
                    top:'',
                    normal:false
                  }
                  Object.extend(this.options, options || {});
              
                  if (this.options.normal){
                      this.options.menubar = 'yes';
                      this.options.status = 'yes';
                      this.options.toolbar = 'yes';
                      this.options.location = 'yes';
                  }
              
                  this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
                  this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
                  var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
                  if (this.options.top!='')openoptions+=',top='+this.options.top;
                  if (this.options.left!='')openoptions+=',left='+this.options.left;
                  window.open(this.options.url, this.options.name,openoptions );
                  return false;
                }
              };

function navInit(){
	    	// einmalige Positionierung der Hovers
	      	var header=$('headerTop');
	      	header.getElements('a').each(function(eE){
	        	var eHover=$('navHover');
	        	var xpos=eE.getPosition()['x']-eHover.getPosition()['x']+123;
	        	var ypos=eE.getPosition()['y']-eHover.getPosition()['y'];
	        	eHover.setStyles({'left':xpos,'top':ypos});
	  		});
	    	// Hover-Effekt
	      	var hover=$('navHover');
	      	hover.fx=new Fx.Style(hover.getElement('img'),'width',{'duration':150});
	      	hover.fx.set(0);
	      	header.getElements('.navItem').each(function(eI){
	        	if(eI.className.indexOf('active')==-1){
	          		eI.addEvent('mouseover', function(){
	            		var xpos=eI.getPosition()['x']+eI.getCoordinates()['width']/2-38.5;
	            		hover.setStyles({'visibility':'visible','left':xpos,'top':99});
	            		hover.fx.stop();
	            		hover.fx.start(77);
	          		});
	          		eI.addEvent('mouseleave', function(){
	            		hover.fx.stop();
	            		hover.fx.start(0);
	          		});
	        	}
	      	});
		}

function displayOldSavedContent(id)
{
	if ($(id+'_saved') && !$(id+'_saved').empty())
	{
		$(id).update($(id+'_saved').innerHTML);
	}
}

function replaceContentWithIndictator(id, saveOldContent)
{
	if (id && $(id))
	{
		if (saveOldContent)
		{
			var saveOldContent_div = new Element('div', 
				{'id': id+'_saved', 'class': $(id).readAttribute('class')}
			).setStyle(
				{
					position: "absolute", 
					left: "-10000px",
					top: "-10000px" 
				}
			);
			$(id).parentNode.appendChild(saveOldContent_div, { position: before });
			$(saveOldContent_div).update($(id).innerHTML);
		}
		$(id).update('<div style="width: 100%; height: 100%; margin: 0 auto; vertical-align: middle; text-align: center;" align="center"><img src="/images/loadingd_tp.gif" align="center" style="text-align: center; vertical-align: middle; margin: 0px auto;" width="50" height="57"/></div>');
	}
	return false;
}

function checkGE(id){if(typeof(id)=='undefined'){alert('Tried to get an undefined element!');return null;}
var obj;if(typeof(id)=='string'){obj=document.getElementById(id);if(!(BrowserDetect.version>=7 && BrowserDetect.browser.subString=="Explorer")){return obj;}
if(!obj){return null;}else if(typeof(obj.id)=='string'&&obj.id==id){return obj;}else{var candidates=document.getElementsByName(id);if(!candidates||!candidates.length){return null;}
var maybe=[];for(var ii=0;ii<candidates.length;ii++){var c=candidates[ii];if(!c.id&&id){continue;}
if(typeof(c.id)=='string'&&c.id!=id){continue;}
maybe.push(candidates[ii]);}
if(maybe.length!=1){Util.error('checkGE() did go wrong.');return null;}
return maybe[0];}}else{return id;}
return null;}

function nl2br_12(str) {
	if(typeof(str)=="string") return str.replace(/(\r\n)|(\n\r)|\r|\n/g,'<br/>&nbsp;');
	else return str;
}

function countLines(strtocount, cols) {
    var hard_lines = 1;
    var last = 0;
    while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
    }
    var soft_lines = Math.round(strtocount.length / (cols-1));
    var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
    if ( hard ) soft_lines = hard_lines;
    return soft_lines;
}
function cleanForm(id) {
    alert(id);
    if( typeof $(id).rows != "number" ) return false;
    $(id).rows = countLines($(id).value, $(id).cols) +1;
}

function cleanDefaultTextInInput(id, defaultText)
{
	searchDefaultVal = new RegExp(defaultText,'ig');
	if (searchDefaultVal.test($(id).value))
	{
		$(id).value = '';
	}
	$(id).style.color = '#000000';
}

function resize_textarea(objId)
{
	var padding=15;
	var shadow_div_id='shadow_'+objId;
	var shadow_div;
	
	if (!$(objId).visible())
		return;
	
	if(!(shadow_div=checkGE(shadow_div_id)))
	{
		var shadow_div = new Element('div', 
				{'id': shadow_div_id, 'class': $(objId).readAttribute('class')}
		).setStyle(
			{
				position: "absolute", 
				left: "-10000px", 
				top: "-10000px", 
				'font-size': $(objId).getStyle('font-size')+'px', 
				width: parseInt($(objId).clientWidth-16)+'px',
				'line-height': $(objId).getStyle('line-height')+'px'
			}
		);

		$(objId).writeAttribute('startHeight', $(objId).clientHeight);
		$(objId).parentNode.appendChild(shadow_div);
	}
	var clientHeight	= $(objId).clientHeight;
	// shadow_div.update(htmlspecialchars($(objId).getValue()).replace(/[\n]/g,'<br />&nbsp;'));	
	shadow_div.update(nl2br_12($(objId).getValue()));
	var shadowHeight	= shadow_div.getHeight();
	var to_height;
	var startHeight		= $(objId).readAttribute('startHeight');	
	
	if(shadowHeight<startHeight)
	{
		to_height=startHeight;
	}
	else
	{
		to_height=shadowHeight+padding;
	}
	if(to_height&&to_height!=clientHeight)
	{
		$(objId).setStyle({ height: to_height+'px' });		
	}
}

/*
Ajax.Responders.register({
    onComplete: function() {
       bindTextareaResize();
    }
 });
*/

function onloadFunctions()
{
	// navInit(); // kann auch als onLoad-Event ausgelagert werden
}
 
function htmlspecialchars(str,typ) {
	if(typeof str=="undefined") str="";
	if(typeof typ!="number") typ=2;
	typ=Math.max(0,Math.min(3,parseInt(typ)));
	var from=new Array(/&/g,/</g,/>/g);
	var to=new Array("&amp;","&lt;","&gt;");
	if(typ==1 || typ==3) {from.push(/'/g); to.push("&#039;");}
	if(typ==2 || typ==3) {from.push(/"/g); to.push("&quot;");}
	for(var i in from) str=str.replace(from[i],to[i]);
	return str;
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}


function GetRandom( min, max ) {

	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}
var clip = null;
function copyToClipboard(copyToClipboardElement, copyToClipBoadText)
{
	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	ZeroClipboard.setMoviePath( '/images/ZeroClipboard.swf' );
	clip.setHandCursor( true );
	clip.setCSSEffects( true );

	if ($(copyToClipboardElement))
	{
		// assign a common mouseover function for all elements using jQuery
		$(copyToClipboardElement).observe('mouseover', function() {
			// set the clip text to our innerHTML
			clip.setText( copyToClipBoadText );
	
			// reposition the movie over our element
			// or create it if this is the first time
			if (clip.div) {
				clip.receiveEvent('mouseout', null);
				clip.reposition(this);
			}
			else clip.glue(this);
	
			// gotta force these events due to the Flash movie
			// moving all around.  This insures the CSS effects
			// are properly updated.
			clip.addEventListener( 'mouseOut', function(client) { 
				clip.hide();
             });
             clip.addEventListener( 'mouseOver', function(client) { 
				clip.show();
             });
			clip.receiveEvent('mouseover', null);
		} );
	}
	return false;
}

function LightboxShow(linkBox, imageNumber) {
   links = jQuery("#"+linkBox+" a");
   if (links)
   {
   		if(!imageNumber)
   		{
   			imageNumber = 0;
   		}
	   images = new Array();
	   jQuery.each(links, function() {
	   	  image = new Array(this.href, this.title);
	      images.push(image);
	    });
	   jQuery.slimbox(images, imageNumber, {loop: true});
   }
}

document.observe('dom:loaded', function() {

		jQuery.ajax({
			  type: "GET",
			  url: keepMemberOnlineURL
			});
	
		$$("textarea").each(function(ef){
			var keepMemberOnlineObserver = new Form.Element.Observer( $(ef), 1.0, keepMemberOnline);
		});
	
		$$("form").each(function(ef){
	    	ef.observe('submit', function(event){
	
				var submitTags = this.getInputs('submit');
				var submitImageTags = this.getInputs('input[type="image"]');
				var submitButtonTags = this.getInputs('input[type="button"]');
	
				if (submitTags)
				{
					submitTags.each(function(efSubmit){
						var submitHiddenTag = new Element('input',{type: 'hidden', id: efSubmit.id, "name": efSubmit.name, value: efSubmit.value, style: 'display: none'});
						efSubmit.insert({after: submitHiddenTag});
						efSubmit.disable();
					});
				}
	
				if (submitImageTags)
				{
					submitImageTags.each(function(efSubmitImageTag){
						var efSubmitHiddenImageTag = new Element('input',{type: 'hidden', id: efSubmitImageTag.id, "name": efSubmitImageTag.name, value: efSubmitImageTag.value, style: 'display: none'});
						efSubmitImageTag.insert({after: efSubmitHiddenImageTag});
						efSubmitImageTag.disable();
					});
				}
	
				if (submitButtonTags)
				{
					submitButtonTags.each(function(efSubmitButtonTag){
						var efSubmitHiddenButtonTag = new Element('input',{type: 'hidden', id: efSubmitButtonTag.id, "name": efSubmitButtonTag.name, value: efSubmitButtonTag.value, style: 'display: none'});
						efSubmitButtonTag.insert({after: efSubmitHiddenButtonTag});
						efSubmitButtonTag.disable();
					});
				}
				
				return false;
	    	});
		});
	
});


function resizePopup()
{
   newWidth  = $('content').getWidth();
   newHeight = $('content').getHeight();
   window.resizeTo(newWidth,newHeight+50);
}

function getRefToDivMod( divID, oDoc ) {
	  if( !oDoc ) { oDoc = document; }
	  if( document.layers ) {
	    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
	      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
	        y = getRefToDivMod(divID,oDoc.layers[x].document); }
	      return y; } }
	  if( document.getElementById ) { return oDoc.getElementById(divID); }
	  if( document.all ) { return oDoc.all[divID]; }
	  return document[divID];
	}

function openPerfectPopup(oW,oTitle,oContent) {
	  var x = window.open('','windowName','width=500,height=400,resizable=1');
	  if( !x ) { return true; }
	  x.document.open();
	  x.document.write('<html><head><title>'+oTitle+'<\/title><\/head><body>'+
	    (document.layers?('<layer left="0" top="0" width="'+oW+'" id="myID">')
	      :('<div style="position:absolute;left:0px;top:0px;display:table;width:'+oW+'px;" '+
	      'id="myID">'))+
	    oContent+(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
	  x.document.close();
	  var oH = getRefToDivMod( 'myID', x.document ); if( !oH ) { return false; }
	  var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
	  x.resizeTo( oW + 200, oH + 200 );
	  var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
	  if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
	  else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
	  else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
	  if( window.opera && !document.childNodes ) { myW += 16; }
	  x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) );
	  if( x.focus ) { x.focus(); }
	  return false;
	}