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('.nav-main > li > a', { 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('.receivedSourcesContent ul li.head', { 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('.portal .portlet  a.chat', { 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('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'});
    }    
}

//center popups
(function($){
   $.fn.vertCenter = function(options) {
         $(this).find(".feedbackLayer").css('top', 100 + (window.pageYOffset || $.boxModel && document.documentElement.scrollTop ||   document.body.scrollTop) + 'px');
     };
})(jQuery);

$(document).ready(function() {

    InitInspirationPageWebParts();

    //sharepoint fix img alignment
    $('.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: "a.accHeader",
        alwaysOpen: false,
        autoheight: false,
        active: false
    });
    //contact pagina contactformulier
    $(".contact .contact_form").accordion({
        header: "a.accHeader",
        autoheight: false
    });
    // overzicht categorieen
    $(".categorieen").accordion({
        autoheight: false
    });
    // aangedragen bronnen op inspiratiepagina
    $(".receivedSourcesForm").accordion({
        header: "a.accHeader",
        alwaysOpen: false,
        autoheight: false,
        active: false
    });

    //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');
        }
    });
    $(".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', '/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);
        }
    });
    //smokelayer of popups
    var SmokeLayer = $('.popup');
    var popupHeight = $(window).height();
    if (popupHeight < $('.container').height()) {
        popupHeight = $('.container').height();
    }
    SmokeLayer.css('height', popupHeight + 'px');
    var oFeedBackLayer = $('.feedbackLayer');
    oFeedBackLayer.css('left', ($(window).width() - 460) / 2 + 'px');

    //// 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");
        });

    SetMultiWorkshopClick();


    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
    function endRequestHandle(sender, Args) {

        $("select.focus").focus().removeClass("focus");

        advancedSearchActive;
        SetMultiWorkshopClick();
        if (Args.get_error() != undefined) {
            $(".ajaxLoader").html("");
        }

        if ($(".ajaxDone").length > 0) {
            $(".multiWorkshop").hide();
        }
        return true;
    }
});

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 SetMultiWorkshopClick()
{
    $(".multiWorkshop .header .clickable").click(function()
        {
            $(".multiWorkshop .header").removeClass("active");
            var obj = $(this).parent().find(".details");
            if ((obj).css("display") == "none")
            {
                $(".multiWorkshop .details").hide()
                obj.show()
                $(this).parent().addClass("active");
            }
            else
            {
                $(".multiWorkshop .details").hide()
            }
        }
    );
    
    $(".ajaxForm input.submit").click(setAjaxLoader);
    $(".multiWorkshop .checkbox input").click(setAjaxLoader);
}

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;
}
var pngObjectAllowHttps = false;
//PNG Object functions
function SetPngTitle(sObjectID, iSize, sHAlignment, sHoverColorElementId) {
    // Check whether the target element exists
    if (document.getElementById(sObjectID) == null) {
                   return;
    }    
    var po = new Tfe.PngObject("/PngHandler.ashx", true);
    po.addParam("font", "Lucida Sans");
    po.addParam("fontSize", iSize);
    po.addParam("halignment", sHAlignment);
    po.allowHttps = pngObjectAllowHttps;
    po.blankImage = "/images/blank.gif";
    //Add parameters for hover image    
    if (sHoverColorElementId != null && (document.getElementById(sHoverColorElementId) != null)) {
       po.addParam("hasHover", "true");
       po.addParam("hoverColor", "FFFFFF");
       po.addParam("hoverBackgroundColor", "FE1000");
    }    
    if (typeof(addDOMLoadEvent) == "function"){
        addDOMLoadEvent(function(){
            po.replaceElementById(sObjectID);
        });
    }
    else{
        po.replaceElementById(sObjectID);
    }
}

// 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;
	});
});


