window.addEvent('domready', function() {
   
    checkFlashPlayer();
    addTooltips();
    positionImageSupermarket();

});


window.addEvent('load', function() {

    positionImageSupermarket();
    insertContactIframe();

});


/**
* checkFlashPlayer
*
* checks availability flash player 10
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function checkFlashPlayer() {

    if (Browser.Plugins.Flash.version >= 10) {

        // check which page
        var thisPath = window.location.pathname.toString();

        if (urlToFlash[thisPath]) {
            location.href = urlToFlash[thisPath];
        }

        if (!$('flashcontent')) return false;

        var params = {
            quality: 'high',
            scale: 'noscale',
            wmode: 'transparent',
            allowscriptaccess: 'always',
            bgcolor: '#ffffff'
        };
        var flashvars = {
            siteXML: '/site.aspx'
        };
        var attributes = {
            id: 'flashcontent_inner',
            name: 'flashcontent_inner'
        };
        swfobject.embedSWF('/include/Mona/swf/ontwikkel/bin/main.swf', 'flashcontent_inner', '100%', '100%', '10.0.0', '/include/Mona/swf/ontwikkel/bin/expressInstall.swf', flashvars, params, attributes);

        $('flashcontent').setStyle('height', document.getElement('body').getSize().y);
        $('flashcontent').setStyle('background', '#ffffff');

    } else {
        
        if (Cookie.read('noflash_mona')) {

            if ($$('div.no_flash')[0]) {
                $$('div.no_flash').getParent('div.speechbubble').removeClass('speechbubble_active');
                initPromoBubbles();
            }

        } else {

            var noFlashButton = $$('a.no_flash_button');
            if (!noFlashButton) return false;

            noFlashButton.addEvent('click', function() {
                this.getParent('div.speechbubble').removeClass('speechbubble_active');
                initPromoBubbles();
            });

            var noFlashCookie = Cookie.write('noflash_mona', 'hide', { duration: false });

        }

        $('flashcontent').addClass('bgwrapper');

        initMainNav();
        closeBubble();
        initRating();
        initVoting();

        window.addEvent('load', function() {
            initCarrousel();
            swapImagesEmployees.periodical(5000);
        });

    }

}

/**
* showHideContact
*
* show / hide contactform
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function showHideContact(showhide) {

    if (!$$('div.flash_pagewrapper')[0]) return false;
    
    if (showhide == "show") {
        $$('div.flash_pagewrapper')[0].setStyle('display', 'block');
    } else {
        $$('div.flash_pagewrapper')[0].setStyle('display', 'none');
    }
        
}

function resizeBody () {
	
	if (!$('flashcontent')) return false;
	$('flashcontent').setStyle('height', (document.getElement('body').getSize().y));
   	
}

/**
* initMainNav
*
* initializes menu navigation
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/


function initMainNav() {

    var mainNav = $$('ul.nav')[0];

    if (!mainNav) return false;

    var mainNavItems = $$('ul.nav>li');
    mainNavItems.addEvent('click', function() {

        mainNavItems.removeClass('activemain');
        if (this.getElement('ul')) {
            this.addClass('activemain');
        }

    });
    
}

/**
* initRollOvers
*
* initializes object rollovers
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function initRollOvers(object) {

    if (!object) return false;
    
    var objectClass = object.className;
    var objectOverClass = objectClass + "_over";

    object.addEvents({
        'mouseenter': function() {
            object.addClass(objectOverClass);
        },
        'mouseleave': function() {
            object.removeClass(objectOverClass);
        }
    });

    
}

var currentBubble = 0;
var totalBubbles;

/**
* initPromoBubbles
*
* initializes promo bubbles
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function initPromoBubbles () {

    var promoBubble = $$('div.actiebubble')[0];
    if (!promoBubble) return false;

    // show bubble
    promoBubble.addClass('actiebubble_active');
    
    // start "slideshow" promobubbles
    var bubbleAutoPlay = nextPromoBubble.periodical(5000);

    // containers
    var promoBubbleContainers = promoBubble.getElements('div.container');
    totalBubbles = promoBubbleContainers.length;

    // show currentbubble
    promoBubbleContainers[currentBubble].addClass('container_active');
        
    // buttons
    if (promoBubble.getElement('a.next')) {
        promoBubble.getElement('a.next').addEvent('click', function() {
			this.blur();
            bubbleAutoPlay = $clear(bubbleAutoPlay);
            nextPromoBubble();
            bubbleAutoPlay = nextPromoBubble.periodical(5000);
        });
    }

    if (promoBubble.getElement('a.prev')) {
        promoBubble.getElement('a.prev').addEvent('click', function() {
			this.blur();
            bubbleAutoPlay = $clear(bubbleAutoPlay);
            prevPromoBubble();
			bubbleAutoPlay = nextPromoBubble.periodical(5000);
        });
    }
    
}

/**
* nextPromoBubble
*
* show next promo bubble
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function nextPromoBubble() {
    
    var promoBubbles = new Array("actiebubble_left", "actiebubble_middle", "actiebubble_right");
    var promoBubble = $$('div.actiebubble')[0];

    promoBubble.removeClass(promoBubbles[currentBubble]);
    if (currentBubble < (totalBubbles - 1)) {
        currentBubble++;
    } else {
        currentBubble = 0;
    }

    if (!promoBubbles[currentBubble]) {
        promoBubble.addClass(promoBubbles[0]);
    } else {
        promoBubble.addClass(promoBubbles[currentBubble]);
    }
    
    // swap content
    var promoBubbleContainers = promoBubble.getElements('div.container');
    promoBubbleContainers.removeClass('container_active');

    promoBubbleContainers[currentBubble].addClass('container_active');
       
}


/**
* prevPromoBubble
*
* show previous promo bubble
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function prevPromoBubble() {

    var promoBubbles = new Array("actiebubble_left", "actiebubble_middle", "actiebubble_right");
    var promoBubble = $$('div.actiebubble')[0];

    promoBubble.removeClass(promoBubbles[currentBubble]);
    if (currentBubble > 0) {
        currentBubble--;
    } else {
        currentBubble = promoBubbles.length - 1;
    }

    if (!promoBubbles[currentBubble]) {
        promoBubble.addClass(promoBubbles[0]);
    } else {
        promoBubble.addClass(promoBubbles[currentBubble]);
    }

    // swap content
    var promoBubbleContainers = promoBubble.getElements('div.container');
    promoBubbleContainers.removeClass('container_active');

    promoBubbleContainers[currentBubble].addClass('container_active');

} 

/**
* closeBubble
*
* closes speechbubble
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/


function closeBubble() {

    var closeButton = $$('div.speechbubble a.close')[0];

    if (!closeButton) return false;

    closeButton.addEvent('click', function() {
        this.getParent('div.speechbubble').removeClass('speechbubble_active');
        initPromoBubbles();
    });

}


/**
* function addTooltips
*
* shows tooltips
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function addTooltips() {
    
    var thisTips = new Tips($$('.adres'), {
        className: 'tooltip', 
        fixed: false,
        offsets: { 'x': 10, 'y': -20 }
    });
    
}


/**
* swapImagesEmployees
*
* swaps detail images of employees
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function swapImagesEmployees() {

    var employeeList = $$('ul.employees_list')[0];
    if (!employeeList) return false;

    var employees = employeeList.getElements('li');
    employees.each(function(item) {

        if (item.currentImg == 0) {
            item.currentImg = 1;
        } else {
            item.currentImg = 0;
        }

        var photos = item.getElements('img.photo');
        photos.removeClass('photoswap');
        photos[item.currentImg].addClass('photoswap');

    });

}

/**
* initCarrousel()
*
* switches pics in a gallery
*
* @author Rou-hun <lowen{AT}efocus.nl>
* @return void
*/

function initCarrousel() {

	if (!document.getElement('div.carrousel')) return false;

	var elCarrousel = document.getElement('div.carrousel');
	var elCarrouselList = elCarrousel.getElement('ul.carrousel_list');
	var arrCarrouselItems = elCarrouselList.getElements('li');

	var intCarrouselListTotalWidth = 0;
	var elCarrouselPrevButton = elCarrousel.getElement('a.carrousel_nav_prev');
	var elCarrouselNextButton = elCarrousel.getElement('a.carrousel_nav_next');
	var intCarrouselItemCurrent = 0;

	var viewPortWidth = elCarrouselList.getParent('div.viewport').getStyle('width').toInt();

	arrCarrouselItems.each(function(elCarrouselItem) {
	    intCarrouselListTotalWidth = intCarrouselListTotalWidth + elCarrouselItem.getWidth();
	});
    
	elCarrouselList.setStyle('width', intCarrouselListTotalWidth);
	elCarrouselPrevButton.addClass('disableditem');
	
	if (intCarrouselListTotalWidth <= viewPortWidth) {
	    elCarrouselNextButton.addClass('disableditem');
    }
	
	elCarrouselPrevButton.addEvent('click', function(event) {
	    event.stop();
	    elCarrouselNextButton.removeClass('disableditem');
        
	    var currentY = (elCarrouselList.getStyle('left').toInt() * -1);
	    
	    if (currentY == 0) {
	        elCarrouselPrevButton.addClass('disableditem');
	    }

	    if (intCarrouselItemCurrent > 0) {
	        intCarrouselItemCurrent--;
	    }
	    slideItem(intCarrouselItemCurrent);
	})

	elCarrouselNextButton.addEvent('click', function(event) {
	    event.stop();

	    elCarrouselPrevButton.removeClass('disableditem');
        
	    var currentY = (elCarrouselList.getStyle('left').toInt() * -1);
	    var currentW = elCarrouselList.getStyle('width').toInt();
	    var nextListW = arrCarrouselItems[(intCarrouselItemCurrent + 1)].getStyle('width').toInt();
        
	    if (currentY >= (currentW - 665)) {
	        elCarrouselNextButton.addClass('disableditem');
	        return false;
	    }

	    if (intCarrouselItemCurrent < arrCarrouselItems.length - 1) {
	        intCarrouselItemCurrent++;
	    }
	    slideItem(intCarrouselItemCurrent);
	})

	var slideItem = function(intThisCarrouselItemCurrent) {
		fxTweenCarrouselOffset = new Fx.Tween(elCarrouselList, {duration: 250, transition: Fx.Transitions.Quint.easeOut});
		intCarrouselItemOffset = arrCarrouselItems[intThisCarrouselItemCurrent].getPosition(elCarrouselList).x;
		fxTweenCarrouselOffset.start('left', elCarrouselList.getStyle('left'), -intCarrouselItemOffset);
    }
	
	if (elCarrousel.hasClass('carrousel_end')) {
		 slideItem(arrCarrouselItems.length - 1);
		 elCarrouselPrevButton.removeClass('disableditem');
		 intCarrouselItemCurrent = arrCarrouselItems.length - 2;
	}
	
	if (elCarrousel.hasClass('carrousel_previous')) {
		 slideItem(arrCarrouselItems.length - 2);
		 elCarrouselPrevButton.removeClass('disableditem');
		 intCarrouselItemCurrent = arrCarrouselItems.length - 2;
	}
	
}

/**
* initRating()
*
* rating
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function initRating() {

    var ratingBar = $$('div.rating')[0];
    if (!ratingBar) return false;

    var ratingBarOptions = ratingBar.getElements('span');
    var ratingMessage = document.getElement('span.ratingmsg');
    var ratingInput = ratingBar.getElement('input');
    
    var checkedRating;
    var currentClass;
    var checkedMessage;

    ratingBarOptions.each(function(item, i) {

        item.addEvents({
            'mouseenter': function() {
                if (checkedRating) {
                    ratingBar.removeClass('rating' + checkedRating + 'of5');
                }
                ratingBar.addClass('rating' + i + 'of5');
                currentClass = 'rating' + i + 'of5';

                ratingMessage.set('text', item.get('text'));

            },
            'mouseleave': function() {
                ratingBar.removeClass(currentClass);
                if (checkedRating) {
                    ratingBar.addClass('rating' + checkedRating + 'of5');
                    ratingMessage.set('text', checkedMessage);
                }
            },
            'click': function() {
                checkedRating = i;
                checkedMessage = item.get('text');
                ratingInput.set('value', checkedRating);
            }

        });


    });

}



/**
* initVoting
* Voting calculation for dessert of the year Top 3.
*
* @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
* @return void
*/

function initVoting() {
	if (!document.getElement('.toetjesvanhetjaar') || !document.getElement('.tussenstand')) return;

	var arrDessertsOfTheYear = document.getElements('.toetjesvanhetjaar li');
	var arrInterimScores = document.getElement('.tussenstand').getElements('li');
	var txtEmptyDessertTitle = '(kies toetje)';

	arrDessertsOfTheYear.each(function(elDessert) {
		var txtDessertTitle = elDessert.getElement('p').get('text');
		var arrDessertInputs = elDessert.getElements('input');
		arrDessertInputs.each(function(elDessertInput, n) {
			elDessertInput.addEvent('click', function() {
				arrInterimScores.each(function(elInterimScore) {
					if (elInterimScore.get('text') == txtDessertTitle) elInterimScore.set('text', txtEmptyDessertTitle);
				});
				arrInterimScores[n].set('text', txtDessertTitle);
				disableCheckedInput(elDessertInput, n);
			});
		});
	});

	var disableCheckedInput = function(elThisDessertInput, intThisInputNumber) {
		arrDessertsOfTheYear.each(function(elDessert) {
			var elDesserteInput = elDessert.getElements('input')[intThisInputNumber];
			if (elDesserteInput != elThisDessertInput) elDesserteInput.setProperty('checked', '');
		});
	}
}



/**
* GetEnter
* Gets the key "enter" on the input
*
* @author Jeroen Datema <jeroen.datema[at]efocus.nl>
* @return void
*/
function GetEnter(url, e, zoekstring) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13) {
        window.location = url + "?q=" + encodeURIComponentNew(zoekstring);
        return false;
    }
    else
        return true;
}

/**
* RedirectZoek
*
* @author Jeroen Datema <jeroen.datema[at]efocus.nl>
* @return void
*/
function RedirectZoek(url, zoekstring) {
    window.location = url + "?q=" + encodeURIComponentNew(zoekstring);
    return false;
}

/** 
* functie encodeURIComponentNew() doet escape() maar dan netjes unicode 
*
* @author Barend <barend[at]efocus.nl>
*/
var hexchars = "0123456789ABCDEF";
var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
function encodeURIComponentNew(s) {
    var s = utf8(s);
    var c;
    var enc = "";
    for (var i = 0; i < s.length; i++) {
        if (okURIchars.indexOf(s.charAt(i)) == -1)
            enc += "%" + toHex(s.charCodeAt(i));
        else
            enc += s.charAt(i);
    }
    return enc;
}
function toHex(n) {
    return hexchars.charAt(n >> 4) + hexchars.charAt(n & 0xF);
}
function utf8(wide) {
    var c, s;
    var enc = "";
    var i = 0;
    while (i < wide.length) {
        c = wide.charCodeAt(i++);
        // handle UTF-16 surrogates
        if (c >= 0xDC00 && c < 0xE000) continue;
        if (c >= 0xD800 && c < 0xDC00) {
            if (i >= wide.length) continue;
            s = wide.charCodeAt(i++);
            if (s < 0xDC00 || c >= 0xDE00) continue;
            c = ((c - 0xD800) << 10) + (s - 0xDC00) + 0x10000;
        }
        // output value
        if (c < 0x80) enc += String.fromCharCode(c);
        else if (c < 0x800) enc += String.fromCharCode(0xC0 + (c >> 6), 0x80 + (c & 0x3F));
        else if (c < 0x10000) enc += String.fromCharCode(0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
        else enc += String.fromCharCode(0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
    }
    return enc;
}


/**
* insertContactIframe
* inserts iFrame on contactpage
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* @return void
*/
function insertContactIframe() {
	if($('contact_iframe_container')) {
		var frameContainer = $('contact_iframe_container');
		var contactWidth = 500;
		var contactHeight = 850;
	} else if (document.getElement('div.flash_contact')) {
		var frameContainer = document.getElement('div.flash_contact');
		var contactWidth = 600;
		var contactHeight = 375;
	} else {
		return false;
	}
	
	var contactFrame = new Element('iframe', {
	    'src':          'http://www.frieslandcampinaconsumentenservice.com/mona_04/suggesties.asp',
		'width':		contactWidth,
		'height':		contactHeight,
		'frameborder':	0
	});
	contactFrame.inject(frameContainer);
}

/**
* positionImageSupermarket
* positions image and decoration in supermarket lightbox
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function positionImageSupermarket() {

    var lightBoxContent = $$('div.lightbox_content_bg')[0];
    if (!lightBoxContent) return false;
    
    /* positioning supermarket image */
    var lightboxW = lightBoxContent.getSize().x.toInt();
    var lightboxH = lightBoxContent.getSize().y.toInt();

    var imgSupermarket = lightBoxContent.getElement('img.supermarkt_img');
    var imgW = imgSupermarket.getSize().x.toInt();
    var imgH = imgSupermarket.getSize().y.toInt();
    
    var newLeft = ((lightboxW - imgW) / 2).round();
    var newTop = ((lightboxH - imgH) / 2).round();

    imgSupermarket.setStyle('left', newLeft);
    imgSupermarket.setStyle('top', newTop);

    /* positioning decoration image left */
    var imgDecorationLeft = lightBoxContent.getElement('img.decoration_left');
    var imgLeftW = imgDecorationLeft.getSize().x.toInt();
    var imgLeftH = imgDecorationLeft.getSize().y.toInt();

    var newLeftLeft = newLeft - (imgLeftW / 2);
    var newLeftTop = ((lightboxH - imgLeftH) / 2);
    
    imgDecorationLeft.setStyle('left', newLeftLeft);
    imgDecorationLeft.setStyle('top', newLeftTop);

    /* positioning decoration image right */
    var imgDecorationRight = lightBoxContent.getElement('img.decoration_right');
    var imgRightW = imgDecorationRight.getSize().x.toInt();
    
    var newRightLeft = (newLeft + imgW) - (imgRightW / 2);
    
    imgDecorationRight.setStyle('left', newRightLeft);
    imgDecorationRight.setStyle('top', newLeftTop);


}

/**
* openLightboxSupermarket
* opens lightbox
*
* @author Mirjam <mirjam{AT}efocus.nl>
* @return void
*/

function openLightboxSupermarket(lightboxUrl) {

    if (!Shadowbox || !lightboxUrl) return false;
    
    Shadowbox.open({
        content: lightboxUrl,
        player: "iframe",
        height: 520,
        width: 715
    });
    
}