function replace_unicode(s) // needed for IE (doesn't like unicode umlauts in email link)
{
   s = s.replace(/[\u00e4]/g, "%C3%A4"); // ae
   s = s.replace(/[\u00f6]/g, "%C3%B6"); // oe
   s = s.replace(/[\u00fc]/g, "%C3%BC"); // ue
   s = s.replace(/[\u00c4]/g, "%C3%84"); // AE
   s = s.replace(/[\u00d6]/g, "%C3%96"); // OE
   s = s.replace(/[\u00dc]/g, "%C3%9C"); // UE
   return s;
}

/*--------------------------------------------------------------------------------*/
function gup(name) // get url parameter
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
    var r = regex.exec(window.location.href);
    if(r) return r[1];
    else return "";
}

/*--------------------------------------------------------------------------------*/
function display_results(data)
{
    var header = data["header"];
    var results = data["results"];

    var s = '';
    
    var start = header["results_von"];
    var end = header["results_bis"];
    var total = header["results_total"];
    var pages = Math.ceil(total / im_objects_per_page);
    var current_page = Math.ceil(start / im_objects_per_page);
    
    if(total == 0)
    {
        document.getElementById("im_website").innerHTML = "Hier gibt es momentan keine Eintr&auml;ge!";
        return;
    }
      
    // ---------------------------------------- top navigation
    s +=
    '<div class="cnavigation"><table><tbody><tr>' +
    '<td class="cleft">Objekte <b>' + start + '</b> bis <b>' + end + '</b> von <b>' + total + '</b></td>' +
    '<td class="cright">';

    if(pages > 1)
    {
        if(current_page > 1)
            s += '<a href="javascript:im_update_page(von=' + ((current_page-2)*im_objects_per_page+1) + ')">< zur&uuml;ck</a> ';
        for(var i = 1; i <= pages; i++)
        {
            if(i != current_page)
                s += '<a href="javascript:im_update_page(von=' + ((i-1)*im_objects_per_page+1) + ')">' + i + '</a> ';
            else
                s += '<b>' + i + '</b> ';
        }
        if(current_page < pages)
            s += '<a href="javascript:im_update_page(von=' + (current_page*im_objects_per_page+1) + ')">weiter ></a> ';
    }
    
    s +=
    '</td>' +
    '</tr></tbody></table></div>';
    
    // ---------------------------------------- objects
    s += '<div class="cresults"><ul>';
    
    var i = 0;
    for(o in results)
    {
        i++;
    
    	var id = o;
    	var status = results[o]["objekt_status"];
    	var image = results[o]["pre_img"];
    	var image_size = results[o]["pre_img_size"];
        var title = results[o]["objekt_title"];
        var type = results[o]["objekt_art"];
        var zip = results[o]["objekt_plz"];
        var city = results[o]["objekt_ort"];
        var price_type = results[o]["objekt_preisname"];
        var price = results[o]["objekt_preis"];
        var area = results[o]["objekt_flaeche"];
        var description = results[o]["objekt_description"];
        
        //console.log(results);
        
        s +=
        '<li class="crow' + i%2 + '"><table class="cobject"><tbody><tr>' +
        '<td class="cleft">';
        
        if(image_size > 0)
        {
            s +=
            '<a href="javascript:im_show_details(' + id + ');">' +
            '<img alt="' + title + '" src="' + image + '"/>' +
            '</a>';
        }
        
        s +=
        '</td>' +
        '<td class="cright">' +
        '<h2><a href="javascript:im_show_details(' + id + ');">' + title + '</a></h2>' +
        '<b>' + type + '</b> | ' + price_type + ': <b>' + price + '</b> | Fl&auml;che: <b>' + area + '</b> | <b>' + zip + ' ' + city + '</b>' +
        '<br/>' + description + '... '+
        '<a href="javascript:im_show_details(' + id + ');">>> zum Objekt</a>' +
        '</td>' +
        '</tr></tbody></table></li>'
    }
    
    s += '</ul></div>';
    
    // ---------------------------------------- bottom navigation
    s +=
    '<div class="cnavigation"><table><tbody><tr>' +
    '<td class="cleft"></td>' +
    '<td class="cright">';

    if(pages > 1)
    {
        if(current_page > 1)
            s += '<a href="javascript:im_update_page(von=' + ((current_page-2)*im_objects_per_page+1) + ')">< zur&uuml;ck</a> ';
        for(var i = 1; i <= pages; i++)
        {
            if(i != current_page)
                s += '<a href="javascript:im_update_page(von=' + ((i-1)*im_objects_per_page+1) + ')">' + i + '</a> ';
            else
                s += '<b>' + i + '</b> ';
        }
        if(current_page < pages)
            s += '<a href="javascript:im_update_page(von=' + (current_page*im_objects_per_page+1) + ')">weiter ></a> ';
    }
    
    s +=
    '</td>' +
    '</tr></tbody></table></div>';
    
    document.getElementById("im_website").innerHTML = s;

}

/*--------------------------------------------------------------------------------*/
function display_details(data)
{
    var header = data["header"];
    var o = data["immo"];
    var details = data["details"];
    var downloads = data["downloads"];
    var images = data["bilder"];
    
    //console.log(o);

    var s = '';
    
    s +=
    '<div class="cnavigation"><a href="javascript:im_back_to_results();"><< zur&uuml;ck</a></div>' +
    '<div class="cdetails">' +
    
    '<table class="cobject"><tbody><tr>' +
    '<td class="cleft">';

    // ---------------------------------------- images
    if(o["image_counttotal"] > 0)
    {
        for(i in images)
        {
            s += 
            '<a rel="lightbox[group]" href="' + images[i]["url"] + '">' +
            '<img src="' + images[i]["url_300"] + '" alt="' + images[i]["name"] + '"/>' +
            '</a>';
        }
    }

    s +=
    '</td>' +
    '<td class="cright">' +

    '<h2>' + o["objekt_title"] + '</h2>';
    
    // ---------------------------------------- contact
    var email = window.location.href.match(/\/\/.*\//)[0];
    email = email.replace(/\//g, "");
    email = email.replace(/www\./, "");
    email = "kontakt@" + email;
    
    s +=
    '<p class="ccontact">Sie sind an dem Objekt interessiert? ' +
    'Dann nehmen Sie mit uns <a href="mailto:' + email + '?subject=Anfrage | ' + replace_unicode(o["objekt_title"]) + '">Kontakt</a> auf.</p>'; 
    
    // ---------------------------------------- downloads
    /*
    if(downloads)
    {
        for(d in downloads)
        {

        }
    }
    */
    
    // ---------------------------------------- details
    s +=
    '<div class="cbox"><h2>Details</h2><div class="cpadding">' +
    '<table class="ctable"><tbody>';
    for(d in details)
    {
        if(d != "Imleben Id" && d != "Objekt Id")
        {
            s += 
            '<tr><td class="cleft">' + d + '</td><td class="cright">' + details[d] + '</td></tr>';
        }
    }
    s +=
    '</tbody></table>' +
    '</div></div>';
    
    // ---------------------------------------- description
    s +=
    '<div class="cbox"><h2>Beschreibung</h2><div class="cpadding">' +
    o["objekt_description"].replace(/\r\n/g, '<br />'); + 
    '</div></div>' +

    '</td>' +
    '</tr></tbody></table>' +
    
    '</div>';
    
    document.getElementById("im_website").innerHTML = s;
    document.title += " | " + o["objekt_title"];
    
    initLightbox(); // important!
}

/*--------------------------------------------------------------------------------*/
function json_data(data)
{
    if(!data)
    {
        document.getElementById("im_website").innerHTML = "Error!";
        return;
    }
    
    if(im_view == "results")
        display_results(data);
    else
        display_details(data);
    
    //console.log(data);
}

/*--------------------------------------------------------------------------------*/
function get_data()
{

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://immo.imleben.com/website/js/widget.php";
    script.src += "?authkey=" + im_authkey;
    script.src += "&view=" + im_view;
    if(im_view == "results")
    {
	script.src += "&im_config=desc_length:" + im_teaser_length + ",title_length:" + im_title_length + ",perpage:" + im_objects_per_page;
        script.src += "&von=" + im_start;
        script.src += "&suchfilter_objektart=" + im_object_type;
        script.src += "&suchfilter_vertrag[]=" + im_contract_type;
    }
    else
    {
        script.src += "&id=" + im_id;
    }
    
    document.getElementById("im_website").innerHTML = "Loading...";
    document.body.appendChild(script);
}

/*--------------------------------------------------------------------------------*/
function reload_page()
{
    var base = window.location.href.split('?')[0];
    url = base + "?id=" + gup("id"); // typo3 page id
    url += "&im_v=" + im_view;
    if(im_view == "results")
    {
        url += "&im_s=" + im_start;
        url += "&im_ot=" + im_object_type;
        url += "&im_ct=" + im_contract_type;
    }
    else
    {
        url += "&im_id=" + im_id;
    }
    window.location.href = url;
}

/*--------------------------------------------------------------------------------*/
var im_authkey = "";
var im_objects_per_page = 10;
var im_teaser_length = 185;
var im_title_length = 100;
var im_view = "results";
var im_start = 1;
var im_object_type = "";
var im_contract_type = "";
var im_id = 0;

function imleben(object_type, contract_type)
{
    //if(window.location.href.toLowerCase().indexOf("immosun") >= 0)
        im_authkey = "ecde98e9e314bfd5469a85553a634772";
    //else
    //    im_authkey = "154500542c3e67ab1c3eb1cdcc24d1ed";

    im_object_type = object_type;
    im_contract_type = contract_type;

    var v = gup("im_v");
    var s = gup("im_s");
    var ot = gup("im_ot");
    var ct = gup("im_ct");
    var id = gup("im_id");
    
    if(v) im_view = v;
    if(s) im_start = s;
    if(ot) im_object_type = ot;
    if(ct) im_contract_type = ct;
    if(id) im_id = id;

    get_data();
}

/*--------------------------------------------------------------------------------*/
function im_update_page(start)
{
    im_view = "results";
    im_start = start;
    reload_page();
}

function im_show_details(id)
{
    im_view = "detail";
    im_id = id;
    reload_page();
}

function im_back_to_results()
{
    history.back();
}
