String.prototype.replaceAll = function(
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    ) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch >= 0) {
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString );
        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
};

function highlight(obj, bold){
	if(bold){
		$('#'+obj).find('.button_label').css('font-weight','bold');
	}else{
		$('#'+obj).find('.button_label').css('font-weight','normal');
	}
}

$(document).ready(function(){
	var tmp1 = screen.height;
	var tmp2 = tmp1 * 0.25;
	var height = tmp1 - tmp2;
	
	$(".gallery_link").each(function() {
        initializeerDialoogLink($(this), 650, 440);
    });

	$(".gallery_link_midden").each(function() {
        initializeerDialoogLink($(this), 720, 520);
    });

	$(".gallery_link_hoog").each(function() {
        initializeerDialoogLink($(this), 650, 540);
    });
    
    $(".hoofdscherm .product_link").each(function() {
        initializeerDialoogLink($(this), 900, height);
    });

    initializeerDialoogLink($(".hoofdscherm #overgoedmeubel_link"), 640, 350);
    initializeerDialoogLink($(".hoofdscherm #voorwaarden_link"), 	768, height);
    initializeerDialoogLink($(".hoofdscherm #reacties_link"), 		640, 370);
    initializeerDialoogLink($(".hoofdscherm #faq_link"), 			768, height);
    initializeerDialoogLink($(".hoofdscherm #contact_link"), 		300, 120);
    initializeerDialoogLink($("#landingpage .vergroot_knop"), 		530, 430);

    if (jQuery().tooltip) {
        $(".tooltip_link").tooltip({
            bodyHandler: function() {
                return $($(this).attr("href")).html();
            },
            fade: 250,
            showURL: false
        });
        $(".tooltip_link").click(function() {
            return false;
        });
    }

    $(".formulier_submit").click(function() {
        var formulier = $(this).parents("form");
        formulier.submit();
        return false;
    });

    $("#bestel_formulier").submit(function() {
        //alert("formulier moet nog gevalideerd worden");
        var valide = true;
        valide = valideerVeldInvoer($("[name='geslacht']", this)) && valide;
        valide = valideerVeldInvoer($("[name='naam']", this)) && valide;
        valide = valideerVeldInvoer($("[name='adres']", this)) && valide;
        valide = valideerVeldInvoer($("[name='postcode']", this)) && valide;
        valide = valideerVeldInvoer($("[name='woonplaats']", this)) && valide;
        valide = valideerVeldInvoer($("[name='land']", this)) && valide;
        valide = valideerVeldInvoer($("[name='telefoon']", this)) && valide;
        valide = valideerVeldInvoer($("[name='email']", this)) && valide;
        valide = valideerVeldInvoer($("[name='akkoord_met_voorwaarden']", this)) && valide;

        if (!valide) {
            $("#foutmelding").fadeIn(250);
        }
        return valide;
    });
    
    $(".input_label").click(function() {
        var input = $(this).prev();
        input.click();
    });

    $("#bestel_formulier input, #bestel_formulier textarea").change(function() {
        if ($(this).parent().hasClass("fout")) {
            valideerVeldInvoer($(this));
        }
    });
    
    $("#reactieFormulier").submit(function() {
        var naamVeld = $("input[name='naam']", this);
        var reactieVeld = $("textarea[name='reactie']", this);

        var valide = true;
        valide = valideerVeldInvoer(naamVeld) && valide;
        valide = valideerVeldInvoer(reactieVeld) && valide;
        
        if (valide) {
            var naam = naamVeld.val();
            var reactie = reactieVeld.val();

            var action = $(this).attr("action");
            var invoer = $(".invoer", this);
            var verstuurd = $(".verstuurd", this);
            var bezig = $(".bezig", this);
            invoer.fadeOut(300);
            bezig.fadeIn(300, function() {
                $.ajax({
                    url: action + "?naam=" + naam + "&reactie=" + escape(reactie),
                    success: function() {
                        $(".naam", verstuurd).text(naam);
                        // Deze linebreak hierachter is erg belangrijk. Indien reactie
                        // een regel bevat, dan zit er geen markup in de reactie. Hier valt
                        // jQuery over.
                        var reactieHTML = reactie.replaceAll("\n", "<br/>") + "<br/>";
                        $(".bericht", verstuurd).append(reactieHTML);
                        bezig.fadeOut(300, function() {
                            verstuurd.fadeIn(300);
                        });
                    }
                });
            });
        }
        return false;
    });

    $("#reactieFormulier input[name='naam'], #reactieFormulier textarea[name='reactie']").focus(function() {
        if ($(this).parent().hasClass("fout")) {
            $(this).parent().removeClass("fout");
        }
    });

    $(".title_als_tekst").each(function() {
        var tekstElement = $(this);
        $(this).focus(function() {
            if (tekstElement.val() == tekstElement.attr("title")) {
                tekstElement.val("");
            }
        });
        var standaardTekst = function() {
            if (tekstElement.val() == "") {
                tekstElement.val(tekstElement.attr("title"));
            }
        };
        $(this).blur(standaardTekst);
        standaardTekst();
    });
});

function cssWaardeNaarGetal(waarde) {
    return new Number(waarde.substring(0, waarde.length - 2));
}

function valideerVeldInvoer(veld) {
    var ok = false;
    if (veld.size() == 0) {
        alert("Veld " + naam + " niet gevonden");
        return false;
    }
    if (veld.attr("type") == "checkbox") {
        ok = veld.is(':checked');
    } else if (veld.attr("type") == "radio") {
        var geselecteerd = $("input[name='" + veld.attr("name") + "']:checked", veld.parents("form"));
        ok = geselecteerd.size() == 1;
    } else if (veld.attr("type") == "text" || veld.get(0).tagName.toLowerCase() == "textarea") {
        var waarde = veld.val();
        if (waarde) {
            if (waarde != '') {
                if (veld.hasClass("title_als_tekst") && waarde == veld.attr("title")) {
                    ok = false;
                } else {
                    ok = true;
                }
            }
        }
    }
    zetVeldInStaat(veld, ok);
    return ok;
}

function zetVeldInStaat(veld, ok) {
    if (ok) {
        veld.parent().removeClass("fout");
    } else {
        veld.parent().addClass("fout");
    }
}

function initializeerDialoogLink(element, breedte, hoogte) {
    var gebruikcolorbox = jQuery().colorbox;
    if (gebruikcolorbox && element.size() > 0) {
        element.colorbox({
            innerWidth:breedte,
            innerHeight:hoogte,
            iframe:true,
            opacity: 0
        });
    } 
}

var colorBoxSluitIcoonHerpositioneerd = false;
function herpositioneerColorBoxSluitIcoon() {
    if (!colorBoxSluitIcoonHerpositioneerd) {
        var closeBox = $("#colorbox #cboxClose");
        closeBox.remove();
        $("#colorbox #cboxWrapper").append(closeBox);
        colorBoxSluitIcoonHerpositioneerd = true;
    }
}
