Event.observe(window, 'load', init, false);

 var pageObjects=new Array;
 var origColour;
 var editIsOn=true;		// Default is no editing
 var CSSskin;
 var cachedInnerHtml;
 var cancelPlace;

 function init(){
     menulist=document.getElementsByTagName("A");
	 items=menulist.length;
	 for (i=0;i<items;i++)
	 {	
		eachObj=menulist[i];
		if (className='caosMenu')
		{
			// Note the bindAsEventListener method which forces the event to pass the event type and eachObj to the handler
			Event.observe(eachObj,'click',popupSelector.bindAsEventListener(eachObj), false);
		}
		
	 }
	 editlist=$$(".jslongtext");
	 items=editlist.length;
	for (i=0;i<items;i++)
	{	
		eachObj=editlist[i];
		Event.observe(eachObj,'mouseover',highlight.bindAsEventListener(eachObj),false);
		Event.observe(eachObj,'mouseout',unhighlight.bindAsEventListener(eachObj),false);
		Event.observe(eachObj,'dblclick',applyLongEdit.bindAsEventListener(eachObj),false);
	}

	editlist=$$(".jsshorttext");
	 items=editlist.length;
	for (i=0;i<items;i++)
	{	
		eachObj=editlist[i];
		Event.observe(eachObj,'mouseover',highlight.bindAsEventListener(eachObj),false);
		Event.observe(eachObj,'mouseout',unhighlight.bindAsEventListener(eachObj),false);
		Event.observe(eachObj,'click',applyShortEdit.bindAsEventListener(eachObj),false);
	}

	/*
	new Draggable('ntntbar');
	$('ntntbar').style.borderLeft='6px #c33 dotted';
	editlist=$$(".moveable");
	items=editlist.length;
	for (i=0;i<items;i++)
	{	
		eachObj=editlist[i];
		eachObj.style.borderTop='6px #c33 solid';
		objName=eachObj.id;
		alert(objName);
		new Draggable(objName);
		new Resizable(objName);
	}
	*/
	moodObj=$('jsPersmood');
	if (moodObj)
	{
		Event.observe(moodObj,'mouseover',highlight.bindAsEventListener(moodObj),false);
		Event.observe(moodObj,'mouseout',unhighlight.bindAsEventListener(moodObj),false);
		Event.observe(moodObj,'click',showMood,false);
	}

	Event.observe('form','submit',showWaiting,false);
 }

 function useEditing(templatelist) {
	 editIsOn=false;
	 CSSskin=templatelist;
 }

 function showWaiting() {
	 // Opacity applies to an image.
	 // FF and IE are different
	 //alert("Submit pressed");
	 
	 window.scrollTo(0,0);
	 var opacity=50;
	 var imageobject= new Element('img', { src: 'images/loadbg.gif' });
	 if (navigator.appName.indexOf("Netscape")!=-1 && parseInt(navigator.appVersion)>=5) {
		imageobject.style.MozOpacity=opacity/100
	 } else if (navigator.appName.indexOf("Microsoft")!= -1 && parseInt(navigator.appVersion)>=4) {
		imageobject.filters.alpha.opacity=opacity
	 }
		imageobject.style.position='absolute';
		imageobject.style.top='0px';
		imageobject.style.left='0px';
		imageobject.style.width='100%';
		imageobject.style.height='100%';
	//$('editbar').insert({ top: imageobject });
	 var loadingobject= new Element('img', { src: 'images/loading_clock.gif' });
	 loadingobject.style.position='absolute';
	 loadingobject.style.top='48%';
	 loadingobject.style.left='48%';
	 loadingobject.zorder='9999';
	 $('editbar').insert({ bottom: loadingobject });
	 
	 //alert("Submit pressed");
 }

 function addButton(id){
     Event.observe(id, 'click', function(){popupSelector($(id))}, false);
 }

 function popupSelector(e) {
	 var data = $A(arguments);
	 data.shift();
	 strName=this.id;
	 
	 URLparts=document.URL.split("?");
	 if (URLparts[1]) {
		getvars=URLparts[1].replace("#","");
	 } else {
		getvars="";
	 }
	 if (strName=='ntntAddImage') {
		imagePopup=window.open('imagesdialogue.php?'+getvars+'&ajax=full','popUp','height=420,width=410,location=no,menubar=no, resizable=yes, scrollbars=yes,toolbar=no,left=40,top=40');
		imagePopup.focus();
		strName="";
	 } else {
		insertComponent(getvars,strName);
		strName="";
	 }
	 if (strName) {
		alert("Button pressed: " + strName);
	 }
	 return(false);

 }
 
 function insertImage(href,alttag) {
	 var id=pageObjects.length+1;
	 objName='ntntImg_'+id;
	 pageObjects.push(objName);
	 insertImg="<img src='"+href+"' title='"+alttag+"' alt='"+alttag+"' id='"+objName+"'>";
	 newObj=$('insertPoint').insert({ top: insertImg });
	 //new Draggable(objName);
 }

 function insertComponent(vars,ajaxName) {
	 // vars is GETvars
	 // listened for is the caosMenu class - ie have class='caosMenu' id='ntnt_topics-2'
	 // ajaxName is the ID of the item clicked
	 var id=pageObjects.length+1;
	 ajaxName=ajaxName.replace(":","[");
	 ajaxName=ajaxName.replace(".","]");
	 strName='ntntDiv_'+id;
	 strHandle='ntntHandle_'+id;
	 pageObjects.push(strName);
	 insertDiv="<div id='"+strName+"' style='width:400px; border:1px #000 solid'><div id='"+strHandle+"'></div></div>";
	 newObj=$('insertPoint').insert({ after: insertDiv });
	 new Ajax.Updater(strHandle, 'showsite.php?'+vars+'&ajax='+ajaxName, {
		  method: 'get',
		  parameters: vars,
		  insertion: Insertion.Top
	 } );
	 //new Draggable(strName);
	 //new Resizable(strName,{handle: strHandle});
	 //Event.observe(newObj,'dblclick',removeObject.bindAsEventListener(newObj), false);
 }

 function removeObject(e) {
	 var data = $A(arguments);
	 data.shift();
	 this.remove
 }

function contextMenu(obj) {
	var menuOptions="<ul><li>Remove</li><li>Save this</li><li>Background</li><li>Text colour</li><li>Font</li></ul>";
}

function applyLongEdit(e) {
	var data = $A(arguments);
	data.shift();
	if (editIsOn==false)
	{
		// Grab any elements marked with the 'ntnt' class
		// Pick off their id and convert to a %%div
		var varname=this.id;
		editlist=$$("#"+varname+" .nt");
		items=editlist.length;
		//alert(items);
		for (i=0;i<items;i++)
		{	
			eachObj=editlist[i];
			ppname=eachObj.id;
			//alert(ppname);
			ppname=ppname.replace(":.","[");
			ppname=ppname.replace(".:","]");
			ppname=ppname.replace("ntnt_","");
			// Replace each with a %%command
			//eachObj.replace("<ins>%%"+ppname+"</ins>");
			eachObj.replace("<p>%%"+ppname+"</p>");
		}
		imagelist=$$("#"+varname+" IMG");
		items=imagelist.length;
		for (i=0;i<items;i++)
		{	
			eachImg=imagelist[i];
			src=eachImg.src;
			src=src.replace("http://"+document.domain,"");
			if (src.substr(0,7)!="http://") {
				//alert(eachImg.src);
				eachImg.src="../../"+src;
			}
		}
		// Tags replacement
		editlist=$$("#"+varname+" .nttag");
		items=editlist.length;
		//alert(items);
		for (i=0;i<items;i++)
		{
			eachObj=editlist[i];
			tagtext=eachObj.innerHTML;
			eachObj.replace("[["+tagtext+"]]");
		}

		// Now grab the munged HTML
		//var insertHTML=this.innerHTML;
		cachedInnerHtml=this.innerHTML;
		var thispage=top.location.href
		document.getElementById('editbar').style.display='inline';
		thispage=thispage.replace("#","");
		thispage=thispage.replace("&edit=y","")+"&edit=y";
		var divHeight=this.offsetHeight+28;
		if (divHeight<60) { divHeight=120; }
		//alert(varname);
		this.innerHTML='';
		var replaceDiv="<div class='formtextarea'><form method='post' action='"+thispage+"'><textarea id='"+varname+"' name='"+varname+"'>"+cachedInnerHtml+"</textarea><input name='submit' value='Save' type='submit' id='fckSubmit'><input name='cancel' type='submit' value='Cancel' id='fckCancel'></form></div>";
		newObj=this.insert({ top: replaceDiv });
		var oFCKeditor = new FCKeditor(varname);
		oFCKeditor.BasePath = './fckeditor/' ;
		oFCKeditor.Height=divHeight+'px';
		//oFCKeditor.Config["ToolbarLocation"]="Out:editbar";
		oFCKeditor.Config["ToolbarStartExpanded"]= false;
		oFCKeditor.Config["StartupFocus"]=true;
		//alert(CSSskin);
		//oFCKeditor.Config['EditorAreaCSS'] = CSSskin;
		oFCKeditor.ReplaceTextarea();
		Event.observe($('fckCancel'),'click',cancelEdit,false);
		this.style.background=origColour;
		editIsOn=true;
	}
}
//This would operate if an image is dragged and dropped into the editor - not used
//function FCKeditor_OnComplete(editorInstance) {
//	editorInstance.Events.AttachEvent('OnPaste',checkPasteImage);
	//alert(editorInstance.name);
//}
//function checkPasteImage(editorInstance) {
//	alert("Here");
//  need to grab the pasted text and check for non-http images
//	return true;
//}
function nullEdit(e) {
	editIsOn=true;
}

function applyShortEdit(e) {
	var data = $A(arguments);
	data.shift();
	if (editIsOn==false)
	{
		var insertHTML=this.innerHTML;
		var varname=this.id;
		var thispage=top.location.href
		thispage=thispage.replace("#","");
		thispage=thispage.replace("&edit=y","")+"&edit=y";
		//alert(thispage);
		this.innerHTML='';
		var replaceDiv="<div class='formright'><form method='post' action='"+thispage+"'><input id='"+varname+"' name='"+varname+"' value='"+insertHTML+"><br><input name='submit' value='Save' type='submit' id='fckSubmit'><input name='cancel' type='submit' value='Cancel' id='fckCancel'></form></div>";
		newObj=this.insert({ top: replaceDiv });
		Event.observe($('fckCancel'),'click',cancelEdit(event),false);
		editIsOn=true;
	}
}

function cancelEdit(e) {
	//top.location=window.location;
	if (cancelPlace) { 
		$(cancelPlace).replace(cachedInnerHtml);
		if (cancelPlace=='jsPersmood')
		{	
			revertObj=$('jsPersmood');
			Event.observe(revertObj,'mouseover',highlight.bindAsEventListener(revertObj),false);
			Event.observe(revertObj,'mouseout',unhighlight.bindAsEventListener(revertObj),false);
			Event.observe(revertObj,'click',showMood,false);
		}
	} else {
		window.location.reload();
		Event.stop(e);
	}
}

function submitForm() {
	Event.stop();
}

function highlight(e) {
	var bgcolor;
	var data = $A(arguments);
	data.shift();
	if (editIsOn==false) {
		origColour=this.style.background;
		if (this.currentStyle)
		{
			// For IE - note that ie doesn't convert color to rgb
			bgtext=this.currentStyle["color"];
			if (bgtext=="white"||bgtext>="#ddd")
			{
				bg="#141414";
			} else {
				bg="#f3f3f3";
			}
		} else if (window.getComputedStyle)
		{
			bgcolor=window.getComputedStyle(this,"");
			bgtext=bgcolor.getPropertyValue("color");
		
			bgtext=bgtext.replace("rgb(","");
			bgtext=bgtext.replace(")","");
			bgarray=bgtext.split(",");
			r=bgarray[0]*1;
			g=bgarray[1]*1;
			b=bgarray[2]*1;
			// Light text (255,255,255) takes dark grey backgound
			// Dark text takes light grey background
			if (r*b*g>2097152)
			{
				bg="#141414";
			} else {
				bg="#f3f3f3";
			}
		}
		//alert("FF"+r+g+b);
		
		//alert(this.style.color);
		this.style.background=bg+" URL('./icons/editbtn.gif') no-repeat top right";
		//this.style.background=bg;
	}
	if (this.id=='jsPersmood')
	{
		origColour=this.style.background
		this.style.background='#f3f3f3';
	}
	
}

function unhighlight() {
	var data = $A(arguments);
	data.shift();
	if (editIsOn==false||this.id=='jsPersmood') {
		this.style.background=origColour;
	}
}

function popup(mylink, name)  {
	if (! window.focus) return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, 'help', 'width=550,height=620,resizable=yes,scrollbars=yes,left=400,top=180');
	return false;
}

// For the menubar at the top
var edithide=1;
var results;
var wzdid;
results = document.cookie.match ( 'edithide=(.*?)(;|$)' );
if ( results ) {
	edithide=results[1];
	if(edithide==1) {
		document.getElementById('editbar').style.display='none';
		document.getElementById('ntntbar').style.borderBottom='none';
	} else {
		document.getElementById('editbar').style.display='inline';
		document.getElementById('ntntbar').style.borderBottom='1px #aaa solid';
		document.getElementById('menuicon').src='menubar/menuicon.gif';
	}
} else {
	edithide=1;
	document.getElementById('editbar').style.display='none';
	document.getElementById('ntntbar').style.borderBottom='none';
}
function hideEdit(wizard) {
	if (wzdid!=-1)
	{
		wzdid=wizard;
		if (!results&&wizard>0) {
			edithide=1;
		}
	}
	if (edithide==0) {
		document.getElementById('editbar').style.display='none';
		document.getElementById('ntntbar').style.borderBottom='none';
		document.getElementById('menuicon').src='menubar/menuicon.gif';
		document.cookie='edithide=1;';
		//if (wzdid>0) {
		//	popwin=window.open('wizard.php?org_id='+wizard, 'guide');
		//	popwin.close();
		//}
		edithide=1;
	} else {
		document.getElementById('editbar').style.display='inline';
		document.getElementById('ntntbar').style.borderBottom='1px #aaa solid';
		document.getElementById('menuicon').src='menubar/menuicon.gif';
		document.cookie='edithide=0;';
		edithide=0;
		//if (wzdid>0) {
		//	popwin=window.open('wizard.php?org_id='+wizard, 'guide', 'width=550,height=420,scrollbars=yes,left=100,top=180');
		//	popwin.focus();
		//}
	}
	return(true);
}

function showWizard(wzdid) {
	if (wzdid>0)
	{
		popwin=window.open('wizard.php?org_id='+wzdid, 'guide', 'width=550,height=520,scrollbars=yes,left=100,top=180');
		popwin.focus();
	}
}

function showMood() {
	var thispage=top.location.href;
	var moodform="<div id='jsPersmood' style='border:2px dotted #000; background:#ffe;'><form method='post' action='"+thispage+"' class='searchform'> \
						<br>Is... <input type='text' name='pers_situation' size='25' class='searchinput'> \
						<br>Mood: <select name='pers_mood' class='searchinput'> \
									<option value=''></option> \
									<option value='%v=happy'>Happy</option> \
									<option value='%v=sad'>Sad</option> \
									<option value='%v=lively'>Lively</option> \
									<option value='%v=mellow'>Mellow</option> \
									<option value='%v=busy'>Busy</option> \
									<option value='%v=overworked'>Overworked</option> \
									<option value='%v=stressed'>Stressed</option> \
									<option value='%v=relaxed'>Relaxed</option> \
									<option value='%v=frustrated'>Frustrated</option> \
									<option value='%v=angry'>Angry</option> \
									<option value='%v=cool'>Cool</option> \
									<option value='%v=chilly'>Chilly</option> \
									<option value='%v=hungover'>Hungover</option> \
									<option value='%v=romantic'>Romantic</option> \
									<option value='%v=blue'>Blue</option> \
									<option value='%v=beautiful'>Beautiful</option> \
									<option value='%v=ugly'>Ugly</option> \
									<option value='%v=sunburnt'>Sunburnt</option> \
									<option value='%v=neutral'>Neutral</option> \
									<option value='%v=buzzing'>Buzzing</option> \
									<option value='%v=sleepy'>Sleepy</option> \
									<option value='%v=drunk'>Drunk</option> \
									<option value='%v=glum'>Glum</option> \
									<option value='%v=sharp'>Sharp</option> \
									<option value='%v=jealous'>Jealous</option> \
									<option value='%v=excited'>Excited</option> \
									<option value='%v=lonely'>Lonely</option> \
									<option value='%v=thoughtful'>Thoughtful</option> \
									<option value='%v=sociable'>Sociable</option> \
									<option value='%v=worried'>Worried</option> \
									<option value='%v=nervous'>Nervous</option> \
									<option value='%v=noisy'>Noisy</option> \
									<option value='%v=sporty'>Sporty</option> \
									<option value='%v=shy'>Shy</option> \
									<option value='%v=over-the-moon'>Over the moon</option> \
									<option value='%v=bored'>Bored</option></select> \
									<br><input type='submit' name='submitMood' value='Update' class='searchinput'><input type='submit' name='cancel' value='Cancel' id='moodCancel' class='searchinput'> \
									</form></div>";
		cachedInnerHtml="<div id='jsPersmood'>" + $('jsPersmood').innerHTML + "</div>";
		newObj=$('jsPersmood').replace(moodform);
		cancelPlace='jsPersmood';
		Event.observe($('moodCancel'),'click',cancelEdit,false);
}

function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

 // document.URL is the URL of the page (including get variables after ?
 // document.anchors[] all anchors on the page
 // Page zooming for IE browsers - change the zoom value
 // * {zoom:1.0;} 

 // For reference all Notanant elements on the page have ids
 //		ntntForm_{objectname}_{refno}
 //		ntntList_{objectname}_{refno}
 //		ntntImage_{refno}
 //		ntntWidget_{objectname}_{refno}

 // Notanant ajax is ajax=pagename:item

 // Pop-up windows cause a problem with CSS (and the ajax HTML isn't technically correct - no header)



