/* NOM javascripts */

/* attaches window.open functions to links with .openvideo */
function openVideo() {
    var links = [];
    links = getElementsByClassName('openvideo', 'a');
    for (var i = 0; i < links.length; i++) {
        //console.log("href: " + links[i].href);
        var windowAttributes = 'width=350,height=340,left=120,top=120,scrollbars=no,location=no';
        popup = function() { window.open(this.href, 'NOM-TV', windowAttributes); return false; };
        links[i].onclick= popup;
        links[i].onkeypress = popup;
    }
}

/* attaches window.open functions to links with .openimage */
function openImage() {
    var links = [];
    links = getElementsByClassName('openimage', 'a');
    for (var i = 0; i < links.length; i++) {
        //console.log("href: " + links[i].href);
        var windowAttributes = 'width=640,height=560,left=120,top=120,scrollbars=no,location=no';
        popup = function() { window.open(this.href, 'Afbeelding', windowAttributes); return false; };
        links[i].onclick= popup;
        links[i].onkeypress = popup;
    }
}

function closeLinks() {
    var links = [];
    links = getElementsByClassName('close');
    for (var i = 0; i < links.length; i++) {
        //console.log("href: " + links[i].href);
        links[i].onclick=function(){self.close();window.opener.location.reload();};
        links[i].onkeypress=function(){self.close();window.opener.location.reload();};
    }
}

/* checks if the calendar needs to be rendered */
function checkCalender() {
    if (!document.getElementById('cal_kalender_display')) return;
    renderCalendar(nomcal);
}

/*
 * More or less like getElementsById but for classes
 * @param classN    name of class to look for
 * @param el        element in which classes appear
*/
function getElementsByClassName(classN, el) {
    var childs, elements = [];
    var childs = el ? document.getElementsByTagName(el) : document.getElementsByTagName('*');
    var pattern = new RegExp("(^|\\s)" + classN + "(\\s|$)");
    for (var i = 0; i < childs.length; i++) {
        if (pattern.test(childs[i].className)) elements.push(childs[i]);
    }
    return elements;
}

/* Onload function loads some scripts */
window.onload = function() {
    checkCalender();
    openVideo();
    openImage();
    closeLinks();
}


