
/////////////////////////////////////////////
//VARIABLES
/////////////////////////////////////////////

var core_stream_objects = Array();

var core_module_objects = Array();

var placeholder=null;

var drag_start_y = 0;
var drag_direction = "";

//alert('JS Module Class');

var current_module = 0;
var current_argument_position = 0;
var current_stream = "mainstream";


var screen_deactivator = new Element('div',{'style':'display:none;text-align:center;z-index:1001;background-color:#000000;left:0px;top:0px;height:100%;width:100%;position:absolute;filter:alpha(opacity=0.5);-moz-opacity:0.5;-khtml-opacity:0.5;opacity:0.5;'});
window.addEvent('domready', function()
{
	screen_deactivator.set('html','<h1>Bitte warten Sie</h1>');
	
	screen_deactivator.setStyle('height',
		Math.max(
			Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
			Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
			Math.max(document.body.clientHeight, document.documentElement.clientHeight)
		)
	+'px');
	
	screen_deactivator.inject(document.body);
	
});


function set_current_module(module,argument_position)
{
	//alert(module);
	current_module = module;
	current_argument_position = argument_position;
}

function set_stream(stream)
{
	//alert(stream);
	current_stream = stream;
}

/////////////////////////////////////////////
//CLASS
/////////////////////////////////////////////
function CoreModule(init)
{

	var that = this;

	if(init)
	{
		//push me to array
		core_module_objects.push(this);
		
		//alert('module_'+init["module_id"]);
				
		this.element = $('module_'+init["module_id"]);
		this.module_stream = init["module_stream"];
		this.server_url = init["server_url"];
		this.module_url = init["module_url"];
		this.module_method_name = init["module_method_name"];
		this.module_class_name = init["module_class_name"];
		this.module_instance_name = init["module_instance_name"];
		this.module_id = init["module_id"];
		//this.module_options_path = init["module_options_path"];
		
		if(!this.element)
		{
			alert("Element "+'module_'+init["module_id"]+" not found!");
		}
		
		//init placeholder once
		if(placeholder==null)
		{
			placeholder = new Element('div',{'style':'position:relative;','html':'<div style="background-image:url('+this.server_url+'/core_extension/core_module/border.png);height:20px;position:absolute;top:-10px;width:100%;"></div>'});
		}
		
		
		
		//debug helper
		//this.element.setStyle('border','1px solid black');
		
		//create edit bar
		this.bar = new Element('div',{'style':'z-index:1000;color:#000000;right:0px;position:absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity:0;opacity:0;'});
		this.bar.inject(this.element,'top');
		
		//create delete button
		this.button_delete = new Element('div',{'title':"Modul von dieser Position entfernen",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_module/delete.png);cursor:pointer;float:left;'});
		this.button_delete.injectInside(this.bar);
		
		//create move button
		this.button_move = new Element('div',{'title':"Modul verschieben",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_module/move.png);cursor:move;float:left;'});
		this.button_move.injectInside(this.bar);
		
		//create rights button
		this.button_rights = new Element('div',{'title':"Rechte vergeben",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_module/paragraph.png);cursor:pointer;float:left;'});
		this.button_rights.injectInside(this.bar);
		
		if(this.module_class_name!="")
		{
			//create add button
			this.button_add = new Element('div',{'title':"Zusatzmodule",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_module/add.png);cursor:pointer;float:left;'});
			this.button_add.injectInside(this.bar);
			
			//click event
			this.button_add.addEvent( 'click' , function(){
				if(!tab)
				{
					//alert(that.module_stream);
					set_stream(that.module_stream);
					var tab = that.window.newContent({'title':'Moduloptionen','content':'iframe','url':that.server_url+"/"+'?admin_modules&js_callback_function=parent.path_add_module&module_instance_name='+that.module_instance_name,'id':'options'});
				}
				tab.open();
			});
			
		}
		
		if(this.module_options_path!="")
		{
			//create options button
			this.button_options = new Element('div',{'title':"Moduloptionen",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_module/options.png);cursor:pointer;float:left;'});
			this.button_options.injectInside(this.bar);
			
			//click event
			this.button_options.addEvent( 'click' , function(){
				if(!tab)
				{
					var tab = that.window.newContent({'title':'Moduleinstellungen','content':'iframe','url':that.server_url+"?edit_module="+that.module_id,'id':'options'});
				}
				tab.open();
			});
		}
		
		//informations
		//this.button_move = new Element('div',{'title':"Module verschieben",'style':'height:24px;width:24px;background-image:url('+this.server_url+'/core_extension/core_path/move.png);cursor:move;float:left;'});
		//this.button_move.injectInside(this.bar);
		
		//click event
		this.button_delete.addEvent( 'click' , function(){
			if(confirm('Soll das Modul von dieser Position entfernt werden?'))
			{
				screen_deactivator.setStyle('display','block');
				window.location.href=that.module_url+'?delete_module='+that.module_id;
			}
		});
		
		//click event
		this.button_rights.addEvent( 'click' , function(){
			if(!tab)
			{
				var tab = that.window.newContent({'title':'Modulrechte','content':'iframe','url':that.server_url+"?edit_rbac_right&right_keys=module_run,module_edit,module_delete&right_object="+that.module_id,'id':'rights'});
			}
			tab.open();
		});
		
		//mouseover
		this.element.addEvent( 'mouseover' , function(){
			that.bar.fade('in');
		});
		
		//mouseout
		this.element.addEvent( 'mouseout' , function(){
			that.bar.fade('out');
		});
		
		
		
		this.restore_relative = function()
		{
			//Drag.Move will override the position to absolute
			//because of that restore it
			that.element.setStyle('position','relative');
			that.element.setStyle('left','0px');
			that.element.setStyle('top','0px');
		};
			
		//drag
		this.dragger = new Drag.Move(that.element, {
			snap: 0,
			handle: that.button_move,
			onStart: function()
			{
				
				drag_start_y = parseInt(that.element.getStyle('top'));
				
			},
			onComplete: function()
			{
				
			},
			onDrag: function(element,droppable)
			{

			
				//get direction
				if(drag_start_y>parseInt(element.getStyle('top')))
				{
					drag_direction = "up";
				}
				else
				{
					drag_direction = "down";
				}
				
				
				drag_start_y = parseInt(element.getStyle('top'));
			
			
				window.status = drag_start_y;
				
			},
			onEnter : function(element, droppable)
			{
			
				
				//if(droppable)//if moved over a container
				//{
					//if(droppable.dropable_type=='module')
					//{
					//	droppable.highlight();
					//}
					
					//if(droppable.dropable_type=='stream')
					//{
					//	droppable.highlight("#ff0000");
					//}

				//}

				if(droppable)//if moved over a container
				{
					if(droppable.dropable_type=='module')
					{
						
						
						
						//placeholder.setStyle('height',element.getStyle('height'));
						placeholder.setStyle('width',element.getStyle('width'));
						

						if(drag_direction=='down')
						{
							//if(element.getNext()!=droppable)
							//{
								placeholder.inject(droppable, 'after');
							//}
						}
						else
						{
							//if(element.getPrevious()!=droppable)
							//{
								placeholder.inject(droppable, 'before');
							//}
							
							
						}
					
						
					}
				}
				
				
			},
			onLeave : function(element, droppable)
			{
				
				placeholder.dispose();
				
			},
			onDrop: function(element, droppable)
			{
				if(droppable)//if dropped over a container
				{
					if(droppable.dropable_type=='module')
					{
						
											
						if(drag_direction=='down')
						{
							that.element.inject(droppable, 'after');
							placeholder.dispose();
							document.location.href=that.module_url+"?module_drag_id="+element.get('id')+"&module_drop_id="+droppable.get('id')+"&drop_mode=after";
							//alert(that.url+"module_drag="+element.get('id')+"&module_drop="+droppable.get('id')+"&drop_mode=after"),
						}
						else
						{
							that.element.inject(droppable, 'before');
							
							document.location.href=that.module_url+"?module_drag_id="+element.get('id')+"&module_drop_id="+droppable.get('id')+"&drop_mode=before";
							//alert(that.url+"module_drag="+element.get('id')+"&module_drop="+droppable.get('id')+"&drop_mode=before");
						}
						
					}
					
					if(droppable.dropable_type=='stream')
					{
						document.location.href=that.module_url+"?module_drag_id="+element.get('id')+"&module_stream="+droppable.get('id');
						//alert(that.url+"module_drag_id="+element.get('id')+"&module_stream="+droppable.get('id'));
						that.element.inject(droppable,'top');
					}

				}
				
				that.restore_relative();
				placeholder.dispose();
			}
		})
		
		//set up dropable modules
		var dropable_modules = $$('.core_module');
		for (var i = 0; i < dropable_modules.length; i++)//for each element
		{
			if(dropable_modules[i]!=this.element)//dont add yourself...
			{
				dropable_modules[i].dropable_type = 'module';//es handelt sich bei dem element um ein modul
				this.dragger.droppables.push(dropable_modules[i]);//add this to my own list
			}
		}
		
	}
	else
	{
		alert("Cannot initiate module! Need initiation data!");
	}

	this.restore_relative();
	
	this.window = new CoreWindow({
		'css':'default','title':'<b>'+this.module_method_name+'</b>','height':500,'width':800,
		'maxheight':600,'maxwidth':1000,'cookie':'".$this->window_name."','saveSize':true,'statusbar':false,'center_h':true,'center_v':true
	});
	
	this.addRessource = function(ressource)
	{
		//create edit button
		var button_edit = new Element('div',{'title':"Ressource bearbeiten",'style':'height:24px;width:24px;background-image:url('+that.server_url+'/core_extension/core_module/edit.png);cursor:pointer;float:left;'});
		button_edit.injectInside(that.bar);
		
		//open on click
		button_edit.addEvent( 'click' , function()
		{
			//create window tab
			if(!tab)
			{
				var tab = that.window.newContent({'title':ressource['title'],'content':'iframe','url':ressource['url'],'id':ressource['id']});
			}
			tab.open();
		});
	}
	
	
}



/////////////////////////////////////////////
//GET TEMPLATE POSITIONS
/////////////////////////////////////////////
/*function CoreGetModules()
{

	//get all modules
	var Modules = $$('.core_module');
	
	//prepare all modules
	for (var i = 0; i < Positions.length; i++)
	{
		//alert(Positions[i].get('class'));
		
		var elem_count = 0;
		
		//get all elements from each position
		var Elements = $$('#'+Positions[i].get('id')+' .core_element');
		for (var ii = 0; ii < Elements.length; ii++)
		{
			var options = new Array();
			options["element"] = Elements[ii];
			options["position"] = Positions[i];
			var elem = new CoreTemplateElement(options);
			elem_count++;
		}
		
		//alert (Positions[i].get('max'));
		
		if(Positions[i].get('max')>elem_count||elem_count==0||Positions[i].get('max')==null)
		{
			//create add button
			var button_add = new Element('div',{'title':"Artikel hier einfügen ("+Positions[i].get('id')+")",'onclick':"openCoreArticleWindow();CoreArticleWindow.setUrl('core_article.php?js_select_function=top.SelectArticleCallback');SelectedPosition='"+Positions[i].get('id')+"';PageId='"+page_id+"';",'style':'clear:left;z-index:1000;display:block;height:24px;width:24px;background-image:url(core_image/add.png);cursor:pointer;'});
			button_add.injectInside(Positions[i]);
		}
	}

}*/

/////////////////////////////////////////////
//DOMREADY
/////////////////////////////////////////////
/*document.addEvent( 'domready' , function()
{
	
	//Browserweische
	if(checkBrowserName('MSIE'))
	{
		if(getIEVersionNumber()>7)
		{
			CoreCreateModules();
		}
		else
		{
			Alert("Information: Ihr Webbrowser ist veraltet oder nicht kompatibel! Die Schnellbearbeitung wurde deaktiviert.");
		}
	}
	else
	{
		CoreGetModules();
	}

	
});*/
