var addthis_config = {
    ui_click: true
}


$(document).ready(function() {


    var doBlink = function(obj,start,finish) { jQuery(obj).fadeOut(300).fadeIn(300); if(start!=finish) { start=start+1; doBlink(obj,start,finish); } }     

    // Auf Änderungen des Landes reagieren
    $("#land").change(function(){
        var land=$(this).children('option:selected').val();
        var bundesland = 'x0';
        var bezirk = 'x0';

        $("#bundesland").load("/ajax/Land",{land: land });
        $("#bezirk").load("/ajax/Bundesland",{bundesland: bundesland }); 
        $("#ort").load("/ajax/Bezirk",{bezirk: bezirk });        
        doBlink("#bundesland",1,1);
        doBlink("#bezirk",1,1);
        doBlink("#ort",1,1);
        if(document.getElementById('objektart')){
            $("#objektart").load("/ajax/Objektart",{land: land, bundesland: bundesland });
            doBlink("#objektart",1,1);
        }

    });


    // auf Änderungen des Bundeslandes reagieren
    $("#bundesland").change(function(){
        var bundesland=$(this).children('option:selected').val();
        var bezirk = 'x0';

        $("#bezirk").load("/ajax/Bundesland",{bundesland: bundesland });
        doBlink("#bezirk",1,1);

        $("#suchFieldsetOrt").load("/ajax/Bezirk",{bezirk: bezirk });        
        doBlink("#suchFieldsetOrt",1,1);

        //         Falls eine Objektart vorhanden ist
        if(document.getElementById('objektart')){
            $("#objektart").load("/ajax/Objektart",{bundesland: bundesland });
            doBlink("#objektart",1,1);
        }

    });

    // Auf Änderung des Bezirkes reagieren
    $("#bezirk").change(function(){
        var  bundesland = document.getElementById('bundesland').value;
        var land = document.getElementById('land').value;
        var bezirk=$(this).children('option:selected').val();

        if($('input[name="ort[]"]')){
            $("#suchFieldsetOrt").load("/ajax/Bezirk",{bezirk: bezirk});
            doBlink("#suchFieldsetOrt",1,1);
            //            alert('FROM Bezirk');
        }
        // Falls eine Objektart vorhanden ist
        if(document.getElementById('objektart')){
            $("#objektart").load("/ajax/Objektart",{land: land, bundesland: bundesland, bezirk: bezirk});

            //            doBlink("#objektart",1,1);
        }
    });

    // Auf Änderung des Ortes reagieren
    //    $("#ort").change(function(){
    //        var  bundesland = document.getElementById('bundesland').value;
    //        var land = document.getElementById('land').value;
    //        var bezirk=document.getElementById('bezirk').value;   
    //        var ort = $(this).children('option:selected').val();

    // Falls eine Objektart vorhanden ist
    //        if(document.getElementById('objektart')){
    //            $("#objektart").load("/ajax/Objektart",{land: land, bundesland: bundesland, bezirk: bezirk, ort:ort});

    //            doBlink("#objektart",1,1);
    //        }
    //    });



    // Suchprofile ########################################

    // Auf Änderungen des Landes reagieren
    $("#sp_land").change(function(){
        var land=$(this).children('option:selected').val();
        var bundesland = 'x0';
        $("#sp_bundesland").load("/ajax/sp_Land",{land: land });
        $("#sp_bezirk").load("/ajax/sp_clear");
        $("#sp_ort").load("/ajax/sp_clear");
        doBlink("#sp_bundesland",1,1);
        doBlink("#sp_bezirk",1,1);
        doBlink("#sp_ort",1,1);
    });


    // auf Änderungen des Bundeslandes reagieren
    $("#sp_bundesland").change(function(){
        var bundesland=$(this).children('option:selected').val();
        $("#sp_bezirk").load("/ajax/sp_Bundesland",{bundesland: bundesland });
        $("#sp_ort").load("/ajax/sp_clear");
        doBlink("#sp_bezirk",1,1);
        doBlink("#sp_ort",1,1);        

    });

    // Auf Änderung des Bezirkes reagieren
    $("#sp_bezirk").change(function(){
        var bezirk=$(this).children('option:selected').val();
        $("#sp_ort").load("/ajax/sp_bezirk",{bezirk: bezirk });
        doBlink("#sp_ort",1,1);
    });


         


    $("input.mailField").focus(function(){
        if($(this).val()=='mail'){
            $(this).val('');
            $('#doggy').removeClass("head"); 
            $('#doggy').addClass("headAktiv");
        }
        return false;
    })           

    $("input.mailField").blur(function(){
        if($(this).val()==''){
            $(this).val('mail');
            $('#doggy').removeClass("headAktiv"); 
            $('#doggy').addClass("head");                    
        }
        return false;
    })               


    // Auf Änderung des Bezirkes reagieren
    $("#orderby").change(function(){
        $("#immoOrder").submit();
        return false;
    });              

    // ###########################################################################

    $('a.DetailPageRight').click(function(){
        var seite = document.getElementById('seite').value;
        var last = document.getElementById('lastListPage').value;
        if(parseInt(seite) < parseInt(last)){
            var height = $("#immoDetailListObjects").height();          
            $("#immoDetailListObjects").height(height);            
            $("#immoDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
            $("#immoDetailListObjects").load("/ajax/Objektlist",{seite: seite, direction: 'right'});
        }

    })

    $('a.DetailPageLeft').click(function(){

        var seite = document.getElementById('seite').value;
        if(parseInt(seite) > 1){
            var height = $("#immoDetailListObjects").height();          
            $("#immoDetailListObjects").height(height);
            $("#immoDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
            $("#immoDetailListObjects").load("/ajax/Objektlist",{seite: seite, direction: 'left'});
        }

    })

    $('a.DetailProjektPageRight').click(function(){
        var seite = document.getElementById('seite').value;
        var last = document.getElementById('lastListPage').value;

        if(parseInt(seite) < parseInt(last)){
            var height = $("#projektDetailListObjects").height();          
            $("#projektDetailListObjects").height(height);            
            $("#projektDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
            $("#projektDetailListObjects").load("/ajax/Projektlist",{seite: seite, direction: 'right'});
        }

    })

    $('a.DetailProjektPageLeft').click(function(){

        var seite = document.getElementById('seite').value;
        if(parseInt(seite) > 1){
            var height = $("#projektDetailListObjects").height();          
            $("#projektDetailListObjects").height(height);
            $("#projektDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
            $("#projektDetailListObjects").load("/ajax/Projektlist",{seite: seite, direction: 'left'});
        }

    })  

    $('div.immoDetailListCountWrapper').find('a').click(function(){
        var perPage = $(this).html();
        var height = 120;
        var multi = Math.floor(perPage/5);
        if(multi>0){
            height = multi * 120;
        }
        $("#immoDetailListObjects").height(height);            
        $("#immoDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
        $("#immoDetailListObjects").load("/ajax/Objektlist",{perPage: perPage});
    })

    $('div.projektDetailListCountWrapper').find('a').click(function(){
        var perPage = $(this).html();
        var height = 120;
        var multi = Math.floor(perPage/5);
        if(multi>0){
            height = multi * 120;
        }
        $("#projektDetailListObjects").height(height);            
        $("#projektDetailListObjects").empty().html('<img src="/images/loading.gif" class="AjaxLoading"/>');
        $("#projektDetailListObjects").load("/ajax/Projektlist",{perPage: perPage});
    })        

    $("a.empfehlen").click(function(){
        var url=$(this).attr('href')+"/js";
        $.fn.colorbox({href:url,width:"420px", height:"410px", open:true, iframe:true});
        return false;
    })

    $("a.kontakt").click(function(){
        var url=$(this).attr('href')+"/js";
        $.fn.colorbox({href:url,width:"750px", height:"570px", open:true, iframe:true});
        return false;
    })

    $("a.interesse").click(function(){
        var url=$(this).attr('href')+"/js";
        $.fn.colorbox({href:url,width:"750px", height:"570px", open:true, iframe:true});
        return false;
    })        
    $("a.merken").click(function(){
        var url=$(this).attr('href')+"/js";
        $.fn.colorbox({href:url,width:"420px", height:"350px", open:true, iframe:true});
        return false;
    })        

    $("a.drucken").click(function(){
        var url=$(this).attr('rel');
        window.open(url,'drucken','width=600,height=600,scrollbars=yes');
        return false;
    })          

    $('a[rel="lostpwForm"]').click(function(){
        var url=$(this).attr('href')+"/js";
        $.fn.colorbox({href:url,width:"420px", height:"350px", open:true, iframe:true});
        return false;
    })

    $("a[rel=\'registerForm\']").click(function(){
        var url=$(this).attr('href')+"";
        $.fn.colorbox({href:url,width:"420px", height:"400px", open:true, iframe:true});
        return false;
    })

    $("#butNewsletterAnmelden").click(function(){
        //        var url=$(this).attr('href')+"/js";
        var url='/myImmoagent/newslettersubscribe/js/1';
        $.fn.colorbox({href:url,width:"500px", height:"350px", open:true, iframe:true});
        return false;
    })

    $("#butNewsletterAbmelden").click(function(){
        //        var url=$(this).attr('href')+"/js";
        var url='/myImmoagent/newsletterunsubscribe/js/1';
        $.fn.colorbox({href:url,width:"500px", height:"350px", open:true, iframe:true});
        return false;
    })

    $("#butSuchprofilSpeichern").click(function(){
        var url=$(this).attr('href')+'js/1/';
        //        var url='/myImmoagent/newsletmetterunsubscribe/js/1';
        $.fn.colorbox({href:url,width:"400px", height:"400px", open:true, iframe:true});

        return false;
    })

    $('#butSubmitSuchprofil').click(function(){
        if ( $('#tbxSearchname').val() == '' ) { alert('Bitte geben Sie einen Namen zur Speicherung Ihres Suchprofils an.'); return false; }

        // Orte auslesen
        try {
            orte = new Array();
            result = parent.$('input[name="ort[]"]:checked');

            for ( i = 0 ; i < result.length ; i++ ) {
                orte[i] = result[i].value;
            }
        } catch (e) {}

        var url=$(this).attr('href')+'js/1/save/1/';
        $("#wrapper").load(url,{
            land: parent.$('#land').val(), bundesland: parent.$('#bundesland').val(),
            region: parent.$('#bezirk').val(), ort: parent.$('#ort').val(),
            kauf: parent.$('#typ-x1').is(':checked'), mietkauf: parent.$('#typ-x2').is(':checked'), 
            miete: parent.$('#typ-x3').is(':checked'), pacht: parent.$('#typ-x4').is(':checked'), 
            objektart: parent.$('#objektart').val(), rdbEmailYes: $('#rdbEmail-1').is(':checked'), 
            rdbEmailNo: $('#rdbEmail-0').is(':checked'), tbxSearchname: $('#tbxSearchname').val(), 
            zustand: parent.$('#zustand').val(), volltext: parent.$('#volltext').val(),
            zimmerVon: parent.$('#zimmerVon').val(), zimmerBis: parent.$('#zimmerBis').val(),
            flaecheVon: parent.$('#flaecheVon').val(), flaecheBis: parent.$('#flaecheBis').val(),
            ort: orte 

        });
        //        $.fn.colorbox({href:url,width:"500px", height:"350px", open:true, iframe:true});
        return false;

    })

    $("#switchAnsprechpartner").click(function(){
        $("#DetailWrapperAnsprechpartner").toggle(800);
    });


    if($('#result')) {
        setTimeout('fadeOut(\'result\')',3000);
    }

    // My-Immoagent-Box neu laden bei login über Suchprofile
    try {
        if(document.getElementById('suchprofilSaveForm') && location.href.indexOf('suchprofilsave') ) {
            parent.$('#myImmoagent').load('/ajax/myimmoagentbox/js/1/',{});            
        }
    } catch (e) {}



    $('a[rel="video"]').click(function(){
        url = this.href;
        $.fn.colorbox({href:url,width:"550px", height:"410px", iframe:true});
        return false;
    })

    if($('#socialshareprivacy').length > 0){
        $('#socialshareprivacy').socialSharePrivacy(); 
    }

    try {
        $('#butSuchprofilSpeichern').cluetip({
            splitTitle: '|', 
            showTitle: true,
            width: 350,
            titleAttribute: 'title',
            topOffset: 50
        });
    } catch (e) {}

    /*    $('#houdini').cluetip({
    splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
    // ...and split the contents into separate divs where there is a "|"
    showTitle: false // hide the clueTip's heading
    });    
    */



});


function fadeOut (id){
    $('#'+id).slideToggle(2000);
}




    

