/**
 * PageModule Loads modules for a Geolive Item.. it gets a list of modules to load from the PageFactory and loads 
 * each module from that list. the modules are given as an object with keys matching the DOM id for the child 
 * element within the Node passed to this module. if the id is not found then the module is not loaded. 
 * additionally this module has 5 second timeout in which it fires it's onLoad event regardless of whether all the 
 * modules have loaded.
 * 
 * the reason for using a page module loader, is similar to the way content managements systems do the same thing, 
 * in the administration control panel, administrators can decide what content to display in an infowindow, and in 
 * which location. this makes it very customizable, additionally, page modules, can be created and added to info windows
 * in an abstract mannor. the flexibility of this is really powerfull there are many interesting ways to display data 
 * within a infowindow, but projects differ greatly. modules allow a lot of expandability.
 * 
 * options:
 * {
 * 	page:"default"
 * }
 * 
 */
var PageModule=MapItemModule.extend({
	initialize:function(mediaMap, item, options)
	{
	var me=this;
	me.parent(mediaMap,item,$merge({template:"geolive"},options));
	this.options.name="FactoryPageLoader";
	me.getModules();
	},
	getModules:function(){
		var me=this;
		me.modules=PageFactory.getPageModules(me.mediaMap, me.item, me.options.page||"default");
	},
	process:function(){
		var me=this;
		
		var groupItems=[];
		//alert($('page'));
		//alert($('content'));
		var number=0;
		if(!me.modules||me.modules.length==0){
			mm_debug("Zero Modules!");
		}
		$each(me.modules,function(value,key){
			number++;
			//alert('processing page'+key+value);
			if(me.node.id==key)
				domNode=me.node;
			else
				var domNode=$(me.node.getElementById(key));
			if(domNode)	{
				$each(me.modules[key],function(mod,key){
					mod.node=domNode; //attach its node for later
					groupItems.push(mod);
				});
			}else{
				mm_debug("Skipped Module @:"+key);
				
			}
		});
		var group=new Groupable();
		group.instances=groupItems;
		if(number==0){
			mm_debug("No Modules!");
			me.fireEvent('onLoad', me);
			return;
			
		}
		var success=false;
		var allDone=function(){
			//alert('all done');
			me.fireEvent('onLoad', me);
			success=true;

			$each(me.modules,function(value,key){
				if(me.node.id==key)
					domNode=me.node;
				else
					var domNode=$(me.node.getElementById(key));
				if(domNode)	{
					domNode.id=null;  //strip id's
				}

			});

		};	
		group.addEvent('onLoad',allDone);
		//$each(group.instances,function(mod){ mod.addEvent('onLoad',function(){alert('module loaded');});}); 
		$each(groupItems,function(mod){
			//mm_debug({mod:mod});
			mod.load(me.viewer,mod.node,me);}); 
		//mm_debug({group:group});
		setTimeout(function(){
			if(!success)
			{
				//mm_debug("PageLoader timed out!");
				me.fireEvent('onLoad', me);
			}
		},me.options.timeout||5000); //5 second timeout. can be overriden.
	}
});

/**
 * Extends PageModule, but instead of retrieving the Modules from the PageFactory, it uses a list of
 * Modules passed as the third argument to the consructor.
 */
var CustomPageModule=PageModule.extend({
	initialize:function(mediaMap, item, modules, options)
	{
	var me=this;
	me.mediaMap=mediaMap;
	me.item=item;
	me.setOptions($merge({template:"geolive"},options
	));
	this.options.name="CustomPageLoader";
	me.modules=modules;
	}
});
