
/* Custom Maker type GMarker - Project CAM
 * Sources : http://econym.org.uk/gmap/example_canvas.htm - http://econym.org.uk/gmap/elabel.js
 * @author : Frédéric GINIOUX - Turbulent Média
*/

function GCAMOverlay(html, options){
	/* Options */
	this.defaultOptions = {latitude: 45.30, longitude: 73.35, className: null, width: 0, height: 0, opacity: 100};
	this.options = $.extend({}, this.defaultOptions, options);
	/* Attributes */
	this.GLatLng = new GLatLng(this.options.latitude, this.options.longitude);
	this.html = html || '<div>&nbsp;</div>';
	this.sizeOffSet = new GSize(this.options.width, this.options.height);
	this.hidden = false;
};

GCAMOverlay.prototype = new GOverlay();

$.extend(GCAMOverlay.prototype, {
	initialize: function(GMapInstance){
		var _div = this.setContents(this.html, document.createElement('div'));
  	GMapInstance.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(_div);
		this._map = GMapInstance;
  	this._div = _div;
		
		if(this.options.callbacks){
			for(var event in this.options.callbacks){
				var fn = this.options.callbacks[event];
				$(this._div).bind(event, fn.bind(this));
			}
		}
		
		this.setOpacity(this.options.opacity);
		if(this.hidden) this.hide();
	},
	
	remove: function(){
		this._div.parentNode.removeChild(this._div);
	},
	
	copy: function(){
		return new GCAMOverlay(this.html, this.options);
	},
	
	redraw: function(force){
		var p = this._map.fromLatLngToDivPixel(this.GLatLng);
  	var h = parseInt(this._div.clientHeight);
  	this._div.style.left = (p.x + this.sizeOffSet.width) + 'px';
  	this._div.style.top = (p.y +this.sizeOffSet.height - h) + 'px';
	},
	
	show: function(){
		if(!this.hidden) return;
		try{
			$(this._div).css({display: ''});
			this.redraw();
			this.hidden = false;
	  } catch(e){}
	},
      
	hide: function(){
		if(this.hidden) return;
		try{ 
			$(this._div).css({display: 'none'});
			this.hidden = true;
		} catch(e){}
  },
	
	isHidden: function() {
		return (this.hidden)? true : false;
	},
	
	setContents: function(html) {
		this.html = html;
		var html = '<div%classname%>' + this.html + '</div>';
		var replacement = (this.options.className)? ' class="' + this.options.className + '"' : '';	
		if(arguments[1]){
			var _div = arguments[1];
			_div.style.position = 'absolute';
			_div.innerHTML = html.replace('%classname%', replacement);
			return _div;
		}
		else{
			this._div.innerHTML = html.replace('%classname%', replacement);
			this.redraw(true);
		}  	
	}, 
	
	setPoint: function(point) {
		this.GLatLng = point;
		this.redraw(true);
	}, 
	
	setOpacity: function(opacity) {
		this.options.opacity = (opacity >= 0 && opacity <= 100)? opacity : 100;
		var opacity = 1;
		if(this.options.opacity < 100){
			var opacity = this.options.opacity / 100;			
		}
		$(this._div).css({opacity: opacity});
	}, 
	
	getPoint: function() {
		return this.GlatLng;
	}	
});
