// Content Editor: Template Block
var editBlockClassName = '-editblock';
var editBlockUrl = '';
function initContentBlocks()
{
	if (document.getElementById && document.createTextNode)
	{
		var divs=document.getElementsByTagName('div');
		for (var i=0;i<divs.length;i++)
		{
			
			
			if(divs[i].className.indexOf('content-block') > -1)
			{
				divs[i].onmouseover 	= function() { showEdit(this); }
				divs[i].onmouseout 		= function() { hideEdit(this); }
				divs[i].onclick 		= function() { makeEdit(this); }
}
		}
		//turn off all other anchors and inputs in the page
		//this is a pretty large security consideration
		//whereby you want to prevent someone who is editing a page they 
		//have access to from jumping to another page in the navigation 
		//and then editing it.
		disableAnchorAndInputTags();
	}
}

function showEdit(_e)
{		
	_e.className = _e.className.replace(/(html|text|form)/, "$1" + editBlockClassName);
	return false;
}

function hideEdit(_e)
{
	_e.className = _e.className.replace(editBlockClassName, '');
	return false;
}

function makeEdit(_e)
{
	var regNum = /\d+/;
	var regContentObject = /html|form|text/i;

	if(regNum.test(_e.className) && regContentObject.test(_e.className))
	{
		var column = regNum.exec(_e.className);
		var type = regContentObject.exec(_e.className);
		var url = editBlockUrl + '&column=' + column + '&type=' + type;
		var input = _e.getElementsByTagName('input');
		//if the contentobject contains a definition, then a hidden input field will exist 
		//and the value it contains is the contentobjectid that should then be passed through
		//the url
		for(var i = 0; i < input.length; i++)
		{
			if(input[i].getAttribute("name") == "contentobjectid")
			{
				url += '&coid=' + input[i].value;
				break;
			}
		}
		//same thing here for form content objects
		for(var i = 0; i < input.length; i++)
		{
			if(input[i].getAttribute("name") == "formid")
			{
				url += '&fid=' + input[i].value;
				break;
			}
		}		
		
		
		popUpWindow(url,'addmode1','640','500');
	}
	return false;	
}

function disableAnchorAndInputTags()
{
	var tags = new Array('a', 'input', 'select', 'textarea');
	for(var j = 0; j < tags.length; j++)
	{
		var nodes = document.getElementsByTagName(tags[j]);
		for(var i= 0; i < nodes.length; i++)
		{
			if(tags[j] == 'a')
			{
				nodes[i].onclick = function() { return false; }
			}
			else
			{
				nodes[i].setAttribute('disabled', 'disabled');
			}
			
		}
	}
}


function popUpWindow(url,winname,width,height) {
		var winParams = "width="+width+",height="+height+",toolbar=0,resizable=0,scrollbars=0,screenX=0,screenY=0,top=0,left=0"
	    var popupWin =window.open(url,winname, winParams )
	    if (!document.all && window.focus) popupWin.focus(); 
	}
