/**
 * 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.
 * 
 * options:
 * {
 * 	page:"default"
 * }
 * 
 */
var PageModule=MGModule.MapItemModule.extend({
	initialize:function(mediaMap, item, options)
	{
	var me=this;
	me.parent(mediaMap,item,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 group=new Group();
		//alert($('page'));
		//alert($('content'));
		$each(me.modules,function(value,key){
			//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
					group.instances.push(mod);
				});
			}
		});
		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(group.instances,function(mod){mod.load(me.viewer,mod.node,me);}); 
		setTimeout(function(){
			if(!success)
			{
				mm_debug("PageLoader timed out!");
				me.fireEvent('onLoad', me);
			}
		},me.options.timeout||5000);
	}
});

/**
 * 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(options);
	this.options.name="CustomPageLoader";
	me.modules=modules;
	}
});