//GMAP
function googleMapOnLoad(fct){
	var fct = fct || function(){};
	if(mapInitialized){
		fct();
	}else{
		google.setOnLoadCallback(fct);
	}
}



//CONSOLE
if(typeof(console)=='undefined'){
	var ConsoleObj = function(){
		this.log = function(log){
			//alert(log);
			return;
		};
	};
	var console = new ConsoleObj();
}


var validateFormDefaultOptions = {
		errorElement: "div",
		errorClass: "formError",
		errorPlacement: function(error, element) {
			$(element).addClass("errorElement");
			$(element).attr('title',error.html());
			if(typeof($(element).data('myTip')) != 'undefined'){
				$(element).data('myTip').remove();
				$(element).removeData('myTip');
			}
			if(error.html() != ""){
				var myTip = $('<div class="tooltip '+this.errorClass+'">'+error.html()+'</div>');
				$(element).data('myTip',myTip);
				//$(document.body).append(myTip);
				$(element).after(myTip);
				var pos = $(element).position();
				var jWidth = $("<span>"+error.html()+"</span>");
				var width = jWidth.appendTo($(document.body)).width();
				jWidth.remove();
				//console.log("top:(pos.top)",(pos.top),$(element).height(),element);
				myTip.css({width: width+'px',position:'absolute',top:(pos.top)+'px',left:(pos.left+$(element).width()-10)+'px'});	
			}
	   },
	   success: function(label) {
		   //this.validClass
		   var element = $("#"+label.attr('htmlfor')).get(0);
		   if(typeof($(element).data('myTip')) != 'undefined'){
				$(element).data('myTip').remove();
				$(element).removeData('myTip');
			}
		   $("#"+label.attr('htmlfor')).removeClass("errorElement");
	   }
};


function handleGeocodeStatusError(geocodeStatus){
	switch(geocodeStatus){
		case gmap.GeocoderStatus.ERROR :
	    	alert("Une erreur est survenue, veuillez relancer l'opération");
	    	break;
		case gmap.GeocoderStatus.INVALID_REQUEST :
			displayUnknownError();
			console.log("This GeocoderRequest was invalid.");
	    	break;
		case gmap.GeocoderStatus.OVER_QUERY_LIMIT :
	    	console.log("The webpage has gone over the requests limit in too short a period of time.");
	    	break;
		case gmap.GeocoderStatus.REQUEST_DENIED :
	    	alert("The webpage is not allowed to use the geocoder.");
	    	break;
		case gmap.GeocoderStatus.UNKNOWN_ERROR :
	    	console.log("A geocoding request could not be processed due to a server error. The request may succeed if you try again.");
	    	displayUnknownError();
	    	break;
	}
}


function displayUnknownError(){
	alert("Une erreur inconnue est survenue, veuillez relancer l'opération");
}





function Dialog(options){
	this.options = $.extend(true,{
		css:{
			width:"300px",
			'background-color':'white'
		},
		title:"dialog",
		autoShow : true,
		cssClass : null,
		destroyOnBlack:true
	},options||{});
	this.blackscreen = null;
	this.dialog = null;
	this.content = null;
	this.init = function(){
		this.blackscreen = displayBlackScreen();
		this.dialog = $("<div class='dialog"+(this.options.cssClass==null?"":(" "+this.options.cssClass))+"'></div>");
		this.dialog.append("<h2>"+this.options.title+"</h2>");
		this.content = $("<div></div>");
		this.dialog.append(this.content);
		this.dialog.attr("style",this.options.style);
		this.dialog.css(this.options.css);
		this.dialog.css({
			position : "fixed",
			top:"50%",
			left:"50%",
			zIndex : this.blackscreen.css("z-index")*1+1
		});
		if(this.options.content){
			this.html(this.options.content);
		}
		
		this.dialog.appendTo(document.body);
		this.dialog.css("margin-left","-"+(this.dialog.outerWidth()/2)+"px");
		this.dialog.css("margin-top","-"+(this.dialog.outerHeight()/2)+"px");
		var $this = this;
		if(this.options.destroyOnBlack){
			this.blackscreen.click(function(){
				$this.destroy();
			});
		}
		if(!this.options.autoShow){
			this.dialog.hide();
			this.blackscreen.hide();
		}
	};
	this.css = function(css){
		this.dialog.css(css);
	};
	this.html = function(html){
		this.content.html(html);
	};
	this.destroy = function(){
		if (this.blackscreen != null) {
			this.blackscreen.remove();
			this.blackscreen = null;
		}
		if(this.dialog != null){
			this.dialog.remove();
			this.dialog = null;
		}
	};
	this.show = function(){
		this.dialog.show();
		this.blackscreen.show();
	};
	this.resize = function(){
		this.dialog.css("margin-left","-"+(this.dialog.outerWidth()/2)+"px");
		this.dialog.css("margin-top","-"+(this.dialog.outerHeight()/2)+"px");
	};
	this.init();
	//this.destroy();
}


















