var aktuelle_zeile;
var anzahl_zeilen;
var pwd_changed = false;
var login_changed = false;
var search_entry_set = false;

function init(bilder_vorladen) {
	if (bilder_vorladen == 1) {
		lade_bilder('images/b1_de_h.gif',
					'images/b2_de_h.gif',
					'images/b3_de_h.gif',
					'images/b4_de_h.gif',
					'images/b5_de_h.gif',
					'images/b6_de_h.gif',
					'images/b7_de_h.gif',
					'images/b1_en_h.gif',
					'images/b2_en_h.gif',
					'images/b3_en_h.gif',
					'images/b4_en_h.gif',
					'images/b5_en_h.gif',
					'images/b6_en_h.gif',
					'images/b7_en_h.gif',
					'images/back_h.gif',
					'images/top_c.gif',
					'images/top_h.gif',
					'images/banner_bg.gif',
					'images/bg_content.gif',
					'images/bgmemlist_h.gif',
					'images/preload.gif',
					'images/logbg.gif',
					'images/search.gif',
					'images/next_h.gif',
					'images/ppfeil_h.gif',
					'images/sm_bg1_a.gif',
					'images/sm_bg1_c.gif',
					'images/sm_bg1_h.gif',
					'images/sm_bg_a.gif',
					'images/sm_bg_c.gif',
					'images/sm_bg_h.gif',
					'images/stli_h.gif',
					'images/bg_liprof_h.gif',
					'images/bg_liprofd_h.gif'
					);
	}

	browname = navigator.appName;
	vendor = navigator.vendor;
	//alert(vendor);

}

function lade_bilder() {
    document.vorladen = new Array();
    if(document.images) {
        for (var i = 0; i < lade_bilder.arguments.length; i++) {
            document.vorladen[i] = new Image();
            document.vorladen[i].src = lade_bilder.arguments[i];
        }
    }
}

function clearSearch(){
	if( !search_entry_set ){
		document.getElementById('suche').value='';
		setValue('aktion','suche');
		search_entry_set = true;
	}
}

function search(){
	var val = document.getElementById('suche').value;
	if( val == '' ){
		search_entry_set = false;
		setValue( 'aktion', 'suche' );
	}
	if( search_entry_set ){
		//alert( "index.php?ctrl=suche&suche=" + phpUrlEncode( val ) );
		setValue( 'aktion', '' );
		//alert(document.form.action);
		location.href = document.form.action + "&aktion=suche&suche=" + phpUrlEncode( val );
		return false;
	}
}

function handleSearch(){
	if( getValue("aktion") == "suche" ){
		search();
		return false;
	}
}

$(function(){
	$( "#suche" ).keypress(function(e){
		if( detectEnterKeyPressed(e) ){
			search();
			return false;
		}
	});
});

function detectEnterKeyPressed( e ){
	if( !e ) e = window.event;
	var code = 0;
	if( e.which ){
		code = e.which;
	}else if( e.keyCode ){
		code = e.keyCode;
	}
	if( code == 13 ){
		return true;
	}
	return false;
}

function phpUrlEncode( text ){
	if( !text ){ return ""; }
	var regexp = /%20/g;	// A regular expression to match an encoded space
	// The global function encodeURIComponent does almost what we want,
	// but it encodes spaces as %20 instead of as "+". We have to
	// fix that with String.replace()
	text = encodeURIComponent( text ).replace( regexp, "+" );
	return text;
}


function clearpw( text ) {
	if( !pwd_changed ){
		/*
		document.getElementById('passwort').value='';
		document.getElementById('passwort').type='password';
		document.getElementById('passwort').focus();
<input type="password" name="passwort" id="passwort" class="feld_login" value="" />

		var pwd_field = document.createElement( 'password' );
		var txt_field = document.getElementById( 'xpwd' );

		pwd_field.name = 'passwort';
		pwd_field.id = 'passwort';
		pwd_field.className = 'feld_login';
		pwd_field.value = '';
		//pwd_field. = '';

		document.getElementById( 'pwd_cont' ).replaceChild( pwd_field, txt_field );
		pwd_cont.removeChild( txt_field );
		pwd_cont.innerHtml = '<input type="password" name="passwort" id="passwort" class="feld_login" value="" />';
		*/

		var pwd_cont = document.getElementById( 'pwd_cont' );
		var txt_field = document.getElementById( 'xpwd' );
		//var pwd_field = document.getElementById( 'passwort' );


//		alert( pwd_cont.innerHTML );
		try{
			pwd_cont.removeChild( txt_field );
		}catch(e){}
		pwd_cont.innerHTML = '<input type="password" name="passwort" id="passwort" class="feld_login" value="" />';
		pwd_cont.focus();
		//pwd_cont.innerHtml = '<p>SEPP</p>';


		setValue('aktion','login');
		document.getElementById( 'passwort' ).focus();
		pwd_changed = true;
	}
}

function clearLogin() {
	if( !login_changed ){
		document.getElementById('nickname').value='';
		document.getElementById('nickname').focus();
		login_changed = true;
	}
}

function init_kmen(akt_zeile, anz_zeilen) {
	aktuelle_zeile = akt_zeile;
	anzahl_zeilen = anz_zeilen;
	if (aktuelle_zeile > 0) {
		document.getElementById("misu"+aktuelle_zeile).style.display = '';
		if (akt_zeile == 1) {
			document.getElementById("mitz"+aktuelle_zeile).style.backgroundImage = 'url(images/sm_bg1_a.gif)';
		}else{
			document.getElementById("mitz"+aktuelle_zeile).style.backgroundImage = 'url(images/sm_bg_a.gif)';
		}
	}
}

function aufklapp(zeile) {
	if (zeile == 1) {
		document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg1_a.gif)';
	}else{
		document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg_a.gif)';
	}
	if (aktuelle_zeile > 0) {
		if (aktuelle_zeile == 1) {
			document.getElementById("mitz"+aktuelle_zeile).style.backgroundImage = 'url(images/sm_bg1_c.gif)';
		}else{
			document.getElementById("mitz"+aktuelle_zeile).style.backgroundImage = 'url(images/sm_bg_c.gif)';
		}
	}
	if (aktuelle_zeile == zeile) {
		document.getElementById("misu"+zeile).style.display = 'none';
		if (zeile == 1) {
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg1_h.gif)';
		}else{
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg_h.gif)';
		}
		aktuelle_zeile = 0;
	}else{
		aktuelle_zeile = zeile;
		alleszu();
		document.getElementById("misu"+zeile).style.display = '';
	}
	showtop();
}

function alleszu() {
	var y = 1;
	while (y < anzahl_zeilen + 1) {
		document.getElementById("misu"+y).style.display = 'none';
		y++;
	}
}

function swapbg(zeile) {
	if (aktuelle_zeile != zeile) {
		if (zeile == 1) {
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg1_h.gif)';
		}else{
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg_h.gif)';
		}
	}
}

function swapbgback(zeile) {
	if (aktuelle_zeile != zeile) {
		if (zeile == 1) {
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg1_c.gif)';
		}else{
			document.getElementById("mitz"+zeile).style.backgroundImage = 'url(images/sm_bg_c.gif)';
		}
	}
}

function swap_nm(zeile) {
	var obj = document.getElementById("mi"+zeile);
	if( !obj ) return;
	if (zeile == '1') {
		obj.style.backgroundImage = 'url(images/sm_bg1_h.gif)';
	}else{
		obj.style.backgroundImage = 'url(images/sm_bg_h.gif)';
	}
}

function swap_nm_back(zeile) {
	if (zeile == '1') {
		document.getElementById("mi"+zeile).style.backgroundImage = 'url(images/sm_bg1_c.gif)';
	}else{
		document.getElementById("mi"+zeile).style.backgroundImage = 'url(images/sm_bg_c.gif)';
	}
}

function pswap(objekt) {
	document.images[objekt].src = 'images/'+objekt+'_h.gif';
}

function pswapback(objekt) {
	document.images[objekt].src = 'images/'+objekt+'_c.gif';
}

function mlswap() {
	document.getElementById('meli').style.backgroundImage = 'url(images/bgmemlist_h.gif)';
	//document.getElementById('meli').style.color = '#ffffff';
}

function mlswapb() {
	document.getElementById('meli').style.backgroundImage = 'url(images/bgmemlist_c.gif)';
	//document.getElementById('meli').style.color = '#9daaaf';
}

function sgal(obj) {
	obj.style.backgroundImage = 'url(images/galnext_h.gif)';
}
function sgalb(obj) {
	obj.style.backgroundImage = 'url(images/galnext_c.gif)';
}

var linki = false;

function linkin() {
	linki = true;
}
function linkout() {
	linki = false;
}

function galjump(id,spr) {
	if (!linki) {
		location.href='index.php?ctrl=galerie&gid=' + id + '&spr=' + spr;
	}
}



function wm() {
	ma_1 = '<a class="link_foot" href="';
	ma_2 = '&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
	ma_3 = '&#111;&#102;&#102;&#105;&#99;&#101;';
	ma_4 = '&#64;';
	ma_5 = '&#102;&#105;&#108;&#109;&#100;&#101;&#115;&#105;&#103;&#110;&#101;&#114;&#115;.&#97;&#116;';
	ma_6 = '">';
	ma_7 = '</a>';
	document.write(ma_1 + ma_2 + ma_3 + ma_4 + ma_5 + ma_6 + ma_3 + ma_4 + ma_5 + ma_7);
}

function mm(mdomain, mname, num) {
	ma_1 = '<a class="link_portait" href="';
	ma_2 = '&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
	ma_3 = mname;
	ma_4 = '&#64;';
	ma_5 = mdomain;
	ma_6 = '">';
	ma_7 = '</a>';
	document.getElementById('memmail'+num).innerHTML=(ma_1 + ma_2 + ma_3 + ma_4 + ma_5 + ma_6 + ma_3 + ma_4 + ma_5 + ma_7);
}

function pm(mdomain, mname, num) {
	ma_1 = '<a class="link_ftext" href="';
	ma_2 = '&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
	ma_3 = mname;
	ma_4 = '&#64;';
	ma_5 = mdomain;
	ma_6 = '">';
	ma_7 = '</a>';
	document.getElementById('memmail'+num).innerHTML=(ma_1 + ma_2 + ma_3 + ma_4 + ma_5 + ma_6 + ma_3 + ma_4 + ma_5 + ma_7);
}

function init_top() {
	window.onresize = showtop;
	showtop();
}

function showtop() {
	var eff_div_hoehe = document.getElementById('maincont').offsetHeight;
	var fensterhoehe = getWindowHeight();
	if (fensterhoehe < eff_div_hoehe) {
		document.getElementById('foot_top').innerHTML='<a href="#"><img id="top" src="images/top_c.gif" alt="Top" border="0" onmouseover="pswap(\'top\')" onmouseout="pswapback(\'top\')" /></a>';
	}else{
		document.getElementById('foot_top').innerHTML='';
	}
}

function gsw(ob) {
	document.getElementById("ga_"+ob).style.backgroundColor = '#c7cccd';
}
function gswb(ob) {
	document.getElementById("ga_"+ob).style.backgroundColor = '#dddddc';
}

function spl(obj,row) {
	obj.style.backgroundColor = '#ede3d1';
}
function spb(obj,row) {
	if (row == 1) { var bgcol = '#eeeeed'; }else{ var bgcol = '#e1e1e0'; }
	obj.style.backgroundColor = bgcol;
}

var descriptor = 0;

function snpl(obj,descr) {
	document.getElementById('prodet_' + descr).style.display = '';
	obj.style.backgroundColor = '#f4d19a';
	descriptor = descr;
}
function snpb(obj,row) {
	document.getElementById('prodet_' + descriptor).style.display = 'none';
	if (row == 1) { var bgcol = '#eeeeed'; }else{ var bgcol = '#e1e1e0'; }
	obj.style.backgroundColor = bgcol;
}

// Popbild

function Zeige_Bild(breite, hoehe, bilddatei, anmerkung) {
	var rahmenbreite = 16;
	window.onscroll = Zentriere_Bild;
	window.onresize = Zentriere_Bild;
	//var aktiv = window.setInterval('Zentriere_Bild()', 100);
	document.getElementById('popbild').style.width = breite + (rahmenbreite * 2) + 'px';
	var output = '<div class=\"popinner\" style=\"width:' + breite + 'px;height:' + hoehe + 'px;\"><img src=\"images/gallery/large/' +bilddatei+ '\" alt=\"\" width=\"' +breite+ '\" height=\"' +hoehe+ '\" border=\"0\" /></div>';
	if (anmerkung != undefined) {
		output = output + '<div class=\"popdesc\" style=\"width:' + breite + 'px;\">'+anmerkung+'</div>';
	}
	document.getElementById('popbild').innerHTML= output;
	var transparenz = 3;
	document.getElementById('maincont').style.opacity = transparenz/10;
	document.getElementById('maincont').style.filter = 'alpha(opacity=' + transparenz*10 + ')';
	document.getElementById('popbild').style.display = '';
	Zentriere_Bild();
}

function Zentriere_Bild() {
	//if (document.getElementById('popbild').style.display == '') {
		if (browname == "Microsoft Internet Explorer" || vendor == "Apple Computer, Inc.") {
			var padding = 0;
		}else{
			var padding = -8;
		}
		var fensterbreite = getWindowWidth();
		var fensterhoehe = getWindowHeight();
		var bildbreite = document.getElementById('popbild').offsetWidth;
		var bildhoehe = document.getElementById('popbild').offsetHeight;
		var abstand_top = GetScrolltop();
		document.getElementById('popbild').style.top = (fensterhoehe/2) - (bildhoehe/2) + (abstand_top) + 'px';
		document.getElementById('popbild').style.left = (fensterbreite/2) - (bildbreite/2) + padding + 'px';
	//}
}

function SchliesseBild() {
	document.getElementById('popbild').style.display = 'none';
	value = 10;
	document.getElementById('maincont').style.opacity = value/10;
	document.getElementById('maincont').style.filter = 'alpha(opacity=' + value*10 + ')';
}

function getWindowHeight(win) {
    if (win == undefined)
        win = window;
    if (win.innerHeight) {
        return win.innerHeight;
    }else{
        if (win.document.documentElement && win.document.documentElement.clientHeight) {
            return win.document.documentElement.clientHeight;
        }
        return win.document.body.offsetHeight;
    }
}

function getWindowWidth(win) {
    if (win == undefined)
        win = window;
    if (win.innerWidth) {
        return win.innerWidth;
    }else{
        if (win.document.documentElement && win.document.documentElement.clientWidth) {
            return win.document.documentElement.clientWidth;
        }
        return win.document.body.offsetWidth;
    }
}

function GetScrolltop() {
	if (self.pageYOffset) {
		x = self.pageXOffset;
		y = self.pageYOffset;
		return y;
	}
	else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
		return y;
	}
	else if (document.body)	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
		return y;
	}
}

function zeigeintro() {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="570" height="127" id="start" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="intro.swf" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="bgcolor" value="#08141c" /><embed src="intro.swf" menu="false" quality="high" bgcolor="#08141c" width="570" height="127" name="start" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function setValue( id, val ){
	try{
		document.getElementById(id).value = val;
	}catch(exception){
		// Do nothing, ignore it.
	}
}

function getValue( id ){
	try{
		var o = document.getElementById( id );
		if( o ){ return o.value; }
		return null;
	}catch(exception){
		// Do nothing, ignore it.
	}
}


function resetAssoc( projectID, projectTitle, hasGallery ){
	var msg = "Willst Du das Projekt '" + projectTitle + "' ";
	msg += "wirklich aus der Liste entfernen";
	msg += hasGallery ? ' und die zugehörige Galerie löschen' : '';
	msg += "?";

	var confirmed = confirm( msg );
	if( confirmed ){
		setValue( 'aktion', 'clear_pm' );
		setValue( 'param', projectID );
		document.form.submit();
	}
}

function removeCustomProject( projectID, projectTitle, hasGallery ){
	var msg = "Willst Du das Projekt '" + projectTitle + "' ";
	msg += "wirklich";
	msg += hasGallery ? ' samt zugehöriger Galerie' : '';
	msg += " löschen?";

	var confirmed = confirm( msg );
	if( confirmed ){
		setValue( 'aktion', 'remove_custom_project' );
		setValue( 'param', projectID );
		document.form.submit();
	}
}

function addProject(){
	var titel_feld = document.getElementById( 'titel' );
	var titel = titel_feld.value;

	if( titel == '' ){
		alert( 'Bitte gib den Titel des Films an!' );
		titel_feld.className = 'feld_5_error';
	}else{
		setValue( 'aktion' , 'add_project' );
		document.form.submit();
	}
}

function createProject( titel ){
	if( titel == '' ){
		alert( 'JsError 26: empty title' );
		location.href = 'index.php';
	}else{
		setValue( 'aktion', 'new_project' );
		setValue( 'param', titel );
		document.form.submit();
	}
}

var projectSaved = false;
function saveProject( id ){
	if( projectSaved ){
		//alert( 'Ned stressen!' );
	}else{
		projectSaved = true;
		setValue( 'aktion', 'save_project' );
		setValue( 'param', id );
		document.form.submit();
	}
}

function saveCustomProject( id ){
	setValue( 'aktion', 'save_custom_project' );
	setValue( 'param', id );
	document.form.submit();
}

function addNewProjectMember( id ){
	setValue( 'aktion', 'add_project_member' );
	setValue( 'param', id );
	var action = document.form.action;
	action += '#pm_list';
	document.form.action = action;
	document.form.submit();
}
function removeProjectMember( id, pm, name ){
	var msg = 'Willst Du die Zuordnung von ' + name + ' zum Projekt wirklich aufheben?';
	var confirmed = confirm( msg );

	if( confirmed ){
		setValue( 'aktion', 'remove_project_member' );
		setValue( 'param', id );
		setValue( 'victim', pm );
		var action = document.form.action;
		action += '#pm_list';
		document.form.action = action;
		document.form.submit();
	}
}

function setTeaser( pid, id, no ){
	performImageAction( pid, id, no, 'set_teaser' );
}

function sortUp( pid, id, no ){
	performImageAction( pid, id, no, 'sort_img_up' );
}

function sortDown( pid, id, no ){
	performImageAction( pid, id, no, 'sort_img_down' );
}

function removeImage( pid, id, no ){
	var msg = 'Willst Du das ' + no + '. Bild wirklich löschen?';
	var confirmed = confirm( msg );

	if( confirmed ){
		performImageAction( pid, id, no, 'del_img' );
	}
}

function editImage( pid, id, no ){
	performImageAction( pid, id, no, 'edit_img' );
}

function saveImage( pid, id ){
	setValue( 'aktion', 'save_gal_img' );
	setValue( 'param', pid );
	setValue( 'victim', id );
	document.form.submit();
}

function performImageAction( pid, id, no, action ){
	setValue( 'aktion', action );
	setValue( 'param', pid );
	setValue( 'victim', id );

	var action = document.form.action;
	//action += '#sec_';
	//action += no;
	document.form.action = action;
	document.form.submit();
}

function confirmAction( msg, url ){
	if( confirm(msg) ){
		location.href = url;
	}
}

function deleteActor( actor_id, msg ){
	if( confirm(msg) ){
		setValue( 'actor', actor_id );
		setValue( 'cmd', 'actor_delete' );
		document.form.submit();
	}
	return false;	// Verhindere Standard-Verhalten des Links.
}

function memlistbtn() {
	var memlist_01 = "<div id=\"meli\" class=\"memberlist\" onmouseover=\"mlswap()\" onmouseout=\"mlswapb()\" onclick=\"var pop = window.open('Mitgliede";
	var memlist_02 = "rverzei";
	var memlist_03 = "chnis.php','popi','scrollbars=no,width=850,height=500')\">MITGLIEDERLISTE</div>";
	document.write(memlist_01 + memlist_02 + memlist_03);
}