function GmapPopup() {
	this.globals = {};
}

function initGmapPopup() {
	GmapPopup.prototype = new GControl();
	var this1 = null;
	
	GmapPopup.prototype.initialize = function(map) {
		this1 = this;
		this1.pause = true;
		this1.map_  = map;
		
		this1.globals = {
		   fadeIn_speed 	: 500,
		   fadeOut_speed 	: 500,
		   popupHidden 		: true // to avoid a popup is shown after mouseout event
		   //popupsrc 			: null 	// To save popup-image to be displayed. We use an Image not an string because image adds %20 for space 
		};

		var div_ = document.createElement("div");
	
		div_.setAttribute('id','gmappopup');
		//$(div_).addClass("barStyle");
		
 		div_.style.display = "none";
 		div_.style.position = "absolute";
    //div_.style.width = "200px";
    //div_.style.height = "20px";
	  //div_.style.backgroundColor = "#738699";
	  //div_.style.padding = "3px";
    //div_.style.border   = "1px solid #FFFFFF";

		/*
	  var overlay_ = document.createElement("div");
	  $(overlay_).addClass("roundoverlay");
	  div_.appendChild(overlay_);
		*/

			var img1Container_ = document.createElement("div");
	 		img1Container_.style.position = "relative";
	 		img1Container_.style.display 	= "none";
			if (ie6 || is_chrome || $.browser.opera) {
				$(img1Container_).addClass("roundedPopupIE6");
			} else {
			  var overlay_ = document.createElement("div");
			  $(overlay_).addClass("roundoverlay");
			  img1Container_.appendChild(overlay_);
			}
			  var text1_ = document.createElement("p");
 		 		text1_.setAttribute('id',"title");
		 		text1_.style.position = "relative";
			  $(text1_).css("opacity",1);
			  img1Container_.appendChild(text1_);
	
			  var anchor_ = document.createElement("a");
 		 		anchor_.setAttribute('title',"Press to go original image");
		 		anchor_.style.position = "relative";
 		 		anchor_.setAttribute('href',"#");
	
					var img1_ = document.createElement("img");
			 		img1_.style.position = "relative";
			 		img1_.setAttribute('alt',"");
			 		img1_.setAttribute('title',"");
				  $(img1_).css("opacity",1);  // because ie7 needs this
				  //img1_.style.border   = "1px solid #FFFFFF";
				  //$(img1_).css({"z-index":999999});
				  anchor_.appendChild(img1_);

			  img1Container_.appendChild(anchor_);

			  var anchor_ = document.createElement("a");
 		 		anchor_.setAttribute('title',"Press to go to owners page");
 		 		anchor_.setAttribute('href',"#");
 		 		//anchor_.setAttribute('linkurl',"");

			  var text1b_ = document.createElement("p");
 		 		text1b_.setAttribute('id',"owner");
		 		text1b_.style.position = "relative";
			  $(text1b_).css("opacity",1);
			  anchor_.appendChild(text1b_);

			  img1Container_.appendChild(anchor_);

		  div_.appendChild(img1Container_);
			this.img1Container_ = img1Container_;

			var img2Container_ = document.createElement("div");
	 		img2Container_.style.position = "absolute";
	 		img2Container_.style.display 	= "none";
			if (ie6 || is_chrome || $.browser.opera) {
				$(img2Container_).addClass("roundedPopupIE6");
			} else {
			  var overlay_ = document.createElement("div");
			  $(overlay_).addClass("roundoverlay");
			  img2Container_.appendChild(overlay_);
			}
			  var text2_ = document.createElement("p");
		 		text2_.setAttribute('id',"title");
		 		text2_.style.position = "relative";
			  $(text2_).css("opacity",1);
			  img2Container_.appendChild(text2_);

			  var anchor_ = document.createElement("a");
 		 		anchor_.setAttribute('title',"Press to go original image");
 		 		anchor_.setAttribute('href',"#");
			  $(anchor_).css("opacity",1);  // because ie7 needs this
		 		anchor_.style.position = "relative";

					var img2_ = document.createElement("img");
				  //img2_.style.border   = "1px solid #FFFFFF";
			 		img2_.setAttribute('alt',"");
			 		img2_.setAttribute('title',"");
			 		img2_.style.top 		= "0px";
			 		img2_.style.left 		= "0px";
				  //$(img2_).css("opacity",1);  // because ie7 needs this
			 		//img2_.style.position = "relative";
				  //$(img2_).css("opacity",1)
			  	anchor_.appendChild(img2_);
			  	
		  	img2Container_.appendChild(anchor_);

			  var anchor_ = document.createElement("a");
 		 		anchor_.setAttribute('title',"Press to go to owners page");
 		 		anchor_.setAttribute('href',"#");
 		 		//anchor_.setAttribute('linkurl',"");

				  var text2b_ = document.createElement("p");
	 		 		text2b_.setAttribute('id',"owner");
			 		text2b_.style.position = "relative";
				  $(text2b_).css("opacity",1);

				  anchor_.appendChild(text2b_);

			  img2Container_.appendChild(anchor_);


		  div_.appendChild(img2Container_);
			this.img2Container_ = img2Container_;
	
		map.getContainer().appendChild(div_);

		//map.getPane(G_MAP_FLOAT_PANE).appendChild(div_);
		
		this.div_ = div_;
		
		this1.crossFade = new CrossFade(this.div_,this.img1Container_,this.img2Container_,500);
	
		$("a",this1.div_).click(function() {
			logit("clicked:"+$("p",this).attr('linkurl'));
			//window.location = $("p",this).attr('linkurl');
			window.open($(this).attr('linkurl'),"_blank");
			return false;
		});
		
		if (ie6 || is_chrome || $.browser.opera) {
			$(".roundedPopupIE6").css("background-color","#738699");
			if ($.browser.opera) {
				$(".roundedPopupIE6").css("border","1px outset #000000");
			} else {
				DD_roundies.addRule('.roundedPopupIE6','10px',true);
			}
		} else {
			DD_roundies.addRule('.roundoverlay','10px',true);
		}	
		return div_;
	}
	// Remove the main DIV from the map pane
	GmapPopup.prototype.remove = function() {
	  this1.div_.parentNode.removeChild(this1.div_);
	}
	// Redraw the block based on the current projection and zoom level
	GmapPopup.prototype.redraw = function(force) {
		logit("GmapPopup redraw:"+force);
	  // We only need to redraw if the coordinate system has changed
	  if (!force) return;
	  
	  if (this1.globals.popupHidden) {
	  	logit("No redraw because not displayed");
	  	return;
	  } // do not draw at init & when not displayed 
	  //if (this1.globals.popupsrc == null) return;
	
	  // Calculate the DIV coordinates of the centre of our block
	  var p = this1.map_.fromLatLngToDivPixel(this1.point_);

		var map_div = this1.map_.getContainer(); //this1.map_.getPane(G_MAP_MAP_PANE);

		var coords = calcPosition(map_div,this1.div_,this1.point_); // calcPosition in commonFunctions.js
		//	var top = p.x + coords.xoffset;
		//var left = p.y + coords.yoffset;
		var top = p.y ;
		var left = p.x;

		var top = coords.yoffset;
		var left = coords.xoffset;

		logit("Redraw Popup at top/left="+top+"/"+left);

	  this1.div_.style.left = left + "px";
	  this1.div_.style.top = top + "px";
	}
	
	GmapPopup.prototype.newImg = function(imgA,img_title) {
		this1.img_title = img_title;

		if (imgA !== null && typeof(imgA) !== 'undefined') {
			logit("GmapPopup newImg:"+imgA.src);
	 		this1.div_.style.width 	= "300px";  // for new popup resize image to match
	 		this1.div_.style.height = "300px";
	 	} else {
	 		this1.div_.style.width 	= "220px";  // for new popup resize image to match
	 		this1.div_.style.height = "20px";
	 	}
		this1.crossFade.newImg(imgA,this1.img_title);

		this1.crossFade.show();
		
	}
	GmapPopup.prototype.nextImg = function(imgA,img_title,CB) {
		logit("GmapPopup newImg:"+imgA.src);
		this1.crossFade.newImg(imgA,this1.img_title);
		this1.crossFade.fade(CB);
	}

	//
	// show : does first hide and then show
	//
	GmapPopup.prototype.show = function(mapPoint,CB) {
		logit("GmapPopup show");
		$(this.div_).hide(function() {
			this1.showUp(mapPoint,CB);
		});
	}
	//
	// showUp : show without hide
	//
	GmapPopup.prototype.showUp = function(mapPoint,CB) {
		logit("GmapPopup showUp");
		this1.point_ = mapPoint;

		this1.globals.popupHidden = false;  // force draw at next redraw

		this1.redraw(true);
		
		if (!CB) CB = dummy;
		
		if (this1.globals.fadeIn_speed == 0) {
			logit("just show popup");
			$(this1.div_).show().css('opacity',1);
			CB();
		} else {
			logit("slowly fadein Popup");
			//$(this1.div_).show().fadeTo(this1.globals.fadeIn_speed,1);
			$(this1.div_).fadeIn(this1.globals.fadeIn_speed,CB); //this1.globals.fadeIn_speed);
			//$(this1.div_).show();
		}
		function dummy() {
			logit("dummy callback function");
		}
	}
	/*========================================================================================
	  
	   Function : hidePopup
	  
	 ========================================================================================*/
	GmapPopup.prototype.hide = function(CB) {
		logit("GmapPopup hide");
		//this1.globals.popupsrc = null;  // prevent loading image to show in popup
		if (this1.globals.popupHidden) {
			if (CB)	CB();
			return;
		}
		this1.globals.popupHidden = true;
		/*
		if ($(this1.div_).is(':animated')) {
			$(this1.div_).stop();
		} 
		*/
		if (this1.globals.fadeOut_speed == 0) {
			logit("just hide popup");
			$(this1.div_).hide();
			if (CB) CB();
		} else {
			logit("slowly fadeout");
			//$(this1.div_).fadeTo(this1.globals.fadeOut_speed,0,function() {$(this).hide();});
			$(this1.div_).fadeOut(this1.globals.fadeOut_speed,CB); //this1.globals.fadeOut_speed);
		}
	}
	function logit(text) {
		if (debug) logitAll("GmapPopup-"+text);
	}
}
