/**
 * MGViewer this class is an abstract class that should be extended to create real viewers.
 * a viewer grabs a template from the template manager and executes a module on the root 
 * node of the template 
 * when the module has loaded the viewer calls it's display method which should be implemented (differently)
 * for each Viewer.
 * 
 * Additionally, like the display method, a link method can be used to open new content from the same viewer using 
 * it's options. this method will eventually provide forward and backward history navigation 
 * 
 * eg var v=new MGViewer(templateMan, {});
 * v.open(new PageModule, marker, opt)  //marker is optional but recomended for proper module loading and display 
 * eg latlng is grabbed from it for infowindow viewer
 * 
 * opt is also provided as a list of optional arguments, the link method fowards the current opt variable 
 * to be used again when it is used to open content. so opt should contain things like history info etc or latlng for infowindow viewers
 */
var MGViewer=new Class({
	options:
	{
	template:"Default"
	},
	intialize:function(templateManager, options){
		mm_debug("MViewer Initializing");
		var me=this;
		me.templateManager=templateManager;
		me.setOptions(options);
	},
	initState:function(){
		var me=this;
		me.state="closed";

		me.addEvent('onOpen',function(){
			me.state="open";
		});
		me.addEvent('onClose',function(){
			me.state="closed";
		});
	},

	open:function(module, item, opt){// , history, future){
		var me=this;
		
		//shouldn't have opened this! this happens if my viewer is 
		//quicker than an geoXML loaded layer item
		if(item && item.parentGeoXml)return;
		
		if(opt){
			me.opt=opt;
		}else{
			me.opt=null;
		}

		/**
		 * Could easily implement forward and backward history
		 * using two arrays, one for history and one for future
		 */	

		me.item=item;
		me.preload(module);
		module.addEvent('onLoad',function(){
			me.display(module);
			me.fireEvent('onOpen',module);
		});
		
		/* load module (waits 500ms if debug mode is on)
		 * mm_execute runs the first method if Debug mode is on otherwise runs the second.
		 * here it just adds a small delay so that there is time to visually inspect it's 'loading window'
		 */
		var loadFunction=function(page){
			me.eventInstance=new ViewerEventObject(); //this is just a handle for modules to share events per page
			me.templateManager.getTemplate(module.options.template,function(page){
				module.load(me,page);	//so modules can have an instance of the view for links etc.
			});
		};
		//here i add the delay if in debug mode.
		mm_execute(
				/*******Debug Only********/
				function(){ 
					setTimeout(function(){
						loadFunction();
					},1); //this delay is to short to see. increase this if you want to check the loading image.
				},
				/********End Debug*******/
				loadFunction
				);
	},
	preload:function(){
		/*implementors should overwrite this method*/
	},
	display:function(options){
		/*implementors should overwrite this method*/
		this.fireEvent('onLoad');
	},
	/**like open however it uses the old item if one was given*/
	link:function(module){
		var me=this;
		me.open(module, me.item, me.opt);
	},
	close:function(){
		/*implementors should overwrite this method*/
		this.fireEvent('onClose'); //probly not neccesary to fire event
	}
});
MGViewer.implement(new Options(), new Events());

var ViewerEventObject=new Class({});
ViewerEventObject.implement(new Events());
