/*
 * Very crude open/close of panel 
 * First child is assumed to be the clickable handle
 * Default is to collapse the content unless contains className "open"
 * Now uses findAll and checks for the "handle" to avoid re-processing when function is called multiple times
 */
function processCollapsible() {
	//Gets all .collapsible elements that are not already processed.
	$$(".collapsible").findAll(function(elm){return !elm.firstDescendant().hasClassName("handle");}).each(function(elm){
		//get first element as handle
		elm.handle = elm.down();
		
		elm.wrapper = new Element('div', {	'class': 'wrapper'});
		
		//Move all but 1st child into the wrapper
		while (elm.childElements().length > 1) {
			elm.wrapper.appendChild(elm.childElements()[1].remove());
		}
		elm.appendChild(elm.wrapper);
		
		elm.wrapper.makePositioned();
		
		elm.tail = new Element('p', {'class': 'tail'	}).update("Expand to see more...");
		elm.appendChild(elm.tail);
		
		//decide if open or shut
		if (elm.hasClassName("open")) {
			//it's open
			elm.handle.writeAttribute("title", "Click to close");
			elm.tail.hide();
		}
		else {
			//close it
			elm.wrapper.hide();
			elm.handle.writeAttribute("title", "Click to expand");
		}
		
		elm.toggle = function(){
			if (this.hasClassName("open")) {
				//close it
				Effect.BlindUp(this.wrapper, {
					duration:0.5,
					queue: {
						position: 'end',
						scope: 'collapsible',
						limit: 1
						},
					beforeStart:function(ef){
						Effect.BlindDown(ef.element.up().tail, {
							duration:0.3,
							queue: {
								position: 'end',
								scope: 'collapsible_tail',
								limit: 1
								}
							});
						},
				 	afterFinish:function(ef){
						ef.element.up().removeClassName("open");
						ef.element.up().handle.writeAttribute("title", "Click to expand");
						}
					});
				
				
				
			}
			else {
				//open it
				Effect.BlindDown(this.wrapper, {
					duration:0.5,
					queue: {
						position: 'end',
						scope: 'collapsible',
						limit: 1
						},
					beforeStart:function(ef){
						Effect.BlindUp(ef.element.up().tail, {
							duration:0.2,
							queue: {
								position: 'end',
								scope: 'collapsible_tail',
								limit: 1
								}
							});
					},						
					afterFinish:function(ef){
						ef.element.up().addClassName("open");
						ef.element.up().handle.writeAttribute("title", "Click to close");
					}
				});
				
			}
		}
		
		//setup first child
		elm.handle.addClassName("handle");

		//make first child clickable
		elm.handle.onclick = function(){
			$(this).up().toggle();
			return false;
		}//onclick
		//make first child clickable
		elm.tail.onclick = function(){
			$(this).up().toggle();
			return false;
		}//onclick
	});//each
}//end func

document.observe("dom:loaded", function(){
	processCollapsible();
}); //start the func on page load

/*
Event.observe(window, "dom:loaded", function(){
	processCollapsible();
}); //start the func on page load
*/


function processPopups() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].className.match("popup")) {
			//alert(i + ' : ' + links[i].href);
			//add onclick code
			links[i].onclick = function() {
				//alert('clicked');
				var sWidth = ",width=600";
				var sHeight = ",height=700";
				var sModal = '';
				var sDescription = "";
				if(this.getAttribute('w')) sWidth = ',width='+ this.getAttribute('w');
				if(this.getAttribute('h')) sHeight = ',height='+ this.getAttribute('h');
				if(this.getAttribute('modal')) sModal = ',modal=1,dependant=1,chrome=0,alwaysRaised=1';
				if (this.getAttribute('title')) {
					if(this.href.indexOf("?") > 0){
						//already has a ? in there
						sDescription = "&d=";
					}else{
						//needs a ?
						sDescription = "?d=";
					}
					sDescription += encodeURIComponent(this.getAttribute('title'));
				}
				//alert('w='+w+' h='+h);
				window.open(this.href + sDescription,'','toolbar=0,resizable=1,menubar=0,scrollbars=1'+sWidth+sHeight+sModal);
				return false;
			}//end func
		}//end if
	}//end if
}//end func

/* Don't change anything below this unless you know what you're doing */
addEvent(window, "load", alternate_init);

document.observe("dom:loaded", function(){
	processPopups();
}); //start the func on page load

/*
Alternating row color script by Joost de Valk ( http://www.joostdevalk.nl/ ) to add alternating row classes to a table.
Copyright (c) 2006 Joost de Valk.
*/


function alternate_init() {
	// Find all tables with class alternate_rows
	if (!document.getElementsByTagName) return;
	tbls = document.getElementsByTagName("table");
	for (ti=0;ti<tbls.length;ti++) {
		thisTbl = tbls[ti];
		if (((' '+thisTbl.className+' ').indexOf("alternate_rows") != -1)) {
			alternate(thisTbl);
		}
	}
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,	NS6 and Mozilla
// By Scott Andrew
{
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be removed");
	}
} 

function replace(s, t, u) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
}

function alternate(table) {
	// Take object table and get all it's tbodies.
	var tableBodies = table.getElementsByTagName("tbody");
	// Loop through these tbodies
	for (var i = 0; i < tableBodies.length; i++) {
		// Take the tbody, and get all it's rows
		var tableRows = tableBodies[i].getElementsByTagName("tr");
		// Loop through these rows
		for (var j = 0; j < tableRows.length; j++) {
			// Check if j is even, and apply classes for both possible results
			if ( (j % 2) == 0  ) {
				if (tableRows[j].className == 'odd' || !(tableRows[j].className.indexOf('odd') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'odd', 'even');
				} else {
					tableRows[j].className += " even";
				}
			} else {
				if (tableRows[j].className == 'even' || !(tableRows[j].className.indexOf('even') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'even', 'odd');
				}
				tableRows[j].className += " odd";
			} 
		}
	}
}