/*
 * MooTools
 * versie <<http://gemini.nl.dev:8000/sbeos/js/lib/mootools.js>>
 * used for site interactivity
*/

if (typeof($pick) == 'undefined') {
	$pick = function (obj, picked){
		return $defined(obj) ? obj : picked;
	};
}

Element.implement({
    hide: function()
    {
        return this.setStyle('opacity',0);
    },

    show: function()
    {
    	return this.setStyle('opacity',0.9);
    }
});

var DropdownMenu = new Class({
    initialize: function(element)
    {
        $A($(element).childNodes).each(function(el)
        {
            if(el.nodeName.toLowerCase() == 'li')
            {
                $A($(el).childNodes).each(function(el2)
                {
                    if(el2.nodeName.toLowerCase() == 'ul')
                    {
                        //$(el2).hide();
                        $(el2).setStyle('opacity', 0);

                        el.addEvent('mouseover', function() {
                            //el2.show();
                            $(el2).setStyle('opacity', 0.9);
                            this.getFirst().setStyles({
                                color: '#ff0000',
                                background: 'url(/images/siteimages/icon-main-navigation-active.gif) left no-repeat'
                            });
                            return false;
                        });

                        el.addEvent('mouseout', function() {
                            //el2.hide();
                            $(el2).setStyle('opacity', 0);
                            //keep parent class selected
                            if (this.getFirst().hasClass('selected')){
                                return false;
                            }
                            this.getFirst().setStyles({
                                color: '#223f86',
                                background: 'url(/images/siteimages/icon-main-navigation.gif) left no-repeat'
                            });
                            return false;
                        });

                        new DropdownMenu(el2);
                    };
                });
            };
        });
        return this;
    }
});
function tooltip(text) {
    var tipFavorites = new Tips($$('#favoritesTab'), {
        initialize:function(){
            this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
        },
        onShow: function(toolTip) {
            this.fx.start(1);
        },
        onHide: function(toolTip) {
            this.fx.start(0);
        }
    });
};

function popup() {
    if ($chk($('closeButton'))) {
        var controlElement = $('closeButton');

        //add unobtrusive javascript for onclick event to <a> tag that closes popup
        controlElement.removeAttribute("href");
        controlElement.addEvent("click", function(event) {
            window.close();
        });
    };
    self.resizeTo(768, 630);
    var offSet = getScrollWidth() - getWidth();
    self.resizeTo(768 + offSet, 630);
}

function imageMapDestinations() {
    if ($chk($('imageMapDestinations'))) {
        var updateEffect = new Fx.Styles($E('table', 'destinations'), {duration: 1000,transition: Fx.Transitions.linear});
        var countryHotSpots = $$('#mapEurope area');

        for (var i = 0; i < countryHotSpots.length; i++) {
            countryHotSpots[i].removeProperty('href');
            countryHotSpots[i].addEvent("click", function(event) {
                alert('ajax call for country ISO: ' + this.getProperty('alt')); // <- Wat is dit?
                /********************************************************************************************
                * event: geklikt op land in kaart europa in destinations page (dp.html)
                * actie: ajax call om tabel met bestemmingen (div#destinations tabel) te updaten
                ********************************************************************************************/

                //opacity from 100% to 0% and back again for visual cue user
                updateEffect.start({
                    'opacity': [1,0]
                }).chain(function(){
                    updateEffect.start({
                    'opacity': [0,1]
                    });
                });
            });
        };
    };
};


function unselectMenus(){
    var menuElements = $$('#dropdownMenu li a');
    //alert(menuElements.length);
    for (var i = 0; i < menuElements.length; i++) {
        menuElements[i].removeClass('selected');
    }
}

function emptyFieldsOnFocus() {
    var inputElements = $$('input');

    if (inputElements.length > 0) {
        //add unobtrusive javascript for onclick event to <input> tag that empties field on entry
        for (var i = 0; i < inputElements.length; i++){
            inputElements[i].onfocus = function(event) {
                  this.value = '';
            };
        };
    };
};

function alignRightSubmenu(){
    var uls = $$('li ul'), Auls;
    if (uls.length > 0) {
        Auls = $A(uls);
        
		
        if (Auls) {
            //try {
                Auls.getLast().addClass('rightItem');
           // } catch(e) {}
        }
    }
}

////Window.onDomReady(function() {emptyFieldsOnFocus()});
window.addEvent('domready',function() {
	new DropdownMenu($('dropdownMenu'));
	alignRightSubmenu();
});

/**
 * Check for global EURO namespace
 */
if(!$chk(EURO)) {
    var EURO = {};
}

/**
 * Redirect for the banners
 * @param {String} destination
 * @param {String} target
 */
EURO.redirect = function(destination, target) {
	console.log(destination);
	console.log(target);
	window.open(destination, target);
	return false;
}

function popitup(url) {
    var newwindow = window.open(url,'name','height=520,width=768,resizable=1,scrollbars=1');
    if (newwindow.focus) { newwindow.focus() }
    return false;
}

function popitup_booking(url) {
    var newwindow = window.open(url,'name','height=500,width=670,resizable=1,scrollbars=1');
    if (newwindow.focus) { newwindow.focus() }
    return false;
}