/**
 * opens content in google maps infowindow. has a loading screen while content is 
 * loading. it has will load content in the center of the screen if no item is specified on 
 * call to open. 
 * 
 * eg var ifwV new MGViewer.InfoWindowViewer(mediaMap, template, {});
 * ifwV.open(new PageLoader(), marker);
 * 
 * 
 */
var InfoWindowViewer=MGViewer.extend({
	initialize:function(mediaMap, templateManager, options)
	{
	//mm_debug("InfoWindowViewer Initializing");
	var me=this;
	//this.parent(templateManager,{});
	this.setOptions($merge({
		loading:{
		src:"/administrator/components/com_mediamapserver/Viewer/images/loading.gif",
		width:"208px",
		height:"13px"
	},
	infoWindowOptions:
	{
		maxWidth:450//,
		//noCloseOnClick:true,
	},
	latlng:null,	//if latlng is not specified then getLatLng will be used
	getLatLng:function()
	{
		return MapFactory.GetGMap(me.mediaMap).getCenter();
	}
	},options));
	me.mediaMap=mediaMap;
	me.templateManager=templateManager;
	//me.setOptions(options);

	/*set up loading gif.*/
	me.loadingGif=new Asset.image(me.options.loading.src, {id: 'loadingImage', title: 'loading', styles:{width:me.options.loading.width,height:me.options.loading.height}});
	me.loadingGif.setStyle('marginTop',"25px");
	me.loadingGif.setStyle('marginRight',"20px");

	me.initState();
	me.addEvent('onOpen',function(){me.fireEvent('onOpened');});

	/**
	 * special method
	 */
	google.maps.event.addListener(me.mediaMap.mainMap, "infowindowbeforeclose" , function(){
		//alert("hello");
		if(me.state=="open"){
			me.fireEvent("beforeClose");
			me.eventInstance.fireEvent('onClose');
		}

	});
	mm_debug(["Viewer - InfoWindowViewer. Loaded",{me:me}]);
	},
	getDisplayObject:function(){
		var me=this;
		var streets=me.mediaMap.mainMap.getStreetView();
		if(streets.getVisible())
			return streets;
		return me.mediaMap.mainMap;
		
		
	},
	display:function(module){
		//alert('display');
		var me=this;
		if(!me.infoWindow){
			mm_debug("makeinfowindow");
			me.infoWindow=new google.maps.InfoWindow();	
			mm_debug("makeinfowindow - done");
			MapFactory.SetInfoWindowClickEvents(me.infoWindow);
		}else{
			
		}
		//me.infoWindow.close();
		me.infoWindow.setOptions(me.options.infoWindowOptions);
		me.infoWindow.setContent(module.getContent());
		mm_debug("open infowindow");
		//me.infoWindow.open(me.mediaMap.mainMap,(me.item||me.options.latLng));
		mm_debug("open infowindow - done");
	},
	preload:function(module){
		var me=this;
		if(!me.infoWindow){
			me.infoWindow=new google.maps.InfoWindow();	
			MapFactory.SetInfoWindowClickEvents(me.infoWindow, me);	
		}else{
			//me.infoWindow.close();
			if(me.state=="open"){
				me.fireEvent('onClose');
			}
			
		}
		if(me.opt){
			me.options.latlng=MapFactory.GetItemWindowLocation(me.opt);
			mm_debug('InfoWindowViewer: Centering on last item');
		}
		else{
			
			me.options.latlng=MapFactory.GetItemWindowLocation(me.item)||me.options.getLatLng();
			me.options.infoWindowOptions.pixelOffset=MapFactory.GetItemWindowOffset(me.item);
			mm_debug('InfoWindowViewer: Centering on Item');
			mm_debug(me.options.latlng);
		}
		var map=MapFactory.GetGMap(me.mediaMap);
		//map.closeInfoWindow();//this should also discard last template (very important!)
		var options=$merge({},me.options.infoWindowOptions);
		options.onCloseFn=function(){me.fireEvent('onPreload');};
		me.infoWindow.setOptions(options);
		me.infoWindow.setContent(me.loadingGif);
		me.infoWindow.open(me.getDisplayObject(), ((me.item?MapFactory.ResolveItem(me.item):false)||me.options.latLng));

	},
	initializePolygon:function(polygon){
		var me=this;
		google.maps.event.addListener(polygon,'click',function(latlng){
			me.mediaMap.infoWindowViewer.open(new PageModule(MapFactory.GetMM(polygon),polygon,{}),polygon, latlng);
		});
		google.maps.event.addListener(polygon,'lineupdated',function(){
			mm_debug("taged with edit");
			if(polygon.wrapper.change==null||polygon.wrapper.change==""){
				polygon.wrapper.change="edit";
				MapFactory.GetLayer(polygon).addItemChange(polygon.wrapper);
			}
			i=((me.item?me.item.wrapper:false)||me.item);
			if(polygon.wrapper==i){
				me.close();
			}
		});

	},
	close:function(){
		var me=this;
		if(me.state=="open"&&me.infoWindow)
		{

				me.infoWindow.close();	
				me.fireEvent('onClose');
		}
	},
	/**
	 * zoom might attach this to right click or something, shows an exploded view of map in infoWindow
	 */
	zoom:function(latlng){
		//TODO: detect current maximum resolution and use that instead of static '16'
		var me=this;
		var opts={
				zoomLevel:16,
				mapType:G_SATELLITE_MAP 
		};
		//alert(Json.toString(opts));
		//alert(Json.toString(latlng));
		MapFactory.GetGMap(me.mediaMap).showMapBlowup(latlng, opts);
	}
});

