var frontend = {
    init: function()
    {
        // Initialize layout
        this.layout.init();

        // Initialize tables
        this.tables.init();

        if($('form').length > 0) this.forms.init();
        if($('#map').length > 0) this.maps.init();
    },

    layout: {
        init: function()
        {
            this.parseExternalLinks();

            if($('.slideshow').length > 0) $('.slideshow').cycle();
            if($('.newsflash').length > 0) frontend.ticker.init();
            if($('.gallery').length > 0) frontend.gallery.init();
        },

        parseExternalLinks: function()
        {
            $('a[rel=external]').click(function(e){
                open(this.href);
                e.preventDefault();
            });
        },

        /**
         * Set the minimum height
         * Is needed because navigation is positioned absolutely
         */

        setMinHeight: function(target)
        {
            // Set min-height of #main div, based on outerheight of target
            $('#main').css('min-height', (target.outerHeight()) + 'px');
        }
    },

    tables: {
        init: function()
        {
            /*
            // Add class 'first' to first th & td in table
            $('table th:first-child').addClass('first');
            $('table td:first-child').addClass('first');

            // Alternating rows
            $('table tr:even').addClass('even');
            $('table tr:odd').addClass('odd');

            // Hide thead section when not first class
            $('table:not(.first) thead').hide();
            */
        }
    },

    forms: {
        init: function() {
            this.setRequiredFieldIndicators();
            this.setFocusListeners();
        },

        /**
         * Set required indicators adds * to elements which have a class="required"
         */
        setRequiredFieldIndicators: function()
        {
            $('.required label:last-child').append('<span class="indicator">*</span>');
        },

        setFocusListeners: function()
        {
            $('dd .field').focus(function(e) {
                // Assign focus class to dd parent
                $(this).addClass('focus');
            });

            $('dd .field').blur(function(e) {
                // Remove focus class form dd parent
                $(this).removeClass('focus');
            });
        }
    },

    ticker: {
        init: function()
        {
            $('.newsflash ul').ticker();
        }
    },

    gallery: {
        init: function()
        {
            $('.gallery').position();
            // Init lightbox
            $('.gallery a').lightBox({
                imageLoading: '/custom/frontend/astrid/1.0.0/img/lightbox/loading.gif',
                imageBtnClose: '/custom/frontend/astrid/1.0.0/img/lightbox/close.gif',
                imageBtnPrev: '/custom/frontend/astrid/1.0.0/img/lightbox/prev.gif',
                imageBtnNext: '/custom/frontend/astrid/1.0.0/img/lightbox/next.gif'
            });
        }
    },

    maps: {
        geocoder: null,
        map: null,

        init: function()
        {
            if (GBrowserIsCompatible()) {
                var map = frontend.maps.map;
                
                map = new GMap2(document.getElementById('map'));
                var latlng = new GLatLng(50.864699,4.349917);
                map.setCenter(latlng, 13);
                map.addControl(new GLargeMapControl());
        	    map.addControl(new GMapTypeControl());

                frontend.maps.map = map;
                frontend.maps.geocoder = new GClientGeocoder();
				frontend.maps.showAddress("Havenlaan 86C,1000 Brussel");
            }
        },

        showAddress: function(address)
        {
            var html = '<strong>Tour & Taxis</strong><br /><br />Havenlaan 86C, 1000 Brussel';
            var map = frontend.maps.map;
            var geocoder = frontend.maps.geocoder;

            if (geocoder) {
                geocoder.getLatLng(
                    address,
                    function(point) {
                        if (point) {
                            map.setCenter(point, 13);
                            var marker = frontend.maps.createMarker(point, address, html);
                            map.addOverlay(marker);
                        }
                    }
                );
            }
        },
        
        createMarker: function(point, name, html)
        {
            var marker = new GMarker(point);
            
	        GEvent.addListener(marker, "click", function() {
	          marker.openInfoWindowHtml(html);
	        });
	        gmarkers = marker;
	        //htmls[i] = html;
	        //i++;
	        return marker;            
        }
    }
};


$(document).ready(function()
{
	// General javascript interaction
	frontend.init();
});
