// HPS new site functionality
document.observe('dom:loaded', function() {new HPS();});

// encapsulate functions in class 
var HPS = Class.create({
    initialize: function(el)
    {
        // show/hide submenus in navigation
        $$('#productnav .showhide').each(function(a)
        {
            a.observe('click', function(e)
            {
                Event.element(e).up('li').down('ul').toggle();
            }.bindAsEventListener(this))
        }, this);
        // trigger news reload
        $$('.vervolgtabs a').each(function(a) {a.observe('click', this.loadNews.bindAsEventListener(this))}, this);
        // trigger news reload
        $$('.optiontoggle').each(function(a) {a.observe('click', function(e) 
        {
            if (Event.element(e).value == 'geometry') {
                $('storage').hide();
                $('geometry').show();
            }
            else {
                $('storage').show();
                $('geometry').hide();
            }
        }.bindAsEventListener(this))}, this);
        // Google map
        if ($('gmap'))
            this.loadMap();

        // home page animation
        this.animInterval = 10;
        if ($('gimmick')) {
            this.currentDiv = parseInt($('gimmick').getAttribute('initial'));
            var divs = $$('div.anim');
            this.divCount = divs.length;
            if (this.divCount > 1)
                this.animator = new PeriodicalExecuter(this.animate.bind(this), this.animInterval);
        }

        // newsletter
        if ($('subscribe'))
            $('subscribe').observe('submit', this.validateSubscribe);
    },

    // load news/projects/background
    loadNews: function(e)
    {
        var elm = Event.element(e);
        var elmName = elm.name.replace(/tab$/, '');
        // activate link
        $$('.vervolgtabs a').each(function(a) {a.removeClassName('active')});
        elm.addClassName('active');
        // determine product group
        var tag = window.location.pathname.replace(/^\//, '').replace(/\/.*/, '');
        switch (tag) {
            case 'industrial-pcs':
                tag = 'Industrial PCs';
                break;
            case 'rackmount-servers':
                tag = 'Rackmount servers';
                break;
            case 'minipcs':
                tag = 'Mini PCs';
                break;
            case 'panelpcs':
                tag = 'Panel PCs';
                break;
            case 'maritieme-pcs':
                tag = 'Maritieme PCs';
                break;
            case 'embedded-computing':
                tag = 'Embedded computing';
                break;
            case 'industrial-io':
                tag = 'Industrial IO';
                break;
        }
        // update news display
        new Ajax.Updater($('nieuws'), '/newsproxy.php', 
        {
            method: 'GET',
            parameters: 'tag[]=' + elmName + '&tag[]=' + tag + '&order=radded&limit=3'
        });
        Event.stop(e);
    },

    // load Google map
    loadMap: function()
    {
        if (GBrowserIsCompatible()) {
            document.observe('unload', GUnload, false);
            // display map
            var map = new GMap2($('gmap'));
            var point = new GLatLng(52.08133568833959, 5.466830134391785);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(point, 15);
            // add marker
            var hpsIcon = new GIcon();
            hpsIcon.image = "/skin/hps-pin.png";
            hpsIcon.iconSize = new GSize(91, 53);
            hpsIcon.iconAnchor = new GPoint(39, 45);
            map.addOverlay(new GMarker(point, hpsIcon));
            // add directions
            var directions = new GDirections(map, $('gdirections'));
            GEvent.addListener(directions, 'addoverlay', function() {
                var last = directions.getMarker(directions.getNumRoutes());
                map.removeOverlay(last);
            });
            $('routeform').observe('submit', function(e)
            {
                var elm = Event.element(e);
                directions.load('from: ' 
                    + elm.routefrom.value.replace(/(\d{4})\W+([A-Za-z]{2})/, "$1$2")
                    + ' to: ' + elm.routeto.value, {locale: 'nl_NL'});
                Event.stop(e);
            });
        }
    },

    // animate divs
    animate: function()
    {
        var swapIn = this.currentDiv + 1;
        if (swapIn > this.divCount)
            swapIn = 1;
        new Effect.Fade('anim' + this.currentDiv, { duration: 2.0 });
        new Effect.Appear('anim' + swapIn, { duration: 2.0 });
        this.currentDiv = swapIn;
    },

    validateSubscribe: function(e)
    {
        var f = Event.element(e);
        var email = /^([\w+\.\-])+\@(([\w\-])+\.)+([a-zA-Z]{2,})+$/;
        if (!email.test(f.field5.value)) {
            alert('Vul a.u.b. een geldig email adres in.');
            Event.stop(e);
            return false;
        }
        else
            return true;
    }
});
