/*
	--------------------------------
	SITE-SPEZIFISCHE Funktionen
	--------------------------------
*/

//Löst die Lightbox aus
	$(function() {$('#maincontent a:has(img)').lightBox();});
	

/* Versieht die Linkbox mit einem Fade-Effect*/	
	$(".linkbox:has(a)").bind("mouseenter", function(e){
        $(this).css('cursor','pointer').fadeTo(300, 0.6);
    });
	$(".linkbox:has(a)").bind("click", function(e){
		var hRef = $(this).children().children().attr("href");
		document.location.href=hRef;
    });
	$(".linkbox:has(a)").bind("mouseleave", function(e){
        $(this).fadeTo(300, 1);
    });
	
// Versieht die verlinkten Bilder mit einem Fade-Effect	
	$("a img").bind("mouseenter", function(e){
        $(this).fadeTo(300, 0.6);
    });
	$("a img").bind("mouseleave", function(e){
        $(this).fadeTo(300, 1);
    });
	
	
/*
	--------------------------------
	Formularprüfung
	--------------------------------
*/
function checkInput(f) {
 for(var i = 0; i < f.length; i++){
     var e = f.elements[i];
     if (e.nn == true && !checkblank(e)) return false;
     if (e.mail == true && !checkmail(e)) return false;
  } // for 
  return true;
}

/*
 	Überprüft ob ein Pflichtfeld leer ist
*/
function checkblank(e){
  if (isblank(e))  {
  showError(e,e.t+" ist ein Pflichtfeld und darf nicht leer sein.");
  return false;
  }
  else{
	return true;
  }
}

function checkmail(e){
  if (ismail(e))  {
	return true;
  }
  else{
	showError(e,"Das ist keine g"+unescape("%FC")+"ltige E-Mailadresse.");
    return false;
  }
}

function isblank(e){
  if (e.value == null || e.value == "" || e.value == "-")
    return true;

  for(var i = 0; i < e.value.length; i++)  {
     var c = e.value.charAt(i);
     if ((c != ' ') &&
         (c != '\n') &&
         (c != '\t'))
        return false;
  }
  return true;
}

function ismail(e){
	var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
	var myrxp = new RegExp(regex);
	var check = (myrxp.test(e.value));
	if (check == false) return false;
	else return true;
}
	
var showError = function(e, txt){
	var o_color = $("p:has(input[name='"+e.name+"'])").css("color");
	var api = $("p:has(input[name='"+e.name+"'])").expose({api: true});
	$("p:has(input[name='"+e.name+"'])").append("<span class='tooltip'>"+txt+"</span>").show(); 
	$("p:has(input[name='"+e.name+"'])").css("color","#eee");
    api.onClose(function() { 
		$("p:has(input[name='"+e.name+"'])").css("color",o_color); 
		$("span.tooltip").remove(); 
    }); 
	$("input[name='"+e.name+"']").bind("focus", function(){api.close()});
	api.load(); 
}

/*
	---------------------------------------------------------------
 	Open Link in New Window or Tab
	---------------------------------------------------------------
/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
addEvent = function( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

removeEvent = function( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

/*
	Create the new window
*/
openInNewWindow = function(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/*
	Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
getNewWindowLinks = function() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		var strNewWindowAlert = "";
		// Find all links
		var links = document.getElementsByTagName('a');
		var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnuwindow\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				objWarningText = document.createElement("em");
				objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

addEvent(window, 'load', getNewWindowLinks);

/*
	--------------------------------------------------------------------------
	$Id: spamspan.js 5 2007-09-29 15:56:26Z moltar $
	--------------------------------------------------------------------------
	Version: 1.03
	Release date: 13/05/2006
	Last update: 07/01/2007

	(c) 2006 SpamSpan (www.spamspan.com)

	This program is distributed under the terms of the GNU General Public
	Licence version 2, available at http://www.gnu.org/licenses/gpl.txt
	--------------------------------------------------------------------------
*/

var spamSpanMainClass		= 'spamspan';
var spamSpanUserClass		= 'u';
var spamSpanDomainClass		= 'd';
var spamSpanAnchorTextClass = 't';
var spamSpanParams			= new Array('subject', 'body');

/*
	--------------------------------------------------------------------------
	Do not edit past this point unless you know what you are doing.
	--------------------------------------------------------------------------
*/

// load SpamSpan
addEvent(window, 'load', spamSpan);

function spamSpan() {
	var allSpamSpans = getElementsByClass(spamSpanMainClass, document, 'span');
	for (var i = 0; i < allSpamSpans.length; i++) {
		// get data
		var user = getSpanValue(spamSpanUserClass, allSpamSpans[i]);
		var domain = getSpanValue(spamSpanDomainClass, allSpamSpans[i]);
		var anchorText = getSpanValue(spamSpanAnchorTextClass, allSpamSpans[i]);
		// prepare parameter data
		var paramValues = new Array();
		for (var j = 0; j < spamSpanParams.length; j++) {
			var paramSpanValue = getSpanValue(spamSpanParams[j], allSpamSpans[i]);
			if (paramSpanValue) {
				paramValues.push(spamSpanParams[j] + '=' +
					encodeURIComponent(paramSpanValue));
			}
		}
		// create new anchor tag
		var at = String.fromCharCode(32*2);
		var email = cleanSpan(user) + at + cleanSpan(domain);
		var anchorTagText = document.createTextNode(anchorText ? anchorText : email);
		var mto = String.fromCharCode(109,97,105,108,116,111,58);
		var hrefAttr = mto + email;
			hrefAttr += paramValues.length ? '?' + paramValues.join('&') : '';
		var anchorTag = document.createElement('a');
			anchorTag.className = spamSpanMainClass;
			anchorTag.setAttribute('href', hrefAttr);
			anchorTag.appendChild(anchorTagText);
		// replace the span with anchor
		allSpamSpans[i].parentNode.replaceChild(anchorTag, allSpamSpans[i]);
	}
}

function getElementsByClass(searchClass, scope, tag) {
	var classElements = new Array();
	if (scope == null) node = document;
	if (tag == null) tag = '*';
	var els = scope.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getSpanValue(searchClass, scope) {
	var span = getElementsByClass(searchClass, scope, 'span');
	if (span[0]) {
		return span[0].firstChild.nodeValue;
	} else {
		return false;
	}
}

function cleanSpan(string) {
	// string = string.replace(//g, '');
	// replace variations of [dot] with .
	string = string.replace(/[\[\(\{]?[dD][oO0][tT][\}\)\]]?/g, '.');
	// replace spaces with nothing
	string = string.replace(/\s+/g, '');
	return string;
}

// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
	{
		obj['e' + type + fn] = fn;
		obj[type + fn] = function() { obj['e' + type + fn](window.event); }
		obj.attachEvent('on' + type, obj[type + fn]);
	}
}
