/*
stable external interfaces:
- an_sort(id, col)
- an_selectTab(sTabId, arrayOfTabIDs, parentNode, activeTabClass, tabClass, listPrefix)

at the end of this file
*/



function omzetten(cel) {
	var oWaarde;                                                                                                  if (cel == null) return "";
	if (cel.getAttribute("value")) oWaarde = cel.getAttribute("value");
	if (cel.firstChild == null) return "";
	if (cel.firstChild.nodeValue) oWaarde = cel.firstChild.nodeValue;
	else if (cel.firstChild.firstChild && cel.firstChild.firstChild.nodeValue) oWaarde = cel.firstChild.firstChild.nodeValue;
	if (oWaarde == " ") return "";
	return oWaarde.toString().toLowerCase().replace(/^\s+/, "");
}

function nogmaals(Kolom) {
	return function vergelijkRij(Rij1, Rij2) {
		var vWaarde1, vWaarde2;
		vWaarde1 = omzetten(Rij1.cells[Kolom]);
		vWaarde2 = omzetten(Rij2.cells[Kolom]);
		if (vWaarde1 < vWaarde2) {return -1;}
		else if (vWaarde1 > vWaarde2) {return 1;}
		else {return 0;}
	};
}

function sorteer(sTabelID, Kolom) {
	var oTabel = document.getElementById(sTabelID);
	var oTBody = oTabel.tBodies[0];
	var Rijen = oTBody.rows;
	var aRij = new Array;
	for (var i=0; i < Rijen.length; i++) {aRij[i] = Rijen[i];}
	if (oTabel.sortCol == Kolom) {aRij.reverse();}
	else {aRij.sort(nogmaals(Kolom));}
	var oFragment = document.createDocumentFragment();
	for (var i=0; i < aRij.length; i++) {oFragment.appendChild(aRij[i]);}
	oTBody.appendChild(oFragment);oTabel.sortCol = Kolom;
	return false;
}



// Make general.js independant of prototype, while still remaining compatible with it
if (typeof $ == "undefined") {
  // there is no dollar function, so define it. This function is nearly identical to prototype's,
  // except it doesn't extend the returned element(s) with prototype specific Element extensions.
  function $(element) {
    if (arguments.length > 1) {
      for (var i = 0, elements = [], len = arguments.length; i < len; i++)
        elements.push($(arguments[i]));
      return elements;
    }
    if (typeof element == 'string')
      element = document.getElementById(element);
    return element;
  }
}

if (typeof Element == "undefined") {
  Element = {};
}

if (typeof Element.hide == "undefined") {
  // There is no function Element.hide, so define it. This function is identical to prototype's
  Element.hide = function(element) {
    $(element).style.display = 'none';
    return element;
  }
}

if (typeof Element.show == "undefined") {
  // There is no function Element.show, so define it. This function is identical to prototype's
  Element.show = function(element) {
    $(element).style.display = '';
    return element;
  }
}

if (typeof Element.visible == "undefined") {
  // There is no function Element.visible, so define it. This function is identical to prototype's
  Element.visible = function(element) {
    return $(element).style.display != 'none';
  }
}

if (typeof Element.toggle == "undefined") {
  // There is no function Element.toggle, so define it. This function is identical to prototype's
  Element.toggle = function(element) {
    element = $(element);
    Element[Element.visible(element) ? 'hide' : 'show'](element);
    return element;
  }
}

function getRef(sId){
  if(document && document.getElementById) {
    aRef = document.getElementById(sId);
    if (aRef)
      return aRef;
  }
}

function selectTab(sTabId, arrayOfTabIDs, parentNode, activeTabClass, tabClass, listPrefix) {


	for(i = 0; i < arrayOfTabIDs.length; i++) {
			Element.hide(arrayOfTabIDs[i]);
			aTabLi = getRef(listPrefix + "_" + arrayOfTabIDs[i]);
			if(aTabLi) {
				aTabLi.className = tabClass;
			}

	}
	parentNode.className = activeTabClass;
	Element.show(sTabId);
	return false;
}

function toggle_box(id, activeTabClass, tabClass, prefix) {
	aToggleNode = Element.toggle(id);
	if (aToggleNode!=null && activeTabClass!=null) {
		aSelectNode = getRef(prefix + "_" + id);
		if (aSelectNode) {
			if (Element.visible(id))
				aSelectNode.className=activeTabClass;
			else
				aSelectNode.className=tabClass;
		}
	}
	return false;
}

/* Interfaces */
function an_toggle(id, activeTabClass, tabClass, prefix)
	{ return toggle_box(id, activeTabClass, tabClass, prefix); }

function an_sort(id, col)
	{ return sorteer(id, col); }

function an_selectTab(sTabId, arrayOfTabIDs, parentNode, activeTabClass, tabClass, listPrefix)
	{ return selectTab(sTabId, arrayOfTabIDs, parentNode, activeTabClass, tabClass, listPrefix);}

function an_searchField(field, prefill) {
	if (field.value == prefill) field.value = '';
	else if (field.value == '') field.value = prefill;
}

/* TODO: more checks! and proper resizing the overlay */
function an_displayLayer(layer, overlay) {
	document.getElementById(layer).style.display='block';
	document.getElementById(overlay).style.display='block';
}

function an_hideLayer(layer, overlay) {
	document.getElementById(layer).style.display='none';
	document.getElementById(overlay).style.display='none';
}