var timer = "";
var timer_target = "";
$(function() {
    tooltip.init();
    $('a.lightbox').lightBox();
    $('a.lightbox_person').lightBox();
});
function showNotice(id,fade){
    if(timer){
        clearTimeout(timer);
        hideNotice(timer_target);
    } 
    if(!fade) fade = 1;
	if($('#' + id)){
		$('#' + id).show('fast');
	}
    if(fade){
        timer = window.setTimeout("hideNotice('" + id + "')", 5000);
        timer_target = id;
    }
}
function hideNotice(id){
	if($('#' + id)){
		$('#' + id).hide('fast');
	}
}
function reloadPage(uri){
	if(uri){
        timer2 = window.setTimeout("setUri('" + uri + "')", 1000);
	}
}
function setUri(uri){
	if(uri){
		window.location = uri;
	}
}
function toggleLogin(){
	if($('#logincontainer')){
		$('#logincontainer').toggle('fast');
		window.setTimeout("toggleLoginImg('logincontainer','login_tab_left')", 500);
	}
}
function toggleLoginImg(){
	if($('#logincontainer').css('display') == 'none'){
		$('#login_tab_left').attr('src',file_root + '/_files/img/glob_layout01a.png');
	} else {
		$('#login_tab_left').attr('src', file_root + '/_files/img/glob_layout01b.png');
	}
}
function toggleContainer(id,img_c,img_o){
	if($('#togglecontainer_' + id)){
		$('#togglecontainer_' + id).toggle('fast');
		window.setTimeout("toggleContainerImg('" + id + "','" + img_c + "','" + img_o + "')", 300);
	}
}
function toggleContainerImg(id,img_c,img_o){
    if(!img_c) img_c = 'arrow.png';
    if(!img_o) img_o = 'arrow_open.png';
	if($('#togglecontainer_' + id).css('display') == 'none'){
		$('#toggleimage_' + id).attr('src', file_root + '/_files/img/' + img_c);
	} else {
		$('#toggleimage_' + id).attr('src', file_root + '/_files/img/' + img_o);
	}
}
function overwriteFile(id){
	if($('#overwrite_file' + id) && $('#overwrite_file_input' + id)){
        $('#overwrite_file' + id).css('display','none');
        $('#overwrite_file_input' + id).css('display','block');
    }
}
function showRegistration(title){
    showLayer('layer_overall');
    $('#layer_overall_title').html(title);
    $('#layer_overall_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/registrierung",
        success: function(content) {
            $('#layer_overall_cont').html(content);
            tooltip.init ();
        }
    });
}
function submitRegistration(){
    var serialized = $("#registrationform").serialize();

    $('#layer_overall_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/registrierung",
        data: serialized,
        success: function(content) {
            $('#layer_overall_cont').html(content);
            tooltip.init ();
        }
    });
}
function showProfile(title){
    showLayer('layer_overall');
    $('#layer_overall_title').html(title);
    loadProfile();
}
function loadProfile(){
    $('#layer_overall_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil",
        success: function(content) {
            $('#layer_overall_cont').html(content);
            tooltip.init ();
            $('a.lightbox').lightBox();
            $('a.lightbox_person').lightBox();
        }
    });
}
function editProfile(section){
    $('#' + section).html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/bearbeiten",
        data: '&section=' + section,
        success: function(content) {
            $('#' + section).html(content);
            tooltip.init ();
        }
    });
}
function submitProfile(section){
    var serialized = $("#profileform").serialize();

    $('#' + section).html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/bearbeiten",
        data: serialized,
        success: function(content) {
            if(content.match('LOGOUT OK')){
                location.reload();
            } else {
                if(content.match('SUBMIT OK')){
                    loadProfile();
                } else {
                    $('#' + section).html(content);
                    tooltip.init ();
                }
            }
        }
    });
}
function loadGlobalProfile(section){
    $('#' + section).html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    location.reload();
}
function editGlobalProfile(section){
    $('#' + section).html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/globalbearbeiten",
        data: '&section=' + section,
        success: function(content) {
            $('#' + section).html(content);
            tooltip.init ();
        }
    });
}
function submitGlobalProfile(section){
    var serialized = $("#globalprofileform").serialize();

    $('#' + section).html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/bearbeiten",
        data: serialized,
        success: function(content) {
            if(content.match('SUBMIT OK')){
                loadGlobalProfile(section);
            } else {
                $('#' + section).html(content);
                tooltip.init ();
            }
        }
    });
}
function deleteProfile(){
	var eingabe;
	eingabe=confirm("Soll wirklich Ihr gesamtes Konto deaktiviert werden?\n\nEs werden alle persönlichen Daten mit Ausnahme des Vor- und Nachnamens sowie der E-Mail-Adresse gelöscht. Das Einloggen und Reaktivieren des Datensatzes ist später nicht mehr möglich.\n\nIhre E-Mail-Adresse wird weiterhin gespeichert, um eine erneute Anmeldung zu missbräuchlichen Zwecken zu verhindern. Es werden keine persönlichen Daten weiter verwendet oder weiter gegeben.");
	if(eingabe==true){
        $.ajax({
            type: "POST",
            url: "/profil/delete",
            data: "&preventdirect=1",
            success: function(content) {
                if(content.match('DELETED OK')){
                    location.reload();
                } 
            }
        });
	}
}
function showKontakt(id_user,title){
    showLayer('layer_content');
    $('#layer_content_title').html(title);
    loadKontakt(id_user);
}
function loadKontakt(id_user){
    $('#layer_content_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/kontakt",
        data: "&id_user=" + id_user,
        success: function(content) {
  			$('#layer_content_cont').html(content);
            tooltip.init ();
        }
    });
}
function submitKontakt(){
    var serialized = $("#userkontaktform").serialize();

    $('#layer_content_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/kontakt",
        data: serialized,
        success: function(content) {
            $('#layer_content_cont').html(content);
            tooltip.init ();
        }
    });
}
function showMissbrauch(id_user,typ,title){
    showLayer('layer_content');
    $('#layer_content_title').html(title);
    loadMissbrauch(id_user,typ);
}
function loadMissbrauch(id_mat,typ){
    $('#layer_content_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/missbrauch",
        data: "&id_mat=" + id_mat + "&typ=" + typ,
        success: function(content) {
  			$('#layer_content_cont').html(content);
            tooltip.init ();
        }
    });
}
function submitMissbrauch(){
    var serialized = $("#missbrauchform").serialize();

    $('#layer_content_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/missbrauch",
        data: serialized,
        success: function(content) {
            $('#layer_content_cont').html(content);
            tooltip.init ();
        }
    });
}

function showPasswortvergessen(title){
    showLayer('layer_overall');
    $('#layer_overall_title').html(title);
    loadPasswortvergessen();
}
function loadPasswortvergessen(){
    $('#layer_overall_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/passwortvergessen",
        success: function(content) {
  			$('#layer_overall_cont').html(content);
            tooltip.init ();
        }
    });
}
function submitPasswortvergessen(){
    var serialized = $("#passwortvergessenform").serialize();

    $('#layer_overall_cont').html('<div class="wartebalken"><br /><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');
    $.ajax({
        type: "POST",
        url: "/profil/passwortvergessen",
        data: serialized,
        success: function(content) {
            $('#layer_overall_cont').html(content);
            tooltip.init ();
        }
    });
}

//layers
function getPageSize(tgt) {    
    if($('#layer_overall_inner').outerHeight() >  $('#content').outerHeight()){
        $('#content').css('height', ($('#layer_overall_inner').outerHeight() + 50) + 'px');
    }
    var h;
	h = $('#content').outerHeight();
	if($(tgt + ':visible')){
		if(tgt == 'layer_overall') {$('#' + tgt).css('height', (h + 98) + 'px');}
		if(tgt == 'layer_content') {$('#' + tgt).css('height', (h + 1) + 'px');}
	}
}
function showLayer(target) {
	$('#' + target).css('display','block');
	window.setTimeout("getPageSize('" + target + "')", 200);
    $('#' + target).bind('click', function(event) { if (event.target.id == 'layer_overall_middle' || event.target.id == 'layer_content_middle') closeLayer(target); });
}
function closeLayer(target) {
    $('#' + target).unbind('click');
	$('#' + target).css('display','none');
}
function newPasswort(){
    $('#passwort_input').css('display','block');
    $('#passwort_input_button').css('display','none');
}
function newBild(){
    $.ajax({
        type: "POST",
        url: "/profil/deletetefoto",
        success: function() {
            $('#bild_input').css('display','block');
            $('#bild_input_button').css('display','none');
        }
    });
}
function submitBild(){
    $('#fotoform').submit();
    $('#bild_input_wartebalken').css('display','block');
    $('#bild_input').css('display','none');
    $('#bild_input_wartebalken').html('<div class="wartebalken"><img src="' + file_root + '/_files/img/wartebalken.gif" width="200" height="10" /></div>');

}
function showAttachment(){
    if($('#typ4').checked){
        $('#typ_anhang').css('display','block');
    } else {
        $('#typ_anhang').css('display','none');
    }

}
/****** BEWERTUNGEN ***************/
function showBewertungen(nr){
   if(!nr) nr=0;
    $('bew_my').src = file_root + "/_files/img/umt/bew_" + nr + "_my.png";
}
function resetBewertungen(){
    if($('#bew_my_reset').val() > 0){
        $('#bew_my').attr('src',file_root + "/_files/img/umt/bew_" + $('#bew_my_reset').value + "_my.png");
    }
}
function setBewertung(bewertung,id_mat){
    var attrib = new Array();
    attrib[0] = "keine Bewertung";
    attrib[1] = "genügend";
    attrib[2] = "befriedigend";
    attrib[3] = "gut";
    attrib[4] = "sehr gut";
    attrib[5] = "hervorragend"
    $('#bew_wartebalken').css('display','block');
    $.ajax({
        type: "POST",
        url: "/material/bewertung",
        data: "&bewertung=" + bewertung + "&id_mat=" + id_mat,
        success: function(content) {
            var vals = content.split("|");
            if(vals[0] > 0){
                $('#bew_my').attr('src', file_root + "/_files/img/umt/bew_" + vals[0] + "_my.png");
                $('#bew_my_reset').val(vals[0]);
            }
            if(vals[1] > 0 && vals[2] > 0){
                $('#bew_dsch').attr('src',file_root + "/_files/img/umt/bew_" + vals[2] + ".png");
                $('#bew_dsch').attr('tiptitle', $('umt_durchsch_bewertungen').value + "<br />" + vals[1] + " = <strong>" + attrib[vals[2]] + "</strong><br />&nbsp;<br />" + $('umt_anz_bewertungen').value + ": " + vals[3]);
            }
			$('#bew_wartebalken').css('display','none');

            if($('#bew_my_text_unrated')){
                $('#bew_my_text_unrated').css('display','none');
            }
            $('#bew_my_text_rated').css('display','block');
        }
    });
}
function submitEnter(field,e){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13){
       field.form.submit();
       return false;
   } else {
       return true;
   }
}
function toggleCheckbox(id, that){
    if(id && !$(that).children().first().is("input")){
        if($('#' + id).attr('checked')){
            $('#' + id).removeAttr('checked');
        } else {
            $('#' + id).attr('checked','checked');
        }
    }
}
function checkNachwas(type){
    if(type==1){
        if($("input[name='nachwas']:checked").val() > 1){
            $('#vik_schnellsuchformular').attr('action','/stellvertreterinnen/suche');
        } else {
            $('#vik_schnellsuchformular').attr('action','/stellvertretungen/suche');        
        }
    }
    if(type==2){
        if($("input[name='nachwas']:checked").val() > 1){
            $('#ste_schnellsuchformular').attr('action','/stellensuchende/suche');
        } else {
            $('#ste_schnellsuchformular').attr('action','/stellen/suche');        
        }
    }
}
