

var VDB = VDB || {};

VDB.application = {
	map: undefined,
	msgTrans: undefined,


	/**
	 * Initializes the user interface
	 * @param {Object} config Object literal containing some initial values for the map object
	 */
	init: function (config) {

		if (!$('map_canvas')) return;

		this.map = map = new VDB.map(config, this);
		// var markerCluster = new MarkerClusterer(this.map, this.markers);
		this.msgTrans = new Fx.Tween($('widget-message'), {'duration': 1000});
		this.map.startMap($('map_canvas'), $('map_controls'));

		//Google Analytics tracking
		if(typeof pageTracker !== 'undefined') this.map.setPagetracker(pageTracker);
		
		window.addEvent('resize', function () {
			map.setMapSize();
		});

		$('searchaddress').addEvent('click', function () {
			var address = $('address-input').get('value');
			if (address.match(/^[0-9]{4}\s+[a-z]{2}$/i)) {
				address = address.replace(' ', '');
			}

			map.executeAddressSearch(address)
		});

		$('address-input').addEvent('keyup', function (e) {
			if (e.key == 'enter') {
				var address = $('address-input').get('value');

				if (address.match(/^[0-9]{4}\s+[a-z]{2}$/i)) {
					address = address.replace(' ', '');
				}

				map.executeAddressSearch(address)
			}
		});

		$('new-problem-btn').addEvent('click', function () {
			if (map.insertionMode == 'problem') {
				map.setInsertionMode('normal');
			} else {
				map.setInsertionMode('problem');
			}
		});

		$('new-idea-btn').addEvent('click', function () {
			if (map.insertionMode == 'idea') {
				map.setInsertionMode('normal');
			} else {
				map.setInsertionMode('idea');
			}
		});

		$('panel-info').addEvent('click', function (e) {
			if (e.target.get('tag') == 'span' && e.target.hasClass('close')) {
				this.setStyle('display', 'none');
				if (VDB.application.lockedMarker) {
					VDB.application.lockedMarker._marker.enableDragging();
					delete VDB.application.lockedMarker;
				}
				
			}
		});

		$('panel-info').addEvent('mousewheel', function (e) {
			e.stopPropagation();
			e.preventDefault();
		});

		//add an eventhandler to show the help information
		$('explanation-btn').addEvent('click', function () {
			map.showMode('info', {'url': '/help?remote=1'});
		});

		///Checken of navigatie en header wel bestaan...

		if($('navigation')){
			$('nav-kaart').getElement('a').addEvent('click', function (e) {
				if ($('panel-welcome'))
					$('panel-welcome').destroy();
	
				$('panel-info').setStyle('display', 'none');
	
				$$('#widget-address', '#widget-addbtns', '#widget-categories').setStyle('display', 'block');
	
				if ($('widget-message').get('text') != '') {
					$('widget-message').setStyle('display', 'block');
				}
				e.stop();
				return false;
			});
	
			//add an eventhandler to show the FAQ
			$('nav-faq').getElement('a').addEvent('click', function (e) {
				if ($('panel-welcome'))
					$('panel-welcome').destroy();
	
				map.showMode('info', {'url': '/faq?remote=1'});
				e.stop();
				return false;
			});
	
			//add an eventhandler to show the scores
			$('nav-ranglijsten').getElement('a').addEvent('click', function (e) {
				if ($('panel-welcome'))
					$('panel-welcome').destroy();
	
				map.showMode('info', {'url': '/ranglijsten?remote=1'});
				e.stop();
				return false;
			});
	
			//add an eventhandler to show the scores
			$('nav-over').getElement('a').addEvent('click', function (e) {
				if ($('panel-welcome'))
					$('panel-welcome').destroy();
	
				map.showMode('info', {'url': '/over-verbeterdebuurt?remote=1'});
				e.stop();
				return false;
			});
	
			//add an eventhandler to show the scores
			$('nav-contact').getElement('a').addEvent('click', function (e) {
				if ($('panel-welcome'))
					$('panel-welcome').destroy();
	
				map.showMode('info', {'type': 'iframe','url': '/contact?remote=1'});
				e.stop();
				return false;
			});
		if($('nav-iframe-code-request'))
			$('nav-iframe-code-request').getElement('a').addEvent('click', function (e) {
        if ($('panel-welcome'))
          $('panel-welcome').destroy();
  
        map.showMode('info', {'type': 'iframe','url': '/iframe-code-aanvragen?remote=1'});
        e.stop();
        return false;
      });
		}

		$$('#widget-categories form strong').addEvent('click', this.showHideCategory);

		$('widget-categories').addEvent('click', function (e) {
			var theTarget = $(e.target);

			if (theTarget.get('tag') != 'strong' && !theTarget.hasClass('mask')) {
				map.showCategories();
			}
		});

		document.addEvent('unload', GUnload);
	},

	/**
	 * Show a message to the user
	 * @param {String} message A string containing the message to display
	 * @param {Boolean} highlight Indicates of the background should fade in order to get attention
	 */
	setMessage: function (title, message, highlight) {
		if (title != '') {
			$('widget-message').set('html', '<strong>' + title + '</strong><br />' + message);
		} else {
			$('widget-message').set('html', message);
		}

		$('widget-message').setStyle('display', 'block');
		this.msgTrans.set('opacity', 1);

		if (highlight)
			this.msgTrans.start('background-color', '#FFFF55', '#FFF');

		window.setTimeout(this.hideMessage.bind(this), 20000);
	},


	/**
	 * Hide the message widget
	 */
	hideMessage: function () {
		this.msgTrans.start('opacity', 1, 0);
		
		window.setTimeout(function () {
			$('widget-message').empty();
			$('widget-message').setStyle('display', 'none');
		}, this.msgTrans.options.duration);
	},


	/**
	 * Shows or hides a category
	 */
	showHideCategory: function () {
		if ($('widget-categories').getElement('div.mask')) return;
		
		if (this.getNext().getStyle('display') == 'block') {
			this.getNext().setStyle('display', 'none');
			this.addClass('closed');
		} else {
			this.getNext().setStyle('display', 'block');
			this.removeClass('closed');
		}
	}
}

document.addEvent('domready', function () {
	VDB.application.init(CONFIG);
});