/*
// +----------------------------------------------------------------------+
// | Orginial Code Care Of:                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// |         http://blog.bitflux.ch/p1735.html                            |
// +----------------------------------------------------------------------+
//
// +----------------------------------------------------------------------+
// | Heavily Modified by Jeff Minard (07/09/04), Jonas Rabbe (2005-05-08) |
// | and Arnaud Froment (2005-07-10)                                      |
// +----------------------------------------------------------------------+
// | Same stuff as above, yo!                                             |
// +----------------------------------------------------------------------+
// | Author: Jeff Minard <jeff-js@creatimation.net>                       |
// |         http://www.creatimation.net                                  |
// | Author: Jonas Rabbe <jonas@rabbe.com>                                |
// |         http://www.jonas.rabbe.com                                   |
// | Author: Arnaud Froment                                               |
// |         http://www.sonsofskadi.net                                   |
// +----------------------------------------------------------------------+
//
// +----------------------------------------------------------------------+
// | What is this nonsense?? (2005-05-08)                                 |
// +----------------------------------------------------------------------+
// | This is a script that, by using XMLHttpRequest javascript objects    |
// | you can quickly add some very click live interactive feed back to    |
// | your pages that require server side interaction.                     |
// |                                                                      |
// | This javascript has been modified to make a 'digging archive' as     |
// | outlined by Michael Heilemann in his dissection of Freya             |
// | [http://binarybonsai.com/archives/2004/11/21/freya-dissection/]      |
// +----------------------------------------------------------------------+
*/

var af_elaLiveReq = false;
var af_elaLiveReqLast = "-";
var af_elaYear = 0;
var af_elaMonth = 0;
var af_elaCategory = 0;
var af_elaMenu = 0;
var af_elaIsIE = false;

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	af_elaLiveReq = new XMLHttpRequest();
}

function af_elaLiveReqDoReq(query) {
	if (af_elaLiveReqLast != query) {
		if (af_elaLiveReq && af_elaLiveReq.readyState < 4) {
			af_elaLiveReq.abort();
		}
		
		if (window.XMLHttpRequest) {
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			af_elaLiveReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		af_elaLiveReq.onreadystatechange = af_elaLiveReqProcessReqChange;
		af_elaLiveReq.open("GET", af_elaProcessURI + "?" + query);
		af_elaLiveReqLast = query;
		af_elaLiveReq.send(null);
	}
}

function af_elaLiveReqProcessReqChange() {
	if (af_elaLiveReq.readyState == 4) {
		var af_elaText = af_elaLiveReq.responseText;
		var resultID = af_aleRemoveSpaces(af_elaText.substring(0, af_elaText.indexOf('|')));
		af_elaText = af_elaText.substring(af_elaText.indexOf('|') + 1, af_elaText.length);
		
		var resultElement = document.getElementById(resultID); /*resultID));*/
		
		if( resultElement == null ) return;
		
		resultElement.innerHTML = af_elaText; 
		
		var menuElement = document.getElementById(resultID+'-menu')
		if( menuElement == null ) {
		} else {
			var menu_list = menuElement.childNodes;
			for( var i = 0; i < menu_list.length; i++ ) {
				if( menu_list[i].nodeName == 'LI' ) {
					menu_list[i].style.cursor = 'pointer';
					var tf = function(e) {
	 					var af_elaID = af_elaEventElement(e).id;
	 					af_elaMenu = af_elaID.substring(af_elaID.lastIndexOf('-') + 1, af_elaID.length);
	 					af_elaSelectMenu();
					}
					
					if( af_elaIsIE ) {
						menu_list[i].attachEvent('onclick',tf);
					} else {
						menu_list[i].addEventListener('click', tf, false);
					}
				}
			}
		}
		var yearElement = document.getElementById(resultID+'-year')
		if( yearElement == null ) {
		} else {
			var year_list = yearElement.childNodes;
			for( var i = 0; i < year_list.length; i++ ) {
				if( year_list[i].nodeName == 'LI' ) {
					year_list[i].style.cursor = 'pointer';
					var tf = function(e) {
	 					var af_elaID = af_elaEventElement(e).id;
	 					af_elaYear = af_elaID.substring(af_elaID.lastIndexOf('-') + 1, af_elaID.length);
	 					af_elaSelectYear();
					}
					
					if( af_elaIsIE ) {
						year_list[i].attachEvent('onclick',tf);
					} else {
						year_list[i].addEventListener('click', tf, false);
					}
				}
			}
		}
		var monthElement = document.getElementById(resultID+'-month');
		if( monthElement == null ) {
		} else {
			var month_list = monthElement.childNodes;
			for( var i = 0; i < month_list.length; i++ ) {
				if( month_list[i].nodeName == 'LI' ) {
					month_list[i].style.cursor = 'pointer';
					var tf = function(e) {
	 					var af_elaID = af_elaEventElement(e).id;
	 					af_elaMonth = af_elaID.substring(af_elaID.lastIndexOf('-') + 1, af_elaID.length);
	 					af_elaSelectMonth();
					}
					
					if( af_elaIsIE ) {
						month_list[i].attachEvent('onclick',tf);
					} else {
						month_list[i].addEventListener('click', tf, false);
					}
				}
			}
		}
		var categoryElement = document.getElementById(resultID+'-category')
		if( categoryElement == null ) return;
		var category_list = categoryElement.childNodes;
		for( var i = 0; i < category_list.length; i++ ) {
			if( category_list[i].nodeName == 'LI' ) {
				category_list[i].style.cursor = 'pointer';
				var tf = function(e) {
 					var af_elaID = af_elaEventElement(e).id;
 					af_elaCategory = af_elaID.substring(af_elaID.lastIndexOf('-') + 1, af_elaID.length);
 					af_elaSelectCategory();
				}
				
				if( af_elaIsIE ) {
					category_list[i].attachEvent('onclick',tf);
				} else {
					category_list[i].addEventListener('click', tf, false);
				}
			}
		}
				
		// Fade Anything.
		if( typeof Fat != 'undefined' && /class="fade"/.test(af_elaText)) {
			Fat.fade_all();
		}
	}
}

function af_elaLiveReqInit() {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		// branch to get to internet explorer
	} else if (navigator.product == "Gecko") {
		// branch to get to internet explorer
	} else {
		af_elaIsIE = true;
	}

	af_elaLiveReqDoReq('');
}

function af_elaSelectYear() {
	af_elaLiveReqDoReq('menu=' + af_elaMenu + '&year=' + af_elaYear);
}

function af_elaSelectMonth() {
	af_elaLiveReqDoReq('menu=' + af_elaMenu + '&year=' + af_elaYear + '&month=' + af_elaMonth);
}

function af_elaSelectCategory() {
	af_elaLiveReqDoReq('menu=' + af_elaMenu + '&category=' + af_elaCategory);
}

function af_elaSelectMenu() {
	af_elaLiveReqDoReq('menu=' + af_elaMenu + '&category=' + af_elaCategory);
}
/*
	Courtesy of Chris Boulton [http://www.surfionline.com]
*/
function af_elaEventElement(e) {
	if( af_elaIsIE ) {
		return e.srcElement;
	} else {
		return e.currentTarget;
	}
}

/* Removing leadin or trailing space just in case... */
function af_aleRemoveSpaces(TextToTrim)
{
  var buffer = "";
  var TextToTrimLen = TextToTrim.length;
  var TextToTrimLenMinusOne = TextToTrim.length - 1;
  for (index = 0; index < TextToTrimLen; index++)
  {
    if (TextToTrim.charAt(index) != ' ')
    {
      buffer += TextToTrim.charAt(index);
    }
    else
    {
      if (buffer.length > 0)
      {
        if (TextToTrim.charAt(index+1) != ' ' && index != TextToTrimLenMinusOne)
        {
          buffer += TextToTrim.charAt(index);
        }
      }
    }
  }
  return buffer;
}


/*
addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events
*/
function af_elaAddEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn); 
		return r;
	} else {
		return false;
	}
}

//af_elaAddLoadEvent(af_elaLiveReqInit);
af_elaAddEvent(window, 'load', af_elaLiveReqInit);