$(document).ready(function(){

    // Ausklappen der Antwort-Statistik nach Parteien
    $("#statistik_link").click(function() {
        $("#statistik_alle").slideToggle("slow");
        $("#statistik_alle").load('/startseite/antwort_statistik.php?build=1&show=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
            $("#statistik_alle").slideToggle(7000);
        });
    });

    // colorboxen binden
    $('a.cboxElement').colorbox({
        width: '700px',
        height: '75%',
        iframe:true
    });
    $('a.cboxElement[rel=newsletter]').colorbox({
        height: '520px'
    });
    $('a.cboxElement[rel=abgeordnetenquartett]').colorbox({
        width: '950px',
        height: '750px',
        scrolling: 'false'
    });
    $('a.cboxElement[rel=bundestagsradar]').colorbox({
        width: '950px',
        height: '900px',
        scrolling: 'false'
    });
    $('div#header_links a.cboxElement[rel=recommend]').colorbox({
        width:'500px',
        height:'600px'
    });

    // Customized Scrollbalken hinzufuegen
    $('div.scroll-pane').livequery(function(){
        if(!$.browser.msie || jQuery.browser.version.substr(0,1) > 6){
            $(this).jScrollPane({
                showArrows:true,
                arrowSize:12,
                scrollbarWidth:11
            });
        }
    });
});

// intelligentes on:blur
$("input[type=text],textarea").livequery('blur', function() {
    if($(this).val() == "") {
        $(this).val($(this).attr("alt"));
    /* if($(this).attr('id') != 'thema'){ $(this).css("color","#a1a1a1"); } */
    }
});

// intelligentes on:focus
$("input[type=text],textarea").livequery('focus', function() {
    $(this).css("color","#000");
    if($(this).val() == $(this).attr("alt")) {
        $(this).val("");
    }
});

// Allen Tabs auf der Seite onclick-Methode verpassen
$('div.tabs li').livequery('click', function() {
    if($(this).attr('class').indexOf('disabled') == -1){
        $(this).parent().children('li').each(function() {
            $(this).removeClass('selected').addClass('enabled');
        });
        $(this).removeClass('enabled').addClass('selected');
        if($(this).attr('id')){
            ReloadTab($(this).attr('id'));
            return false;
        }
        return true;
    }
    return false;
});

// Klicks zaehlen, z.B. um Performance von Buttons zu ermitteln (#c_)
if(location.hash.length > 3 && location.hash.substr(0, 3) == '#c_'){
    $.get("/scripts/ajax/count_klicks.php?art=" + location.hash.substr(3), function(){
        location.href = location.href.substr(0, location.href.indexOf('#'));
    });
}

// Ergebnis blinken lassen
$('.blink').livequery(function(){
    $(this).animate({
        opacity: 0
    }, 500, function(){
        $(this).children().css('color', '#f00');
        $(this).css('color', '#f00');
        $(this).animate({
            opacity: 1
        }, 1000, function(){
            $(this).animate({
                opacity: 0
            }, 1500, function(){
                $(this).animate({
                    opacity: 1
                }, 1000);
            });
        });
    });
});

// onclick auf Card bei Profilen (Liste)
$('div.card').livequery('click', function(){

    // Schauen, ob base target gesetzt
    if($('base').length == 1 && $('base').attr('target') == '_blank'){
        window.open($(this).find('a').attr('href'));
    }
    else{
        location.href = $(this).find('a').attr('href');
    }
    return false;
});

// Debug
function DumpObject(obj){
    var str = '';
    for (var a in obj){
        str += a+"\n";
    }
    alert(str);
}

// Weiterlesen-Text anzeigen
function TextReadOn(a_elems_off){
    if(a_elems_off){
        // Elemente unsichtbar machenecho $_GET['show'];
        $('div.readon').animate({
            opacity: 0
        }, 500);
        for(var i=0;i<a_elems_off.length;i++){
            a_elems_off[i].animate({
                height: 0
            }, 500, function(){
                if(i == a_elems_off.length){

                    // Willkommenstext anzeigen
                    $('div.pe_welcome').animate({
                        height: saved_height+'px'
                    }, 1500, function(){
                        $('div.pe_welcome').addClass('scroll-pane');
                        $('div.pe_welcome').css('height', 'auto');
                    });
                }
            });
        }
    }
    return false;
}

// Lade Grafik anzeigen / verstecken
function ShowLoadImage(n){
    if(n){
        var n = FindDependingNode(n, 'loading');
        if(n) $(n).css('display', 'inline');
    }
    else{
        $('img.loading').each(function(){
            $(this).css('display', 'none');
        });
    }
}

// In der Herarchie hoeher steigen bis in den Kindknoten oder in dem Knoten
// selbst das Ziel des loads auftaucht (class=reload).
function FindDependingNode(n, cls_name){
    while(n.parents().length > 0){
        if(n.attr('class').indexOf(cls_name) > -1){
            return n;
        }
        n = n.parent();
        if(n.find('.'+cls_name).length > 0){
            return n.find('.'+cls_name+':first');
        }
    }
    return false;
}

// Profilbild ein- und ausblenden
function ProfilPortraitVisible(visible){
    var op = visible?1:0;
    $('div.pe_version div.pe div.portrait').animate({
        opacity: op
    }, 1000);
}

// Bei einem Klick auf einen Tab Inhalte nachladen.
function ReloadTab(tab_id, reload_name){

    if($('.tabs:has(#'+tab_id+')')){

        // Name / Klasse des Reloadknotens bestimmen
        reload_name = reload_name || 'reload';

        // Tabs-Collection (<div>) raussuchen in dem der geklickte Tab liegt.
        var n_tab = $('.tabs:has(#'+tab_id+')');

        var n_reload = FindDependingNode(n_tab, reload_name);

        // Reloadknoten wurde gefunden
        if(n_reload && $('#'+tab_id+' a').length > 0){
            // n_reload.children().filter('.effect').each(function(){ });

            // Loading...
            ShowLoadImage(n_reload);

            // Anhand der ID des Tabs entscheiden welche Datei nachgeladen
            // werden soll.
            var param_show = tab_id.indexOf('tab_') > -1?tab_id.substr(4):tab_id;
            switch(param_show){

                // Tabs fuer Startseite
                case 'antworten_wahlen':
                    n_reload.load('/startseite/antworten/list.php?build=1&show=antworten_wahlen&filter=all&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'antworten_parlamente':
                    n_reload.load('/startseite/antworten/list.php?build=1&show=antworten_parlamente&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;

                // Tabs fuer parlamentarische Arbeit bei Abgeordneten
                case 'profil_ausschussmitgliedschaften':
                    n_reload.parent().load('/profile/parlament.php?build=1&show=ausschussmitgliedschaften&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profil_redebeitraege':
                    n_reload.parent().load('/profile/parlament.php?build=1&show=redebeitraege&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profil_nebentaetigkeiten':
                    n_reload.parent().load('/profile/parlament.php?build=1&show=nebentaetigkeiten&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profil_abstimmungsverhalten':
                    if($('#'+tab_id+' a').attr('name')){
                        var load_url = '/profile/parlament.php?build=1&show=abstimmungsverhalten&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=' + $('#'+tab_id+' a').attr('name');
                        $('#'+tab_id+' a').removeAttr('name');
                    }
                    else{
                        var load_url = '/profile/parlament.php?build=1&show=abstimmungsverhalten&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=all';
                    }
                    n_reload.parent().load(load_url, function(){
                        ShowLoadImage(false);
                    });
                    break;

                // Tabs fuer Abstimmungsverhalten auf Abstimmungsdetailseite
                case 'abstimmungsverhalten':
                    n_reload.load('/abstimmungen/abstimmungsverhalten.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'abstimmung':
                    n_reload.load('/abstimmungen/hintergrund.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
					
                // Tabs fuer Ausschusseite
                case 'mitglieder':
                    n_reload.load('/ausschuesse/list.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'stellvertreter':
                    n_reload.load('/ausschuesse/list.php?build=1&show=stellvertreter&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;

                // Tabs oben in der PE
                case 'profil_grunddaten':
                    var load_url = '/profile/pe/grunddaten.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
                case 'profil_uebersicht':
                    if(!load_url) var load_url = '/profile/pe/uebersicht.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
                case 'profil_person':
                    if(!load_url) var load_url = '/profile/pe/person.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
                case 'profil_ziele':
                    if(!load_url) var load_url = '/profile/pe/ziele.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
                /*
 * case 'profil_video': if(!load_url) var load_url =
 * '/profile/pe/video.php?build=1&cmd=' + a_conf.cmd + '&id=' +
 * a_conf.id;
 */
                case 'profil_termine':
                    if(!load_url){
                        if($('#'+tab_id+' a').attr('name')){
                            var load_url = '/profile/pe/termine.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&filter=' + $('#'+tab_id+' a').attr('name');
                            $('#'+tab_id+' a').removeAttr('name');
                        }
                        else{
                            var load_url = '/profile/pe/termine.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;
                        }
                    }
                case 'profil_links':
                    if(!load_url) var load_url = '/profile/pe/links.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id;

                    // Profilbild ausblenden
                    /*
 * if(param_show == 'profil_video'){
 * ProfilPortraitVisible(false); }
 */

                    // Aktuelle Inhalte ausblenden
                    n_reload.animate({
                        opacity: 0
                    }, 1000, function(){
                        // Neue Inhalte einblenden
                        $(this).load(load_url, function(){
                            $(this).animate({
                                opacity: 1
                            }, 0, function(){
                                ShowLoadImage(false);
                            });
                        });
                    });
                    break;

                // Tabs bei den Fragen und Antworten
                case 'profil_fragen_antworten':
                    n_reload.load('/profile/public_questions.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profil_twitter':
                    n_reload.load('/profile/public_twitter.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profil_kandidatencheck':
                    n_reload.load('/profile/public_kandidatencheck.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'kandidatencheck_starten':
                    n_reload.load('/profile/kandidatencheck_starten.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'kandidatencheck_antworten':
                    n_reload.load('/profile/kandidatencheck_antworten.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;

                // Tabs in der Liste der Profile
                case 'profile_wahlkreise':
                    n_reload.load('/profile/list/profile_wahlkreise.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'profile_liste':
                    n_reload.load('/profile/list/profile_liste.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&show=' + param_show, function(){
                        ShowLoadImage(false);
                    });
                    break;
                default:
                    if(param_show.indexOf('_plz_') > -1){
                        n_reload.load('/startseite/einstieg_plz.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id + '&show=' + param_show, function(){
                            ShowLoadImage(false);
                        });
                    }
                    break;

                // Tabs im Mitgliederbereich
                case 'mitglieder_login':
                    n_reload.load('/mitglieder/form_login.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'mitglieder_register':
                    n_reload.load('/mitglieder/form_register.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'mitglieder_no_password':
                    n_reload.load('/mitglieder/form_password.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
                case 'mitglieder_politiker':
                    n_reload.load('/mitglieder/politiker.php?build=1&cmd=' + a_conf.cmd + '&id=' + a_conf.id, function(){
                        ShowLoadImage(false);
                    });
                    break;
            }
            return false;
        }
    }
    return true;
}

// Pop-Up fuer Share-Buttons von Facebook, Twitter & Co
function OpenWinShare(url){
    var win = window.open(url, 'SHARE', 'width=790,height=480,top=0,left=0,scrollbars=1');
    win.focus();
    $.get('/tools/count_shares.php', {
        url: url
    }, function(data) {
        if(data != ''){
            $.get("/scripts/ajax/count_klicks.php?art=" + data);
        }
    });
}

// Email fuer Newsletter speichern
function SaveNewsletterMember(email, msg_container, localization){
    if(msg_container){
        msg_container.html('<img src="/images/loading.gif" style="margin: 5px" />');
        msg_container.removeClass();
    }
    $.get('/tools/newsletter.php', {
        email: email,
        is_ajax: 1
    }, function(data) {
        if(msg_container){
            if(localization == 'en_IE'){
                switch(data){
                    case 'email_error':
                        msg_container.html('Please check this email-address.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'unknown_error':
                        msg_container.html('Something went wrong. Please try again or inform the team of candidatewatch.ie.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'already_in_list':
                        msg_container.html('You are already registered.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'please_confirm':
                        msg_container.html('Please confirm the email we have sent you.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'success':
                        msg_container.html('Thank you! You will receive an e-mail with a link to confirm your registration.');
                        msg_container.addClass('msg_success');
                        break;
                }
            }
            else{
                switch(data){
                    case 'email_error':
                        msg_container.html('Bitte &uuml;berpr&uuml;fen Sie die E-Mail-Adresse.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'unknown_error':
                        msg_container.html('Da ist etwas schief gelaufen. Bitte informieren Sie das Team von abgeordnetenwatch.de');
                        msg_container.addClass('msg_error');
                        break;
                    case 'already_in_list':
                        msg_container.html('Sie sind bereits angemeldet.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'please_confirm':
                        msg_container.html('Bitte best&auml;tigen Sie die E-Mail, die wir Ihnen geschickt haben.');
                        msg_container.addClass('msg_error');
                        break;
                    case 'success':
                        msg_container.html('Vielen Dank! Sie sollten nun eine E-Mail bekommen, in der Sie zur Best&auml;tigung einen Link anklicken m&uuml;ssen.');
                        msg_container.addClass('msg_success');
                        break;
                }
            }
        }
    });
    return false;
}

// Quartett Fenster oeffnen
function OpenWinQuartett(){
    var win = window.open('http://www.abgeordnetenwatch.de/popup-281-0.html#c_20111207_quartett', 'QUARTETT', 'width=900,height=700,left=0,top=0,scrollbars=0');
    win.focus();
}

// Bundestagsradar Fenster oeffnen
function OpenWinRadar(){
    var win = window.open('http://www.abgeordnetenwatch.de/popup-720-0.html#c_20111207_bundestagsradar', 'RADAR', 'width=900,height=850,left=0,top=0,scrollbars=0');
    win.focus();
}
