function Tooltip(marker, data, padding)
{
  this.marker_ = marker;
  this.text_ = data.title;
  this.padding_ = padding;
  this.href = data.link;
  this.type = data.type;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map)
{
    var div = document.createElement("div");
    div.appendChild(document.createTextNode(this.text_));
	div.className = 'gmap-tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

Tooltip.prototype.remove = function()
{
  this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function()
{
  return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force)
{
  if (!force) return;
  var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
  var iconAnchor = this.marker_.getIcon().iconAnchor;
  var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
  var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
  this.div_.style.top = yPos + 'px';
  this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function()
{
  this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function()
{
  this.div_.style.visibility = 'hidden';
}

Tooltip.prototype.click = function()
{
    if (this.type == "panorama")
    {
        initPanoBox(this.href);
    }
    else
    {
        var el = jQuery("#store-" + this.href);
        jQuery("HTML,BODY").animate({ scrollTop: el.offset().top - 50 }, 1000);
	    el.css("background", "#FFCF00");
	    el.animate({ backgroundColor: "#EEEEEE" }, 5000);
    }
}

function initPanoBox(id)
{
	var pano_id = parseInt(id, 10);
    var image = "" + pano_id;
    
    while (image.length < 6)
    {
    	image = "0" + image;
    }
    
    var viewer = new PTGuiViewer();
    viewer.setSwfUrl("/swf/PTGuiViewer.swf");
    
    viewer.preferHtmlViewer();
    viewer.setVars({
    	pano: "/img/dynamic/panorama/" + image + "-",
        format: "14faces",
		pan: 140,
		minpan: -180,
		maxpan: 180,
		tilt:50,
		mintilt: -90,
		maxtilt: 90,
		fov: 100,
		minfov: 80,
		maxfov: 140,
		autorotatespeed: 3,
		autorotatedelay: 10,
		maxiosdimension: 567,
		showfullscreenbutton_flash: 1,
		showfullscreenbutton_html: 1,
		enablegyroscope: 1

    });
    
    var body = jQuery("BODY");
    var overlay = jQuery("<div>").attr("id", "pano-overlay").html("&nbsp;");
    var box = jQuery("<div>").attr("id", "pano-overlay-box");
    var close_button = jQuery("<img />").attr("id", "pano-close-button").attr("src", "/img/button-close.png");
    
    close_button.bind("mouseover", function()
    {
		jQuery(this).css("cursor", "pointer");
    });
    
    close_button.bind("mouseout", function()
    {
    	jQuery(this).css("cursor", "normal");
    });
    
    close_button.bind("click", function()
    {
    	jQuery("#pano-overlay").remove();
    	jQuery("#pano-overlay-box").remove();
    	jQuery(this).remove();
    });
    
    overlay.bind("click", function()
    {
    	jQuery(this).remove();
    	jQuery("#pano-overlay-box").remove();
    	jQuery("#pano-close-button").remove();
    });
    
    body.prepend(overlay);
	body.prepend(box);
	body.prepend(close_button);
	
	var margin_left = jQuery(window).width() / 2 - box.width() / 2;
	var margin_top = jQuery(window).height() / 2 - box.height() / 2;
	box.css("left", margin_left + "px");
	box.css("top", margin_top + "px");
	
	close_button.css("left", margin_left + box.outerWidth() - 32);
	close_button.css("top", box.outerHeight() + margin_top - 32);
	
	body.append(close_button);
	
	var frame = jQuery("<div>").attr("id", "pano-overlay-box-frame");
	
	box.append(frame);
	
	var info = jQuery("<div>").attr("id", "pano-overlay-box-data").load("/pano.php?pano=" + pano_id);
	
	box.append(info);
    
    viewer.embed("pano-overlay-box-frame");
}

