// ===============================================================================================================================

$(document).ready(function(){ 

	set_height_spacer();
	set_homebox_heights();
	
	// ANIMATED GO TO TOP LINK
	$("a[href='#top']").click(function(){
		$("html, body").animate({ scrollTop: 0 }, 500);
		return false;
	});

});


// ===============================================================================================================================
// ===============================================================================================================================

var win = null;

// ===============================================================================================================================

function NewWindow(mypage, myname, w, h, scroll)
{		
	if (win && !win.closed) win.close();

	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	win = window.open(mypage, myname, settings);
	if (win.window.focus) { win.window.focus(); }
}

function set_homebox_heights()
{
	// SET BOXES IN ONE ROW TO THE SAME HEIGHT
	$(".boxrow").each(function(i) {
		if ($(this).children(".contentbox").length == 2)
		{
			var boxleft = $(this).find(".contentbox-content:first");
			var boxright = $(this).find(".contentbox-content:last");
			$(boxleft).height("auto");
			$(boxright).height("auto");
			if ($(boxright).height() > $(boxleft).height()) $(boxleft).height($(boxright).height());
			else $(boxright).height($(boxleft).height());
		}
	});
}

// ===============================================================================================================================

function email_valid(email) 
{
 	var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
  	var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,6}";
  	var regex  = "^" + usr + "\@" + domain + "$";
  	var rgx    = new RegExp(regex);
  	return rgx.exec(email) ? true : false;
}

// ===============================================================================================================================

function format_fieldcolor(idDiv, showError)
{
	showError = showError | false;
	var errorClass = "fc-error";
	var normalClass = "fc-normal";
	var labelColor = (showError) ? "#CC0000" : "";
	var d = document.getElementById(idDiv);
	var currentClass = d.className;
	var myLabel = "labelfor_" + idDiv;
	
	if (showError)
	{
		var showClass = errorClass;
		var oppClass = normalClass;
	}
	else
	{
		var showClass = normalClass;
		var oppClass = errorClass;
	}
	
	if (currentClass == "" || currentClass == oppClass)
	{
		d.className = showClass;
	}
	else
	{
		var indexOfSpace = currentClass.indexOf(" ");
		if(indexOfSpace == -1)
		{
			d.className = currentClass + " " + showClass;
		}
		else
		{
			var mainClass = currentClass.substring(0, indexOfSpace + 1);
			d.className = mainClass + " " + showClass;
		}
	}
	
	if (document.getElementById(myLabel)) document.getElementById(myLabel).style.color = labelColor;
}

// ===============================================================================================================================

function fieldcolor_normal(idDiv)
{
	format_fieldcolor(idDiv);
}

// ===============================================================================================================================

function fieldcolor_error(idDiv)
{
	format_fieldcolor(idDiv, true);
}

// ===============================================================================================================================

function get_viewport(returnmode)
{
	// Gibt Höhe oder Breite des Viewports zurück
	var viewportwidth;
	var viewportheight;
	 
	// standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof(window.innerWidth) != 'undefined')
	{
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	}
	 
	// IE6 in standards compliant mode
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	}
	 
	// older versions of IE 
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	if (returnmode == "width") return viewportwidth;
	else return viewportheight;
	
}

// ===============================================================================================================================

function set_height_spacer()
{
	var spacerHeight;
	var fullHeight = get_viewport(); // get current viewport height
	
	var headerHeight = document.getElementById("topheader").offsetHeight + document.getElementById("header").offsetHeight;
	var footerHeight = document.getElementById("footer").offsetHeight;
	
	var remainingHeight = fullHeight - (headerHeight + footerHeight) - 0;
	
	if (document.all) remainingHeight = remainingHeight - 0; // Wenn IE

	
	if (remainingHeight > 0)
	{
		document.getElementById("content-table").style.height = remainingHeight + "px";
	}
}

// ===============================================================================================================================

function writemaillink (mailIndex)
{
	mails = new Array("");
	mailIndex = mailIndex | 0;
	document.write("<a class=\"textlink\" href=\"javascript:void(0)\" onclick=\"makemail('" + mails[mailIndex] + "'); return false;\"><script type=\"text/javascript\">document.write(maildecode('" + mails[mailIndex] + "'));</script></a>");
}

// ===============================================================================================================================

function maildecode (input)
{
	var output = input;
	output = output.replace(/\|/, '@');
	output = output.replace("#xxcom#", ".");
	return (output);
}

// ===============================================================================================================================

function makemail($link)
{
	$link = maildecode($link);
	this.location.href = "mailto:" + $link;
}

// ===============================================================================================================================

function toggle_editmode(newmode)
{

	// SWITCH OFF
	if (newmode=='0')
	{
		$("#toggle_editmode_on").css("display", "none");
		$("#toggle_editmode_off").css("display", "block");
		
		$(".admin").each(function(){
			$(this).css("display", "none");
		});
	}
	// SWITCH ON
	else
	{
		$("#toggle_editmode_on").css("display", "block");
		$("#toggle_editmode_off").css("display", "none");
		
		$(".admin").each(function(){
			$(this).css("display", "block");
		});
		
		$("span.admin").each(function(){
			$(this).css("display", "inline");
		});
	}

	createCookie('xseditmode', newmode);
	
}

// ===============================================================================================================================

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

