var NUM_RECIPIENTS; // number of added recipients

function init(){
    if(document.getElementById){
        obj = document.getElementById("login_slide");
        if (!obj.style.left) {
            obj.style.left = "-1000px";
        }
    }
}

function slideRight(){
    if(obj = document.getElementById("login_slide")){
        if(parseInt(obj.style.left) < -30){
            obj.style.left = parseInt(obj.style.left) + 30 + "px";
            setTimeout("slideRight()",.0001);
        }
    }
}

function slideLeft(){
    if(document.getElementById("login_slide")){
        if(parseInt(obj.style.left) > -1000){
            obj.style.left = parseInt(obj.style.left) - 30 + "px";
            setTimeout("slideLeft()",.0001);
        }
    }
}

function slideIn(){
    if(document.getElementById("ecard")){
        obj = document.getElementById("ecard");
        if (!obj.style.top) {
            obj.style.top = "-1000px";
        }
        if(parseInt(obj.style.top) < 0){
            obj.style.top = parseInt(obj.style.top) + 30 + "px";
            setTimeout("slideIn()",.0001);
        }
    }
}

function slideUp(){

    if(document.getElementById("ecard_form")){
        obj = document.getElementById("ecard_form");
        if (!obj.style.top) {
            obj.style.top = "300px";
        }
        if(parseInt(obj.style.top) > -800){
            obj.style.top = parseInt(obj.style.top) - 10 + "px";
            setTimeout("slideUp()",15);
        } else {
            obj.style.display = "block";
            slideOut();
        }
    }
}

function slideOut(){
    if(document.getElementById("ecard_send")){
        obj = document.getElementById("ecard_send");
        if (!obj.style.left) {
            obj.style.left = "0px";
        }
        if(parseInt(obj.style.left) > -700){
            obj.style.left = parseInt(obj.style.left) - 30 + "px";
            setTimeout("slideOut()",20);
        } else {
            obj2 = document.getElementById("ecard_thankyou");
            if (!obj2.style.display) {
                obj2.style.display = "block";
            }
        }
    }
}

/**
 *
 */
function moreRecipients()
{
    var button = document.createElement('a');
    button.setAttribute('id', 'morerecipients');
    button.setAttribute('href', '#');
    button.innerHTML = 'Meer ontvangers';
    applyEvent(button, 'click', addRecipient);

    var dt = document.createElement('dt');
    addTextNode(dt, ' ');
    var dd = document.createElement('dd');
    dd.appendChild(button);

    var recipientemail = document.getElementById('fldrecipientemail');
    var insertedNode = insertAfter(dt, recipientemail.parentNode);
    insertAfter(dd, insertedNode);

    NUM_RECIPIENTS = 1;
}

/**
 *
 */
function addRecipient(e)
{
    var dt, dd, input, insertedNode;
    var target = e.target || e.srcElement;
    var referenceNode = target.parentNode.previousSibling;

    cancelEvent(e);

    // add label
    dt = document.createElement('dt');
    dt.className = 'header';
    addTextNode(dt, getText('recipientdata'));

    dd = document.createElement('dd');
    addTextNode(dd, '');

    insertedNode = referenceNode.parentNode.insertBefore(dt, referenceNode);
    insertedNode = insertAfter(dd, insertedNode);

    // add name field
    input = document.createElement('input');
    input.setAttribute('type', 'text');
    input.setAttribute('id', 'fldrecipientname' + NUM_RECIPIENTS);
    input.setAttribute('name', 'fldrecipientname' + NUM_RECIPIENTS);

    dt = document.createElement('dt');
    addTextNode(dt, getText('recipientname'));

    dd = document.createElement('dd');
    dd.appendChild(input);

    insertedNode = insertAfter(dt, insertedNode);
    insertedNode = insertAfter(dd, insertedNode);

    // add email field
    input = document.createElement('input');
    input.setAttribute('type', 'text');
    input.setAttribute('id', 'fldrecipientemail' + NUM_RECIPIENTS);
    input.setAttribute('name', 'fldrecipientemail' + NUM_RECIPIENTS);

    dt = document.createElement('dt');
    addTextNode(dt, getText('recipientemail'));

    dd = document.createElement('dd');
    dd.appendChild(input);

    insertedNode = insertAfter(dt, insertedNode);
    insertedNode = insertAfter(dd, insertedNode);

    NUM_RECIPIENTS++;
}

/**
 * This is what insertAfter would do, if there was a specification for it (similar to insertBefore, which does exist)
 * @param Node node The new node to insert
 * @param Node referenceNode The node to insert it after
 * @return Node insertedNode The inserted node
 */
function insertAfter(node, referenceNode)
{
    var insertedNode;
    var parentNode = referenceNode.parentNode;

    if (referenceNode == parentNode.lastSibling) {
        insertedNode = parentNode.appendChild(node);
    } else {
        var sibling = referenceNode.nextSibling;
        insertedNode = parentNode.insertBefore(node, sibling);
    }
    return insertedNode;
}

/**
 * Adds text node to node
 */
function addTextNode(node, text)
{
    var txt = document.createTextNode(text);
    node.appendChild(txt);
    return txt;
}

/**
 * Gets CMS_Language text
 * @access public
 * @param string name The text name
 * @return string The translated text in current language
 */
function getText(name)
{
    var textvarname = 'text_' + name;
    var text = '';
    if (window[textvarname]) text = window[textvarname];
    return text;
}

/**
 * Sets CMS_Language text
 * @access public
 * @param string name The text name
 * @param string text The translated text in current language
 */
function setText(name, text)
{
    var textvarname = 'text_' + name;
    window[textvarname] = text;
}

function getPosition(obj)
{
    var position = 0;
    if (obj.offsetParent) {
        position = obj.offsetTop
        while (obj = obj.offsetParent) {
            position += obj.offsetTop
        }
    }
    return position;
}

function showLargeImage(id, src)
{
    // create div if it does not yet exist
    if (!document.getElementById('large_photo')) {

        // create elements
        var parent = document.getElementById('content');
        var div = document.createElement('div');
        var anchor = document.createElement('a');

        // div
        div.setAttribute('id', 'large_photo');

        // anchor
        anchor.setAttribute('title', 'Klik om te sluiten');
        anchor.setAttribute('id', 'large_photo_anchor');
        applyEvent(anchor, 'click', hideLargeImage);

        // close button
        var close = document.createElement('span');
        var txt = document.createTextNode('Sluiten');

        close.className = 'close-button';
        applyEvent(close, 'click', hideLargeImage);

        close.appendChild(txt);

        // append children
        div.appendChild(anchor);
        div.appendChild(close);

        // append to parent
        parent.appendChild(div);
    }

    if (document.getElementById(id)) {

        var anchor = document.getElementById('large_photo_anchor');
        if (anchor) {

            // remove children
            while (anchor.firstChild) anchor.removeChild(anchor.firstChild);

            var div = document.getElementById('large_photo');
            var img = document.createElement('img');
            img.setAttribute('src', src);

            anchor.appendChild(img);

            // IE6 hack...
            if (div.currentStyle && div.currentStyle['position']) {
                if (div.currentStyle['position'] == 'absolute') {
                    var listitem = document.getElementById(id);
                    div.style.top = getPosition(listitem)+'px';
                }
            }

            div.style.display = 'block';

        }
    }
}

function hideLargeImage()
{
    var div = document.getElementById('large_photo');
    div.style.display = 'none';
}

/*
 * functions for photoalbum
 */

function toggleWindow(window, id)
{
    objwindow = new getObj(window);
    objref = new getObj(id);
    this.obj = objref.obj;

    if (this.obj.style.display == 'undefined' || this.obj.style.display == 'none') {
        this.obj.style.display = 'block';
        objwindow.obj.className = 'window maximized';
    } else {
        this.obj.style.display = 'none';
        objwindow.obj.className = 'window minimized';
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
        }
    }
    return curleft;
}

function initPhotoslide(){
    if(document.getElementById){
        obj = document.getElementById("photoslide");
        if (!obj.style.left) {
            posx = findPos(obj);
            posx -= 244;
            obj.style.left = posx;
        }
    }
}

function movePhotoslide(direction){
    if(document.getElementById){
        obj = document.getElementById("photoslide");
        width = obj.offsetWidth;
        posx = findPos(obj);
        if (direction == 'left') {
            newposx = parseInt(posx - 122);
            if (newposx < 260) {
                obj.style.left = newposx+'px';
            }
        }
        if (direction == 'right') {
            newposx = posx -354;
            if (newposx > (width - (width*2 - 353))) {
                obj.style.left = newposx+'px';
            }
        }
    }
}

function changeRouteday(select)
{
    if (select) {
        select.form.submit();
    }
}

/**
 * FUNCTIONS FOR IMAGESLIDER
 */
function isIE6() {
    var navi = navigator.appVersion;
    if(navi.indexOf("MSIE")>-1){
        var navAr = navi.split(";");
        if(parseFloat(navAr[1].split(" ")[2])<7) {
            return true;
        }
    }
    return false;
}

function findPosY(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curtop += obj.offsetTop
        }
    }
    return curtop;
}

function changeSlider(){
    obj = document.getElementById('imageslider');
    obj.className = 'noscroll';
}

function changeHotelTransportSlider(){
    obj = document.getElementById('hotel-transport-imageslider');
    obj.className = 'noscroll';
}

function replaceLargeImage(number, id, src){
    // move slider
    position = (number * 96) -130;
    moveImageslidePresentationTo('hotel-transport-images', position);
    // replace image
    obj = document.getElementById(id);
    obj.setAttribute('src', src);
}

function replaceText(id, text){
    obj = document.getElementById(id);
    if (obj.firstChild) {
        obj.removeChild(obj.firstChild);
    }
    obj.appendChild(document.createTextNode(text));
}

/* two functions for the slider on the normal page */
function initImageslide(id){
    if(document.getElementById){
        obj = document.getElementById(id);
        if (!obj.style.top) {
            posy = findPosY(obj);
            posy = posy -136;
            obj.style.top = posy+'px';
        }
    }
}

function moveImageslide(id, direction){
    if(document.getElementById){
        obj = document.getElementById(id);
        height = obj.offsetHeight;
        posy = findPosY(obj);
        posy = posy -133;
        if (direction == 'up') {
            newposy = parseInt(posy - 15);
            if (newposy > (height - (height*2 - 150))) {
                obj.style.top = newposy+'px';
            }
        }
        if (direction == 'down') {
            newposy = posy +15;
            if (posy < 30) {
                obj.style.top = newposy+'px';
            }
        }
    }
}

/* functions for the slider on the presentation overlay-page */
function initImageslidePresentation(id){
    if(document.getElementById){
        obj = document.getElementById(id);
        if (!obj.style.top) {
            posy = findPosY(obj);
            if (isIE6()) { posy = posy -152; } else { posy = posy -150; }
            obj.style.top = posy+'px';
        }
    }
}

function moveImageslidePresentation(id, direction){
    if(document.getElementById){
        obj = document.getElementById(id);
        height = obj.offsetHeight;
        posy = findPosY(obj);
        if (isIE6()) { posy = posy -152; } else { posy = posy -150; }

        if (direction == 'up') {
            newposy = parseInt(posy - 10);
            if (newposy > (height - (height*2 - 150))) {
                obj.style.top = newposy+'px';
            }
        }
        if (direction == 'down') {
            newposy = posy +10;
            if (posy < 30) {
                obj.style.top = newposy+'px';
            }
        }
    }
}

function moveImageslidePresentationTo(id, toPosition){
    if(document.getElementById){
        obj = document.getElementById(id);
        height = obj.offsetHeight;
        posy = findPosY(obj);
        if (isIE6()) { posy = posy -152; } else { posy = posy -150; }

        posy = parseInt(posy);

        if (posy > -toPosition) {
            newPosition = posy - 10;
            setTimeout(function() { moveImageslidePresentationTo(id, toPosition); }, 10);
            obj.style.top = newPosition+'px';
        } else if (posy < -(toPosition + 10)) {
            newPosition = posy + 10;
            setTimeout(function() { moveImageslidePresentationTo(id, toPosition); }, 10);
            obj.style.top = newPosition+'px';
        }
    }
}

var visualNumbers = new Array();

function displayVisual(blockname, numberOfVisuals, year, extension, domain)
{
    // pick a random number from the available visuals (if global variable is not yet set)
    if (visualNumbers[blockname] > 0) {
        var visualNumber = visualNumbers[blockname];
    } else {
        var visualNumber = Math.floor((numberOfVisuals-1)*Math.random()) + 1;
        visualNumbers[blockname] = visualNumber;
    }

    // pad the number with zero's if it's below 10
    if (visualNumber < 10) {
        visualNumber = "0" + visualNumber;
    }

    // format the visual filename (eg. "/images/2009/default/portal/visual_junior03.jpg")
    if (domain == 'be' && blockname == 'default') {
        var visualFilename = "/images/" + year + "/default/portal/visual_" + blockname + visualNumber + "_be." + extension;
    } else {
        var visualFilename = "/images/" + year + "/default/portal/visual_" + blockname + visualNumber + "." + extension;
    }

    var tab = document.getElementById('body');
    tab.style.backgroundImage = 'url('+visualFilename+')';
}

function initTourButton()
{
    if (typeof($) !== 'undefined') {

        var tourButton = $('#tour-start-button');
        if (tourButton.size() > 0) {
            tourButton.css('cursor', 'pointer');
            tourButton.click(startTour);
        }
    }
}

function startTour()
{
    var object = '<object width="1" height="1" data="/swf/2009/bg_sound_tour.swf" type="application/x-shockwave-flash"><param name="movie" value="/swf/2009/bg_sound_tour.swf"/><param name="wmode" value="transparent"/><param name="menu" value="false"/></object>';
    var overlay = '<div id="tour-overlay">' + object + '</div>';

    var border = '<div id="tour-border"><img class="png" src="/images/2009/tour/bg_tour.png" alt="" width="852" height="526"/></div>';
    var tour = '<div id="tour"><div id="tour-contents"></div><div id="tour-container">' + border + '<span class="pngbg" id="tour-close-button"><span>Sluiten</span></span></div></div>';

    $('body').append(overlay + tour);


    $('html, body').css('overflow', 'hidden');
    $('#tour-overlay').css('display', 'block');
    $('#tour-close-button').click(closeTour);

    $('#tour-contents').crossSlide(
        {
            'fade' : 1.5,
            'loop' : 1
        },
        [
            { 'src' : '/images/2009/tour/0001.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',        'time' : 3  },
            { 'src' : '/images/2009/tour/001.jpg',  'from' : 'center bottom 1x',    'to' : 'center top 1x',        'time' : 2  },
            { 'src' : '/images/2009/tour/002.jpg',  'from' : 'center bottom 1x',    'to' : 'center center 1x',    'time' : 3  },
            { 'src' : '/images/2009/tour/006.jpg',  'from' : 'left bottom 1x',      'to' : 'center center 1x',    'time' : 4  },
            { 'src' : '/images/2009/tour/007.jpg',  'from' : 'left center 1x',      'to' : 'right center 1x',     'time' : 4  },
            { 'src' : '/images/2009/tour/008.jpg',  'from' : 'center center 1x',  'to' : 'center center 1x',    'time' : 4  },
            { 'src' : '/images/2009/tour/009.jpg',  'from' : 'center top 1x',  'to' : 'center center 1x',    'time' : 4  },
//            { 'src' : '/images/2009/tour/010.jpg',  'from' : 'center center 1x',  'to' : 'center center 1x',    'time' : 3  },
            { 'src' : '/images/2009/tour/011.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',  'time' : 3  },
            { 'src' : '/images/2009/tour/012.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',    'time' : 3  },
//            { 'src' : '/images/2009/tour/013.jpg',  'from' : 'center center 1x',  'to' : 'center top 1x',       'time' : 4  },
            { 'src' : '/images/2009/tour/003.jpg',  'from' : 'left center 1x',    'to' : 'left center 1x',      'time' : 3  },
            { 'src' : '/images/2009/tour/004.jpg',  'from' : 'right center 1x',     'to' : 'left center 1x',      'time' : 4  },
//            { 'src' : '/images/2009/tour/005.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',       'time' : 2  },
            { 'src' : '/images/2009/tour/015.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',    'time' : 2  },
            { 'src' : '/images/2009/tour/014.jpg',  'from' : 'right center 1x',     'to' : 'left center 1x',      'time' : 3  },
            { 'src' : '/images/2009/tour/016.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',     'time' : 3  },
            { 'src' : '/images/2009/tour/017.jpg',  'from' : 'center center 1x',    'to' : 'right center 1x',     'time' : 2  },
            { 'src' : '/images/2009/tour/018.jpg',  'from' : 'left center 1x',      'to' : 'right center 1x',     'time' : 3  },
            { 'src' : '/images/2009/tour/019.jpg',  'from' : 'right center 1x',     'to' : 'left center 1x',      'time' : 3  },
            { 'src' : '/images/2009/tour/020.jpg',  'from' : 'left center 1x',        'to' : 'left center 1x',      'time' : 3  },
            { 'src' : '/images/2009/tour/021.jpg',  'from' : 'left bottom 1x',      'to' : 'right top 1x',        'time' : 4  },
            { 'src' : '/images/2009/tour/022.jpg',  'from' : 'left bottom 1x',      'to' : 'right center 1x',     'time' : 3  },
//            { 'src' : '/images/2009/tour/023.jpg',  'from' : 'left top 1x',         'to' : 'center center 1x',    'time' : 4  },
            { 'src' : '/images/2009/tour/024.jpg',  'from' : 'center 1.4x',         'to' : 'center center .7x',   'time' : 4  },
            { 'src' : '/images/2009/tour/025.jpg',  'from' : 'center bottom 1x',    'to' : 'center top 1x',  'time' : 2.5  },
            { 'src' : '/images/2009/tour/026.jpg',  'from' : 'center center 1x',    'to' : 'center center 1x',  'time' : 3  },
            { 'src' : '/images/2009/tour/027.jpg',  'from' : 'left bottom 1x',      'to' : 'center center 1x',    'time' : 4  },
            { 'src' : '/images/2009/tour/028.jpg',  'from' : 'center center 1x',     'to' : 'center center 1x',      'time' : 3  },
//            { 'src' : '/images/2009/tour/029.jpg',  'from' : 'center center 1x',      'to' : 'center center 1x',     'time' : 3  },
            { 'src' : '/images/2009/tour/030.jpg',  'from' : 'left center 1x',        'to' : 'center center 1x',          'time' : 4  },
            { 'src' : '/images/2009/tour/031.jpg',  'from' : 'top center 1x',       'to' : 'top center 1x',       'time' : 10 }
        ]
    );
}

function closeTour()
{
    $('#tour-overlay').remove();
    $('#tour').remove();
}

addLoadEvent(initTourButton);

function SliderClass( elementId ) {

    this.slider = $('#'+elementId);

    if( this.slider.length > 0 ) {
        this.slider.addClass('noscroll');
    }

    this.imagelist = $('#'+elementId+" div.images");

    this.up = $('#'+elementId+" .up");
    this.down = $('#'+elementId+" .down");

    var self = this;

    this.up.bind( 'click', function() {
        self.move(-1);
    } );

    this.down.bind( 'click', function() {
        self.move(1);
    } );

    this.imageHeight = 83;

    this.offset = 35;

    this.scrollDelay = 500;

    this.position = -1;

    this.setPosition = function( newPos ) {
            if( newPos < 0 ) { newPos = 0; }
        if( newPos > (this.imagelist.children().length-3) ) { newPos = this.imagelist.children().length-3; }
        this.position = newPos;
    }

    this.move = function( offset ) {
        this.setPosition( this.position + offset );
        if( ! this.slider[0].id.match(/^imageslider/) ) {
            this.display( this.selected+offset );
        }
        this.show();
    }

    this.show =  function( ) {
        this.imagelist.animate({
            top: (this.offset + (this.position * (-1*this.imageHeight))) + 'px'
          }, this.scrollDelay );
    }

    this.display = function( id ) {
        var img = $("#view_slideshow_visual"+id+" img")[0];
        if( ! img ) return;

        var str = img.src.substring(img.src.lastIndexOf('/')+1);
        $("#large-photo").attr('src','/images/cached/2009/default/hotel-transport/'+str);

        this.imagelist.children().each( function(el) {
                   $("#thumb_"+el).removeClass('selected');
        });
        self.setPosition(id-1);
        this.selected = id;
        $("#thumb_"+(id)).addClass('selected');

        self.show();
    }

       this.imagelist.children().each(
        function( id ) {
            var el = self.imagelist.children()[id];
            $("#"+el.id).bind( 'click', function(e) {

                if( document.getElementById('hotel-transport-presentation-overlay') ) {
                    cancelEvent(e);
                    try {
                        var e = window.event;
                        if(!e) e = window.Event;
                        if(e) {
                            e.returnValue = false;
                            e.cancelBubble = true;
                            e.stopPropagation();
                        }
                    } catch(c) {}
                }


                if( ! self.slider[0].id.match(/^imageslider/) ) {
                    self.display(id);
                } else {
                    if( document.getElementById('hotel-transport-presentation-overlay') ) {
                        showHotelTransportPresentationOverlay();
                        hotel_slider.display(id);
                    }
                }
            });
        }
       );

    this.setPosition(0);
}

function setCookie(cookiename, value, expiredays) {
    var exdate= new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = cookiename + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(cookiename) {
    if (document.cookie.length > 0) {
        cookiestart = document.cookie.indexOf(cookiename + "=");
        if (cookiestart != -1) {
            cookiestart = cookiestart + cookiename.length+1;
            cookieend = document.cookie.indexOf(";", cookiestart);
            if (cookieend == -1) cookieend = document.cookie.length;
            return unescape(document.cookie.substring(cookiestart,cookieend));
        }
    }
    return "";
}
