// init pulldows
$(function() {

    $('.popup select').selectmenu({
        width: 110,
        style:'dropdown'
    });

    $('.dropdown-long select').selectmenu({
        width: 236,
        style:'dropdown'
    });

    $('.dropdown-short select').selectmenu({
        width: 110,
        style:'dropdown'
    });


    $("#scope").bind("change", function(event, ui) {
        if (event.target.value == "Nieuws") $(".newsScope").show(); else $(".newsScope").hide();
    })

    // init search field
    if ($("#scope")[0])
        if ($("#scope")[0].value == "Nieuws") $(".newsScope").show()
})

// init tab functions
$(document).ready(function() {
    $("#tab_populair a").click(function() {
        showTabsPopulair(this);
        return false;
    });
    $("#tab_reacties a").click(function() {
        showTabsReacties(this);
        return false;
    });
    $("#tab_vacatures a").click(function() {
        showTabsVacatures(this);
        return false;
    });
})

function showTabsPopulair() {
    $("#tab_populair").siblings().removeClass("current");
    $("#tab_populair").addClass("current");

    $("#items_populair").show();
    $("#items_reacties").hide();
    $("#items_vacatures").hide();
}

function showTabsReacties() {
    $("#tab_reacties").siblings().removeClass("current");
    $("#tab_reacties").addClass("current");

    $("#items_populair").hide();
    $("#items_reacties").show();
    $("#items_vacatures").hide();
}

function showTabsVacatures() {
    $("#tab_vacatures").siblings().removeClass("current");
    $("#tab_vacatures").addClass("current");

    $("#items_populair").hide();
    $("#items_reacties").hide();
    $("#items_vacatures").show();
}

// set last childs
$(document).ready(function() {
    // Adds CSS class "last-child" to all "last child LI elements"
    $("ul li:last-child").addClass("last-child");
})

// set elements class to last for sponsors
$(function () {
    var baseElement = $(".C41_teaserSponsor .inner:last-child")

    baseElement.addClass("last")
    baseElement.prev().filter(".odd").addClass("last")
})

// init teaser events
$(document).ready(function() {
    // get number of events to show
    events = $(".C31_teaserEvenement .inner");
    numberOfEvents = events.length;

    // if 2 events, add double class and space class to last item
    if (numberOfEvents == 2) {
        events.addClass("double");
        $(events[1]).addClass("space");
    }
    // if 3 events, add triple class and space class to last item
    else if (numberOfEvents == 3) {
        events.addClass("triple");
        $(events[1]).addClass("space");
        $(events[2]).addClass("space");
    }
})

// init intro Promo
$(document).ready(function() {
    // get number of events to show
    events = $(".C_introPromo .inner");
    numberOfEvents = events.length;

    // if 2 events, add double class and space class to last item
    if (numberOfEvents == 2) {
        events.addClass("double");
        $(events[1]).addClass("space");
    }
    // if 3 events, add triple class and space class to last item
    else if (numberOfEvents == 3) {
        events.addClass("triple");
        $(events[1]).addClass("space");
        $(events[2]).addClass("space");
    }
})


// used by photoGallery
$(document).ready(function() {

    $(".scrollable").scrollable();

    $(".items img").click(
        function() {

            // see if same thumb is being clicked
            if ($(this).hasClass("active")) {
                return;
            }
            // calclulate large image's URL based on the thumbnail URL (flickr specific)

            // remove this default
            // var url = $(this).attr("src").replace("_t", "");
            // replace for rel tag
            var url = $(this).attr("rel");
            var alt = $(this).attr("alt");
            var title = $(this).attr("title");
            var myclass = $(this).attr("class");

            // get handle to element that wraps the image and make it semi-transparent
            var wrap = $(".detail").fadeTo("medium", 0.5);
            // the large image from www.flickr.com
            var img = new Image();

            // call this function after it's loaded
            img.onload = function() {
                // make wrapper fully visible
                wrap.fadeTo("fast", 1);
                // change the image
                wrap.find("img").attr("src", url);
                wrap.find("img").attr("alt", alt);
                wrap.find("img").attr("title", title);
                wrap.find("img").attr("class", myclass);
            };

            // begin loading the image from www.flickr.com
            img.src = url;

            // activate item
            $(".items img").removeClass("active");
            $(this).addClass("active");

            // update caption
            $(".browser h3").html($(this).attr("data-description"));

            // update counter
            var total = $(".scrollable .items img").length
            var index = $(".scrollable .items img").index($(this));

            $(".browser .index").html(index + 1 + "/" + total);

            // when page loads simulate a "click" on the first image
        }).filter(":first").click();

});

// used by events
$(function() {
    var maxheight = 0;

    $(".C31_teaserEvenement .inner").each(function(i) {
        maxheight = Math.max(maxheight, $(this).height())
    })

    $(".C31_teaserEvenement .inner").height(maxheight)
})

// used by introPromo homepage
$(function() {
    var maxheight = 0;

    $(".C_introPromo .inner").each(function(i) {
        maxheight = Math.max(maxheight, $(this).height())
    })

    $(".C_introPromo .inner").height(maxheight)
})

// used by searchform C40
$(function() {

    $(".C40_zoeken .choice").click(function() {
        $(".C40_zoeken").css("z-index", 500);
        $(this).next().show();
    })

    $(".C40_zoeken .menu a").click(function() {
        $(this).parents(".menu").prev().text($(this).text());
        $(this).parents(".menu").hide();
        $(this).parents(".menu").next().val($(this).text());
        return false;
    })

    $(".C40_zoeken .button").click(function() {
        $(this).parents("form").submit();
    })

})

// used by searchform header searchform
$(function() {

    $(".service .choice").click(function() {
        $(".C40_zoeken").css("z-index", 500);
        $(this).next().show();
    })

    $(".service .menu a").click(function() {
        $(this).parents(".menu").prev().text($(this).text());
        $(this).parents(".menu").hide();
        $(this).parents(".menu").next().val($(this).text());
        return false;
    })

    $(".service .button").click(function() {
        $(this).parents("form").submit();
    })

})

// set round corners
$(document).ready(function() {
    /* [MSc] 2010-0811: Causes problems in IE, disabled for now */
    /*$(".rounded").corner("round 8px");*/
});

// set focus background color

$(document).ready(function() {
    $("input").focus(function() {
        $(this).addClass("focus")
    });

    $("input").blur(function() {
        $(this).removeClass("focus")
    });

    $("textarea").focus(function() {
        $(this).parent().addClass("focus")
    });

    $("textarea").blur(function() {
        $(this).parent().removeClass("focus")
    });

    $("textarea").keyup(function() {
        maxLength = 500;
        if ($(this).val().length > maxLength) {
            $(this).val($(this).val().substr(0, maxLength));
        }
        $(".counter").text(maxLength - $(this).val().length);
    });

});

//used by downloadform C58
$(function() {

    $(".C58_downloadWhitepaper .ui-selectmenu-menu").each(function(index) {
        $(this).css("position", "relative");
        $(this).css("z-index", 1000 - index * 10)
    })

    /*
     $(".C58_downloadWhitepaper .choice").click(function() {
     $(this).next().css("z-index",200);
     $(".C58_downloadWhitepaper .menu").hide();
     $(this).next().show();
     })

     $(".C58_downloadWhitepaper .menu a").click(function() {
     $(this).parents(".menu").prev().text($(this).text());
     $(this).parents(".menu").hide();
     $(this).parents(".menu").next().val($(this).text());
     return false;
     })
     */

    $(".C58_downloadWhitepaper .menu").blur(function() {
        $(this).hide();
    })

    $(".C58_downloadWhitepaper a.download").click(function() {
        $(this).parents("form").submit();
    })
})

// submit form generic

submitForm = function (elem) {
    $(elem).closest('form').submit();
}

validateForm = function (elem) {
    $(elem).closest('form').validate({errorElement:'div'})
}

// searchform keywords

$(function() {
    $(".keywords input").click(function() {
        if ($(this).val() == "Trefwoord(en)") $(this).val("")
    })

    $(".keywords input").blur(function() {
        if ($(this).val() == "") $(this).val("Trefwoord(en)")
    })
})

/* W24_faq */

$(function() {
    $(".CW24_faq ul a").click(function() {
        var li = $(this).closest("li")
        if (li.hasClass('expand'))
            li.removeClass("expand");
        else
            li.addClass("expand");
        return false;
    })
})


// init main menu

$(function() {

    $(".navigation-sub").css("text-align", "left")

    if ($(".navigation-sub").length > 0) {
        params = new Object()
        params.currentItem = $("#navigation-main li.current");
        params.submenu = $(".navigation-sub");
        params.lastSubmenuItem = $(".navigation-sub .last-child");

        params.middle = params.currentItem.position().left + 0.5 * params.currentItem.width();
        params.submenuRoom = params.submenu.innerWidth()
        params.submenuSize = params.lastSubmenuItem.position().left + params.lastSubmenuItem.width()
        params.slack = params.submenuRoom - params.submenuSize

        if (params.middle + .5 * params.submenuSize < params.submenuRoom && params.middle - .5 * params.submenuSize > 0) {
            // it should fit, align..
            $($(".navigation-sub li")[0]).css("margin-left", params.middle - .5 * params.submenuSize)
        } else {
            if (params.middle + .5 * params.submenuSize > params.submenuRoom) {
                //align right
                $(".navigation-sub").css("text-align", "right")
            }
            /* else {
             // align left [default - do nothing]
             $(".navigation-sub").css("text-align","left")
             } */
        }
    }

})
$(document).ready(function() {
    $("#tab_vgm a").click(function() {
        showResultsVGM();
    });
    $("#tab_magazine a").click(function() {
        showResultsMagazine();
    });
    $("#tab_aanbod a").click(function() {
        showResultsAanbod();
    });
});

function showResultsVGM() {
    $("#tab_vgm").siblings().removeClass("current");
    $("#tab_vgm").addClass("current");

    $(".results_vgm").show();
    $(".results_magazine").hide();
    $(".results_aanbod").removeClass("repositionTabContent");
    $(".results_aanbod").addClass("hideTabContent");
}

function showResultsMagazine() {
    $("#tab_magazine").siblings().removeClass("current");
    $("#tab_magazine").addClass("current");

    $(".results_magazine").show();
    $(".results_vgm").hide();
    $(".results_aanbod").removeClass("repositionTabContent");
    $(".results_aanbod").addClass("hideTabContent");
}
function showResultsAanbod() {
    $("#tab_aanbod").siblings().removeClass("current");
    $("#tab_aanbod").addClass("current");

    $(".results_aanbod").removeClass("hideTabContent");
    $(".results_aanbod").addClass("repositionTabContent");
    $(".results_vgm").hide();
    $(".results_magazine").hide();
}
