/**
 * MSJS Name space
 */

MSJS = function () {};
MSJS.dd = function () {};

// This array holds the content "group" boxes
MSJS.dd.groups  = new Array();
// Groups count integer
MSJS.dd.groupCount = 1;
// This array holds the column numbers for the current template
MSJS.dd.columns = new Array();
// This array holds the objects representing the current content boxes
MSJS.dd.boxes = new Array();
// This array holds objects representing the blobs (text/image) pieces on the page
MSJS.dd.blobs = new Array();

/**
 * Creates a dragable group
 *
 * This function adds a group content-box to the drag-able items array
 *
 * @param string grpID  HTML element ID for the content-box
 * @return void
 */
MSJS.dd.addGroup = function (grpID, group) {
	group = ((group)?group:1);
	var grp = new MSJS.dd.drg(grpID, group);
	grp.addInvalidHandleClass('toolsPanel');
	grp.isTarget = false;
	MSJS.dd.groups.push(grp);
	this.groupCount++;
}

/**
 * Creats drop points (columns)
 *
 * This function adds a column to the drop-able (columns) items array
 *
 * @param string grpID  HTML element ID for the column
 * @return void
 */
MSJS.dd.addColumn = function (columnID, group) {
	group = ((group)?group:1);
	var drp = new MSJS.dd.drp(columnID, group);
	MSJS.dd.columns.push(columnID);
}


/**
 * Initialize insertion points
 *
 * This function loops through div items looking for divs with
 * the MSJS.dd.IPClassName class name and attaches insertion points
 * events to them.
 * 
 * @return void 
 */
MSJS.dd.initIP = function () {
	var insertionPoints = document.getElementsByTagName("div");
    for (i=0; i<insertionPoints.length; i++) {
	    if (insertionPoints[i].className == 'enter') {
        	insertionPoints[i].style.padding = '10px';

	        YAHOO.util.Event.addListener(insertionPoints[i],
	        'mouseover', function (){this.style.backgroundColor = 'E8E8E8';});

	        YAHOO.util.Event.addListener(insertionPoints[i],
    	    'mouseout', function (){this.style.backgroundColor = '';});

	        YAHOO.util.Event.addListener(insertionPoints[i],
	        'click', function (){popInsertion(this);});
	        insertionPoints[i].className = 'enter2';
	        MSJS.dd.addColumn(insertionPoints[i].id, 1);
	        insertionPoints[i].title = 'Click here to add more content';
        } else if (insertionPoints[i].className == 'enterblob') {
	        insertionPoints[i].className = 'enterblob2';
	        MSJS.dd.addColumn(insertionPoints[i].id, 2);
        } else if (insertionPoints[i].className == 'enterc') {
	        insertionPoints[i].className = 'enterc2';
	        MSJS.dd.addColumn(insertionPoints[i].id, 2);
        }
	}
}


MSJS.dd.updateBox = function (o) {
	if (o.responseText.length < 1) {
		alert('Could not fetch any data for the content box');
		return;
	}
	var rs = eval(o.responseText);

	var box = rs[0];
	ul = document.createElement('ul');
	ul.id = 'grpUl' + box;
	box.innerHTML = '';		
	box = document.getElementById(box);
	box.innerHTML = '';
	box.appendChild(ul);

	ul = document.getElementById(ul.id);
	for (i=1; i<rs.length; i++) {
		li = document.createElement('li');
		li.id = 'grpUI' + box.id;
		ul.appendChild(li);
		li = document.getElementById(li.id);
		li.innerHTML = '<a href="">'+rs[i].title+'</a>';
	}
}

/**
 * Place holder for insertion point
 */
var insertPosition;
var customInsertPosition;

MSJS.dd.drg = function (id, Group) {
	this.doinit(id, Group);
}

MSJS.dd.drg.prototype = new YAHOO.util.DD();
MSJS.dd.drg.parent = '';
MSJS.dd.drg.prototype.doinit = function (id, Group) {
	if (!id) {
		return;
	}
	// Intersect
    YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
	this.init(id, Group);
    if (this.getEl()) {
	    this.parent = this.getEl().parentNode;
	}
}

MSJS.dd.drg.shadow = '';

MSJS.dd.drg.prototype.startDrag = function (x, y) {
	var element = this.getEl();
	this.shadow = createShadow(element);
	w = element.clientWidth;
	h = element.clientHeight;
	if (w && h) {
		element.style.width = w + 'px';
		element.style.height = h + 'px';
	}
	
	element.style.position = 'absolute';
	element.style.filter = "alpha(opacity=50)";
	element.style.opacity = .5;
	// Hide dashboard
	var undefined;
	var dashboardHeight;
	if (dashboardHeight == undefined) dashboardHeight = false;
	if (dashboardHeight && dashboardHeight.element.offsetHeight > 0) {
		dashboardHeight.toggle();
	}
	hideTiny();
}

function createShadow(element) {
	var group = element.id;
	if (document.getElementById(group + 'shadow')) {
		return group + 'shadow';
	}
	div = document.createElement('div');
	div.id = group + 'shadow';
	//div.style.width  = (element.clientWidth - 1) + 'px';
	div.style.height = ((element.clientHeight)?((element.clientHeight - 1) + 'px'):'150px');
	div.style.border = '3px dashed #C9C9C9';
	insertPosition = element;
	insertBox(div);
	return div.id;
}

MSJS.dd.drg.prototype.lastElement = '';
MSJS.dd.drg.prototype.lastIP = '';
MSJS.dd.drg.prototype.onDragOver = function (e,id) {
	id = YAHOO.util.DDM.getBestMatch(id).getEl().id;
	var enter = document.getElementById(id);

	var reg = new RegExp(".*enterc.*");

	if(this.id == enter.parentNode.id) {
		return;
	}
	
	if (this.lastIP && this.lastIP.id == id) {
		return;
	}

	this.lastIP = enter;

	if (reg.exec(enter.id)) {
		insertPosition = enter;
	} else {
		insertPosition = enter.parentNode;
	}

	insertBox(document.getElementById(this.shadow));
}

MSJS.dd.drg.prototype.endDrag = function(e) {
	var element = this.getEl();

	element.style.opacity = 1;
	element.style.filter = "alpha(opacity=100)";
	element.style.width  = '';

	element.style.left = '';
	element.style.top = '';
	element.style.height = '';
	element.style.width = '';
	element.style.position = '';
	document.getElementById(this.shadow).parentNode.replaceChild(element, document.getElementById(this.shadow));
	if (element.id.indexOf('customblob') > -1) {
		if (element.parentNode.id.indexOf('contentBlockgrp') > -1) {
			YAHOO.util.Event.addListener(element.id,
	        'mouseover', function (e, element){(element.className == 'addHandled')?element.className = 'moveHandle':'';}, element);
	        YAHOO.util.Event.addListener(element.id,
	        'mouseout', function (e, element){(element.className == 'moveHandle')?element.className = 'addHandled':'';}, element);
	        element.className = 'addHandled';
		} else {
	        element.className = 'addHandle';
		}
	}
}

MSJS.dd.drp = function (id, Group) {
	this.init(id, Group);
}

MSJS.dd.newGroupID = function () {
	return this.groupCount;
}

MSJS.dd.drp.prototype = new YAHOO.util.DDTarget();
MSJS.dd.drp.prototype.Elements = new Array();

function initEditors() {
	YAHOO.util.Event.addListener(document, 'click', function (e) {hideTiny(e);});
}

function hideTiny(e) {
	if (e && e.target) {
		var parent = e.target;
	} else {
		if (e && e.srcElement) {
			var parent = e.srcElement;		
		} else {
			var parent = document.body;
		}
	}

	var found = false;
	if(parent && parent.className && (parent.className == 'richEditor' || parent.className == 'simpleEditor')) {
			found = parent.id;
	} else {
		while(parent) {
			if (parent.parentNode && parent.parentNode.className == 'mceEditorContainer') {
				return; // Ignore clicks inside editor's controls
			}
			if(parent.parentNode && (parent.parentNode.className == 'richEditor' || parent.parentNode.className == 'simpleEditor')) {
				found = parent.parentNode.id;
				break;
			}
			parent = parent.parentNode;
		}
	}
	var exists = false;
	if ((!e || !e.target) || !e.target.className
			   || (e.target.className && e.target.className != 'richEditor') 
			   || (e.target.className && e.target.className != 'simpleEditor')
	    ) {
		for (n in tinyMCE.instances) {
			if (tinyMCE.isInstance(tinyMCE.instances[n])) {
				if (found != tinyMCE.instances[n].oldTargetElement.id) {
					var inst = tinyMCE.instances[n];
					tinyMCE.triggerSave();
					document.getElementById(inst.oldTargetElement.id+'Contents').value =
						document.getElementById(inst.oldTargetElement.id).value;
					var nodeToRemove = document.getElementById(inst.oldTargetElement.id);
					nodeToRemove.parentNode.removeChild(nodeToRemove);
					tinyMCE.execCommand('mceRemoveControl', false, tinyMCE.instances[n].editorId);
				} else {
					exists = true;
				}
			}
		}
		if (!exists && found) {
			enableTiny(document.getElementById(found));
		}
	}
}
