(function () {
  var CGMapManager = new Class({
	   options: {
	   	   lang : 'en',
	       apikey : '',
	       apiloaded: false,
	       defWidth: 500,
	       defHeight: 400,
	       containerId : 'ib-global-gmap-container'
	   },
	   initialize: function(options) {
	      this.setOptions(options);
	   },
	   setApiKey: function (newkey) {
	   	  this.options.apikey = newkey;
	   },
	   setLang : function (lang) {
	   	this.options.lang = lang;
	   },
	   show: function (config) {
	   	  if (this.options.apiloaded) {
	   	  	 if (!config.width) config.width = this.options.defWidth;
	   	  	 if (!config.height) config.height = this.options.defHeight;
	   	  	 this.createUpdateGMapContainer(config);
	   	  	 TB_show(config.marker.title, '#TB_inline?height='+config.height+'&width='+config.width+'&inlineId='+this.options.containerId, '', (function () {
	   	  	     var map = new GMap2($('TB_ajaxContent')),
	   	  	         marker = new GMarker(new GLatLng(config.marker.lat, config.marker.lng), { title: config.marker.title });
	   	  	     map.disableDoubleClickZoom();
	   	  	     map.enableScrollWheelZoom();
	   	  	     map.addControl(new GMapTypeControl());
	   	  	     map.addControl(new GLargeMapControl3D());
	   	  	     if (config.marker.infoWindow) marker.bindInfoWindowHtml(config.marker.infoWindow, { maxWidth : config.marker.winWidth });
	   	  	     map.addOverlay(marker);	   	  	     
                 map.setCenter(new GLatLng(config.lat, config.lng), config.zoom);
                 if (config.marker.infoWindow) marker.openInfoWindowHtml(config.marker.infoWindow, { maxWidth : config.marker.winWidth });
	   	  	 }).bind(this));
  
	   	  } else {
	   	     this.checkLoadApi(true, this.show, [config]);
	   	  }
	   },
	   createUpdateGMapContainer : function (config) {
	   	  if (!this.gMapContainer) {
	   	  	var bodyEl = $$('body')[0],
	   	  	    containerEl = new Element('div', {
	   	  	       'id' : this.options.containerId/*,
	   	  	       'styles': { 'display' :' none' }*/
	   	  	    });
	   	  	    containerEl.style.display = 'none';
	   	  	    this.gMapContainer = containerEl;
	   	  	    bodyEl.appendChild(containerEl);
	   	  }
	   	  this.gMapContainer.setStyle('width', config.width+'px');
	   	  this.gMapContainer.setStyle('height', config.height+'px');
	   },
	   checkLoadApi : function (load_or_callback, cb, cbParams) {	 
	   	if (load_or_callback) {
	   	  if (this.options.apiloaded) return;	
	   	  this.currentCbParams = cbParams;
	   	  this.currentCb = cb;
	   	  var headEl = $$('head')[0],
	   	      scriptEl = new Element('script', { 
	   	      	'type' : 'text/javascript',	   	      	
	   	      	'src'  : 'http://maps.google.com/maps?file=api&v=2.x&key='+this.options.apikey+'&hl='+this.options.lang+'&async=2&callback=GMapManager.checkLoadApi'
	   	      });
	   	      headEl.appendChild(scriptEl);
	   	  } else {
	   	  	this.options.apiloaded = true;
	   	  	this.currentCb.run(this.currentCbParams, this);
	   	  }
	   }
   });   
   CGMapManager.implement(new Options, new Events);
   GMapManager = new CGMapManager();
})();
