// Custom Popup 

function CustomPopup(map, options){
	
	var pos = (typeof options.pos == 'object') ? options.pos : (function(){ var latlng = options.pos.split(','); return new google.maps.LatLng(latlng[0], latlng[1])})();
	var self = this;
	this.div_height = 300;
	this.type = options.type;
	this.map = map;
	this.loc = pos;
	this.css = "custom-popup";
	this.html = options.html;
	this.div_ = document.createElement("DIV");
	this.div_.className = this.css;
	

	google.maps.event.addDomListener(this.div_, 'mousedown', cancelEvent);  // cancels drag/click 
	google.maps.event.addDomListener(this.div_, 'click', cancelEvent);  // cancels click 
	google.maps.event.addDomListener(this.div_, 'dblclick', cancelEvent);  // cancels double click 
	google.maps.event.addDomListener(this.div_, 'contextmenu', cancelEvent);  // cancels double right click 
	
	/*$(this.div_).click(function(){
		self.hide();
	});*/

	if (this.map) {
		if(this.html) {
			this.setMap(this.map);
			return this;
		} else if(console) {
			console.error('CustomPopup: No html data');
		} else {
			throw('CustomPopup: No html data');
		}
	} else {
		if(console) {
			console.error('CustomPopup: No html data');
		} else {
		throw('No map');
		}
	}
}


CustomPopup.prototype = new google.maps.OverlayView();


CustomPopup.prototype.hide = function(){
	if(this.map.__active_CustomPopup)
		this.map.__active_CustomPopup.setMap(null);
		
}

CustomPopup.prototype.onAdd = function(){
	
	var self = this;
	
	this.div_.style.position = "absolute";
	
	var myLatLng = this.loc;
	var thismap = this.map
	
	this.div_.innerHTML = this.html;
	this.getPanes().floatPane.appendChild(this.div_);
	
	// this is an events listing window
	if (this.type == 'marker') {
	/// parse the links 
		$(this.div_).find('a.eventlink').each(function(){
			activatePopupContent(this,myLatLng,'window','ajax',thismap);
		
		});
	} else if (this.type == 'window') {
		// do tabs
		$(this.div_).find('ul.horiz a').each(function(){
			if ($(this).hasClass('info')) {
				// info tab
				activatePopupContent(this,myLatLng,'window','ajax',thismap);
			} else if ($(this).hasClass('comments')) {
				// comments tag
				activatePopupContent(this,myLatLng,'window','comments',thismap);
			
				
			} else {
				// flickr tab
				activatePopupContent(this,myLatLng,'window','flickr',thismap);
			}
		
		
		});
		
		// do comments form
		$(this.div_).find('form#commentForm').each(function(){
			$(this).find('input').add('textarea').click(function(){
				$(this).focus();
			});
		
			$(this).submit(function(me){
				me.preventDefault();
				
				var values = $(this).serialize();
				values += '&submit=Comment';	
	
				var url = $(self.div_).find('ul.horiz a.info').attr('href');
				url += url+'.comments';
				$.post(url, values,function(data){
					var popup = new CustomPopup(thismap, { pos: myLatLng, html: data, type:'window'  });
				});	
				
				
				
				return false;
			});
		
		});
		
		
		
		
		// do backlink
		$(this.div_).find('a.back-link').each(function(){
			activatePopupContent(this,myLatLng,'marker','ajax',thismap);
		
		});
	
	
	}
	
	
	if(this.map.__active_CustomPopup)
		this.map.__active_CustomPopup.setMap(null);
	this.map.__active_CustomPopup = this;
	
	if (this.type == 'marker') {
		this.position = this.getProjection().fromLatLngToDivPixel(this.loc);
		var new_position =this.map.FIX.getProjection().fromDivPixelToLatLng(new google.maps.Point(this.position.x, (this.position.y + this.div_height) / 2));
	
		var mapcenter = this.map.getCenter();
		var mapbounds = this.map.getBounds();
	
		// make new bounds object for only the top half of the map
		var mapne = mapbounds.getNorthEast();
		var mapsw = mapbounds.getSouthWest();
		
		var newsw = new google.maps.LatLng(mapcenter.b,mapsw.c);
		var tophalf = new google.maps.LatLngBounds(newsw,mapne);
	
		if (tophalf.contains(this.loc)) {
			this.map.panTo(new_position);
		}
	}
	
}

CustomPopup.prototype.draw = function(){
	this.position = this.getProjection().fromLatLngToDivPixel(this.loc);
	this.div_.style.left = this.position.x + 0 + 'px';
	this.div_.style.top = this.position.y - this.div_height + 'px';
}

CustomPopup.prototype.onRemove = function(){
	google.maps.event.clearInstanceListeners(this.div_);
	this.div_.parentNode.removeChild(this.div_);
	this.div_ = null;
}

// fix to get fromDivPixelToLatLng

function _pixelFix(map) { this.setMap(map); }
_pixelFix.prototype = new google.maps.OverlayView();
_pixelFix.prototype.draw = function() { };


function activatePopupContent(which,myLatLng,type,suffix,thismap) {
	var url = $(which).attr('href')+'.'+suffix;
	$(which).attr('href','');
	$(which).click(function(me){
		me.preventDefault();
		$.get(url, function(data){
			var popup = new CustomPopup(thismap, { pos: myLatLng, html: data, type:type  });
		});
		return false;
	});
}

function cancelEvent(e) { 
  e.cancelBubble = true; 
  if (e.stopPropagation) e.stopPropagation(); 
} 
