if(window['console'] === undefined) window.console = {log: function(){}};

var SiteFunctions = Class.create({
	initialize: function() {

	window.custom = this;
	
	Cufon.replace(['.footer','#menu-placer','h1','h2','h3','#more-for-brands ul li'], {
    	hover: true
    });
    
    }
});

var RoutePlanner = Class.create({
    initialize: function(addresses, options) {

        this.map, this.geocoder, this.addressFrom, this.addressTo, this.directions = false;

        this.addresses = addresses;

        this.config = options || {};
        this.config.centerMap = this.config.centerMap || {};

        if ($('form-address-to'))   this._populateDropdown();
        if ($('map'))               this._createMap();

        if ($('form-planner')) {
            $('form-planner').observe('submit', function(e){
                this._route();
                e.stop();
            }.bind(this));
        }
        
        if ($('form-plan-route')) {
        	$('form-plan-route').observe('click', this._route.bind(this));
        }
    },
    route: function(from, to) {
        
    },
    _addressFrom: function() {
        this.addressFrom = $F('form-address-from') + ', ' + $F('form-place-from') + ', Nederland';
        return this.addressFrom;
    },
    _populateDropdown: function() {
        this.addresses.each(function(item){this._createAddressOption(item);}.bind(this));
        $('form-address-to').observe('change', function(e){
            this.addressTo = e.target.value;
        }.bind(this));
    },
    _createAddressOption: function(item) {
        $('form-address-to').appendChild(
            new Element('option', {
                id: 'rpOption-' + item.name,
                value: item.address}
            ).update(item.name)
        );
    },
    _createMap: function() {
        this.map = new GMap2($('map'));
        var lat = this.config.centerMap.lat || 52.325111;
        var lon = this.config.centerMap.lon || 6.655312;
        var zoom = this.config.centerMap.zoom || 11;
        this.map.setCenter(new GLatLng(lat, lon), zoom);

        this.directions = new GDirections(this.map, $('directions'));

        if (this.config.showMarkers) this._setMarkers();
    },
    _setMarkers: function() {
        this.geocoder = new GClientGeocoder();
        this.addresses.each(function(item){this._setMarkerByAddress(item)}.bind(this));
    },
    _setMarkerByAddress: function(item) {
        this.geocoder.getLatLng(item.address, function(point){
            var marker = new GMarker(point);
            this.map.addOverlay(marker);
            GEvent.addListener(marker, 'click', function() {
                this._selectAddressFromMarker(item);
            }.bind(this));
            GEvent.addListener(marker, 'mouseover', function() {
                marker.openInfoWindowHtml(item.name);
            });
            GEvent.addListener(marker, 'mouseout', function() {
                marker.closeInfoWindow();
            });
        }.bind(this));
    },
    _selectAddressFromMarker: function(item) {
        $('form-address-to').selectedIndex = this.addresses.indexOf(item)+1;
    },
    _route: function() {

        if (!this.addressFrom)  this.addressFrom = this._addressFrom();
        if (!this.addressTo)    this.addressTo = $F('form-address-to');

        this.directions.load('from: ' + this._addressFrom() + ' to: ' + this.addressTo, {"locale": "nl_NL", "getSteps": true});

        console.log(this.directions);
    }
});

document.observe('dom:loaded', function() {
	new SiteFunctions();
});

