var googleeditor = Class.create();
googleeditor.prototype = {
	initialize: function(div) {		
		this.cookies = new CookieJar({
			 // 10 jours
			 expires:864000
			,path: '/'
		});
		
		if(this.cookies.get('inscription')!=undefined){
			$('email').value=this.cookies.get('inscription').email;
			$('password').value=this.cookies.get('inscription').password;
		}
		
		this.div=div;
		this.enable=false;
	},
	activGooglemap:function(){
		if(this.enable==false){
			this.enable=true;
			this.div=$(this.div);
			this.map = new GMap2(this.div);
			this.geocoder = new GClientGeocoder();
		    this.centerdefault=new GLatLng(46.6795944656402, 2.548828125);
		    this.zoomdefault=5;
		    this.map.setCenter(this.centerdefault, this.zoomdefault);
			this.zoommaxi=16;
			this.zoommini=5;
			this.map.addControl(new GSmallMapControl());
			this.map.enableScrollWheelZoom();
		    this.polygones = [
		    	this.zoomdefault,
		    	new Array()
		    ];
		    
		    this.serialize();
		    GEvent.addListener(this.map, 'zoomend', this.zoomend.bind(this));
		    //GEvent.addListener(this.map, 'moveend', this.moveend.bind(this));
		    GEvent.addListener(this.map, 'click', this.addpoint.bind(this));
	 		Event.observe(window, 'unload', function(){ GUnload();     });
		}
	},
	polygonize:function(){
	},
	moveend:function(){
		console.debug(this.map);
	},
	zoomend:function(){
		if(this.map.getZoom()<this.zoommini) 
			this.map.setZoom(this.zoommini);
		this.serialize();
	},
	addpoint:function(a,b){
        this.polygones[1].push([b.y,b.x]);
		this.polygonize();
	},
	addMarker:function(y,x,offset){
		var marker = new GMarker( new GLatLng(y,x),{draggable:true});
		GEvent.addListener(marker, 'dragend', this.updateMarkerPos.bind(this,offset));
        this.map.addOverlay(marker);
	},
	updateMarkerPos:function(a,b,c){
		this.polygones[1][a]=[b.y,b.x];
		this.polygonize();
	},
    serialize:function(){
	    this.polygones[0] = this.map.getZoom();
	     $('polygones').update(this.polygones.inspect());
	    
    },
    reset:function(){
	    this.map.clearOverlays();
	    this.polygones = [
	    	this.zoomdefault,
	    	new Array()
	    ];
	    this.serialize();
    },
    polygonize:function(){
	    this.map.clearOverlays();
		var _polylines = new Array();
		var allPost = this.polygones[1];
		for(x=0;x<allPost.length;x++){
			_polylines.push(new GLatLng(allPost[x][0],allPost[x][1]));
			this.addMarker(allPost[x][0],allPost[x][1],x);
		}
		this.map.addOverlay(new GPolyline(_polylines, "#0000ff", 5,.5));
		this.serialize();
    },
	search:function (address) {
      if (this.geocoder) {
        this.geocoder.getLatLng(
          address,this.setCenter.bind(this));
      }
    },
    setCenter:function(point){
		this.map.setCenter(point, 15);   
    },
    submit:function(f){
	    if(CheckPost(f)==false){
		    return false;
	 	}
	    if(this.polygones[1].length<2){
		    alert("Veuillez tracer un spot.");
		    return false;
	    }
	    return true;
    },
    inscription_bonplans:function(){
	    this.params = $('AccountForm').serialize(true);
	    var error=false;
	    var msgError="Veuillez corriger les erreurs suivantes :\n";
	    allFieldRequired = ['nom','address','prenom','cp','email','ville','password','captcha','conf_pass'];
	    for(x=0;x<allFieldRequired.length;x++){
		    b = allFieldRequired[x];
		     if(this.params[b]=="" && error==false){
			    alert("Vous devez remplir tous les champs obligatoires");
			    error=true;
			    return false;
		    }
	    }
	    if(checkMail(this.params.email)==false){
		    msgError+="- Email incorrect\n";
			error=true;
	    }
		if(this.params.password!=this.params.conf_pass){
			msgError+="- Votre  confirmation de mot de passe est incorrecte\n";
			error=true;
		}
		if(this.IsNumeric(this.params.cp)==false){
			msgError+="- Le code postal doit etre en chiffre.\n";
			error=true;
		}
		
	    if($('reglement').checked==false){
		    msgError+="- Vous devez accepter le reglement\n";
			error=true;
	    }
	    
	    
		if(error==false){
			new Ajax.Request(
				'includes/php/process.php',
				{
					method: 'post',
					parameters:this.params,
					onSuccess:this.inscription_retour.bind(this)
				}
			);	
		}else{
			alert(msgError);
		}
	},
	inscription_retour:function(e){
		/*
			error :
			-------
	 		0 Refus d'effectuer des operations
			1 Aucune erreur
			2 Email existans
			3 Erreur captcha
			4 Champs obligatoire
		*/	
		
		if(e.responseText==0){
			var _string = "Vous devez remplir tous les champs obligatoires";
		}else if(e.responseText==2){
			var _string = "Un compte existe déjà avec cet e-mail";
		}else if(e.responseText==3){
			var _string = "Code dé sécurité incorrect";
		}else if(e.responseText==4){
			var _string = "Vous devez remplir tous les champs obligatoires";
		}
		
		this.cookies.put('inscription',this.params);
		
		$('inscription_error').update(_string);
		if(e.responseText==1){
			if($('inscript_first'))
				$('inscript_first').remove();
			if($('create_account'))
				$('create_account').remove();
			if($('email'))
				$('email').value=this.params.email;
			if($('password'))
				$('password').value=this.params.password;
			alert("Votre inscription est terminée.");
		}
		
	},
	getmotdepasse:function(){
		new Ajax.Request(
			'includes/php/process.php',
			{
				method: 'post',
				parameters:{getmdp:true,email:$('emailmdp').value},
				onSuccess:function(e){
					if(e.responseText==1){
						alert("Mot de passe envoyé !");
						//SetObjDisplay2("P_Pass", "none");
						$('P_Pass').hide();
					}else{
						alert("Ce compte est inexistant.");
					}
					
				}
			}
		);	
	},
	IsNumeric:function (sText){
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;
		for (i = 0; i < sText.length && IsNumber == true; i++) { 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) {
				IsNumber = false;
			}
		}
		return IsNumber;
	}
	
	
};

