var Global = {
	modules: [],
	map: null,
	markerHighlight: null,
	domainpath: '',
	
	init: function () {
		if (!GBrowserIsCompatible()) {
			return;
		}

		var mapdiv = document.getElementById("map");
		if (!mapdiv) {
			mapdiv = document.getElementById("viewmap");
		}

		if (!mapdiv) {
			return;
		}

		Global.map = new GMap2(mapdiv);
		if (mapdiv.className == 'terrain') {
			Global.map.setCenter(new GLatLng(g_map.lat, g_map.lng), g_map.zoom - 2, G_PHYSICAL_MAP);
		}
		else if (mapdiv.id == 'viewmap') {
			Global.map.setCenter(new GLatLng(g_map.lat, g_map.lng), 15);
		}
		else {
			Global.map.setCenter(new GLatLng(g_map.lat, g_map.lng), g_map.zoom);
		}
		
		Global.map.addControl(new GSmallMapControl());
	
		Global.markerHighlight = new GIcon();
		Global.markerHighlight.image = 'http://fuengirolaguide/img/marker_highlight.png';
		Global.markerHighlight.iconSize = new GSize(20, 34);
		Global.markerHighlight.iconAnchor = new GPoint(9, 34);
	
		$(window).unload(function() {
			GUnload();
		});
	},

	addMarker: function(lat, lng, text, href, a) {
		var html = '<a href="' + href + '">' + text + '</a>';
		var point = new GLatLng(lat, lng);
		var marker = new GMarker(point);
    	GEvent.addListener(marker, "click", function() {
    		marker.openInfoWindowHtml(html);
    	});
	
		Global.map.addOverlay(marker);
		if (a == null) {
			return marker;
		}
				
		var markerHighlight = new GMarker(marker.getPoint(), {clickable: false, icon: Global.markerHighlight});
		var anchor = a[0];
		Global.map.addOverlay(markerHighlight);
		markerHighlight.hide();

		GEvent.addListener(marker, 'mouseover', function(){
			markerHighlight.show();
			anchor.style.textDecoration = 'underline';
		});
		
		GEvent.addListener(marker, 'mouseout', function(){
			markerHighlight.hide();
			anchor.style.textDecoration = '';
		});

		return markerHighlight;
	},
	
	initModules: function () {
		for(var m in Global.modules) {
			id = m.toLowerCase();

			if ($('#' + id).length && typeof(this.modules[m].init) == 'function') {
				Global.modules[m].init();
			}
		}
	}
}

Global.modules.reviews = {
	init: function() {
		$('#reviews li').each(function() {
			var a = $('div a', this);
			var marker = Global.addMarker(
				parseFloat($('span.lat', this).text()),
				parseFloat($('span.lng', this).text()),
				$('h3', this).text(),
				a.attr('href'),
				a
			);
			
			$('a', this).hover(
				function() {marker.show();},
				function() {marker.hide();}
			);
		});
	}
}

Global.modules.viewcontent = {
	init: function() {
		var lat = parseFloat($('#viewcontent span.lat').text());
		var lng = parseFloat($('#viewcontent span.lng').text()); 
		Global.addMarker(
			lat,
			lng,
			$('#viewcontent h2').text(),
			window.location,
			null
		);
		Global.map.setCenter(new GLatLng(lat, lng));
	}
}

Global.modules.gmapform = {
	_stars: 0,
	_marker: null,
	_map: null,

	init: function() {
		var regex = new RegExp("/img/([^\/]*)\/icon_grade");
		var paths = regex.exec($('#grades img:first-child')[0].src);
		Global.domainpath = paths[1];
	
		this._setupMap();
		this._setupGrades();
		this._setupValidate();
		this._setupAjax();
		$('#title').focus();
	},

	_setupValidate: function() {
		$('#gmapform').validate({
		
			rules: {
				description: {
				minlength: 20
				}
			},
			invalidHandler: function(e, validator) {
				if ($('#lat').val() == ''|| $('#lng').val() == '') {
					$('#gmapform_errors').show();
					$('#gmapform_errors').html('<label class="texterror">' + _('missing_lat_lng') + '</label>');
				}
				else {
					$('#gmapform_errors').hide();
				}
			},
			submitHandler: function(frm) {
				if ($('#lat').val() == ''|| $('#lng').val() == '') {
					$('#gmapform_errors').show();
					$('#gmapform_errors').html('<label class="texterror">' + _('missing_lat_lng') + '</label>');
					return false;
				}
				else {
					$('#gmapform_errors').hide();
					$('#btnsubmit').attr('disabled', 'disabled');
					frm.submit();
				}
			}
		});
	},

	_setupAjax: function() {
		$('#email').change(
			function() {
				if (this.value == '') {
					return;
				}

				$.post('/emailok', {email: this.value}, function(available) {
					if (eval(available) == false) {
						$('#email').after('<br><label class="error" id="email-notavailable" generated="true">' + _('email_unavailable') + '</label>');
					}
					else {
						$('#email-notavailable').remove();
					}
				});
			}
		);
	},
	
	_setupMap: function() {		
		$('#btnsubmit').click(function(){
			if (!Global.modules.gmapform._marker) {
				return;
			}
			$('#lat').val(Global.modules.gmapform._marker.getPoint().lat());
			$('#lng').val(Global.modules.gmapform._marker.getPoint().lng());
			$('#gmapform_errors').hide();
		});

		GEvent.addListener(Global.map, "click", function(overlay, point) {
			if (!overlay && !Global.modules.gmapform._marker) {
				var marker = new GMarker(point, {draggable: true, icon: G_START_ICON});
				Global.map.addOverlay(marker);
				Global.modules.gmapform._marker = marker;
			}
		});
	},
	
	_setupGrades: function() {
		$('#grades').hover(
			function() {},
			function() {
				Global.modules.gmapform._showStars(0);
			}
		);
		
		$('#grades img').each(function(i, e) {
			$(e).hover(
				function() {
					Global.modules.gmapform._showStars(this.alt);
				},
				function() {}
			).click(
				function() {
					Global.modules.gmapform._stars = this.alt;
					$('#grade').val(this.alt);
				}
			);
		});
	},
	
	_showStars: function(no) {
		$('#grades img').each(
			function(i, e) {
				if (i < no || no == 0 && i < Global.modules.gmapform._stars) {
					e.src = '/img/' + Global.domainpath + '/icon_grade.png';
				} else {
					e.src = '/img/' + Global.domainpath + '/icon_grade_gray.png';
				}
			}
		);
	}
}

Global.modules.photoview = {
	init: function() {
		$('.userReviewInfo').append('<br style="clear: both;" />');
		$('#photoview img').click(
			function() {
				var src = this.src;
				this.src = this.alt;
				this.alt = src;
				
				var e = $('.reviewInfo-inline');
				if (e && e.attr('class') == 'reviewInfo-inline') {
					e.removeClass('reviewInfo-inline').addClass('reviewInfo-block');
				}
				else {
					$('.reviewInfo-block').removeClass('reviewInfo-block').addClass('reviewInfo-inline');
				}
			}
		);
	}
}

Global.modules.greviewcomment = {
	_stars: 0,
	
	init: function() {
		var regex = new RegExp("/img/([^\/]*)\/icon_grade");
		var paths = regex.exec($('#grades img:first-child')[0].src);
		Global.domainpath = paths[1];
	
		this._setupAjax();
		this._setupGrades();
		this._setupValidate();
	},

	_setupValidate: function() {
		$('#greviewcomment').validate({
		
			rules: {
				description: {
				minlength: 20
				}
			},
			submitHandler: function(frm) {
				$('#btnsubmit').attr('disabled', 'disabled');
				frm.submit();
			}
		});
	},

	_setupAjax: function() {
		$('#email').change(
			function() {
				if (this.value == '') {
					return;
				}

				$.post('/emailok', {email: this.value}, function(available) {
					if (eval(available) == false) {
						$('#email').after('<br><label class="error" id="email-notavailable" generated="true">' + _('email_unavailable') + '</label>');
					}
					else {
						$('#email-notavailable').remove();
					}
				});
			}
		);
	},
	
	_setupGrades: function() {
		$('#grades').hover(
			function() {},
			function() {
				Global.modules.greviewcomment._showStars(0);
			}
		);
		
		$('#grades img').each(function(i, e) {
			$(e).hover(
				function() {
					Global.modules.greviewcomment._showStars(this.alt);
				},
				function() {}
			).click(
				function() {
					Global.modules.greviewcomment._stars = this.alt;
					$('#grade').val(this.alt);
				}
			);
		});
	},
	
	_showStars: function(no) {
		$('#grades img').each(
			function(i, e) {
				if (i < no || no == 0 && i < Global.modules.greviewcomment._stars) {
					e.src = '/img/' + Global.domainpath + '/icon_grade.png';
				} else {
					e.src = '/img/' + Global.domainpath + '/icon_grade_gray.png';
				}
			}
		);
	}
}

Global.modules.signupform = {
	init: function() {
		this._setupValidate();
		this._setupAjax();
		$('#name').focus();
	},

	_setupValidate: function() {
	
	},
	
	_setupAjax: function() {
		$('#email').change(
			function() {
				if (this.value == '') {
					return;
				}

				$.post('/emailok', {email: this.value}, function(available) {
					if (eval(available) == false) {
						$('#email').after('<br><label class="error" id="email-notavailable" generated="true">' + _('email_unavailable') + '</label>');
					}
					else {
						$('#email-notavailable').remove();
					}
				});
			}
		);
	}
}


function _(str) {
	return typeof(Strings[str]) != 'undefined' ? Strings[str] : '{' + str + '}';
}

$(document).ready(function () {
	Global.init();
	Global.initModules();
});
