YAHOO.namespace("example.container");

var ModuleManager = {
 		
		destroy:function(elID,account){
			var Dom = YAHOO.util.Dom;

			var moduleContainer = Dom.get(elID);
		
			if ( account != "" ){
				
				var delUrl = "/moduleSettings/deleteModule/" + moduleContainer.id;
				var objConn = new ObjectConnection('dummy',delUrl,'',0);
				objConn.requestUrl();
				
			}
			
			Dom.setStyle(moduleContainer, "display", "none");		
			
			ModuleProperties.getZoneHeight( targetZones );
			
			var zoneId = this.getTarget(moduleContainer,'SPAN');
			this.removeModule(moduleContainer.id,zoneId);
	        
		},
		
		showAlertMessage:function(elID,moduleTitle,account){
			ModuleManager.initDeleteAlert(moduleTitle,elID,account)
			YAHOO.example.container.simpledialog1.show();
		},
		
		initDeleteAlert:function(modTitle,elID,account) {
	
			var handleYes = function() {
				ModuleManager.destroy(elID,account);
				this.hide();
			};
			
			var handleNo = function() {
				this.hide();
			};
		
			YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog   
			("simpledialog1", 
			{ width: "400px",
			 height: "100px",
			fixedcenter: true,
			visible: false,
			draggable: false,
			modal : true,
			text: "You sure you want to delete '" + modTitle + "' ?",
			icon: YAHOO.widget.SimpleDialog.ICON_HELP,
			constraintoviewport: true,
			buttons: [ { text:"Yes", handler:handleYes },
				       { text:"No",  handler:handleNo } ]
			} );
			
			YAHOO.example.container.simpledialog1.setHeader("Confirmation");
			YAHOO.example.container.simpledialog1.render("deleteAlert");	
		},

		showDuplicate:function(){
			ModuleManager.initDuplicateAlert()
			YAHOO.example.container.simpledialog1.show();
		},
		
		initDuplicateAlert:function() {
	
			var handleYes = function() {
				this.hide();
			};
			
			YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog   
			("simpledialog1", 
			{ width: "450px",
			  height: "100px",
			fixedcenter: true,
			visible: false,
			draggable: false,
			modal : true,
			text: "Module is already loaded! Please select another one.",
			icon: YAHOO.widget.SimpleDialog.ICON_HELP,
			constraintoviewport: true,
			buttons: [ { text:"Close", handler:handleYes } ]
			} );
			
			YAHOO.example.container.simpledialog1.setHeader("Duplicate Modules!");
			YAHOO.example.container.simpledialog1.render("deleteAlert");	
		},
		
		moduleStyle:function(el,color,animation){
			Dom.get(el).innerHTML = "<img src='img/" + animation + "'>";
			Dom.setStyle(el, "border", "none");
			Dom.setStyle(el, "background-color", color);
			Dom.setStyle(el, "width", "98%");
			Dom.setStyle(el, "height", "auto");
			Dom.setStyle(el, "padding", "3px");
		},

		getTarget:function(ref,tag){
			ok=0; 

			while (!ok){
				ref = ref.parentNode;
				if (ref.nodeType==1) //check that the node is a tag, not text (type=3)
				{
					if (String(ref.nodeName)==tag){
						return ref.id;
					}
					
				}
			}
		},
	
		removeModule:function(e,id){
			var d = document.getElementById(id); 
			var d_nested = document.getElementById(e); 
			var throwawayNode = d.removeChild(d_nested);
		},
		
		listModules:function(arrZones)
		{
		   for (i=0; i < arrZones.length; i++) {
				
				var objZone = document.getElementById(arrZones[i]);
				
				for (var j=0;j<objZone.childNodes.length;j++){
					
					if( objZone.childNodes[j].id != null ){	
						alert("Zone:" + arrZones[i] + " : " + objZone.childNodes[j].id);
					}
				}
			}
		},
		
		moduleOrder:function(DDtarget)
		{
			var objZone = document.getElementById(DDtarget);
			var strMod;
			
			strMod ="";
			
			for (var j=0;j<objZone.childNodes.length;j++){
				
				if( objZone.childNodes[j].id != null ){	
					
					if (j+1 == objZone.childNodes.length){
						strMod += objZone.childNodes[j].id ;
					}else{
						strMod += objZone.childNodes[j].id + "|";
					}
					
				}
			}
			
			return strMod;
			
		},
		
		checkModules:function(arrZones,newDiv)
		{
		   for (i=0; i < arrZones.length; i++) {
				
				var objZone = document.getElementById(arrZones[i]);
				
				for (var j=0;j<objZone.childNodes.length;j++){
					
					if( objZone.childNodes[j].id != null ){
						
						if( objZone.childNodes[j].id == newDiv ){	
							
							return true;
					
						}
					}
				}
			}
		}
	
	};

	//This class sets the heights
	
	var ModuleProperties = {
	
		getMaxHeight : function( arrHeight ){
			return Math.max.apply( Math, arrHeight );
   		},

		setZoneHeight : function( maxheight ){
			
			for ( var i=0; i < this.arrZones.length; i++) {
        		Dom.get( this.arrZones[i] ).style.height = maxheight + 100 + "px";
   	 		}
   	 				
		},

		//This gets the zone height with all the modules within that zone 
		getZoneHeight : function( arrH ){
			
			this.arrZones = arrH;
			
			var i;
			var j;
			var modH = 0; //module height
			var totalH = 0; //module height
			var arrHeight = new Array();//an array of all the heights
			var maxH = 0;
			
			for (i=0; i < this.arrZones.length; i++) {
				
				var objZone = document.getElementById(this.arrZones[i]);
				
				for (var j=0;j<objZone.childNodes.length;j++){
					
					if( objZone.childNodes[j].id != null ){
						modH = Dom.get(objZone.childNodes[j].id).offsetHeight;
						totalH = totalH + modH;
						
					}
				}
				
				arrHeight.push(totalH);
				totalH = 0;		
			}
		
			maxH = this.getMaxHeight(arrHeight);
			
			this.setZoneHeight(maxH);		
		}
		
	};