﻿SetCufon();

function SetCufon() {
    //The editor of sharepoint generate a javascript error if cufon hover is true. So turn it of in edit mode
    if (formMode == 'display') {
        Cufon.replace('.mainMenu > li > a', { fontFamily: 'lucida', hover: true });
        Cufon.replace('.footerMenu > li > a', { fontFamily: 'lucida', hover: true });
        Cufon.replace('.footerMenu > li > span', { fontFamily: 'lucida', hover: true });
        Cufon.replace('.advisors-portlet ul li a span.name', { fontFamily: 'lucida', hover: true });
        Cufon.replace('.oneliner .text', { fontFamily: 'lucida', hover: true });
        Cufon.replace('.portal .portlet  h2', { fontFamily: 'lucida' });
        Cufon.replace('.portal .portlet  h3', { fontFamily: 'lucida' });
        Cufon.replace('.portal .portlet  a.adviseur', { fontFamily: 'lucida' });
        Cufon.replace('.sectoroverview .sector .title', { fontFamily: 'lucida' });
        Cufon.replace('.slide .title', { fontFamily: 'lucida' });
        Cufon.replace('.slide .subtitle', { fontFamily: 'lucida' });
        Cufon.replace('.frontpage-content .banner .small_title', { fontFamily: 'lucida' });
        Cufon.replace('.frontpage-content .banner .large_title', { fontFamily: 'lucida' });
        Cufon.replace('.homeBanner h3', { fontFamily: 'lucida' });
        Cufon.replace('.events .date span', { fontFamily: 'lucida' });
        Cufon.replace('.column-right h5', { fontFamily: 'lucida' });
        Cufon.replace('.adviesButtons h5', { fontFamily: 'lucida' });
        Cufon.replace('h2', { fontFamily: 'lucida' });
        Cufon.replace('h1', { fontFamily: 'lucida' });
        Cufon.replace('.popup .subTitle', { fontFamily: 'lucida' });
        Cufon.replace('.popup .title', { fontFamily: 'lucida' });
        Cufon.replace('.projectbanner .title1', { fontFamily: 'lucida' });
        Cufon.replace('.projectbanner .title2', { fontFamily: 'lucida' });
        Cufon.replace('.projectsite a.projectName', { fontFamily: 'lucida' });
        Cufon.replace('.megatrend .title', { fontFamily: 'lucida' });
        Cufon.replace('.megatrend .subtitle', { fontFamily: 'lucida' });
        Cufon.replace('.trend .title', { fontFamily: 'lucida' });
        Cufon.replace('.trend .subtitle', { fontFamily: 'lucida' });
        Cufon.replace('.networkRelatedWebPart h5', { fontFamily: 'lucida' });
        Cufon.replace('.networkRelatedWebPart ul a.title', { fontFamily: 'lucida' });
        Cufon.replace('.agendaItem  .agendadate', { fontFamily: 'lucida' });
        Cufon.replace('.article_summary .top', { fontFamily: 'lucida' });
    }
}

//center popups
(function($) {
    $.fn.vertCenter = function(options) {
        //smokelayer of popups
        var SmokeLayer = $('.popup');
        var popupHeight = $(window).height();

        if (popupHeight < $('.container').height()) {
            popupHeight = $('.container').height();
        }
        if (popupHeight < $('body').height()) {
            popupHeight = $('body').height();
        }

        SmokeLayer.css('height', popupHeight + 'px');

        $(this).find(".feedbackLayer").css('top', 100 + (window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop) + 'px').css('left', ($(window).width() - 460) / 2 + 'px');
    };
})(jQuery);

$(document).ready(function() {



    InitInspirationPageWebParts();

    //sharepoint fix img alignment
    var ie7 = $.browser.msie && $.browser.version.slice(0, 1) == "7";
    $(".uitgelicht-content img, div.userContent img, #ctl00_PlaceHolderMain_NewsDetail img").each(function() {
        var $this = $(this);

        var align = $this.attr("align");
        if (align) {
            $this.css('float', align);
        }

        if (!ie7) {
            var vspace = $this.attr("vspace"),
            hspace = $this.attr("hspace");
            if (vspace >= 0) {                
                $this.css('margin-top', vspace + 'px');
                $this.css('margin-bottom', vspace + 'px');
            }
            if (hspace >= 0) {
                $this.css('margin-right', hspace + 'px');
                $this.css('margin-left', hspace + 'px');
            }
        }
    });

    $('.uitgelicht-content img[@align="right"],div.userContent img[@align="right"],#ctl00_PlaceHolderMain_NewsDetail img[@align="right"]').css('float', 'right');
    $('.uitgelicht-content img[@align="left"],div.userContent img[@align="left"],#ctl00_PlaceHolderMain_NewsDetail img[@align="left"]').css('float', 'left');

    //update the content
    $("div.userContent p + ul").css("margin-top", "-1em");
    $("#ctl00_PlaceHolderMain_NewsDetail p + ul").css("margin-top", "-1em");
    $(".uitgelicht-content p + ul").css("margin-top", "-1em");
    $("div.userContent p + ol").css("margin-top", "-1em");
    $("#ctl00_PlaceHolderMain_NewsDetail p + ol").css("margin-top", "-1em");
    $(".uitgelicht-content p + ol").css("margin-top", "-1em");

    // contactformulier in de rechterkolom						   
    $(".column-right .contact_form").accordion({
        header: "h5",
        alwaysOpen: false,
        autoheight: false,
        active: false,
        animated: false
    });
    //contact pagina contactformulier
    $(".contact .contact_form").accordion({
        header: "a.accHeader",
        autoheight: false,
        change: function(event, ui) {
            var s = $('li.selected > fieldset', $(this));
            if ($('span.error:visible', s).length == 0) {
                $('.error:visible', s).not('span.error').removeClass('error');
            }           
        }
    });

    //open and close panels with class toggable
    $(".toggle").click(function() {
        if ($(".toggable").is(':visible')) {
            $(".toggable").animate({ height: 'hide', opacity: 'hide' }, 'fast');
        }
        else {
            $(".toggable").animate({ height: 'show', opacity: 'show' }, 'fast');
        }
    });
    $(".toggle2").click(function() {
        if ($(".toggable2").is(':visible')) {
            $(".toggable2").animate({ height: 'hide', opacity: 'hide' }, 'fast');
        }
        else {
            $(".toggable2").animate({ height: 'show', opacity: 'show' }, 'fast');
        }
    });
    $(".toggle3").click(function() {
        if ($(".toggable3").is(':visible')) {
            $(".toggable3").animate({ height: 'hide', opacity: 'hide' }, 'fast');
        }
        else {
            $(".toggable3").animate({ height: 'show', opacity: 'show' }, 'fast');
        }
    });
    $(".toggleExt").click(function() {
        if ($(".toggable").is(':visible')) {
            $(".toggable").animate({ height: 'hide', opacity: 'hide' }, 'fast');
        }
        else {
            $(".toggable").animate({ height: 'show', opacity: 'show' }, 'fast');
            setTimeout(function() { window.scroll(1, $(window).height()) }, 200);
        }
    });
    activateComments();
    //search on every page hide en show of advanced search
    var advancedSearchActive = false;
    var searchfieldFocus = false;
    var form_search_background = $('.form_search').css('background-image') + ' ' + $('.form_search').css('background-repeat');
    var form_search_submit = $('.form_search .submit').attr("src");
    $('.searchfield').focus(function() {
        $('.advancedSearch').show();
        $('.form_search').css('background', 'none');
        $('.form_search .submit').attr('src', '/_layouts/syntens/images/btn-search-red.gif');
        searchfieldFocus = true;
    }).blur(function() {
        if (advancedSearchActive == false) {
            $('.advancedSearch').hide();
            $('.form_search').css('background', form_search_background);
            $('.form_search .submit').attr('src', form_search_submit);
        }
        searchfieldFocus = false;
    });
    $('.advancedSearch').mouseover(function() {
        advancedSearchActive = true;
    }).mouseout(function() {
        advancedSearchActive = false;
        if (searchfieldFocus == false) {
            $('.advancedSearch').hide();
            $('.form_search').css('background', form_search_background);
            $('.form_search .submit').attr('src', form_search_submit);
        }
    });

    //// Publieksstemming: set link on entire block
    $(".votingCandidate")
        .css("cursor", "pointer")
        .click(function() {
            location.href = $(this).find("a.votingCandidatePicture").attr("href");
        });
    $(".votingBoothTop")
        .click(function() {
            $(this).parent().toggleClass("active");
        });


    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
    function endRequestHandle(sender, Args) {

        $("select.focus").focus().removeClass("focus");

        advancedSearchActive;

        if (Args.get_error() != undefined) {
            $(".ajaxLoader").html("");
        }

        if ($(".ajaxDone").length > 0) {
            $(".multiWorkshop").hide();
        }
        return true;
    }

    // Replace Telerik RadEditor for MOSS in Telerik RadEditor
    $(".column-right.inspiratieCollege .ms-standardheader span").each(function() {
        $(this).text($(this).text().replace(" for MOSS", ""));
    });

    // Fix to display WebPart Zones on Inspiratie College content horizontal
    $(".inspiratieCollegeZone .ms-WPAddButton, .homepage .ms-WPAddButton").each(function() {
        var td = $(this).parent();
        var tr = td.parent();
        td.attr("colspan", "3");
        td.find("table:first").attr("style", "width: 100%");
        tr.before("<tr></tr>");
        var tbody = tr.parent();
        td.appendTo(tbody.find("tr:first"));
    });
    $(".inspiratieCollegeZone .ms-SPZone").attr("style", "width: 455px");
    $(".column-content.inspiratieCollege .inspiratieCollegeZone .ms-SPZone").attr("style", "width: 658px");
    $(".inspiratieCollegeZone .ms-SPZone").attr("style", "width: 675px");
    $(".column-content.inspiratieCollege .inspiratieCollegeZone .ms-SPZone").attr("style", "width: 878px");

    // Fix to display WebPart Zones on Generieke template content horizontal
    $(".genericWebPartZone .ms-WPAddButton").each(function() {
        var td = $(this).parent();
        var tr = td.parent();
        td.attr("colspan", "3");
        td.find("table:first").attr("style", "width: 100%");
        tr.before("<tr></tr>");
        var tbody = tr.parent();
        td.appendTo(tbody.find("tr:first"));
    });
    $(".genericWebPartZone .ms-SPZone").attr("style", "width: 675px");
    $(".column-content .genericWebPartZone .ms-SPZone").attr("style", "width: 455px");


    $(".menu > li").each(function() {
        if ($(this).find("ul").length > 0) {
            $(this).hover(
                function() {
                    $(this).addClass("hover");
                },
                function() {
                    $(this).removeClass("hover");
                }
            );
        }
    });

    $(".homeBanner").click(function() {
        var links = $(this).find("a");
        if (links.length > 0) {
            $(links[0]).trigger('click');
            if ($(links[0]).attr("href")) {
                location.href = $(links[0]).attr("href");
            }
        }
    });

    var followTimeoutID = null;
    $("#follow").hover(
        function() {
            if (followTimeoutID != null) {
                clearTimeout(followTimeoutID);
                followTimeoutID = null;
            }
            if ($(this).css("width") == "0px") {
                $(this).animate({ 'width': '50px' }, 500);
            }
        },
        function() {
            var $this = $(this);
            followTimeoutID = setTimeout(function() { $this.animate({ 'width': '0px' }, 500); }, 1000);
        }
    );

    $(window).scroll(function() {
        var newTop = $(window).scrollTop() + 122;
        $("#follow").css("top", newTop + "px");
    });

    $("ul.tabs li a").click(function() {
        $("ul.tabs li a").removeClass("active");
        $(this).addClass("active");
        $(this).parents(".networkTabs").find(" > div").hide();

        if ($(this).parent().hasClass("tabVisual")) {
            $(".networkViewFlash").show();
        }
        else {
            $(".networkView").show();
        }
    });

    setInterval("UpdateChatStatus();", 2500);
    UpdateChatStatus();
});

function UpdateChatStatus() {
    $(".lc5element").each(function() {
        var status = $(this).find("a").text().toLowerCase();
        if (status) {
            $(this).attr("class", "lc5element");
            $(this).addClass(status);
            if (status == "online") {
                $(".openChat").html("<a href=\"#\">Open de chat</a>");
                $(".openChat a").click(function() { $("#lc5_0").click(); });
            }
            else {
                $(".openChat").html("");
            }
        }
    });
}

function InitInspirationPageWebParts() {
    var left = true;
    var right = false;

    $(".center .small").each(
        function() {
            if (left) {
                $(this).addClass("left");

                if ($(this).next(".center .megatrend").attr("class") == "megatrend small") {
                    right = true;
                    left = false;
                }
                else {
                    $(this).after('<div class="clear"></div>');
                }
            }
            else {
                $(this).addClass("right");
                right = false;
                left = true;
                $(this).after('<div class="clear"></div>');
            }
        }
    );
}



function setAjaxLoader() {
    $(".ajaxForm").find(".ajaxLoader").html("<div class='overlay'>&nbsp;</div><div class='overlayLoader'>&nbsp;</div>");
}

/* Javascript for the menu */
function switchMenuLayer(linkElement, layerId) {
    var element = MM_findObj(layerId);
    if (element != null) {
        if (element.style.display == 'block') {
            linkElement.className = 'closed';
            element.style.display = 'none';
        } else {
            linkElement.className = 'open';
            element.style.display = 'block';
        }
    }
}

function ResetDefaultIfEmpty(element, defaultValue) {
    if (element.value == '') {
        element.value = defaultValue;
    }
}

function ClearDefaultOnFocus(element, defaultValue) {
    if (element.value == defaultValue) {
        element.value = '';
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

// activate the click function of all toggleComment elements
function activateComments() {
    $(".toggleComment").click(function() {

        if ($(".toggable").is(':visible')) {
            $(".toggable").animate({ height: 'hide', opacity: 'hide' }, 'fast');
        }
        else {
            var commentId = ($(this).attr("data-commentId") == undefined ? "" : $(this).attr("data-commentId"));

            var root = ($(this).attr("data-root") == "true");

            // set comment id
            $(".commentId").val(commentId);

            // move form to the correct node
            if (root) {
                $(this).parent().append($(".toggable"));
            } else {
                $(this).parent().parent().append($(".toggable"));
            }

            // open form
            $(".toggable").animate({ height: 'show', opacity: 'show' }, 'fast');
        }
    });
}

// Open / close a case
jQuery(function() {
    currentOpenFaq = null;
    // Close all narrow cases
    jQuery("ul.faq div.answer").hide();
    jQuery("ul.faq a.expand").click(function(e) {
        var childElements = jQuery(jQuery(e.target).parent("li").get(0)).children();
        if (jQuery(e.target).hasClass("collapse")) { // Close current
            jQuery(e.target).removeClass("collapse");
            jQuery(childElements[1]).hide();
            currentOpenFaq = null;
        } else { // Open new
            jQuery(childElements[1]).show();
            jQuery(e.target).addClass("collapse");
            if (currentOpenFaq != null) {
                var oldChilds = jQuery(jQuery(currentOpenFaq.target).parent("li").get(0)).children();
                jQuery(oldChilds[1]).hide();
                jQuery(currentOpenFaq.target).removeClass("collapse");
            }
            currentOpenFaq = e;
        }
        e.preventDefault();
        return false;
    });
    jQuery("ul.faq a.expand").click(function(e) {
        var childElements = jQuery(jQuery(e.target).parent("li").get(0)).children();
        if (jQuery(e.target).hasClass("collapse")) { // Close current
            jQuery(childElements[1]).css({ visibility: "visible" });
            jQuery(childElements[2]).hide();
            jQuery(e.target).removeClass("collapse");
            currentOpenFaq = null;
        } else { // open new
            jQuery(childElements[1]).css({ visibility: "hidden" });
            jQuery(childElements[2]).show();
            jQuery(e.target).addClass("collapse");
            if (currentOpenFaq != null) {
                var oldChilds = jQuery(jQuery(currentOpenFaq.target).parent("li").get(0)).children();
                jQuery(oldChilds[2]).hide();
                jQuery(oldChilds[1]).css({ visibility: "visible" });
                jQuery(currentOpenFaq.target).removeClass("collapse");
            }
            currentOpenCaseWide = e;
        }
        e.preventDefault();
        return false;
    });
});

/* Agenda */
var totalweeks = 4;
var sectors = '';
var regions = '';
var subjects = '';
var formula = '';

var agenda = null;

function Agenda() {
    this.tempLastWeek = 1;
    this.weeksPerPage = totalweeks;
    this.cache = new Array();
    this.loaded = function() {
        $('.loader').hide();

        var oldHeight = $('.agenda .items div.wrapper:visible').height();

        $('.fadeIn').fadeIn(500, function() { $(this).removeClass("fadeIn").css("display", ""); });
        $('.fadeIn .filler').css('height', '0px');
        $('.agenda .items div.wrapper li.filler').each(function() {
            var myHeight = $(this).height();
            var olHeight = $(this).parent().height();
            var newHeight = agenda.height() + myHeight - olHeight;

            $(this).animate({
                height: newHeight + 'px'
            }, 500);
        });
    }

    this.started = function() {
        $('.agenda .items div.wrapper:visible li.filler').each(function() {
            var myHeight = $(this).height();
            var olHeight = $(this).parent().height();
            var newHeight = agenda.height() + myHeight - olHeight;

            $(this).animate({
                height: newHeight + 'px'
            }, 1000);
        });
    }

    this.height = function() {
        var h = 64;
        $('.agenda .items div.wrapper:visible ol').each(function() {
            var height = $(this).height() - $(this).find("li.filler").height();
            if (height > h) {
                h = height;
            }
        });
        return h;
    }

    this.prepend = function(obj) {
        $(".agenda .items .old").removeClass("old").addClass("fadeIn");
        $(".agenda .items").prepend(obj);
        $(".agenda .items").find(".wrapper:last").remove();
        $(".agenda .items .wrapper:last").addClass("new");
    }

    this.append = function(obj) {
        $(".agenda .items .new").removeClass("new").addClass("fadeIn");
        $(".agenda .items").append(obj);
        $(".agenda .items").find(".wrapper:first").remove();
        $(".agenda .items .wrapper:first").addClass("old");
    }
       
    this.showLoader = function() {
        var height = agenda.height();
        var navheight = $('.agenda .items .nav').height()
        $('.loader').show().css('height', (height - navheight + 2) + 'px').css('top', navheight + 'px');
        $('.loader .image').show();
    }

    this.previous = function(e) {
    e.preventDefault();
   
        startDate.setDate(startDate.getDate() - 7);
       
        var previous = new Date(startDate);
        previous.setDate(previous.getDate() - 7);
       
        var cacheKey = getCacheKey(previous);       
        var cached = agenda.cache[cacheKey];
        if (cached) {
            $(cached).removeClass("new").addClass("old");
            agenda.prepend(cached);
            agenda.loaded();
        }
        else {
            agenda.showLoader();           
            GetAgendaItemsPrev(cacheKey, regions, sectors, subjects, formula, servicePath);
        }
    }

    this.next = function(e) {
        e.preventDefault();

        startDate.setDate(startDate.getDate() + 7);

        var next = new Date(startDate);
        next.setDate(startDate.getDate() + (7 * (agenda.weeksPerPage)));

        var cacheKey = getCacheKey(next);   
        var cached = agenda.cache[cacheKey];
        if (cached) {
            $(cached).removeClass("old").addClass("new");
            agenda.append(cached);
            agenda.loaded();
        }
        else {
            agenda.showLoader();
            GetAgendaItemsNext(cacheKey, regions, sectors, subjects, formula, servicePath);
        }
    }

   function getCacheKey(date) {
        
        var d = date.getDate();
        var day = (d < 10) ? '0' + d : d;
        var m = date.getMonth() + 1;
        var month = (m < 10) ? '0' + m : m;
        var yy = date.getYear();
        var year = (yy < 1000) ? yy + 1900 : yy;

        return day + "-" + month + "-" + year;
    }

    this.error = function(error) {
       if (error.get_statusCode() != "0") {
            alert(error.get_Message());        
       }
    }
    
    this.loadedNext = function(data) {
        var element = $(data).addClass("new");
        agenda.cache[$(element).attr("rel")] = element;
        agenda.append(element);
        agenda.filterChanged();
    }

    this.loadedPrev = function(data) {      
        var element = $(data).addClass("old");
        agenda.cache[$(element).attr("rel")] = element;
        agenda.prepend(element);
        agenda.filterChanged();
    }

    this.filterChanged = function() {
        var sector = $(".agenda .selectwrapper .sectorFilter").val();
        var region = $(".agenda .selectwrapper .regioFilter").val();
        
        $items = $(".agenda .items .wrapper .agendaItem");

        if ((sector == undefined && region == undefined) || (sector == "" && region == "")) {
            $items.show();
        }
        else {
            $items.each(function() {
                var show = true;

                if (sector != "" && sector != undefined) {
                     if (typeof ($(this).attr("data-sector")) != "undefined") {
                        var sectoren = $(this).attr("data-sector").split(";");
                        if ($.inArray(sector, sectoren) == -1) {
                            show = false;
                        }
                    }
                    else {
                        show = false;
                    }
                }

                if (region != "" && region != undefined) {
                    if (typeof ($(this).attr("data-region")) != "undefined") {
                        var regions = $(this).attr("data-region").split(";");
                        if ($.inArray(region, regions) == -1) {
                            show = false;
                        }
                    }
                    else {
                        show = false;
                    }
                }

                if (show) {
                    $(this).show(); 
                }
                else {
                    $(this).hide();
                }

                

            });
        }
       
        agenda.loaded();
    }
}

$(document).ready(function() {
    if (agenda != null) {
        agenda.started();

        $(".agenda .nav .prev").click(agenda.previous);
        $(".agenda .nav .next").click(agenda.next);

        $(".agenda .items .wrapper").each(function() {
            if ($(this).attr("rel")) {
                agenda.cache[$(this).attr("rel")] = this;
            }
        });
       
        $(".agenda .selectwrapper .sectorFilter").change(agenda.filterChanged);
        $(".agenda .selectwrapper .regioFilter").change(agenda.filterChanged);
    }
    setTimeout
    clearTimeout
});

$.extend({ URLEncode: function(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        } 
    } return o;
},
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});


$('.searchfield').keyup(function(){
    var keywords = $(this).val();
    if (keywords != pkeywords) {
        pkeywords = keywords;
        var c = $('.searchresults .content ul li.active a').attr('rel');
        c = c ? '&cat=' + c : ''
        var url = '/search.ashx?q=' + $.URLEncode(keywords) + c; 
        
        LoadSearchResult(url);
    }
});

