var Stb = {
	'version': '0.2.0'
};

/**
 *  Additions to mootools
 */
Element.implement({
	/**
	 * Same as mootools' Element.empty(), but the child
	 * elements aren't destroyed, but disposed so they can
	 * be injectected back.
	 * @see http://mootools.net/docs/Element/Element#Element:dispose
	 */
	disposeChilds: function() {
		$A(this.childNodes).each(function(node){
			Element.dispose(node);
		});
		return this;
	},
	
	insertData: function(data, prefix) {
		if(!$chk(prefix)) {
			prefix = '';
		}
		if($type(data) == 'object') {
			data = $H(data);
		}
		data.each(function(value, name) {
			className = name;
			if(prefix != '') {
				className = prefix+className.ucFirst();  
			}
			this.getElements('.'+className).each(function(node) {
				node.set('html', value);
			}, this);
		},this);
	},
	
	addActions: function(actions, bind, prefix) {
		if(!$chk(prefix)) {
			prefix = '';
		}
		if($type(actions) == 'object') {
			actions = $H(actions);
		}
		actions.each(function(action, name) {
			className = 'action'+name.ucFirst();
			if(prefix != '') {
				className = prefix+className.ucFirst();  
			}
			
			this.getElements('.'+className).each(function(node) {
				var event = $chk(action.event) ? action.event : 'click',
					func = $chk(action.action) ? action.action : action;
				node.addEvent(event, func.bind(bind));
			}, this);
		},this);
	}
});

String.implement({
	/**
	 * Make the string's first character uppercase
	 */
	ucFirst: function() {
	    return this.charAt(0).toUpperCase() + this.substr(1);
	}
});


Window.implement({
	/**
	 * Cross browser method te retrieve the innerSizes of the window.
	 * 
	 * @see http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	 */
	getInnerSize: function() {
		if($type(window.innerWidth) == 'number') {
			width = window.innerWidth;
			height = window.innerHeight;
		} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		return new Hash({
			x: width,
			y: height 
		});
	}
});

Stb.Google = new Class();

Stb.Google.Map = new Class({
	Extends: Options,
	
	options: {
		//address: '',
		latitude: '51.980832',
		longitude: '5.946002',
		zoom: 13
	},
	
	initialize: function(map, options) {
		this.setOptions(options);
		if(!GBrowserIsCompatible()) {
			return false;
		}
		this.map = new GMap2(map);
		this.geocoder = new GClientGeocoder();
		if(this.options.address && this.options.infoWindow) {
			this.showInfoWindow(this.options.address, this.options.infoWindow, {
				zoom: this.options.zoom
			});
		} else {
			this.map.setCenter(new GLatLng(this.options.latitude, this.options.longitude), this.options.zoom);
		}
		this.map.setUIToDefault();
	},
	
	showInfoWindow: function(address, infoWindow, options) {
		new Stb.Google.Map.InfoWindow(this, $extend({
			address: address,
			infoWindow: infoWindow
		}, options));
	},
	
	addAddressFinder: function(options) {
		new Stb.Google.Map.AddressFinder(this, options);
	}
});

Stb.Google.Map.InfoWindow = new Class({
	Extends: Options,
	
	initialize: function(map, options) {
		this.setOptions(options);
		this.map = map;
		
		this.showAddress(this.options.address, this.options.infoWindow);
	},
	
	showAddress: function(address, infoWindow) {
		this.map.geocoder.getLatLng(address, function(point) {
			if($chk(point)) {
				this.addressFound(address, point);
			} else {
				this.addressNotFound(address);
			}
		}.bind(this));
	},
	
	addressFound: function(address, point) {
		this.map.map.setCenter(point, this.options.zoom);
		this.map.geocoder.getLocations(address, function(response) {
			this.map.map.clearOverlays();
			if(!$chk(response) || response.Status.code != 200) {
				return this.addressNotFound(address);
			}
			this.place = response.Placemark[0];
			var point = new GLatLng(this.place.Point.coordinates[1], this.place.Point.coordinates[0]),
				marker = new GMarker(point),
				infoWindow = $('infoWindow').clone();
			infoWindow.removeClass('stbTpl');
			
			infoWindow.insertData({
				address: this.place.address
			});
			infoWindow.addActions({
				directions: function() {
					window.open('http://maps.google.nl/maps?daddr='+this.place.address);
				}
			}, this);
			
			if($chk(this.options.zoom)) {
				this.map.map.setZoom(this.options.zoom);
			}
			this.map.map.addOverlay(marker);
			//this.map.map.openInfoWindow(point, infoWindow);
		}.bind(this));
	},
	
	addressNotFound: function(address) {
		alert('Het adres '+address+' is niet gevonden');
	}
});