lightboxPopup = {
        // Functie voor het maken van de lightbox
        open: function(title, inPage, inId) {
            // Ga helemaal naar boven
            scroll(0, 0);
            
            // We gaan de gegevens opvragen
            $.ajax({
               url: "/ajax/getLightbox",
               type: "POST",
               cache: false,
               async: false,
               data: { action: "loadContent", page: inPage, id: inId },
               success: function(msg) {
                content = msg;
               }
            });
                        
            // We gaan de maten ophalen
            var htmlWidth = $("html").width();
            var htmlHeight = $("body").height();
            var viewport = getViewport();
            
            // We gaan de boxen maken
            var htmlpopup = '<div id="lightboxpopup_background"></div>';
            htmlpopup += '<div id="lightboxpopup_box">';
                htmlpopup += '<a href="" id="lightboxpopup_close" onclick="lightboxPopup.close(); return false;">&nbsp;</a>';
                htmlpopup += '<div id="lightboxpopup_title">' + title + '</div>';
                htmlpopup += '<div id="lightboxpopup_content">' + content + '</div>';
            htmlpopup += '</div>';
            
            // We gaan de boxen plaatsen
            $("body").html($("body").html() + htmlpopup);
            
            // We gaan de css instellen
            $("#lightboxpopup_background").css("width", viewport.width + "px");
            $("#lightboxpopup_background").css("height", htmlHeight + "px");
            
            $("#lightboxpopup_box").css("left", ((htmlWidth / 2) - ($("#lightboxpopup_box").width() / 2)) + "px");
            $("#lightboxpopup_box").css("top", ((((viewport.height / 2) - ($("#lightboxpopup_box").height() / 2)) > 2) ? ((viewport.height / 2) - ($("#lightboxpopup_box").height() / 2)) : (((viewport.height / 2) - ($("#lightboxpopup_box").height() / 2)) + 100)) + "px");
        },
        // Functie voor het sluiten van de lightbox
        close: function() {
            $("#lightboxpopup_box").remove();
            $("#lightboxpopup_background").remove();
        },
        // Functie voor het veranderen van de content
        setContent: function(newhtml) {
            $("#lightboxpopup_content").html(newhtml);
        },
        // Functie voor het afhandelen van een bieding
        saveBid: function() {
            // We gaan kijken of alle velden wel ingevuld zijn
            if($("input[name='voornaam']").val()!='' && $("input[name='achternaam']").val() != '' && $("input[name='adres']").val() != '' && $("input[name='postcode']").val() != '' && $("input[name='plaats']").val() != '' && $("input[name='telefoon']").val() != '' && $("input[name='email']").val() != '' && $("input[name='bod']").val() != '' && ($("input[name='financiering']").is(":checked") || $("input[name='bouwtechnisch']").is(":checked")) && $("input[name='transport']").val() != '' && $("input[name='ikgaakkoord']").is(":checked"))
            {
                var str = "";
                
                // We gaan de gegevens string maken
                $("input, select, textarea").each(function() {
                    if($(this).attr("type")=="checkbox") {
                        if($(this).is(":checked")) {
                            $(this).val('1');
                        } else {
                            $(this).val('0');
                        }
                    }
                    
                    str += "&" + $(this).attr("name") + "=" + escape($(this).val()); 
                });
                
                // We gaan de ajax versturen
                $.ajax({
                   url: "/ajax/getLightbox",
                   type: "POST",
                   cache: false,
                   async: true,
                   data: "action=doBid" + str,
                   success: function(msg) {
                    // We gaan kijken of het goed gegaan is
                    if(msg.substr(0, 4)=="FAIL") {
                        alert(msg.substr(4));
                    } else {
                        lightboxPopup.setContent(msg);
                    }
                   }
                });
            } else {
                alert("U dient alle velden met een * in te vullen");
            }
        },
        // Functie voor het versturen van een bezichtings aanvraag
        saveBezich: function() {
            // We gaan kijken of alle nodige velden ingevuld zijn
            if($("input[name='voornaam']").val()!='' && $("input[name='achternaam']").val()!='' && $("input[name='adres']").val()!='' && $("input[name='postcode']").val()!='' && $("input[name='plaats']").val()!='' && $("input[name='tel']").val()!='' && $("input[name='email']").val()!='')
            {
                var str = "";
                
                // We gaan de gegevens string maken
                $("input").each(function() {                    
                    str += "&" + $(this).attr("name") + "=" + escape($(this).val()); 
                });
                
                // We gaan de ajax versturen
                $.ajax({
                   url: "/ajax/getLightbox",
                   type: "POST",
                   cache: false,
                   async: true,
                   data: "action=doBezichtiging" + str,
                   success: function(msg) {
                    // We gaan kijken of het goed gegaan is
                    if(msg.substr(0, 4)=="FAIL") {
                        alert(msg.substr(4));
                    } else {
                        lightboxPopup.setContent(msg);
                    }
                   }
                });
            } else {
                alert("U dient alle velden met een * in te vullen");
            }
        },
        // Functie voor het openen van bezichtiging
        openBezichtiging: function() {
            var title = $("#lightboxpopup_title").html();
                       
            lightboxPopup.close();
            lightboxPopup.open(title, 'bezichtigen', $("input[name='house_id']").val());
        },
        // Functie voor het opslaan van mail bekende
        sendMail: function() {
            var str = "";
            
            // We gaan kijken of alle velden ingevuld zijn
            if($("input[name='naam']").val() != '' && $("input[name='naamontvanger']").val() != '' && $("input[name='emailontvanger']").val() != '')
            {
                // We gaan de gegevens string maken
                $("input, textarea").each(function() {                    
                    str += "&" + $(this).attr("name") + "=" + escape($(this).val()); 
                });
                
                // We gaan de request plaatsen
                $.ajax({
                   url: "/ajax/getLightbox",
                   type: "POST",
                   cache: false,
                   async: true,
                   data: "action=doMail" + str,
                   success: function(msg) {
                    // We gaan kijken of het goed gegaan is
                    if(msg.substr(0, 4)=="FAIL") {
                        alert(msg.substr(4));
                    } else {
                        lightboxPopup.setContent(msg);
                    }
                   } 
                });
                
            } else {
                alert("U dient alle velden met een * in te vullen");
            }
        }
};

function getViewport()
{
    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth;
        viewportheight = window.innerHeight;
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
        viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    }

    return {width:viewportwidth,height:viewportheight};
}
