/* =================== */
/* help popup          */
/* =================== */

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=534,left = 312,top = 234');");
}

function popupWindow(url, windowsettings) {
	var child = window.open(url, "popup", windowsettings); 
	if (child.opener == null) child.opener = self;
	child.focus();
}

//for print function
var gl_printwindow = null;

function openWindowAndPrint () {
  var browserWidth = screen.availWidth-20;
	var positionRight = location.href.length;
	if (location.href.indexOf("#") != -1) {
		positionRight = location.href.indexOf("#");
	}
	var printUrl = location.href.substr(0,positionRight);
	if (printUrl.indexOf("popup=true") == -1)
	{
		printUrl = printUrl + ((printUrl.indexOf("?") != -1) ? "&" : "?") + "popup=true";
	}
	
	if (printUrl.indexOf("printpreview") == -1)
	{	    
	    printUrl = printUrl + ((printUrl.indexOf("?") != -1) ? "&" : "?") + "printpreview=true";
	}		

	if (browserWidth<780)
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 350) / 2;
		var printwindow = window.open(printUrl, "newwindow", "left="+left+", top="+top+", width=550, height=350, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	}
	else 
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 600) / 2;
		var printwindow = window.open(printUrl, "newwindow","left="+left+", top="+top+", width=550, height=600, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	}

    gl_printwindow = printwindow;
    printThisWindow();
}

//delay the print call because the dyframe script has to set the domain first...
function printThisWindow(){
    try {
            if (gl_printwindow.print) {
                gl_printwindow.focus();
                if (navigator.appName == 'Netscape' && navigator.userAgent.indexOf('Firefox') != -1) {
                    gl_printwindow.onload = gl_printwindow.print;
                } else {
                    gl_printwindow.print();
                }
            } else {
                var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
                document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
                WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
            }
        return;
    }
    catch(e)
    {
        window.setTimeout(printThisWindow,200); 
    }
}

// -------------------- podcast functions

function podcastHover(id, evt)
{
	var hover = document.getElementById(id);
	if (hover)
	{
		if (hover.style.display == 'none')
		{
      hover.style.zIndex = 600;
			hover.style.left = mouseX(evt) + 5 + 'px';
			hover.style.top = mouseY(evt) + 5 + 'px';
      hover.style.display = 'block';			
		}
		else
		{
			hover.style.display = 'none';
		}
	}
}

function podcastPlay(id)
{
	var feat = 'status=0,toolbar=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=535,width=640';
	var sUrl = '/podcast?popup=true&id=' + id;
	w = window.open(sUrl, 'podcast', feat);
	w.moveTo(0,0);
}

function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}
function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
}


if(document.attachEvent)
{
	document.attachEvent('onkeyup', AlertKeyCode);
}
else if(document.addEventListener)
{
	document.addEventListener('keyup', AlertKeyCode, true);
}


var originalColor = "";
var originalBackgroundColor = "";
var focusedElement = null;

function AlertKeyCode(e)
{
	if(document.attachEvent)
	{
		e=event;
	}
	
	if(e.keyCode == 9)
	{  
	    if(focusedElement != null)
	    {
	        focusedElement.style.color = originalColor;
	        focusedElement.style.backgroundColor = originalBackgroundColor;
	    }
	    
	    var elem = null;
	    if ("activeElement" in document)
	    {
	        elem = document.activeElement;
	    }else{
	        elem = e ? e.explicitOriginalTarget : null; 
	    }
	    
	    if(elem)
	    {
	        if(elem.tagName == "A")
	        {
	            focusedElement = elem;
	            originalColor = elem.style.color;
	            originalBackgroundColor = elem.style.backgroundColor;
	            elem.style.color = "#cccccc";   
	            elem.style.backgroundColor = "#000000";
	        }
	    }
	}
}

function changeHeightForBackground() {
    $(".cz_default_content_center").height("auto");
    if ($(".quickwin").length == 0) {      
        var windowHeight = $(window).height();
        var contentHeight = $(".cz_default_content_center").height();
        if (contentHeight < (windowHeight - 195) ) {
            $(".cz_default_content_center").height(windowHeight - 198);
        }
    }
}

function zoeken_submit(el) {
    var form = document.getElementById("advsearch");
    form.submit();
}

function clearInput(el) {
    el.value = "";
}
function setColor(el) {
    el.style.color = "black";
}

function zoeken_submit_google(el) {
    var form = document.getElementById("searchform");
    form.submit();
}

var currentQuestionDiv = null;
var currentAnswerDiv = null;

function ShowDiv(strDivId) {
    var divQuestionElement = document.getElementById('Question' + strDivId);
    var divAnswerElement = document.getElementById('Answer' + strDivId);

    if (divQuestionElement) {
        divQuestionElement.style.display = "none";
        divAnswerElement.style.display = "block";

        if (currentQuestionDiv) {
            currentQuestionDiv.style.display = "block";
            currentAnswerDiv.style.display = "none";
        }

        currentQuestionDiv = divQuestionElement;
        currentAnswerDiv = divAnswerElement;
    }
    changeHeightForBackground();
}

/* */
$(document).ready(function() {
    $('#ppmenuWrapper').css('width', '30px');
    $('#ppmenuWrapper').css('left', '966px');
    $('#ppmenu').css('right', '-912px');
    $('#ppmenubutton').addClass('button_open');
    $('#ppmenubutton').attr('title', $('#openbuttonalttext').text());

    $('#ppmenubutton').toggle(
        function() {
            $('#ppmenuWrapper').css('width', '940px');
            $('#ppmenuWrapper').css('left', '56px');
            $('#ppmenu').animate({
                right: "0px"
            }, 1000);
            $('#ppmenubutton').removeClass();
            $('#ppmenubutton').addClass('button_sluiten');
            $('#ppmenubutton').attr('title', $('#closebuttonalttext').text());
        }, function() {

            $('#ppmenu').animate({
                right: "-912px"
                }, 1000, function() {
                    $('#ppmenuWrapper').css('width', '30px');
                    $('#ppmenuWrapper').css('left', '966px');
                });

            $('#ppmenubutton').removeClass();
            $('#ppmenubutton').addClass('button_open');
            $('#ppmenubutton').attr('title', $('#openbuttonalttext').text());
        }
    );

    $('#ppmenubutton').click(function() {
        var clickName = $('#ppmenubutton').is('.button_open') ? $('#managementinfcloseclickname').text() : $('#managementinfopenclickname').text();
        managementinfo.registerElementClick(clickName, $(this));
    });

    changeHeightForBackground();
    IFrameManager.registerAfterResizeEvents(changeHeightForBackground);
});

/* function for matrixlab popup, see also metrixlab.xsl */
function metrixlab_onready(el, func) {
    this.args = new Array(el, func);
    this.doTry = function() {
        try {
            var el = eval(this.args[0]);
            el.onloading = this.args[1];
            el.onloading();
            clearInterval(this.args[2]);
        }
        catch (e) { }
    }
    this.doTry.bind = function(object) {
        var method = this;
        return function() {
            method.apply(object);
        }
    }
    this.args[2] = setInterval(this.doTry.bind(this), 250);
    return this;
}

/* Used on error pages (error.xml) */
function swapHtml(divId1, divId2) {
    var elem1 = document.getElementById(divId1);
    var elem2 = document.getElementById(divId2);

    if (elem1 && elem2) {
        elem1.innerHTML = elem2.innerHTML;
        if (changeHeightForBackground != undefined) {
            changeHeightForBackground();
        }
    }
}

window.isIframeOnline = new Array();

$(document).ready(function() {
    $(".iframeErrorImage").each(function() {
        window.isIframeOnline[$(this).attr("id")] = true;
    });

    $(".iframeErrorImage").error(function() {
        window.isIframeOnline[$(this).attr("id")] = false;
        $(".autooffline_" + $(this).attr("id")).hide();
    });
    
    //Selected menuitem on detailpage
    var noderef = $("#cz_main").attr("noderef");
    var docref = $("#cz_main").attr("docref");
    $("#detailpage #marginleft .quicklink").each(function() {
        if ($(this).attr("ref") == noderef || $(this).attr("ref") == docref ) {
            $(this).addClass("selected");
        }
    });
    
    //Hover leftmenuitem
    $("#marginleft .quicklink").hover(function() { 
        if (!$(this).hasClass("selected")) {
            $(this).addClass("hover");
        }
    },function() {
        $(this).removeClass("hover");
    });
    
    //Hover buttons
    $(".button").hover(function() {
        $(this).addClass("hover");
    },function() {
        $(this).removeClass("hover");
    });
     
    //outline newsitems
    $(".quickwin #content > div").each(function() {
        $(this).find(".newsitem:not(.hasbackground) .newsitemtitle").outlineelements();
        $(this).find(".newsitem:not(.hasbackground) .newsitemsubtitle").outlineelements();
        $(this).find(".newsitem:not(.hasbackground) .newsitemtext").outlineelements();
    });
    //outline readmore links
    var rollbackquicklink = false;
    var quicklinktextitems = $("#homepage #news .newsitem:not(.hasbackground) .quicklinktext");
    quicklinktextitems.each(function () {      
        if ($(this).height() > 14) {
            var extraheight = $(this).parent().prev().css("padding-bottom").replace("px", "") + 3;
            var margintop = $(this).height() - 14;   
            $(this).parent().css("margin-top", "-" + (extraheight > margintop ? margintop : 0) + "px");
            if (extraheight < margintop) {
                rollbackquicklink = true;
            }  
        }
    });
    if (rollbackquicklink) {
         quicklinktextitems.parent().css("margin-top", "0px");
    } 
    
    //highlightbar
    $(".highlights").each(function() {
       //Hide highlightbar
        if ($(this).find(".newsitem").length == 0) {
             $(this).hide();       
        } else {  
            //divider line    
			$(this).find("div.middle > div.top").height($(this).height() - 40);
        }
    });
    
    
//    $("#homepage .highlights, #marginleft .quicklink").click(function() {
//        var link = $("a:first", this);
//        if (link.attr("target") != "_blank") {
//            window.location = link.attr("href");
//        } else {
//            window.open(link.attr("href"));
//        }
//        return false;
//    });

    //Make the full item clickable    
    $("#homepage .highlights, #marginleft .quicklink").wrap(function() {
        var link = $("a:first", this);      
        if (link.length > 0) {
            var newlink = "<a ";
            $.each(link[0].attributes, function(i, attr) {
                if (attr.specified) {
                    newlink += (attr.name + "='" + attr.value+ "' ");   
                }
            }); 
            return newlink + " />";
        }
    });
    
    
    //hide breadcrumb if home
    if ($(".quickwin .breadcrumb_content .breadcrumb_links > *").length == 1) {
        $(".quickwin .breadcrumb_content").hide();
    }
    
     $(".buttonbar").each(function() {
       //Hide buttonbar
        if ($(this).find(".button").length == 0) {
             $(this).hide();       
        }
    });
    
    //style the newsitems
    $("#news div.top").height($("#news .newsitem:not(.hasbackground)").height());
    //first item each row shouldnt have margin-left
    $("#homepage #news .newsitem").filter(function(index) { return index % 4 == 0; }).css('margin-left', '0px');
    $("<div class='clear' />").insertAfter($("#homepage #news .newsitem").filter(function(index) { return index % 4 == 3; }));
   
    // Homepage slider
    $('#slides').slides({
        preload: true,
        preloadImage: '/loading',
        play: 5000,
        pause: 2500,
        hoverPause: true,
        effect: 'fade',
        fadeSpeed: 700,
        crossfade: true
    });
 
});

jQuery.fn.outlineelements = function() {
    // outline 
    var items = $(this);
    var tallest = 0;
    items.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
	        tallest = thisHeight;
	    }			
    });
    items.each(function() {
	    var heightdif = tallest - $(this).height();
	    if (heightdif != 0) {
	        $(this).css('padding-bottom', heightdif + 'px');
	    }
	})
}
