function toggle_nav(id) {
	nav = $('mainmenu');
	current = $(id).id;

	for ( var i = 0; i < nav.childNodes.length; i++) {
		elem = nav.childNodes[i];

		if (elem.className == 'msubContainer'
				&& elem.id != null
				&& (elem.id == current || (elem.id != current && elem.style.display != 'none'))) {
			//alert(elem.id+','+elem.className+','+(elem.className=='msubContainer'?'y':'n'));
			Effect.toggle(elem.id, 'blind');
		}
	}

	//Effect.toggle($(id).nextSibling.id,'blind');
}

function start_nav(id) {
	$(id).style.display = 'none';
}

function UnCryptMail(s, shift) {
	var n = 0;
	base = "_-.0123456789:@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	replace = ":HucKXg8ARpI4y.mQ1eLBJNkn20vjFsad@zVtO5GEYPZwCM-qSixU_WTl793rbfoDh6";
	len = replace.length;
	var r = "";

	for ( var i = 0; i < s.length; i++) {
		n = replace.indexOf(s.substr(i, 1))
		n -= shift;
		if (n < 0)
			n += len;
		r += base.substr(n, 1);
	}
	return r;
}
function linkTo_UnCryptMailto(s, shift) {
	location.href = UnCryptMail(s, shift);
}

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveEnd('character', o.value.length);
		if (r.text == '')
			return o.value.length;
		return o.value.lastIndexOf(r.text);
	} else
		return o.selectionStart;
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveStart('character', -o.value.length);
		return r.text.length;
	} else
		return o.selectionEnd;
}

function getSelection(myField) {
	// IE support
	var selText = "";
	if (typeof document.selection != 'undefined') {
		myField.focus();
		sel = document.selection.createRange();
		selText = sel.text;
	}

	//MOZILLA/NETSCAPE support
	else if (typeof myField.selectionStart != 'undefined') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		selText = myField.value.substring(startPos, endPos);
	} else {
		selText = '';
	}
	return selText;
}

function setSelection(myField, myText) {
	// IE support
	if (typeof document.selection != 'undefined') {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myText;
		range = document.selection.createRange();
		range.moveStart('character', (myText).length);
	}

	//MOZILLA/NETSCAPE support
	else if (typeof myField.selectionStart != 'undefined') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myText
				+ myField.value.substring(endPos, myField.value.length);
		startPos += myText.length;
		myField.selectionStart = startPos;
		myField.selectionEnd = startPos;
	} else {
		myField.innerHTML += myText;
	}
}

function insertText(fieldname, text) {
	myField = $(fieldname);
	myText = getSelection(myField);
	setSelection(myField, text);
}

function insertBBCode(fieldname, myTag) {
	myField = document.getElementById(fieldname);
	myText = getSelection(myField);
	if (myText != '')
		setSelection(myField, '[' + myTag + ']' + myText + '[/' + myTag + ']')
}

function insertTag(fieldname, openTag, closeTag) {
	myField = document.getElementById(fieldname);
	myText = getSelection(myField);
	if (myText != '')
		setSelection(myField, '[' + openTag + ']' + myText + '[/' + closeTag
				+ ']')
}

function makeLi(text) {
	return "[li]" + text.replace(/\n/g, "[/li]\n[li]") + "[/li]";
}

function insertBBList(fieldname, myTag) {
	myField = document.getElementById(fieldname);
	myText = getSelection(myField);
	if (myText != '')
		setSelection(myField, '[' + myTag + ']\n' + makeLi(myText) + '\n[/'
				+ myTag + ']');

}

function insertBBRef(fieldname, myTag) {
	myField = document.getElementById(fieldname);
	myText = getSelection(myField);
	if (myText != '') {
		link = prompt("Link:");
		if (link != null) {
			setSelection(myField, '[' + myTag + '=' + link + ']' + myText
					+ '[/' + myTag + ']');
		}
	}

}

function addTable(fieldname) {
	myField = document.getElementById(fieldname);
	// alert(myField.innerHTML);
	myField.innerHTML += '[tabelle]\n'
			+ '[zeile][ttitel]Titel1[/ttitel][ttitel]Titel2[/ttitel][/zeile]\n'
			+ '[zeile][spalte]1-1[/spalte][spalte]1-2[/spalte][/zeile]\n'
			+ '[zeile][spalte]2-1[/spalte][spalte]2-2[/spalte][/zeile]\n'
			+ '[/tabelle]';
}

/* createUpload(name,postUrl)
 * Creates a hidden div with an iframe, which will be submitted
 * 
 */
/*
 * function createUpload(name, postUrl) {
 * 
 * var n = 'f' + Math.floor(Math.random() * 99999); var d =
 * document.createElement('DIV'); d.style.display = "none" d.innerHTML = '<iframe
 * style="display:none" src="about:blank" id="' + n + '" name="' + n + '"
 * onload="completeCallback' + name + '(window.frames[\'' + n +
 * '\'].document.body.innerHTML)"></iframe>';
 * 
 * f = document.createElement('FORM'); f.method = "post"; f.enctype =
 * "multipart/form-data"; f.action = postUrl; f.target = n;
 * 
 * org = document.getElementById(name); for (i = 0; i < org.childNodes.length;
 * i++) { child = org.childNodes[i].cloneNode(true); if (org.childNodes[i].type ==
 * 'file') { //alert("child type:"+org.childNodes[i].type+" val:
 * "+org.childNodes[i].value); alert(child.value);// =org.childNodes[i].value;
 * child.setAttribute("value", org.childNodes[i].value); } f.appendChild(child); }
 * 
 * d.appendChild(f);
 * 
 * document.body.appendChild(d);
 * 
 * func = 'startCallback' + name; if (typeof (func) == 'function') func();
 * 
 * f.submit(); }
 */

/*
 * Google Maps API
 */
var stdZoom = 14;
var stdLng = 48.209274;
var stdLat = 15.644274;

// google.load("maps", "2.x");
var geocoder = new GClientGeocoder();
geocoder.setBaseCountryCode('AT');

function showAddress(mymap, address) {
	geocoder.getLatLng(address, function(point) {
		if (!point) {
			alert(address + " nicht gefunden!");
		} else {
			mymap.setCenter(point, 13);
			var marker = new GMarker(point);
			mymap.addOverlay(marker);
			marker.openInfoWindowHtml(address);
		}
	});
}

function createMap(element) {
	var map = new GMap2(document.getElementById(element));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	// map.enableScrollWheelZoom();
	map.enableGoogleBar();
	map.setCenter(new GLatLng(stdLng, stdLat), stdZoom);
	return map;
}

function showMap(element, address) {
	var map = createMap(element);
	showAddress(map, address);
}

function showKoord(element, lat, lng, address) {
	var map = createMap(element);
	var point = new GLatLng(lat, lng);
	map.setCenter(point, stdZoom);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(address);
}

function showPlace(map, lat, lng, title) {
	var point = new GLatLng(lat, lng);
	map.setCenter(point, stdZoom);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(title);
	return marker;
}

function makeBBMap(fieldname) {
	myField = document.getElementById(fieldname);
	text = getSelection(myField);
	if (text != '') {
		geocoder.getLatLng(text, function(point) {
			if (!point) {
				alert(text + " nicht gefunden! ");
			} else {
				setSelection(myField, '[karte=' + point.lat() + ','
						+ point.lng() + ']' + text + '[/karte]');
			}
		});
	}

}

function geoCode(fieldname) {
	myField = document.getElementById(fieldname);
	text = myField.value;
	if (text != '') {
		if (text.match(/^\d+\.\d+,\d+\.\d+;/))
			text = text.replace(/^\d+\.\d+,\d+\.\d+;/, '');
		geocoder.getLatLng(text, function(point) {
			if (!point) {
				alert(text + " nicht gefunden! ");
			} else {
				myField.value = point.lat() + ',' + point.lng() + ';' + text;
			}
		});
	}
}

function getGeoCode(fieldname, text) {
	myField = document.getElementById(fieldname);
	if (text != '') {
		geocoder.getLatLng(text, function(point) {
			if (!point) {
				alert(text + " nicht gefunden! ");
			} else {
				myField.value = point.lat() + ',' + point.lng();
			}
		});
	}
}

function def(variable, val) {
	return (typeof (variable) != 'undefined' ? variable : val);
}

function icon(name, alt_text, attributes) {
	alt_text = def(alt_text, '');
	attributes = def(attributes, {});

	attributes.src = conf_base + '/images/icons/' + name + '.png';
	attributes.alt = alt_text;
	attributes.title = alt_text;
	attributes.cssClass = 'icon';
	// var img='<img src="'+conf_base+'/images/icons/'+name+'.png"
	// alt="'+alt_text+'" title="'+alt_text+'" class="icon"';
	return Builder.node('img', attributes);
}

function getMouseXY(e) {
	e = e || window.event;
	var cursor = {
		x : 0,
		y : 0
	};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft)
				- (de.clientLeft || 0);
		cursor.y = e.clientY + (de.scrollTop || b.scrollTop)
				- (de.clientTop || 0);
	}
	return cursor;
}

var boxNum = 0;
function showMessageBox(title, content) {
	var bodyObj = $$('body')[0];

	// if($('msgBox')!=null){
	// bodyObj.removeChild($('msgBox'));
	// }

	var Id = ++boxNum;
	var msgBoxId = 'msgBox' + Id;
	bodyObj.appendChild(Builder.node('div', {
		className : 'msgBox',
		id : msgBoxId
	}, [ Builder.node('div', {
		className : 'msgOptions',
		id : 'msgOptions' + Id
	}, [ Builder.node('a', {
		className : 'msgBoxClose',
		id : 'msgBoxClose' + Id
	}, [ icon('cross', 'schliessen') ]) ]), Builder.node('div', {
		className : 'msgTitle',
		id : 'msgTitle' + Id
	}), Builder.node('div', {
		className : 'msgContent',
		id : 'msgContent' + Id
	}) ]));
	$('msgBoxClose' + Id).observe('click', (function() {
		bodyObj.removeChild($(msgBoxId));
	}));
	if (typeof (title) == 'object')
		$('msgTitle' + Id).appendChild(title);
	else
		$('msgTitle' + Id).innerHTML = title;

	if (typeof (content) == 'object')
		$('msgContent' + Id).appendChild(content);
	else
		$('msgContent' + Id).innerHTML = content;

	new Draggable(msgBoxId);
	var msgBox = $(msgBoxId);
	if (cursorPos.y != 0) {
		msgBox.style.top = cursorPos.y + 'px';
	}
	//	msgBox.style.left=window.innerWidth-msgBox.style.width/2;
	if (cursorPos.x != 0) {
		//		alert(msgBox.offsetWidth);
		if (cursorPos.x + msgBox.offsetWidth / 2 > window.innerWidth) {
			msgBox.style.left = window.innerWidth - msgBox.offsetWidth - 15
					+ 'px';
		} else if (cursorPos.x - (msgBox.offsetWidth / 2) < 0) {
			msgBox.style.left = '0px';
		} else {
			msgBox.style.left = cursorPos.x - (msgBox.offsetWidth / 2) + 'px';
		}
	}
	msgBox.style.display = 'none';
	Effect.Appear('msgBox' + Id);

}

var cursorPos = {
	x : 0,
	y : 0
};

function siteLoaded() {
	$('body').observe('mousemove', function(event) {
		cursorPos = getMouseXY(event);
	});

	fixups();

}

function fixups(){
	jQuery('video')
	.each(
			function() {
//				alert("video loaded");
				if (typeof (this.error) != "object"
						|| (this.error != null)
						&& (this.error.code == 3 || this.error.code == 4)) {
					jQuery(this)
							.jwplayer(
									{
										flashplayer : conf_url + '/libraries/jwplayer/player.swf',
										skin : conf_url + '/libraries/jwplayer/five/five.xml'
									});
				}
			});
}

function getFullTimestamp() {
	var now = new Date();
	return now.toTimestamp();
}

Date.prototype.toTimestamp = function() {
	return '' + this.getFullYear() + '-'
			+ (this.getMonth() + 1).toPaddedString(2) + '-'
			+ this.getDate().toPaddedString(2) + ' '
			+ this.getHours().toPaddedString(2) + ':'
			+ this.getMinutes().toPaddedString(2) + ':'
			+ this.getSeconds().toPaddedString(2) + '';
};

function videoChooser(elem) {
	showMessageBox(
			"Video ausw&auml;hlen",
			'<div id="video_'
					+ elem
					+ '"><img src="'
					+ conf_base
					+ '/images/lightbox/loading.gif" alt="loading..." title="loading..." /></div>');
	x_videoChooser(elem, "/");
}

function startVideoUpload(id,n) {
//		alert(id+" "+n);
	jQuery('#'+id).append(
					'<img src="' + conf_base + '/images/lightbox/loading.gif" alt="loading..." title="loading..." />');
	return true;
}

function stopVideoUpload(id,n,bodyHTML) {
//	alert(id+" "+n);
//	$(id).each(function() {
		x_videoChooser($(id).identify().replace(/^form/, ''),$(id).elements[0].value);
//	});
	return true;
}

