var blockstyles=new Array;

function addblockstyle(id,style) {
	if (!blockstyles[id]) {
		// set block style, and start callback
		blockstyles[id]=style;
		setTimeout("applyblockstyle("+id+")",100);
	} else {
		// update block style, but do not call callback
		blockstyles[id]=style;
	}
}

function applyblockstyle(id) {
	if (document.getElementById("cmsblock"+id).className!=blockstyles[id]) document.getElementById("cmsblock"+id).className=blockstyles[id]
	blockstyles[id]=null;
}

function cmsonblockover(block) {
	if (isadmin()) {
		var id=block.id.substring(8);
		addblockstyle(id,"cmsblockover");
	}
}

function cmsonblockout(block) {
	if (isadmin()) {
		var id=block.id.substring(8);
		addblockstyle(id,"cmsblock");
	}
}

function cmseditblock(blockid) {
	if (isadmin()) {
		cmscloseblockmenu(blockid);
		var popup;
		popup=window.open("/admin/EditBlockRad.aspx?block="+blockid,"cmspopup","dependent,location=no,toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=760");
		popup.focus();
	}
}

var lastblockmenu;

function cmsonblockclick(e,block) {
	if (isadmin()) {
		if (!e) e=window.event;
		var newblock=block.substring(8);
		if (newblock!=lastblockmenu) {
			if (lastblockmenu) document.getElementById("blockmenu"+lastblockmenu).style.display="none";
			lastblockmenu=newblock;
			var menu=document.getElementById("blockmenu"+newblock);
			if (menu) {
				var posx = 0;
				var posy = 0;
				
				if (e.pageX || e.pageY) {
					posx = e.pageX;
					posy = e.pageY;
				} else if (e.clientX || e.clientY) {
					posx = e.clientX + document.body.scrollLeft;
					posy = e.clientY + document.body.scrollTop;
				}
				
				posx=posx-document.getElementById("content").offsetLeft-document.getElementById("center").offsetLeft+360;
				posy=posy-document.getElementById("content").offsetTop;
				
				var scrolly;
				if (self.pageYOffset) {
					scrolly = 0;
				} else if (document.documentElement && document.documentElement.scrollTop) {
					scrolly = document.documentElement.scrollTop;
				} else if (document.body) {
					scrolly = document.body.scrollTop;
				}
				
				posy=posy+scrolly;
				
				menu.style.left=posx+"px";
				menu.style.top=posy+"px";
				menu.style.display="block";
			}
		}
	}
}

function cmscloseblockmenu(blockid) {
	if (isadmin()) {
		document.getElementById("blockmenu"+blockid).style.display="none";
		lastblockmenu=null;
	}
}

function cmsonblockaclick() {
	return !isadmin();
}

function cmsgetpos(e) {

	var pos = new Array(2);
	
	if (e.pageX || e.pageY) {
		pos[0] = e.pageX;
		pos[1] = e.pageY;
	} else if (e.clientX || e.clientY) 	{
		pos[0] = e.clientX;
		pos[1] = e.clientY;
		
		if (document.body.scrollTop) {
			pos[0]=pos[0]+document.body.scrollLeft;
			pos[1]=pos[1]+document.body.scrollTop;
		} else if (document.documentElement.scrollTop) {
			pos[0]=pos[0]+document.documentElement.scrollLeft;
			pos[1]=pos[1]+document.documentElement.scrollTop;
		}
	}
	
	return pos;
}

function getradio(form,radioname) {
	if (document.forms[form][radioname].length) {
		var i;
		for (i=0;i<document.forms[form][radioname].length;i++) {
			if (document.forms[form][radioname][i].checked) {
				return document.forms[form][radioname][i].value;
			}
		}
		return "";
	} else {
		if (document.forms[form][radioname].checked) {
			return document.forms[form][radioname].value;
		} else {
			return "";	
		}
	}
}

function cmsonunload() {
	if (window.parent && window.parent.frames[0]) {
		if (!window.parent.frames[0].isactive() && !window.parent.frames[0].dontsave) {
			if (confirm("This page hasn't been activated. Would you like to activate it now?")) {
				window.parent.frames[0].__doPostBack('lnkActivatePage','');
			}
		}
	
		window.parent.frames[0].dontsave=false;
	}
}

var dontsave=false;

var sharedblock_id;
var sharedblock_blocktypeid;

var blockorder=1;

 function writetoolbar(pageid,id,node,isgroup,isshared,blocktypeid) {
	if(isadmin()) {
		document.write("<div class='blockmenu' id='blockmenu"+id+"'><p>");
		if (isgroup) {
			document.write("<b>content block</b><br/>");
			document.write("<a href='javascript:cmseditblock("+id+");'>edit block</a><br/>");
			document.write("<a href='javascript:deleteblock("+pageid+","+id+");' onclick='return confirm(\"Are you sure you want to delete this block?\");'>delete block</a><br/>");
			document.write("<a href='javascript:insertblock("+id+","+pageid+","+blockorder+");'>insert before</a><br/>");
			document.write("<a href='javascript:insertblock("+id+","+pageid+","+(blockorder+1)+");'>insert after</a><br/>");
			
			blockorder=blockorder+1;
		} else if (isshared) {
			document.write("<b>shared content block</b><br/>");		
			document.write("<a href='javascript:cmseditblock("+id+");'>edit block</a><br/>");
			document.write("<a href='javascript:breakblock(" + pageid + "," + id + ");'>break link to share</a><br/>");
		} else {
			document.write("<b>content block</b><br/>");
			document.write("<a href='javascript:cmseditblock("+id+");'>edit block</a><br/>");
			if (!sharedblock_id) document.write("<a href='javascript:findblock(" + pageid + "," + id + "," + blocktypeid + ");'>get content from another block</a><br/>");		
		}
		
		if (sharedblock_id && !isgroup && id!=sharedblock_id && blocktypeid == sharedblock_blocktypeid) document.write("<a href='javascript:useblock(" + id + ");'>use content from this block</a><br/>");		
		
		document.write("<a href='javascript:cmscloseblockmenu("+id+");' >cancel</a><br/>");
		
		document.write("</p></div>");
		
	} 
}

function loadxmldoc(url,callback) {
	if (window.XMLHttpRequest)  {
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=callback;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}	else if (window.ActiveXObject)  {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)   {
			xmlhttp.onreadystatechange=callback;
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	}
}

function insertblock(blockid,pageid,orderno) {
	cmscloseblockmenu(blockid);
	window.parent.frames[0].dontsave=true;
	window.location.href="/admin/cms/addblock.aspx?pageid="+pageid+"&OrderNo="+orderno;
}

function deleteblock(pageid,blockid) {
	cmscloseblockmenu(blockid);
	window.parent.frames[0].dontsave=true;
	window.location="/admin/deleteblock.aspx?id="+ blockid +"&pageid="+ pageid
}

function findblock(pageid,blockid,blocktypeid) {
	cmscloseblockmenu(blockid);
	alert("Please navigate to the block you want to copy, and select 'use content from this block' from its menu.");
	window.parent.frames[0].dontsave=true;
	window.location.href="/admin/findblock.aspx?id=" + blockid + "&pageid=" + pageid + "&blocktypeid=" + blocktypeid
}

function useblock(blockid) {
	cmscloseblockmenu(blockid);
	window.parent.frames[0].dontsave=true;
	window.location.href="/admin/useblock.aspx?id=" + blockid
}

function breakblock(pageid,blockid) {
	cmscloseblockmenu(blockid);
	window.parent.frames[0].dontsave=true;
	window.location.href="/admin/breakblock.aspx?pageid=" + pageid + "&pageblockid=" + blockid + "&goto=" + escape(window.location.href)
}

function fixnav(node) {
	if (!isadmin()) return;
	
	if (!node) node=document.getElementById("nav");		
		
	if (node.href && !node.href.match(/#$/) && node.innerHTML!="" && !node.href.match(/\.ashx/i)) {
			
		var dummynode=document.createElement("span");
		dummynode.innerHTML=node.innerHTML;
		dummynode.style.color="#aebbce";
		dummynode.style.margin=node.style.margin;
		dummynode.style.padding=node.style.padding;
		node.parentNode.replaceChild(dummynode,node);
	} else {
		var i;
		for (i=0; i<node.childNodes.length; i++) {
			fixnav(node.childNodes[i]);
		}
	}
}

function writenavmessage() {
	if (isadmin()) {
		document.write("<div class='adminmessage'>NAVIGATE TO THE PAGE YOU WANT TO EDIT:</div>");
	}
}

function writebottomtoolbar(pageid) {
	if (isadmin()) {
		document.write("<div class='adminmessage'><a href='/admin/cms/addblock.aspx?pageid="+pageid+"' onclick='window.top.frames[0].dontsave=true;'>ADD BLOCK</a></div>");
	}
}
