$.noConflict();

function stop(e) {
    if (!e) e = window.event;
    (e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
    (e.preventDefault) ? e.preventDefault() : e.returnValue = false;
    return false;
}

function prevDef(e) {
    if (!e) e = window.event;
    (e.preventDefault) ? e.preventDefault() : e.returnValue = false;
    return false;
}

function log(message) {
    if (typeof console != 'undefined' && typeof log != 'undefined') {
        log(message);
    }
};

jQuery(document).ready(function ($) {

    //init carouselNav
    $("#slideShow").each(function () { $(this).initSlideShow() })

    //init mainnav dropdown
    $("#nav .root").each(function () {
        var context = this;
        var dropdown = $("ul:first", context);
        $(dropdown).hide();
        $("li:first", context).mouseover(function () { $(dropdown).show(); });
        $("li:first", context).mouseout(function () { $(dropdown).hide(); });
    })

    $(".lightbox").colorbox({ width: "800", height: "522", iframe: true });
});


//SlideShow
(function($) {
	$.fn.initSlideShow = function() {

        var slideShow = $(this);
        var slideWidth = slideShow.width();
        var divisionNav = $("#divisionNav");
        var detailopen = -1; //none open

        //calculate canvas width
        var totalslides = $(".slide", slideShow).length - 1;
        var canvasWidth = -(totalslides - 1) * slideWidth;
        var slideSpeed = .9;

        var WaitTime = 4;
        if ($(".WaitTime").length) {
            WaitTime = $(".WaitTime").val();
        }

        var timeUnit = 1000; //timeUnit = seconds
        var speed = slideSpeed * timeUnit;
        var slideTimer = null;
        var activeSlide = 0; //which slide is active
        var loop = "true";
        var indexmode = "thumb"; // [number|image|thumb] default="number"

        //init slide actions ----------

        $(".slide", slideShow).each(function (i) {
            $('a.close', this).attr('href', 'javascript:void(0)').click(function () {
                slideDetailClose(i)
            })
        })

        //init division nav ----------

        $("li a", divisionNav).attr('href', 'javascript:void(0)').each(function (i) {
            $(this).click(function () {
                $(".current", divisionNav).removeClass('current');
                $(this).parent().addClass("current");
                if ((detailopen >= 0) && (i != detailopen)) {
                    slideChange(i);
                }
                else {
                    clearInterval(slideTimer);
                    slide(i, function () {
                        detailopen = i;
                        slideDetailOpen(i);
                    });
                }
            });
        })

        //set detail animation ----------

        function slideDetailOpen(pos) {
            $('.label', '.slide').animate({ top: '-150' }, 600, 'easeInOutCubic');
            $('.projectinfo,.divisioninfo', '.slide').animate({ bottom: '-300' }, 600, 'easeInOutCubic', function () {
                $('.slide:eq(' + pos + ') .detail').fadeIn('fast');
                detailopen = pos;
            });
        }
        function slideDetailClose(pos) {
            $('.slide:eq(' + pos + ') .detail').fadeOut('fast', function () {
                $('.label', '.slide').animate({ top: '12' }, 600, 'easeInOutCubic');
                $('.projectinfo', '.slide').animate({ bottom: '0' }, 600, 'easeInOutCubic');
                $('.divisioninfo', '.slide').animate({ bottom: '20' }, 600, 'easeInOutCubic', function () {
                    $(".current", divisionNav).removeClass('current');
                });
                detailopen = -1;
            });
        }
        function slideChange(pos) {
            $('.slide:eq(' + detailopen + ') .detail').fadeOut('fast', function () {
                //$(".current",divisionNav).removeClass('current');
                slide(pos, function () {
                    detailopen = pos;
                    $('.slide:eq(' + pos + ') .detail').fadeIn('fast');
                });
            });
        }

        if (loop == "true") {
            $("#canvas", slideShow).append($(".slide:first", slideShow).clone());
        }

        //set user interaction actions ----------

        slideShow.bind("mouseenter", function () {
            clearInterval(slideTimer)
        });

        slideShow.bind("mouseleave", function () {
            if (detailopen < 0) {
                clearInterval(slideTimer);
                autoplay();
            }
        });


        //init slide magic ----------

        function autoplay() {
            slideTimer = setInterval(function () { slideNext() }, WaitTime * timeUnit)
        }

        function slideNext() {
            if (activeSlide < totalslides) { slide(activeSlide + 1) }
            else {
                if (loop == "true") { loopslide() }
                else { slide(0) }
            }
        }

        function slidePrev() {
            if (activeSlide == 0) { slide(totalslides) }
            else { slide(activeSlide - 1) }
        }

        function slide(pos, ready) {
            $("#canvas", slideShow).animate({ "margin-left": -slideWidth * pos }, speed / 2, ready);
            activeSlide = pos;
        }
        function loopslide() {
            $("#canvas", slideShow).animate({ "margin-left": -slideWidth * (activeSlide + 1) }, speed / 2, function () { $("#canvas", slideShow).css({ "margin-left": 0 }) })
            activeSlide = 0;
        }

        //start the show
        autoplay();

	};
})(jQuery);

// Vanancies
function VacancyFilterSelected() {
    var url = window.location.pathname;
    var params = "";

    var selectedDate = jQuery(".VacancyDate :selected").val();
    if (selectedDate != "") {
        if (params != "") {
            params += "&";
        }
        params += "VacancyDate=" + selectedDate;
    }

    var selectedLocation = jQuery(".VacancyLocation :selected").val();
    if (selectedLocation != "") {
        if (params != "") {
            params += "&";
        }
        params += "VacancyLocation=" + selectedLocation;
    }

    var selectedDivision = jQuery(".VacancyDivision :selected").val();
    if (selectedDivision != "") {
        if (params != "") {
            params += "&";
        }
        params += "VacancyDivision=" + selectedDivision;
    }

    if (params != "") {
        url += "?" + params;
    }
    window.location = url;
}

// embedded flash movie dialog script

function flashInsertCallBack(dialogResult, results) {
    if (results == null || results == undefined)
        return;

    if (dialogResult == SP.UI.DialogResult.OK) {
        SP.UI.Notify.addNotification("Flash inserted", false);

        RTE.Cursor.get_range().deleteContent();

        var rng = RTE.Cursor.get_range().$3_0;

        var d = rng.ownerDocument;
        var a = d.createElement("a");

        var url = "/_layouts/Rabovastgoed.Internet/FlashLoadPage.aspx?swf=" + results[1];

        a.setAttribute("class", "lightbox");
        a.setAttribute("href", url);
        a.setAttribute("title", results[0]);

        a.innerHTML = results[0];

        SP.UI.UIUtility.insertAfter(a, rng);

    }
}

function launchFlashInsert() {

    var options = {
        url: '/_layouts/RaboVastgoed.Internet/InsertFlashPage.aspx',
        title: 'Insert flash',
        width: 400,
        height: 300,
        dialogReturnValueCallback: flashInsertCallBack
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

		
