// JavaScript Document
// file contains logic for populating centerpanel content on various link selections

YAHOO.namespace("mrespta.calendar");

(function()
{
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var MRns = YAHOO.mrespta;
	var csSelected = "selected"; 
	// set up all there is to at startup
	MRns.init = function (initArgs) 
	{
		// set up the links
		MRns.setupLinks();

		// create a new calendar widget and render it for the main frame
		MRns.setupCalendar(initArgs);
	}

	MRns.setupLinks = function()
	{
		try
		{
			var els = Dom.getElementsByClassName("headanchor");
			els = els.concat(Dom.getElementsByClassName("navitem"));
			for (var i=0,len=els.length; i < len; i++)
			{
				els[i].onclick = function()
				{
					MRns.navigate(this.id);
				}
			}

			var loc = document.location.href;
			var pos = loc.lastIndexOf("#tab_"); // tab_xyz xyz could be boarditem id or a true url path for 
												// non board item eg #tab_./messages/archive.php
			var startitem;
			if (pos && pos != -1)
				startitem = loc.substring(pos+5); // length of #tab_ is 5
			startitem = startitem || "home";
			if (Dom.get(startitem))
				Dom.get(startitem).onclick(); // boarditem 
			else
				MRns.navigate(startitem);
		}
		catch(e)
		{
		
		}
	}

	MRns.setupCalendar = function (evArgs)
	{
		//var evCal = new YAHOO.extwidget.CalendarList({id:evArgs.evcalid,idCalCont: evArgs.evcalcontid, idList:evArgs.evlistid, url:"./ws/tojson.php5?url=" + evArgs.xmldata});
		var evCal = new YAHOO.extwidget.CalendarList({id:evArgs.id, url:"./ws/tojson.php5?url=" + evArgs.xmldata});
	}

	MRns.navigate = function (subpath)
	{
		function ajaxHttp()
		{
			try { return new XMLHttpRequest();}catch(e){}
			try { return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
			try { return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
			alert("XMLHttp not supported");
			return null;
		}
		try
		{
			if (!subpath)
			{
				alert("Path missing!");
				return;
			}
				
			var xmlhttp = ajaxHttp();
			MRns.currSel = subpath;

			xmlhttp.open("GET","./" + subpath); // index.php is the default under the subpath
			xmlhttp.onreadystatechange = function(){
										if (xmlhttp.readyState == 4)
											MRns.applyContent(xmlhttp);
									 }
			xmlhttp.send(null);
		}
		catch(e)
		{
			alert("Navigation to " + subpath + "failed.");
		}
	}

	MRns.applyContent = function (xmlhttp)
	{
		try
		{
			if (xmlhttp.status == 200)
			{
				if (MRns.lastSel && MRns.lastSel == MRns.currSel) // already there .. nothing to do
					return;
				var styleEl;
				var iepngcls;
				if (MRns.lastSel && Dom.get(MRns.lastSel)) // may be absent if the link with this id got tore down when navigating to its content - this can happen for links in the center pane whose contents get replaced
				{
					styleEl = Dom.get(MRns.lastSel).parentNode;
					Dom.removeClass(styleEl, csSelected);
					// ie6 png support hack needs a special filter managed by classname
					iepngcls = styleEl.className.match(/(ie6png\d)sel/);
					if (iepngcls)
					{
						Dom.removeClass(styleEl, iepngcls[0]); // iepng?sel filter
						Dom.addClass(styleEl, iepngcls[1]); // restore the original - iepng? filter
					}
				}
				if (Dom.get(MRns.currSel)) // this could be absent if the subpath sent in is not a el id (= subpath)
				{
					styleEl = Dom.get(MRns.currSel).parentNode; 
					Dom.addClass(styleEl,csSelected);
					// ie6 png support hack needs a special filter managed by classname
					iepngcls = styleEl.className.match(/ie6png\d/);
					if (iepngcls)
					{
						Dom.removeClass(styleEl, iepngcls[0]);
						Dom.addClass(styleEl, iepngcls[0] + "sel");
					}
				}
				MRns.lastSel = MRns.currSel;
				
				var content = xmlhttp.responseText;
				Dom.get("centerpanel").innerHTML = content;
				document.location = "#tab_" + MRns.currSel;
			}
			else
			{
			
				alert("Navigation failed : " + xmlhttp.responseText);
			}	
		}
		catch(e)
		{
			alert(e.tostring());
		}			
	}

	//Event.onDOMReady(init);

})();
