
/**
 * The rip shockwave module checks the DOM Node (id passed as second argument to constructor) 
 * if it finds any embedded application/x-shockwave-flash objects it extracts them and replaces them with a link to 
 * open the content in a new Viewer. the three modules used for this are (this module)-> "RipShockwaveModule", as well and the 
 * ShockwaveModule and ShockwaveLinkModule the later being injected in the place of the actual embed content and 
 * linking to the ShockwaveModule which is very simple.
 */
var RipShockwaveModule=MapItemModule.extend({
	process:function(){ 
	var me=this;
	var contentId=me.item;
	var content=me.pageLoader.node.getElementById(contentId);
	var removeList=[];
	var container=[];
	//private method rip performs DOM removal **TODO: IE7 and some other browsers ignore this removal
	var rip=function(tag){
		$each(content.getElementsByTagName(tag),function(value){
			var type=value.getAttribute("type");
			if(type=="application/x-shockwave-flash"){
				removeList.push(value);
			}
		});
		var embedContent=null;
		while(removeList.length>0){
			var top=removeList.pop();//removes elements from list

			var url=top.getAttribute("src");
			var data=top.getAttribute("data");

			if(top.parentNode.nodeName=='object')
				top=top.parentNode;
			var shockwaveLink=new Element('a');
			//top.parentNode.insertBefore(shockwaveLink, top);
			if(top.parentNode==me.node)
				shockwaveLink.inject(top.parentNode,'before');
			else{
				if(embedContent==null){
					embedContent=new Element('div',{id:'embedLinks'});
					embedContent.injectInside(me.node);
				}
				shockwaveLink.injectInside(embedContent);
			}
			top.parentNode.removeChild(top);
			container.push(new ShockwaveLinkModule(me.mediaMap,{link:shockwaveLink, embed:top, 'url':url, 'data':data},{}));
		}
	};
	rip("embed");
	rip("object");
	if(container.length>0)
	{
		var group=new Group();
		$each(container,function(mod){
			group.instances.push(mod);
		});
		var allDone=function(){				
			me.fireEvent('onLoad', me);
		};	
		group.addEvent('onLoad',allDone);
		$each(group.instances,function(mod){mod.load(me.viewer, me.node, me.pageLoader);}); 
	}
	else
		me.fireEvent('onLoad');
}
});



var ShockwaveModule=MapItemModule.extend({
	process:function(){
	var me=this;
	$(me.node).appendChild($(me.item.embed));
	me.fireEvent('onLoad');
}
});

var ShockwaveLinkModule=MapItemModule.extend({
	process:function()
	{
	var me=this;
	var shockwave=me.item;
	var linkModule=me.createShockwaveLoader();
	shockwave.link.addEvent('click',function(event){
		me.viewer.link(new CustomPageModule(me.mediaMap, me.item, {'geoliveWindow_middle': [linkModule]},{}));
		var e=new Event(event); //Makes sure that this event is now a mootools event
		e.stop();	/*a link will try to open a new page otherwise*/
	});
	me.createShockwaveImage(function(){me.fireEvent('onLoad');});
	},
	createShockwaveImage:function(callback){
		var me=this;
		var shockwave=me.item;
		var img=new Asset.image(me.mediaMap.options.dir+'images/media.png',{onload:function(){
			//me.shockwave.link.injectInside(img);
			img.injectInside(shockwave.link);
			img.setStyle('height','50px');
			callback();
		}});

	},
	createShockwaveLoader:function(){
		var me=this;
		return new ShockwaveModule(me.mediaMap, me.item, {});
	}
});

