/* 
	Copyright (c) 2005-2008 Metablocks, Inc, Metablocks, Ltd. and their affiliates. 
	This software is under non-exclusive licensed to the party using it. 
	For more information contact: info@metablocks.com
	The software cannot reused without permission. All Rights Reserved.
	
	Do not remove this copyright notice.
	
	v. 2.0.1	
	@requires dragdrop.js, v. 1.3.2
*/

function Module(){
	var module=this;
	module.customData={};
	module.fullSize=[0,0];	// module width [0] and height [1]
	module.minimized=false;
	module.containerEl=document.createElement('DIV');
	module.containerEl.style.display='none';
	module.imagesFolder='http://appscdn.grouptivity.com/socialmail/groups/images/LightBlue';
	var tableHTML='<table cellspacing="0" cellpadding="0" class="modulelayouttbl">';
	tableHTML+='<tr class="modulelayouttbltoprow"><td class="tl">&nbsp;</td><td class="t">&nbsp;</td><td class="tr">&nbsp;</td></tr>';
	tableHTML+='<tr><td class="hl"></td><td class="moduleheader"></td><td class="hr"></td></tr>';
	tableHTML+='<tr><td class="l">&nbsp;</td><td class="m"><div></div></td><td class="r">&nbsp;</td></tr>';
	tableHTML+='<tr class="modulelayouttblbottomrow"><td class="bl">&nbsp;</td><td class="b">&nbsp;</td><td class="br">&nbsp;</td></tr>';
	tableHTML+='</table>';
	module.containerEl.innerHTML=tableHTML;
	module.layoutTable=module.containerEl.getElementsByTagName('TABLE')[0];
	module.contentEl=module.layoutTable.rows[2].cells[1].getElementsByTagName('DIV')[0];
	module.contentEl.className='modulecontent';
	module.bodyEl=module.contentEl.appendChild(document.createElement('DIV'));
	module.bodyEl.className='modulebody';
	module.bodyEl.style.display='block';
	document.body.appendChild(module.containerEl); 
	module.stopBtnEvent=function(evt){  
		if(evt && evt.stopPropagation) {
			evt.stopPropagation();
		} else {
			event.cancelBubble=true;
		}
	};
	module.btnRefreshClick=function(evt){ 
		if(module.editorEl!==null) {
			module.detachEditor();
		}
		module.reloadContent();
	};
	module.btnEditClick=function(evt){ 
		if(module.editorEl!==null) {
			module.detachEditor();
			module.reloadContent();
		} else if(module.onEditorNeeded(module)){
			if(typeof module.editorEl=='object'){
				if(module.minimized) {
					module.setMinimized(false);
				}
				module.attachEditor();
			}
		}
		return false;
	};
	module.btnMinMaxClick=function(evt){ 
		if(module.editorEl!==null) {
			module.detachEditor();
		}
		module.setMinimized(!module.minimized);
	};
	module.setMinimized=function(minimized){
		if(minimized){
			module.minimized=true;
			module.contentEl.style.visibility='hidden';
			module.contentEl.style.margin='0px';
			module.removeClass(module.headerEl.buttons.btnMinMax, 'min');
			module.addClass(module.headerEl.buttons.btnMinMax, 'max');
			module.headerEl.buttons.btnMinMax.setAttribute('title', 'Maximize');
		}else{
			module.minimized=false;
			module.contentEl.style.visibility='visible';
			module.contentEl.style.margin='';
			module.addClass(module.headerEl.buttons.btnMinMax, 'min');
			module.removeClass(module.headerEl.buttons.btnMinMax, 'max');
			module.headerEl.buttons.btnMinMax.setAttribute('title', 'Minimize');
		}
		module.setElementsLayout();
	};
	module.btnCloseClick=function(evt){ 
		if(module.editorEl!==null) {
			module.detachEditor();
		}
		module.parkPlace.parentNode.removeChild(module.parkPlace);
	};
	module.headerEl=module.layoutTable.rows[1].cells[1];
	module.headerEl.className='moduleheader';
	module.headerEl.titleEl=module.headerEl.appendChild(document.createElement('SPAN'));
	module.headerEl.titleEl.className='headertitle';
	module.headerEl.buttons=module.headerEl.appendChild(document.createElement('SPAN'));
	module.headerEl.buttons.className='headerbuttons';

	// Refresh
	// module.headerEl.buttons.btnRefresh=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	// module.headerEl.buttons.btnRefresh.className='btn refr';
	// module.headerEl.buttons.btnRefresh.src=module.imagesFolder+'/btn.gif';
	// module.headerEl.buttons.btnRefresh.setAttribute('title', 'Refresh');
	// module.headerEl.buttons.btnRefresh.onmousedown=module.stopBtnEvent;
	// module.headerEl.buttons.btnRefresh.onclick=module.btnRefreshClick;
	// var delimiterImg=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	// delimiterImg.src=module.imagesFolder+'/delim.gif';
	// delimiterImg.className='btn delim';
	
	module.headerEl.buttons.btnEdit=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	module.headerEl.buttons.btnEdit.className='btn edit';
	module.headerEl.buttons.btnEdit.src=module.imagesFolder+'/btn.gif';
	module.headerEl.buttons.btnEdit.setAttribute('title', 'Edit');
	module.headerEl.buttons.btnEdit.onmousedown=module.stopBtnEvent;
	module.headerEl.buttons.btnEdit.onclick=module.btnEditClick;
	var delimiterImg=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	delimiterImg.src=module.imagesFolder+'/delim.gif';
	delimiterImg.className='btn delim';
	
	module.headerEl.buttons.btnMinMax=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	module.headerEl.buttons.btnMinMax.className='btn min';
	module.headerEl.buttons.btnMinMax.src=module.imagesFolder+'/btn.gif';
	module.headerEl.buttons.btnMinMax.setAttribute('title', 'Minimize');
	module.headerEl.buttons.btnMinMax.onmousedown=module.stopBtnEvent;
	module.headerEl.buttons.btnMinMax.onclick=module.btnMinMaxClick;
	// delimiterImg=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	// delimiterImg.src=module.imagesFolder+'/delim.gif';
	// delimiterImg.className='btn delim';
	
	// module.headerEl.buttons.btnClose=module.headerEl.buttons.appendChild(document.createElement('IMG'));
	// module.headerEl.buttons.btnClose.className='btn close';
	// module.headerEl.buttons.btnClose.src=module.imagesFolder+'/btn.gif';
	// module.headerEl.buttons.btnClose.setAttribute('title', 'Close');
	// module.headerEl.buttons.btnClose.onmousedown=module.stopBtnEvent;
	// module.headerEl.buttons.btnClose.onclick=module.btnCloseClick;
	module.show=function(){
		module.containerEl.style.display='block';	
	};
	module.setSize=function(h, w){ 	
		if(h) {module.fullSize[1]=h;}
		if(w) {module.fullSize[0]=w;}	
		if(h || w){module.setElementsLayout();}
	};
	module.setElementsLayout=function(){
/*		if(module.fullSize[0])
			module.contentEl.style.width=module.fullSize[0]+'px'; 
		else{	
			module.contentEl.style.width='';
			var contBorderWidth=parseInt(module.getStyle(module.contentEl, 'border-left-width'));
			module.fullSize[0]=module.contentEl.offsetWidth-2*(isNaN(contBorderWidth)?0:contBorderWidth); 
		}*/ 
		if (module.layoutTable.offsetWidth < 396) {
			// Table is still drawing...
			module.containerEl.style.width='396px';
		} else {
			module.containerEl.style.width=module.layoutTable.offsetWidth+'px';	
		}

		if (module.containerEl.offsetWidth < 395) {
			module.parkPlace.style.width='395px';
		} else {
			 module.parkPlace.style.width=module.containerEl.offsetWidth+'px';
		}

		if(module.minimized) {
			module.contentEl.style.height='1px';
		} else if(module.fullSize[1]) {
			module.contentEl.style.height=module.fullSize[1]+'px';
		}else{
			module.contentEl.style.height='';
			var contBorderWidth=parseInt(module.getStyle(module.contentEl, 'border-top-width'),10);
			module.fullSize[1]=module.contentEl.offsetHeight-2*(isNaN(contBorderWidth)?0:contBorderWidth); 
		}
//			module.bodyEl.parentNode.style.width=module.bodyEl.offsetWidth)+'px';
//			module.bodyEl.style.height=(module.bodyEl.offsetHeight-8)+'px';
//			module.bodyEl.style.margin='4px';
		module.parkPlace.style.height=module.containerEl.offsetHeight+'px'; 
	};
	module.setTitle=function(title){
		module.headerEl.titleEl.innerHTML=title;
	};
	module.hideEdit=function() {
		module.headerEl.buttons.removeChild(module.headerEl.buttons.btnEdit.nextSibling);
		module.headerEl.buttons.removeChild(module.headerEl.buttons.btnEdit);
	};
	module.startDrag=function(di){ 
		module.containerEl.style.top=module.draggableObject.util.getY(module.containerEl)+'px'; 
		module.containerEl.style.left=module.draggableObject.util.getX(module.containerEl)+'px';
		module.addClass(module.containerEl, 'beingdragged');
		module.draggableObject.setDragIcon(module.containerEl);
		module.draggableObject.data={module:module};
		return true;
	};
	module.endDrag=function(di){ 
		module.removeClass(module.containerEl, 'beingdragged');
		return false;
	};
	module.draggableObject=new Draggable();
	module.draggableObject.dragCursor='move';
	module.draggableObject.setDragHandle(module.headerEl);
	module.draggableObject.onStartDrag=module.startDrag;
	module.draggableObject.onEndDrag=module.endDrag;
	module.parkPlace=document.createElement('DIV');
	document.body.appendChild(module.parkPlace);
	module.parkPlace.style.display='none';
	module.parkPlace.className='moduleparkplace';
	module.parkPlace.module=module;
	module.reloadContent=function(){ 
		module.onContentNeeded(module);
	};
	module.onContentNeeded=function(moduleObj){ 
	};
/*
	onEditorNeeded
	handler should set module.editorEl to null if it does not intend to create
	an editor at all or set it to undefined and return false if it needs some time
	to complete editor creation. In the latter case it must call module.attachEditor()
	to display the editor in the module.
	@returns true/false if editor is ready to be displayed
*/
	module.onEditorNeeded=function(moduleObj){ 
		module.editorEl=null;
		return false;
	};
	module.editorEl=null;
	module.detachEditor=function(){
		module.contentEl.style.height=module.fullSize[1]+'px';
		module.removeClass(module.contentEl, 'modulecontentwitheditor');
		if(typeof module.editorEl=='object'){
			module.contentEl.removeChild(module.editorEl);
		}
		module.bodyEl.style.display='block';
		module.editorEl=null;
		module.setElementsLayout();
	};
	module.attachEditor=function(){
		if(typeof module.editorEl=='object'){
			module.bodyEl.style.display='none';
			module.contentEl.appendChild(module.editorEl); 
			module.addClass(module.contentEl, 'modulecontentwitheditor');
			if((module.editorEl.offsetHeight+10)>module.contentEl.offsetHeight){
				module.contentEl.style.height=(module.editorEl.offsetHeight+10)+'px';
			}
		}
	};
}
Module.prototype.addClass=function(el, className){
	if(el.className!=className){
		if(el.className.indexOf(className+' ')==-1 && el.className.indexOf(' '+className)==-1) {
			el.className+=' '+className;
		}
	}
};
Module.prototype.removeClass=function(el, className){ 
	if(el.className.indexOf(className)!=-1){ 
		if(el.className.indexOf(className+' ')!=-1) {
			el.className=el.className.replace(className+' ', '');
		}else if(el.className.indexOf(' '+className)!=-1) {
			el.className=el.className.replace(' '+className, '');
		}else{
			el.className=el.className.replace(className, '');
		}
	}
};
Module.prototype.getStyle=function(el, attrName){ 
	var res='';
	if(el.currentStyle){
		var attrParts=attrName.split('-');
		attrName=attrParts[0];
		for(var i=1; i<attrParts.length; ++i) {
			attrName+=attrParts[i].charAt(0).toUpperCase()+attrParts[i].substr(1);
		}
		res=el.currentStyle[attrName];
	}else if(window.getComputedStyle) {
		res=window.getComputedStyle(el, null).getPropertyValue(attrName);
	}
	return res;
};
function Module_setSkinFolder(path){
	path=path.replace(/\/$/, '');
	var linkEl=document.getElementsByTagName("head").item(0).appendChild(document.createElement('LINK'));
	linkEl.setAttribute('rel', 'stylesheet');
	linkEl.setAttribute('type', 'text/css');
	linkEl.setAttribute('href', path+'/module-skin.css');
	// var skinStyleSheet=linkEl.sheet||linkEl.styleSheet;
	
/*	Parameter src of DXImageTransform.Microsoft.AlphaImageLoader treats values as relative to the HTML page
	and not the CSS file as normal url() does. So next two lines adds relative location to the src parameter of
	all filters of type DXImageTransform.Microsoft.AlphaImageLoader .
*/
//alert('skinStyleSheet.cssText: '+skinStyleSheet.cssText);
/*	if(skinStyleSheet.rules)
		skinStyleSheet.cssText=skinStyleSheet.cssText.replace(/DXImageTransform\.Microsoft\.AlphaImageLoader\(src=\'/g, 'DXImageTransform.Microsoft.AlphaImageLoader(src=\''+path+'/'); */
}
function ModuleHolder(el){
	var holder=this;
	holder.el=(typeof el =='string')?document.getElementById(el):el;
	holder.addModule=function(module, pos){
		module.show();
		module.setElementsLayout();
		var col=0;
		var ind=0;
		if(pos){ col=pos[0]; ind=pos[1]; }
		if(holder.el.rows[0].cells[col].childNodes.length>0 && holder.el.rows[0].cells[col].childNodes.length>ind) {
			holder.el.rows[0].cells[col].insertBefore(module.parkPlace, holder.el.rows[0].cells[col].childNodes.item(ind));
		} else {
			holder.el.rows[0].cells[col].appendChild(module.parkPlace);
		}
		module.parkPlace.style.display='block';
		module.containerEl.style.position='';
		module.parkPlace.appendChild(module.containerEl);
	};
	holder.dragOver=function(di){
		for(var i=0; i<holder.el.rows[0].cells.length; ++i){
			var col=holder.el.rows[0].cells[i];
			if(di.targetX<(col.offsetLeft+col.offsetWidth)){
				var ind=0;
				for(var j=0; j<col.childNodes.length; ++j){
					var p=col.childNodes.item(j);
					if(p.module && di.targetY<(p.offsetTop+p.offsetHeight)){
						ind=j;
						break;
					}
				}
				var mp=di.data.module.parkPlace;
				mp.style.width=di.data.module.containerEl.offsetWidth+'px';
				mp.style.height=di.data.module.containerEl.offsetHeight+'px';
				if(col.childNodes.length>0 && col.childNodes.length>ind){
					col.insertBefore(mp, col.childNodes.item(ind));
				} else {
					col.appendChild(mp);
				}
				mp.style.display='block';
				break;
			}
		}
	};
	holder.dragDrop=function(){
	};
	holder.dragDropTargetObject=new DragDropTarget(holder.el);
	holder.dragDropTargetObject.onDragOver=holder.dragOver;
	holder.dragDropTargetObject.onDragDrop=holder.dragDrop;
}
