function showEditHandles()
{
	var divs = document.getElementsByTagName('DIV');
	var id ="";
	var selectedId = "";
	var marginPosition="top";
	var obj;

	var contentsColumn = document.getElementById("ContentsColumn");
	if (contentsColumn.offsetWidth < 650) { //3col template
		marginPosition = "bottom";
		contentsColumn.style.position="relative";
		contentsColumn.style.top="-21px";
		contentsColumn.style.paddingTop="21px";
		contentsColumn.style.background="transparent";
	}
	if (browser.ie && browser.version=="6") {
		contentsColumn.style.width=(contentsColumn.offsetWidth+2)+"px"; 
	}


	for(i=0; i<divs.length; i++) {
		id = divs[i].getAttribute("id");
		if(id && isValidModuleId(id)) { // only modules have id.
			if(id.indexOf("info_")==-1 && id.indexOf("addItem_")==-1 && id.indexOf("hotellist_")==-1 && id.indexOf("restaurantlist_")==-1) {
				selectedId = id;
				obj = document.getElementById(selectedId);
				showEditHandle(obj,selectedId, marginPosition);
			}
		}
	}


	return selectedId;
}

function showEditHandle(obj, id, marginPosition)
{
	var element = document.createElement("DIV");
	var eidEtype, eid, etype, addUrl, editUrl, deleteUrl, commentsNode, tableNode;

	eidEtype = id.split('_');
	eid = eidEtype[1];
	etype = eidEtype[0];
	
	var elementId = "element"+id;	
	element.setAttribute("id",elementId); 
	element.style.position = "absolute";
	element.style.zIndex=100;
	element.style.top="-18px";
	if (browser.ie && browser.version=="6") {
		element.style.top="-16px";
	}
	element.style.right="24px";
	
	editUrl = "http://www.yiqilai.com.cn/bin/view/travel/guizhou?action=formedit&version=49&eid=" + eid;
	if(etype!="addItem" && etype!="addTodoItem") {
		html = '<a href="' + editUrl +'" style="padding:0 8px 0 8px;height:16px;border:solid #d7e5fd;border-width:1px 1px 0 1px;background-color:#f5f5f5;">编辑</a>';
	}
	element.innerHTML = html;

	obj.appendChild(element);

	obj.style.position = "relative";
	if (marginPosition == "bottom") {
		obj.style.marginBottom = "24px";
	}
	else {
		obj.style.marginTop = "24px";
	}


	//draw border where needed
	var needBorder = true;
	if (id.indexOf("hotellist_")==0) needBorder = false;
	if (id.indexOf("restaurantlist_")==0) needBorder = false;
	commentsNode = getChildNodeByClass(obj, "comments1");
	tableNode = null;
	if (commentsNode != null) {
		if (getChildNodeByClass(commentsNode, "modulect2")!=null) needBorder = false;
		if (getChildNodeByClass(commentsNode, "bginfo")!=null) needBorder = false;
		
	}
	if (needBorder) {
		obj.style.border = "1px solid #95BEDE";
		obj.style.padding = "6px";
	}
}

function getChildNodeByClass(obj, nodeClass) {

	var i = 0;
	var nodes = obj.childNodes;
	for (i = 0; i < nodes.length; i++) {
		if (nodes[i].className == nodeClass) {	
			return nodes[i];
		}
	}
	return null;
}

function showAddElementButton() {
	var element = document.createElement("DIV");
	var editUrl, html, obj;
	element.setAttribute("id","addElementDiv"); 
	element.style.width = "100%";
	element.style.height = "30px";
	element.style.marginBottom = "20px";
	element.style.background = "url(http://img.yiqilai.com.cn/cns/icon/icon_plus.jpg) no-repeat 0px 0px";

	editUrl = "http://www.yiqilai.com.cn/bin/view/travel/guizhou?action=formadd&eid=0";
	html = '<a href="' + editUrl +'" style="margin-left:25px;">添加一段内容到这个页面</a>';
	element.innerHTML = html;

	obj = document.getElementById("contributorList");
	obj.parentNode.insertBefore(element,obj);
}
function isValidModuleId(eid)
{
	var ids = new Array("link_", "text_", "list_", "image_", "flash_", "musicmovie_", "todo_", "todoitem_", "rss_","bbs_","container_", "info_", "addItem_","addTodoItem_","hotellist_","restaurantlist_","travelstorylist_","externalvideo_");
	var i, l = ids.length;
	var peid, index;

	index = eid.indexOf("_");
	if(-1==index) return false;
	peid = eid.substring(0,index+1);
	if(peid=="pl_" || peid=="sl_" || peid=="cl_") return false;
	
	for(i=0; i<l; i++) {
		if(peid==ids[i]) return true;
	}	
	return false;
}
window.onload = function() {
	if(document.getElementById("ContentsColumn").offsetWidth < 900) {
		showEditHandles();
		showAddElementButton();
	}
}

