// container for a control plus element, after gmaps
var ControlElement=Class.create();
ControlElement.prototype={
    initialize:function(c,e){
	this.control=c;
	this.element=$(e);
    }
}

// DummyMap is for use when you do not want a map, but you want slider
// bars, popup windows, etc anyway
var DummyMap =Class.create();
DummyMap.prototype = {
    initialize: function(element,zoomLevels,name) {
	this.element   = $(element);
	this.elementName=name;
	this.container=this.element;
	this.ownerDocument=this.element.ownerDocument||document;
	var a=this.ownerDocument.createElement("div");
	a.style.position="absolute";
	var c=new coord(0,0);
	c.apply(a);
	a.style.zIndex=0;
	this.div=a;
	this.element.appendChild(this.div);

	// variables needed to spoof slider creation
	this.mapTypes=[];
	this.spec={numZoomLevels:zoomLevels};
	this.mapTypes.push({numZoomLevels:this.spec.numZoomLevels});
	this.zoomLevel=4;
	this.controls=[];
	this.viewSize=Element.getBox(this.element);
	this.centerScreen=new coord(0,5,0.5);
    },

    //stick the marker in the element
    addOverlay: function(marker){
	this.element.appendChild(marker.element);
    },

    // functions needed for slider
    zoomTo: function(zoom){
	//	if(_debug){_debug.log("zooming to "+zoom);}
	var b;
	if(zoom<0){
	    zoom=-1;
	}else if(zoom>=this.spec.numZoomLevels){
	    zoom=this.spec.numZoomLevels;
	};
	
	if(zoom != this.zoomLevel){
	    b=this.zoomLevel;
	    this.zoomLevel=zoom;
	}
	//if(_debug){_debug.log("triggering "+GEvent.getPropertyName(this.elementName+'Zoom')+" with "+ b +", "+ this.zoomLevel +", "+this.elementName);}
	GEvent.trigger(this,this.elementName+"Zoom",b,this.zoomLevel,this.elementName)
    },
    hideOverlays:function(){
	if(_debug){_debug.log("call to hideOverlays");}
    },
    addControl:function(a){
	var d=a.initialize(this);
	var e=a.getDefaultPosition();
	e.apply(d);
	this.container.appendChild(d);
	this.controls.push(new ControlElement(a,d));
    },
    removeControl:function(a){
	for(var i=0;i<this.controls.length;i++){
	    var c=this.controls[i];
	    if(c.control==a){
		this.container.removeChild(c.element);
		this.controls.splice(i,1);
		return;
	    }
	}
    }
};


// set up a small class for icons and the images thereof
function myIcon(){}
myIcon.prototype={
    imagePath:"",
    imageHash:{},
    markerList:{},
    unloadHash:function(){
	try{
	    for (element in this.markerList) {
		if(element.length){
		    for(var i=0;i<element.length;i++){
			element[i].remove();
		    }
		}else{
		    element.remove();
		}
	    }
	    for (element in this.imageHash) {
		Sarissa.clearChildNodes(element);
	    }
	}catch(EX){}
	this.imageHash={};
	this.markerList={};
    },
    buildBaseIcon:function(options){
	var pinIcon=new GIcon();
	pinIcon.image=this.imagePath+options.image;
	pinIcon.shadow=this.imagePath+options.shadow;
	pinIcon.iconSize=options.iconSize;
	pinIcon.shadowSize=options.shadowSize;
	pinIcon.iconAnchor=options.iconAnchor;
	pinIcon.infoWindowAnchor=options.infoWindowAnchor;
	pinIcon.infoShadowAnchor=options.infoShadowAnchor;
	pinIcon.transparent=this.imagePath+options.transparent;
	pinIcon.printImage=options.printImage;
	pinIcon.printShadow=this.imagePath+options.printShadow;
	pinIcon.setImage(this.imagePath+options.image);
	pinIcon.imageMap=options.imageMap;
	return new GIcon(pinIcon);
    },
    iconDefaults:{image:"black.png",
		  iconSize:     new GSize(11,14),
		  shadowSize:   new GSize(22,14),
		  iconAnchor:   new GPoint(5,14),
		  infoWindowAnchor:new coord(5,2),
		  infoShadowAnchor:new coord(9,14),
		  shadow:"pinshadow2.png",
		  printShadow:"pinshadow2.gif",
		  printImage:"blackie.gif",
		  mozPrintImage:"blackff.gif",
		  transparent:"transppin2.png",
		  imageMap:[ 3,0, 7,0, 10,3, 10,7, 7,10, 6,10, 5,13, 4,10, 3,10, 0,7, 0,3, 3,0]
    },
    initialize:function(options){
	this.mapObject=options.mapObject;
	// use this for creating new icons
	var defaults=Object.extend(this.iconDefaults,options);
	this.baseIcon=this.buildBaseIcon(defaults);
	this.harvestMarker(this.baseIcon,defaults.image);
	/* prevent memory leaks */
	Event.observe(window, 'unload', this.unloadHash.bind(this),false );
    },
    harvestMarker:function(icon,imgSrc){
	// got bugs.  need to make image map have unique names.  for
	// now, get rid of them
	var tempMarker=new GMarker(new GPoint(0,0),icon);
	// I am not calling addoverlay, so I must do the same steps manually...
	// this.mapObject.addOverlay(tempMarker);
	if(!this.markerList[imgSrc]){
	    this.markerList[imgSrc]=tempMarker.copy();
	}

	tempMarker.initialize(this.mapObject);
	tempMarker.display(true);
	// if(_debug){_debug.log("marker is "+tempMarker.images+" "+tempMarker.images[0].src );}

// 	// is it my marker?
//  	var anotherMarker=new GMarker(new GPoint(-117.7,33.6),icon);
//  	this.mapObject.addOverlay(anotherMarker);
//  	if(_debug){
// 	    _debug.log("anothermarker is: ");
// 	    for(var i=0;i<anotherMarker.images.length;i++){
// 		_debug.log(anotherMarker.images[i]+" = "+Sarissa.serialize(anotherMarker.images[i]));
// 		_debug.log(anotherMarker.images[i]+".style = "+(anotherMarker.images[i]).style);
// 	    }
// 	}
// no.  that marker is also squished.  perhaps the icon is not fully specified?

	// make image set once, copy for later use
	var ownerDocument=this.mapObject.ownerDocument;
	
	var e=ownerDocument.createElement("div");
	var images=tempMarker.images;
	var imageMap=tempMarker.imageMap;
	var icon=tempMarker.iconImage;
	var shadow=tempMarker.shadow;
	var transparent=tempMarker.transparentIcon;
	
	// tempMarker.remove();
	//this.mapObject.removeOverlay(tempMarker);

	var zeroC=new coord(0,0);
	for(var i=0;i<images.length;i++){
	    e.appendChild(images[i]);
	    e.lastChild.style.zIndex=30;
	    //zeroC.apply(e.lastChild);
	}
	icon.style.zIndex=40;
	shadow.style.zIndex=20;
	shadow.style.cursor="auto";
	if(transparent){
	    Element.remove(transparent);
	    //.style.zIndex=60;
	}
	// image map is still an issue, have to be able to translate image map to new point
	//if(imageMap){
	//    e.appendChild(imageMap);
 	//}

	this.imageHash[imgSrc]=e;
	if(_debug){_debug.log("e is "+e+" serialized as "+Sarissa.serialize(e));}
    },
    newImageFace:function(imgSrc){
	if(!this.imageHash[imgSrc]){
	    var pinIcon=new GIcon(this.baseIcon);
	    pinIcon.setImage(this.imagePath+imgSrc);
	    this.harvestMarker(pinIcon,imgSrc);
	}
	return this.imageHash[imgSrc];
    },
    newImage_Complete:function(options){
	var defaults=Object.extend(this.iconDefaults,options);
	if(!this.imageHash[defaults.image]){
	    var pinIcon=this.buildBaseIcon(defaults);
	    this.harvestMarker(pinIcon,defaults.image);
	}
	return this.imageHash[defaults.image];
    },
    newImage_Marker:function(options){
	var defaults=Object.extend(this.iconDefaults,options);
	if(!this.markerList[defaults.image]){
	    var pinIcon=this.buildBaseIcon(defaults);
	    this.harvestMarker(pinIcon,defaults.image);
	}
	return this.markerList[defaults.image].copy();
    }
};
