// some globals

var gmap, umarkers, umarkers_popups, emarkers, emarkers_popups, smarkers, smarkers_popups;

var umodifier = true;
var emodifier = true;
var smodifier = true;

jQuery(function() {
	if( jQuery('#profile_edit').size() ) {
		// gmap
    	var gmap = new GMap2(document.getElementById("gmap"));
        gmap.setCenter(new GLatLng((jQuery.trim(jQuery('#lat').val()) != '0') ? jQuery('#lat').val() : 48.853153, (jQuery.trim(jQuery('#lng').val()) != '0') ? jQuery('#lng').val() : 2.285156), 8);
        gmap.setUIToDefault();
        gmap.disableScrollWheelZoom();
        gmap.disableContinuousZoom();
	
		GEvent.addListener(gmap, "click", function(e, point) {
			jQuery('#lat').val(point.lat());
			jQuery('#lng').val(point.lng());
		});
		
		var marker = new GMarker(new GLatLng((jQuery('#lat').val() != '0') ? jQuery('#lat').val() : 48.853153, (jQuery('#lng').val() != '0') ? jQuery('#lng').val() : 2.285156), {
			draggable: true,
			bouncy: true
		});
		gmap.addOverlay(marker);
		
		GEvent.addListener(marker, "dragend", function(e, point) {
			jQuery('#lat').val(e.lat());
			jQuery('#lng').val(e.lng());
		});
	}
	
	if( jQuery('#subscribe_form').size() ) {
		// ------------ validation subscription form
			// email validation
		var email_validation = false;
		jQuery('#email').blur(function(e) {
			if( jQuery.trim( jQuery(this).val() ) == '' ) {
				jQuery('#email_indic').attr('title','Le champs est vide !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				email_validation = false;
			} else {
				// ajax verification
				jQuery.post(absolute_url+'utilisateurs/email_check/', {'email':jQuery(this).val()}, function(data) {
					if( data == 'invalid' ) {
						jQuery('#email_indic').attr('title',"L'email n'est pas un email valide !").fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						email_validation = false;
					} else if( data == 'existing' ) {
						jQuery('#email_indic').attr('title',"L'email existe déjà dans la base de données !").fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						email_validation = false;
					} else {
						jQuery('#email_indic').attr('title',"L'email est valide.").fadeOut().attr('src', absolute_url_icons+'tick.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						email_validation = true;
					}
				}, 'text');
			}
		});
			
			// password validation
		var password_validation = false;
		jQuery('#pass').blur(function(e) {
			if( jQuery(this).val() == '') {
				jQuery('#pass_indic').attr('title','Le champs est vide !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				password_validation = false;
			} else if( jQuery(this).val().length < 6 ) {
				jQuery('#pass_indic').attr('title','Le mot de passe doit contenir au minimum 6 caractères !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				password_validation = false;
			} else {
				jQuery('#pass_indic').attr('title','Le mot de passe est valide.').fadeOut().attr('src', absolute_url_icons+'tick.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				password_validation = true;
			}
		});
		
			// confirm password validation
		var confirm_password_validation = false;
		jQuery('#confirm_pass').blur(function(e) {
			if( jQuery(this).val() == '') {
				jQuery('#confirm_pass_indic').attr('title','Le champs est vide !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				confirm_password_validation = false;
			} else if( jQuery(this).val() != jQuery('#pass').val() ) {
				jQuery('#confirm_pass_indic').attr('title','Les deux mots de passe ne sont pas identiques !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				confirm_password_validation = false;
			} else {
				jQuery('#confirm_pass_indic').attr('title','Les deux mots de passe sont identiques.').fadeOut().attr('src', absolute_url_icons+'tick.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				confirm_password_validation = true;
			}
		});
		
			// pseudo validation
		var pseudo_validation = false;
		jQuery('#pseudo').blur(function(e) {
			if( jQuery.trim( jQuery(this).val() ) == '' ) {
				jQuery('#pseudo_indic').attr('title','Le champs est vide !').fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
				pseudo_validation = false;
			} else {
				// ajax verification
				jQuery.post(absolute_url+'utilisateurs/pseudo_check/', {'pseudo':jQuery(this).val()}, function(data) {
					if( data == 'existing' ) {
						jQuery('#pseudo_indic').attr('title',"Le pseudo existe déjà dans la base de données !").fadeOut().attr('src', absolute_url_icons+'cross.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						pseudo_validation = false;
					} else {
						jQuery('#pseudo_indic').attr('title',"Le pseudo est valide.").fadeOut().attr('src', absolute_url_icons+'tick.png').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						pseudo_validation = true;
					}
				}, 'text');
			}
		});
		
			// form validation
		jQuery('#subscribe_form').submit(function(e) {
			if(email_validation && password_validation && confirm_password_validation && pseudo_validation) {
				if( jQuery('input#statistic').attr('checked') ) {
					e.preventDefault();
					alert("On dirait que vous avez oublié de cocher la preuve que vous n'êtes pas un spammeur...");
				}
				return true;
			} else {
				e.preventDefault();
				alert('Les champs ne sont pas tous remplis convenablement !');
			}
		});		
	}
	
	if( jQuery('#map_left').size() && GBrowserIsCompatible() ) {
    	// gmap
    	var gmap = new GMap2(document.getElementById("gmap"));
        gmap.setCenter(new GLatLng(48.853153, 2.285156), 5);
        gmap.setUIToDefault();
        gmap.disableScrollWheelZoom();
        gmap.disableContinuousZoom();
	
		var gmap_user_icon = new GIcon(G_DEFAULT_ICON);
		gmap_user_icon.image = absolute_url + 'styles/gmap/user_marker.png';
		gmap_user_icon.shadow = absolute_url + 'styles/gmap/user_shadow.png';
		gmap_user_icon.iconSize = new GSize(50, 40);
		gmap_user_icon.shadowSize = new GSize(50, 40);
		gmap_user_icon.iconAnchor = new GPoint(10, 40);
		gmap_user_icon.infoWindowAnchor = new GPoint(12, 25);
		
		var gmap_event_icon = new GIcon(G_DEFAULT_ICON);
		gmap_event_icon.image = absolute_url + 'styles/gmap/event_marker.png';
		gmap_event_icon.shadow = absolute_url + 'styles/gmap/event_shadow.png';
		gmap_event_icon.iconSize = new GSize(63, 40);
		gmap_event_icon.shadowSize = new GSize(63, 40);
		gmap_event_icon.iconAnchor = new GPoint(10, 40);
		gmap_event_icon.infoWindowAnchor = new GPoint(10, 40);
		
		var gmap_shop_icon = new GIcon(G_DEFAULT_ICON);
		gmap_shop_icon.image = absolute_url + 'styles/gmap/shop_marker.png';
		gmap_shop_icon.shadow = absolute_url + 'styles/gmap/shop_shadow.png';
		gmap_shop_icon.iconSize = new GSize(63, 40);
		gmap_shop_icon.shadowSize = new GSize(63, 40);
		gmap_shop_icon.iconAnchor = new GPoint(10, 40);
		gmap_shop_icon.infoWindowAnchor = new GPoint(10, 40);
		
		// users markers
		umarkers = [];
		umarkers_popups = [];
		var markers_amount = users_markers.length;
    	for(var i=0;i<markers_amount;i++) {
			var usr = users_markers[i];
  			if( usr.lat != 0 && usr.lng != 0 ) {
				umarkers[usr.id] = new GMarker(new GLatLng(usr.lat, usr.lng), {
					id: usr.id,
					index: i,
					draggable: false,
					icon: gmap_user_icon,
					window_content: template('user_marker_popup_template', usr)
				});
				gmap.addOverlay(umarkers[usr.id]);
				
				GEvent.addListener(umarkers[usr.id], 'click', function(e) {
					this.openInfoWindowHtml(this.ga.window_content);
				});
			}
		}
		
		// events markers
		emarkers = [];
		emarkers_popups = [];
		var markers_amount = events_markers.length;
    	for(var i=0;i<markers_amount;i++) {
			var evt = events_markers[i];
			if( evt.lat != 0 && evt.lng != 0 ) {
				evt.content = Base64.decode(evt.content);
				evt.address = Base64.decode(evt.address);
				emarkers[evt.id] = new GMarker(new GLatLng(evt.lat, evt.lng), {
					id: evt.id,
					index: i,
					draggable: false,
					icon: gmap_event_icon,
					window_content: template('event_marker_popup_template', evt)
				});
				gmap.addOverlay(emarkers[evt.id]);
				
				GEvent.addListener(emarkers[evt.id], 'click', function(e) {
					this.openInfoWindowHtml(this.ga.window_content);
				});
			}
		}
		
		/*
		// shops markers
		smarkers = [];
		smarkers_popups = [];
		var markers_amount = shops_markers.length;
    	for(var i=0;i<markers_amount;i++) {
			var shp = shops_markers[i];
			if( shp.lat != 0 && shp.lng != 0 ) {
				shp.infos = Base64.decode(shp.infos);
				shp.address = Base64.decode(shp.address);
				smarkers[shp.id] = new google.maps.Marker({
					position: new google.maps.LatLng(shp.lat, shp.lng), 
					map: gmap,
					draggable: false,
					icon: gmap_shop_icon,
					shadow: gmap_shop_shadow,
					index: i
				});	
				smarkers_popups[shp.id] = new google.maps.InfoWindow({
					content: template('shop_marker_popup_template', shp)
				});
				google.maps.event.addListener(smarkers[shp.id], 'click', function(e) {
					smarkers_popups[this.id].open(gmap, smarkers[this.index]);
				});
			}
		}
		*/
		
		// events
		jQuery('a#switch_users').click(function(e) {
			e.preventDefault();
			if( umarkers[1].isHidden() )
				umarkers.invoke('show');
			else
				umarkers.invoke('hide');
		});
		
		jQuery('a#switch_events').click(function(e) {
			e.preventDefault();
			if( emarkers[1].isHidden() )
				emarkers.invoke('show');
			else
				emarkers.invoke('hide');
		});
		
		jQuery('a#switch_stores').click(function(e) {
			e.preventDefault();
			if( smarkers[1].isHidden() )
				smarkers.invoke('show');
			else
				smarkers.invoke('hide');
		});
		
		jQuery('a.find_on_map').click(function(e) {
			if( umarkers[1].isHidden() )
				umarkers.invoke('show');
			var user_id = jQuery(this).attr('rel');
			gmap.setCenter(umarkers[user_id].getLatLng(), 9);
		});
    }
});