
// Suche im Profil abschicken
function SubmitQuestionSearch(frm){
    $('#thema_key').val('');
    $('#filter').val(frm.filter.value);
    ReloadQuestions($('#filter'));
    return false;
}

function ReloadQuestions(obj, is_browse, is_first_load){

    //if(!is_browse){$('#page_num').val('1');
    var	page = is_browse?'&num='+$('#page_num').val():'';
    var fragen = $('#thema_key').val().length > 0?'&fragen=' + $('#thema_key').val():'';
    var filter = $('#filter').val().length > 0 && $('#filter').val() != $('#filter').attr('alt')?'&filter=' + escape($('#filter').val()):'';

    // Seite wurde neu geladen...
    if(is_first_load && filter.length == 0 && fragen.length == 0) return false;

    ShowLoadImage($(obj));

    $("#reload_questions").parent().load('/profile/public_questions.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + page + fragen + filter + ' #reload_questions', function(){
        ShowLoadImage(false);
        location.href = '#questions';
    //$.scrollTo('#reload_questions', 800, {queue:true} );
    });
    return false;
}

// Frageformular abschicken
function SubmitQuestionForm(){
    $('input[name=fragesubmit]').attr('disabled', 'disabled');
    $.post("/profile/question_form.php", $('#frm_question_form').serialize() + '&build=1', function(data){
        $('#question_form').parent().html(data);
    });
    return false;
}

// Profile in der Liste neu laden
function ReloadProfilesNEU(obj, is_browse, is_first_load){
    var n = FindDependingNode($(obj), 'reload');

    if(!is_browse) $('#page_num').val('1');

    // Reloadknoten wurde gefunden
    if(n){

        // Filter bestimmen
        var filter = '';
        $('.is_filter').each(function(){
            if($(this).val().length > 0){
                filter += '_' + $(this).val();
            }
        });

        // Seite wurde neu geladen, der Filter ist leer oder nur der Filter fuer Liste Ja|Nein ist gesetzt...
        if(is_first_load && (filter.length == 0 || filter == '_in')) return false;

        if(filter.length > 0){
            filter = '&filter=' + filter.substr(1);
        }

        // Loading...
        ShowLoadImage($(obj));

        // PLZ Suche wegschalten
        if(!is_first_load && $('div.plz_result').css('height') && $('div.plz_result').css('height') != '0px'){
            $('div.plz_result').animate({
                opacity: 0,
                height: 0
            }, 2000, function(){
                $(this).css('display', 'none');
            });
        }

        // Datei bestimmen, die nachgeladen werden soll. Wenn Filter i auf y gesetzt, sollen Listenkandidaten geladen werden.
        var file_to_load = filter.indexOf('iy') > -1?'profile_liste.php':'profile_wahlkreise.php';

        // Profile nachladen
        n.load('/profile/list/' + file_to_load + '?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&num=' + $('#page_num').val() + filter, function(){
            ShowLoadImage(false);
        });
    }
    return false;
}

// Seite neuladen nach Klick auf Buchstabe
$('a.ReloadByLetterNEU').live('click', function(){
    $('a.ReloadByLetterNEU').removeClass('bold');
    if($(this).text().length == 1){
        $(this).addClass('bold');
        $('input[name=filter_nachname]').val('n' + $(this).text());
        $('div.menuletter span').css('display', 'inline');
    }
    else{
        $('input[name=filter_nachname]').val('');
        $('div.menuletter span').css('display', 'none');
    }
    ReloadProfilesNEU($(this));
    return false;
});

// Profile in der Liste neu laden
function ReloadProfiles(obj, is_browse, is_first_load){
    var n = FindDependingNode($(obj), 'reload_profiles');

    if(!is_browse) $('#page_num').val('1');

    // Reloadknoten wurde gefunden
    if(n){

        // Filter bestimmen
        var filter = '';
        $('.is_filter').each(function(){
            if($(this).val().length > 0){
                filter += '_' + $(this).val();
            }
        });
        if(filter.length > 0){
            filter = '&filter=' + filter.substr(1);
        }

        // Seite wurde neu geladen...
        if(is_first_load && (filter.length == 0 || filter == 'in')) return false;

        // Loading...
        ShowLoadImage($(obj));

        // PLZ Suche wegschalten
        if(!is_first_load && $('div.plz_result').css('height') && $('div.plz_result').css('height') != '0px'){
            $('div.plz_result').animate({
                opacity: 0,
                height: 0
            }, 2000, function(){
                $(this).css('display', 'none');
            });
        }

        // Profile nachladen
        n.load('/profile/list/list.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&num=' + $('#page_num').val() + filter, function(){
            ShowLoadImage(false);
        // location.href = '#profile';
        });
    }
    return false;
}

// Seite neuladen nach Klick auf Buchstabe
$('a.ReloadByLetter').live('click', function(){
    $('a.ReloadByLetter').removeClass('bold');
    if($(this).text().length == 1){
        $(this).addClass('bold');
        $('input[name=filter_nachname]').val('n' + $(this).text());
        $('div.menuletter span').css('display', 'inline');
    }
    else{
        $('input[name=filter_nachname]').val('');
        $('div.menuletter span').css('display', 'none');
    }
    ReloadProfiles($(this));
    return false;
});


// Seite neuladen nach Klick auf Seitenzahl
function GetCurrentPageNum(elem_clicked){
    if($(elem_clicked).text().length < 4){
        return parseInt($(elem_clicked).text());
    }
    else{
        var n_pages = FindDependingNode($(elem_clicked), 'pages');
        var page_num = parseInt(n_pages.find('b').text());
        page_num += $(elem_clicked).parent().attr('class').indexOf('next') > -1?1:-1;
        return page_num;
    }
}
$('a.ReloadByPageProfiles').livequery('click', function(){
    var page_num = GetCurrentPageNum($(this));
    $('#page_num').val(page_num);
    ReloadProfilesNEU($(this), true);
    return false;
});
$('a.ReloadByPageProfile').livequery('click', function(){
    var page_num = GetCurrentPageNum($(this));
    $('#page_num').val(page_num);
    ReloadQuestions($(this), true);
    return false;
});

// Hoehe des Textes festlegen
$('div.profil div.pe_version div.reload,div.profil div.pe_version div.pe_text').livequery(function(){
    $(this).css('height', saved_height+'px');
});

// Info-Seite zur PE oeffnen
function OpenPeInfo(){
    var w = window.open('http://www.abgeordnetenwatch.de/profile/profilerweiterung.html','PEINFO','width=650,height=700,top=10,left=10,scrollbars=yes');
    w.focus();
//return false;
}

// ### ANFANG - TOOLBAR ###
// Tools bei Antworten anzeigen
$('div.questions div.tools').livequery(function(){
    $(this).css('display', 'block');
});

// Empfehlung zaehlen
$('div.questions div.tools div.recommendation').live('click', function(){

    // Mail-Knoten suchen
    var n_mail = $(this).closest('div[id^=mail_]');

    // Anzahl Empfehlungen suchen und hochzaehlen
    var n_recomm = n_mail.find('div.answer div.recommendation span:first');
    if(n_recomm){
        var num_recomm = parseInt(n_recomm.text());
        n_recomm.text(num_recomm?num_recomm+1:1);
    }

    // Empfehlung / EmpfehlungEN
    n_recomm = n_mail.find('div.answer div.recommendation span:last');
    if(n_recomm){
        n_recomm.text(num_recomm?a_conf.localization['empfehlungen']:a_conf.localization['empfehlung']);
    }

    // Zaehlen
    $.get('/scripts/ajax/recomm.php', {
        mail_id:n_mail.attr('id').substr(5)
    });

    // Danke faden
    $(this).fadeOut('slow', function(){
        $(this).html('<b>' + a_conf.localization['vielen_dank'] + '</b>').fadeIn('slow');
    });
    $(this).die('click');
    return false;
});

// Knoten toolbar_options finden
function GetNodeToolbarOptions(elem){
    var n_toolbar = FindDependingNode($(elem), 'toolbar');
    if(n_toolbar && n_toolbar.prev().attr('class') == 'toolbar_options'){
        return n_toolbar.prev();
    }
    return false;
}

// E-Mail-Adresse zur Benachrichtigung eintragen
function InsertEmailInform(frm){
    // Mail-Knoten suchen
    var n_mail = $(frm).closest('div[id^=mail_]');
    $.get('/scripts/ajax/inform_answer.php', {
        mail_id: n_mail.attr('id').substr(5),
        email: $(frm).find('.[name=inform_email]').val()
    }, function(data){
        var n_toolbar_options = FindDependingNode($(frm), 'toolbar_options');
        if(data == 'inserted'){
            n_toolbar_options.find('.container').fadeOut('slow', function(){
                $(this).html('<b>' + a_conf.localization['vielen_dank'] + '</b> ' + a_conf.localization['inform_antwort']).fadeIn('slow');
            });
            var n_tool_inform = FindDependingNode($(frm), 'div.inform');
        }
        else if(data == 'exists'){
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['hinweis'] + ':</b> ' + a_conf.localization['adresse_exists']);
        }
        else{
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['fehler'] + ':</b> ' + a_conf.localization['adresse_error']);
        }
    });
    if($(frm).find('.[name=order_newsletter]:checked').length > 0){
        SaveNewsletterMember($(frm).find('.[name=inform_email]').val());
    }
    return false;
}

// Bookmark eintragen
function InsertBookmark(frm){
    // Mail-Knoten suchen
    var n_mail = $(frm).closest('div[id^=bookmark_]');
    $.get('/scripts/ajax/bookmark.php', {
        mail_id: n_mail.attr('id').substr(9),
        descr: $(frm).find('.[name=descr]').val()
    }, function(data){
        var n_toolbar_options = FindDependingNode($(frm), 'toolbar_options');
        if(data == 'inserted'){
            n_toolbar_options.find('.container').fadeOut('slow', function(){
                $(this).html(a_conf.localization['set_bookmark']).fadeIn('slow');
            });
            var n_tool_inform = FindDependingNode($(frm), 'div.bookmark');
        }
        else if(data == 'max_reached'){
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['hinweis'] + ':</b> ' + a_conf.localization['max_reached']);
        }
        else if(data == 'exists'){
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['hinweis'] + ':</b> ' + a_conf.localization['bookmark_exists']);
        }
        else if(data == 'register'){
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['hinweis'] + ':</b> ' + a_conf.localization['login']);
        }
        else{
            n_toolbar_options.find('.container div.text').html('<b class="blink">' + a_conf.localization['fehler'] + ':</b> ' + a_conf.localization['descr_error']);
        }
    });
    return false;
}

// Bookmark setzen
$('div.questions div.tools div.bookmark').live('click', function(){
    var n_toolbar_options = GetNodeToolbarOptions($(this));
    n_toolbar_options.animate({
        opacity: 0
    }, 500, function(){
        $(this).find('.container').html($('div#set_bookmark').html());
        $(this).animate({
            height: '80px',
            opacity: 1
        }, 'slow', 'swing');
    });
    return false;
});

// Benachrichtigen beim Eintreffen einer Antwort
$('div.questions div.tools div.inform').live('click', function(){
    var n_toolbar_options = GetNodeToolbarOptions($(this));
    n_toolbar_options.animate({
        opacity: 0
    }, 500, function(){
        $(this).find('.container').html($('div#inform_email').html());
        $(this).animate({
            height: '120px',
            opacity: 1
        }, 'slow', 'swing');
    });
    return false;
});

// Perma-Link
$('div.questions div.tools div.permalink').live('click', function(){
    var n_toolbar_options = GetNodeToolbarOptions($(this));

    // Mail-Knoten suchen
    var n_mail = $(this).closest('div[id^=mail_]');

    // Frage ID
    var frage_id = $(this).find('span').attr('id').substr(9);

    // Link bauen
    var link = $('div#perma_link').find('input').val();
    link = link.replace(/__mail_id__/g, frage_id);

    n_toolbar_options.animate({
        opacity: 0
    }, 500, function(){
        $(this).find('.container').html($('div#perma_link').html());
        $(this).find('.container input').val(link);
        $(this).animate({
            height: '80px',
            opacity: 1
        }, 'slow', 'swing');
    });
    return false;
});

$('div.questions div.toolbar_options div.close').live('click', function(){
    var n_toolbar_options = FindDependingNode($(this), 'toolbar_options');
    n_toolbar_options.animate({
        height: 0,
        opacity: 0
    }, function(){
        n_toolbar_options.css('display', 'none');
    });
});
// ### ENDE - TOOLBAR ###
