$(document).ready(function () {
    $("a.mapbutton").nyroModal({ "height": 533, "width": 650 });

    /* Groter klikoppervlak in het menu */
    $("#submenu li").bind("click", function () {
        window.location = $(this).find("a").attr("href"); return false;
    }).css({ "cursor": "pointer" });
    ohoogte = $("#contentbody").height();

    /* Window Resize */
    function resizeWindow() {
        /*
        whoogte=$(window).height(); 
        hoogte=$("#contentbody").height();
        POhoogte=$("#productoverview").height()+300;
        Choogte=$("#content").height()+140;
		
        alert ("windowhoogte: "+whoogte+"\nContentbody hoogte: "+hoogte+"\nContent hoogte: "+Choogte+"\nProduct overview hoogte: "+POhoogte); 
		
        if (whoogte>hoogte){
        var meethoogte=ohoogte+190;
        if (whoogte>=meethoogte) {$("#contentbody").height(whoogte-198);}		
        }
		
        if (POhoogte>hoogte) {$("#contentbody").height(POhoogte);} else {if (Choogte>hoogte) {$("#contentbody").height(Choogte);} else {$("#contentbody").height(whoogte-198);}}
        */
    }
    /*
    resizeWindow();
    $(window).bind("resize", resizeWindow);
    */

    /* Producten overview */


    //$(".mapbutton").bind("click", function() {var klikstad=$(this).attr("rel"); initialize(klikstad); return false;});



    /* Zoek Highlight */
    // Read a page's GET URL variables and return them as an associative array.
    function getUrlVars() {
        var map = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
            map[key] = value;
        });
        return map;
    }
    var urlVars = getUrlVars();




    /* Search inladen */
    if (urlVars['PAGE'] === undefined) { page = urlVars['page']; } else { page = urlVars['PAGE']; } if (page === undefined) { page = ""; }
    if (urlVars['SEARCHQUERY'] === undefined) { searchQuery = urlVars['searchquery']; } else { searchQuery = urlVars['SEARCHQUERY']; } if (searchQuery === undefined) { searchQuery = ""; }
    if (urlVars['SEARCHLOCATION'] === undefined) { searchLocation = urlVars['searchlocation']; } else { searchLocation = urlVars['SEARCHLOCATION']; } if (searchLocation === undefined) { searchLocation = ""; }

    if ($("div#searchresults_container").length > 0) {
        jQuery.get("searchajax.asp", { page: page, searchquery: searchQuery, searchlocation: searchLocation }, function (data) {
            $("#searchresults_container").html(data).css({ "background": "none", "height": "auto" });

            /* Tabellen Opmaak */
            $("#searchresults tbody tr").bind("click", function () {
                window.location = $(this).find("a").attr("href"); return false;
            }).css({ "cursor": "pointer" });

        });
    }



    /* Producten inladen */
    if (urlVars['SID'] === undefined) { productid = urlVars['sid']; } else { productid = urlVars['SID']; }

    if (urlVars['groepNmr'] === undefined) { productGroep = urlVars['groepNmr']; } else { productGroep = urlVars['groepNmr']; }

    //alert($("ul#productoverview").length);
    if ($("ul#productoverview").length > 0) {
        if (productGroep != "" && productGroep != undefined) {
            jQuery.get("artikelajax2.asp", { sid: productid, groepNmr: productGroep }, function (data) {
                $("#productoverview").html(data).css({ "background": "none", "height": "auto" });

                var searchHighlight = urlVars["Search"];
                if (searchHighlight !== undefined) {
                    $('#content').highlight(searchHighlight);
                }

                // Ophalen anchor en daar naar toe navigeren.
                var myFile = document.location.toString();
                if (myFile.match('#')) { // the URL contains an anchor
                    var myAnchor = '#' + myFile.split('#')[1];
                    window.location = myAnchor;
                }
                setTimeout("imageResizer();", 1);
                setTimeout("imageResizer();", 10);
                setTimeout("imageResizer();", 100);
                setTimeout("imageResizer();", 1000);
                setTimeout("imageResizer();", 10000);

                $(".table1 a.nyroModal").nyroModal();
                $(".table2 a.nyroModal").nyroModal();
                $(".table3 a.nyroModal").nyroModal();
            });
        }
        else {
            jQuery.get("artikelajax.asp", { sid: productid }, function (data) {
                $("#productoverview").html(data).css({ "background": "none", "height": "auto" });

                var searchHighlight = urlVars["Search"];
                if (searchHighlight !== undefined) {
                    $('#content').highlight(searchHighlight);
                }

                // Ophalen anchor en daar naar toe navigeren.
                var myFile = document.location.toString();
                if (myFile.match('#')) { // the URL contains an anchor
                    var myAnchor = '#' + myFile.split('#')[1];
                    window.location = myAnchor;
                }
                setTimeout("imageResizer();", 1);
                setTimeout("imageResizer();", 10);
                setTimeout("imageResizer();", 100);
                setTimeout("imageResizer();", 1000);
                setTimeout("imageResizer();", 10000);

                $(".table1 a.nyroModal").nyroModal();
                $(".table2 a.nyroModal").nyroModal();
                $(".table3 a.nyroModal").nyroModal();
            });
        }
    }



    /* Form Validatie */
    var labelsearch;
    var errors = 0;
    var rotterrors = 0;
    var shaerrors = 0;

    function checkforerrors() {
        labelsearch = "label[for='" + $(this).attr("id") + "']";
        if ($(this).val() == "") {
            if ($(this).hasClass("incorrect")) { } else {
                if ($(this).parent().parent().attr("id") == "form_rotterdamForm") { rotterrors = rotterrors + 1; } else { shaerrors = shaerrors + 1; }
                $(this).addClass("incorrect");
                $(labelsearch).addClass("incorrect");
            }
        } else {
            if ($(this).hasClass("incorrect")) {
                if ($(this).parent().parent().attr("id") == "form_rotterdamForm") { rotterrors = rotterrors - 1; } else { shaerrors = shaerrors - 1; }
                $(this).removeClass("incorrect");
                $(labelsearch).removeClass("incorrect");
            }
            $(this).removeClass("incorrect");
            $(labelsearch).removeClass("incorrect");
        }

        if ($(this).attr("name") == "email") {
            if ($(this).val().indexOf("@") < 0 || $(this).val().indexOf(".") < 0) {
                if ($(this).hasClass("incorrect")) { } else {
                    if ($(this).parent().parent().attr("id") == "form_rotterdamForm") { rotterrors = rotterrors + 1; } else { shaerrors = shaerrors + 1; }
                    $(this).addClass("incorrect");
                    $(labelsearch).addClass("incorrect");
                }
            } else {
                if ($(this).hasClass("incorrect")) {
                    if ($(this).parent().parent().attr("id") == "form_rotterdamForm") { rotterrors = rotterrors - 1; } else { shaerrors = shaerrors - 1; }
                    $(this).removeClass("incorrect");
                    $(labelsearch).removeClass("incorrect");
                }
                $(this).removeClass("incorrect");
                $(labelsearch).removeClass("incorrect");

            }
        }
    }

    $("#form_rotterdam input.required").bind("blur", checkforerrors);
    $("#form_shanghai input.required").bind("blur", checkforerrors);

    $(".locationcontactform form").submit(function () {
        if ($(this).parent().attr("id") == "form_rotterdam") {

            $(this + "#form_rotterdam input.required").each(checkforerrors); errors = rotterrors;
        } else {
            $(this + "#form_shanghai input.required").each(checkforerrors); errors = shaerrors;
        }

        if (errors === 0) {
            return true;
        } else {
            alert("please fill in the required fields");
            return false;
        }

    });


    $(".productcategory").bind("click", function () {
        var categoryID = "#" + $(this).attr("id") + "_details";
        var index = $(".productcategory").index(this) + 1;
        if ($(this).hasClass("even")) { var classStyle = "even"; } else { var classStyle = "odd"; }

        /* Index ophalen */
        index = (Math.ceil(index / 3) * 3);
        /* Als de index waarnaar hij gaat zoeken groter is dan het aantal categorieën, gaat hij zoeken naar het maximale nummer */
        if (index >= $(".productcategory").length) { index = $(".productcategory").length; }

        function expandDetails() {
            $(".expanded").removeClass("expanded");
            index = index - 1;
            $(categoryID).insertAfter(".productcategory:eq(" + index + ")").addClass(classStyle).slideDown(500).addClass("expanded");
        }

        if (!$(categoryID + ".expanded").length) {
            if ($('.expanded').length >= 1) {
                $(".expanded").slideUp(500, expandDetails);

            } else { expandDetails(); }
        }


        /* Dit is nodig, anders gaat hij alsnog naar de originele link */
        return false;
    }).css({ "cursor": "pointer" });

    $(".formbutton").bind("click", function () {
        var stad = $(this).attr("rel");
        var selector = "#form_" + stad;

        $(selector).slideDown(500);
    });


    $("#photosrotterdam .showroomphotos").easySlider({
        auto: false,
        continuous: true,
        numeric: true,
        speed: 650,
        numericId: "controlsrotterdam"
    });

    $("#photosshanghai .showroomphotos").easySlider({
        auto: false,
        continuous: true,
        numeric: true,
        speed: 650,
        numericId: "controlsshanghai"
    });

    $("#content a.group_subcategory img").qtip({
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topMiddle'
            }
        },
        style: {
            name: 'light',
            tip: 'topMiddle',
            border: {
                width: 1,
                radius: 5
            }
        }
    });

    $.fn.nyroModal.settings.hideContent = function (elts, settings, callback) {
        elts.contentWrapper.fadeOut(400);
        callback();
    };


    var vacatureid;
    function vacSlideDown() {
        $("#vacaturelijst>li>div." + vacatureid).slideDown(500, resizeWindow).parent().addClass("vacActief");

    }
    /* Vacatures */
    $("#vacaturelijst>li>a").bind("click", function () {
        vacatureid = $(this).attr("class");

        if ($(this).parent().hasClass("vacActief")) {
            $('#vacaturelijst>li.vacActief>div').slideUp(400);
        } else {
            if ($('#vacaturelijst>li.vacActief"').length >= 1) {
                $('#vacaturelijst>li.vacActief>div').slideUp(500, vacSlideDown).parent().removeClass("vacActief");
            } else { vacSlideDown(); }
        }
        return false;
    });

    $("#vacaturelijst>li:odd").addClass("even");




});


function imageResizer() {
    $(".overzichttext img").each(function () {
        if ($(this).width() > 502 && $(this).attr("resized") == undefined) {
            $(this).width(502);
        }
        $(this).attr("resized", "true")
    });
}


function WieNaam(MedID) {

    for (i = 0; i <= document.all.length; i++) {
        if (document.all[i]) {
            if (document.all[i].id.substring(0, 12) == "ShowWieNaam_") {
                document.all[i].style.visibility = 'hidden';
                document.all[i].style.display = 'none';
            }
        }
    }

    if (document.all["ShowWieNaam_" + MedID]) {
        document.all["ShowWieNaam_" + MedID].style.visibility = 'visible';
        document.all["ShowWieNaam_" + MedID].style.display = '';
    }
}


