// Browser ------------------------------------------------------------------------
var browserName=navigator.appName;
var browserVersion=parseFloat(navigator.appVersion);
if (browserName == "Netscape") {
    var browser = "NS";
} else {
    var browser = "IE";
}

// Misc ------------------------------------------------------------------------
function getAncestors(obj) {
    var parents=new Array();
    var position=0;

    while (obj.parentNode) {
	parents[position++]=obj.parentNode;
	obj=obj.parentNode;
    }

    return parents;
}

function isDescendant(oAncestor, oDescendant) {
    var parents=getAncestors(oDescendant);
    for (index in parents) {
	if (parents[index] == oAncestor) {
	    return true;
	}
    }

    return false;
}


// Tables ------------------------------------------------------------------------
var tableStyles=new Array();
if (browser == "NS") {
    var tableDisplay="table";
} else {
    var tableDisplay="block";
}

function minimizeTable(tableName, temporary) {
    if (!document.getElementById(tableName)) return;
    restoreTableStyle(tableName);
    storeTableStyle(tableName);
    showSelectBoxes();
    if (!temporary) {
	persist(tableName, 't', 'min');
    }
    document.getElementById(tableName+'_placeholder').style.display="inline";
    document.getElementById(tableName+'_restore').style.display="inline";
    document.getElementById(tableName+'_maximize').style.display="inline";
    document.getElementById(tableName+'_minimize').style.display="none";

    document.getElementById(tableName).style.display="none";	
}

function restoreTable(tableName, temporary) {
    if (!document.getElementById(tableName)) return;

    restoreTableStyle(tableName);
    showSelectBoxes();

    if (!temporary) {
	persist(tableName, 't', '-');
    }

    document.getElementById(tableName+'_placeholder').style.display="none";
    document.getElementById(tableName+'_restore').style.display="none";
    document.getElementById(tableName+'_maximize').style.display="inline";
    document.getElementById(tableName+'_minimize').style.display="inline";

    document.getElementById(tableName).style.display=tableDisplay;	
}

function maximizeTable(tableName, temporary) {
    if (!document.getElementById(tableName)) return;

    storeTableStyle(tableName);
    hideSelectBoxes(document.getElementById(tableName));

    if (!temporary) {
	persist(tableName, 't', 'max');
    }

    document.getElementById(tableName+'_placeholder').style.display="none";
    document.getElementById(tableName+'_restore').style.display="inline";
    document.getElementById(tableName+'_maximize').style.display="none";
    document.getElementById(tableName+'_minimize').style.display="inline";

    document.getElementById(tableName).style.position="absolute";
    document.getElementById(tableName).style.zIndex="9999";
    document.getElementById(tableName).style.top="0";
    document.getElementById(tableName).style.left="0";
    document.getElementById(tableName).style.margin="0";
    document.getElementById(tableName).style.width=window.innerWidth || window.document.documentElement.clientWidth;
    document.getElementById(tableName).style.height=window.innerHeight || window.document.documentElement.clientHeight;
    scrollTo(0,0);
}

function storeTableStyle(tableName) {
    if (!document.getElementById(tableName)) return;

    var o=document.getElementById(tableName).style;

    tableStyles[tableName]=new Array();	
    tableStyles[tableName]['position']=o.position;
    tableStyles[tableName]['width']=o.width;
    tableStyles[tableName]['height']=o.height;
    tableStyles[tableName]['top']=o.top;
    tableStyles[tableName]['left']=o.left;
    tableStyles[tableName]['margin']=o.margin;
    tableStyles[tableName]['zIndex']=o.zIndex;
}

function restoreTableStyle(tableName) {
    if (!tableStyles[tableName]) return;
    if (!document.getElementById(tableName)) return;

    var o=document.getElementById(tableName).style;

    o.position=tableStyles[tableName]['position'];
    o.width=tableStyles[tableName]['width'];
    o.height=tableStyles[tableName]['height'];
    o.top=tableStyles[tableName]['top'];
    o.left=tableStyles[tableName]['left'];
    o.margin=tableStyles[tableName]['margin'];
    o.zIndex=tableStyles[tableName]['zIndex'];
}

// Persistor ------------------------------------------------------------------------

var persistor=new Array();

function persist(id, category, status) {
    persistor[id+':'+category]=status;
}

function savePersist(path) {
    try {
	var query=new Array();
	
	for (index in persistor) {
	    query.push(index+':'+persistor[index]);
	}

	var link=path+'/persist.php/'+query.join(",")+'?'+Math.random();

	page=new Image(1,1); 
	page.src=link;
    } catch(e) {
	// hmmm... @todo
    }
}


// IE hacks ========================================================================

/*
  Following methods address Microsoft's stupidity "<select> is Windowed element":
  http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/askgui05072002.asp
*/

function hideSelectBoxes(exceptDescendantsOf) {
    if (browser != 'IE') return;

    var ancestors;
    var aElements = document.getElementsByTagName('SELECT');

    for (var i = 0; i < aElements.length; i++) {
	if (isDescendant(exceptDescendantsOf, aElements[i])) continue;
	aElements[i].style.visibility = 'hidden';
    }
}

function showSelectBoxes() {
    if (browser != 'IE') return;

    var aElements = document.getElementsByTagName('SELECT');
    for (var i = 0; i < aElements.length; i++) {
	// if (isDescendant(exceptDescendantsOf, aElements[i])) continue;
	aElements[i].style.visibility = 'visible';
    }
}

// Absolute positioning ========================================================================

/**
 * Return absoltue X, Y position measured from the left top corner of the document.
 *
 * Example: 
 *   getPosition(divElement).x
 *   getPosition(divElement).y
 *
 * @param DOMElement node
 * @return array {x, y}
 */
function getPosition(node) {
    var x=0, y=0;
    while(node){
	x+=node.offsetLeft;
	y+=node.offsetTop;				
	node=node.offsetParent;
    }
    return {x: x, y: y};
}

/**
 * Make the element (what) absolutely positioned and move it over the
 * other element (where), align top left corner of both boxes. You can
 * position the element relatively to the other element by specifying
 * offset values.
 *
 * Example:
 *   alignBlock(table, link, link.clientWidth, 0); // Align the table with the right edge of the link
 *   
 * @param DOMElement what Element that will be absolutely positioned (it is advised to set the position:absolute on the element before calling alignBlock() to avoid reflow delay...)
 * @param DOMElement where Other element that we will position "what" relatively to.
 * @param int offsetX move the "what" element relatively to "where" by specified amount of pixels.
 * @param int offsetY move the "what" element relatively to "where" by specified amount of pixels. 
 * @return void
 */
function alignBlock(what, where, offsetX, offsetY) {
    offsetX=(isNaN(offsetX) ? 0 : parseInt(offsetX));
    offsetY=(isNaN(offsetY) ? 0 : parseInt(offsetY));		
		
    what.style.position='absolute';

    var x=getPosition(where).x - getPosition(what.offsetParent).x + offsetX;
    var y=getPosition(where).y - getPosition(what.offsetParent).y + offsetY;

    if ((where.tagName.toLowerCase() == 'input' && where.type.toLowerCase() == 'hidden') || (!x && !y)) { // Top left abs position - the "where" may have no position - hidden input field? - center
	var winSize=getWindowSize();
	x=(winSize.width - what.clientWidth) / 2 + document.documentElement.scrollLeft;
	y=(winSize.height - what.clientHeight) / 2 + document.documentElement.scrollTop;

	//alert('Window Size: '+winSize.width+'x'+winSize.height+', Final: '+x+'x'+y);
    } else {
	//alert('Final: '+x+'x'+y);
    }

    
    what.style.left=x + 'px';
    what.style.top=y + 'px';
}

function getWindowSize() {
    var windowWidth, windowHeight;
    if (self.innerHeight) {
	windowWidth = self.innerWidth;
	windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
	windowWidth = document.documentElement.clientWidth;
	windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
	windowWidth = document.body.clientWidth;
	windowHeight = document.body.clientHeight;
    }
    return {'width': windowWidth, 'height': windowHeight};
}

