//
// ZEN LIBRARY 1.0
// Copyright 2010, ZenWebware
// www.zenwebware.com
//


//
// Javascript extension functions.
//
document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b' + cl + '\\b');
    var elem = this.getElementsByTagName('*');
    for(var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if(myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
}; 


//
// Layer functions.
//
function show_layer(div_id) {
	var div = document.getElementById(div_id);
	div.style.display = "block";
}


function hide_layer(div_id) {
	var div = document.getElementById(div_id);
	div.style.display = "none";
}


function show_layers(div_class) {
	var divs = document.getElementsByClassName(div_class);
	for(var i = 0; i < divs.length; i++) {
		divs[i].style.display = "block";
	}
}


function hide_layers(div_class) {
	var divs = document.getElementsByClassName(div_class);
	for(var i = 0; i < divs.length; i++) {
		divs[i].style.display = "none";
	}
}


//
// Window helper functions.
//
function f_clientWidth() {
    return f_filterResults (
    	window.innerWidth ? window.innerWidth : 0,
    	document.documentElement ? document.documentElement.clientWidth : 0,
    	document.body ? document.body.clientWidth : 0
    );
}


function f_clientHeight() {
    return f_filterResults (
    	window.innerHeight ? window.innerHeight : 0,
    	document.documentElement ? document.documentElement.clientHeight : 0,
    	document.body ? document.body.clientHeight : 0
    );
}


function f_scrollLeft() {
    return f_filterResults (
    	window.pageXOffset ? window.pageXOffset : 0,
    	document.documentElement ? document.documentElement.scrollLeft : 0,
    	document.body ? document.body.scrollLeft : 0
    );
}


function f_scrollTop() {
    return f_filterResults (
    	window.pageYOffset ? window.pageYOffset : 0,
    	document.documentElement ? document.documentElement.scrollTop : 0,
    	document.body ? document.body.scrollTop : 0
    );
}


function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
    	n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function page_width() {
    if(window.innerHeight && window.scrollMaxY) { // Firefox
        return window.innerWidth + window.scrollMaxX;
    } else if(document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        return document.body.scrollWidth;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        return document.body.offsetWidth + document.body.offsetLeft;
    }
}


function page_height() {
    if(window.innerHeight && window.scrollMaxY) { // Firefox
        return window.innerHeight + window.scrollMaxY;
    } else if(document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        return document.body.scrollHeight;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        return document.body.offsetHeight + document.body.offsetTop;
    }
}


//
// XML HTTP functions.
//
var xmlhttp;
var xmlhttp_div_id;
var xmlhttp_loading_div_id;


function load_xml(url, div_id, loading_div_id) {
	xmlhttp = null;
	xmlhttp_div_id = div_id;
	xmlhttp_loading_div_id = loading_div_id;
	if(window.XMLHttpRequest) {
		// code for all new browsers
		xmlhttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		// code for IE5 and IE6
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xmlhttp != null) {
		var div = document.getElementById(xmlhttp_div_id);
		//div.innerHTML = "Loading ...";
		if(xmlhttp_loading_div_id != null) show_layer(xmlhttp_loading_div_id);
		xmlhttp.onreadystatechange = load_xml_state_change;
		xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}


function load_xml_state_change() {
	if(xmlhttp.readyState == 4) {
		// 4 = "loaded"
		if(xmlhttp.status == 200) {
			// 200 = OK
			// ...our code here...
			var div = document.getElementById(xmlhttp_div_id);
			div.innerHTML = xmlhttp.responseText;
		} else {
			//alert("Problem retrieving XML data");
		}
		if(xmlhttp_loading_div_id != null) hide_layer(xmlhttp_loading_div_id);
	}
}


//
// Cookie functions.
//
function save_cookie(name, value) {
    document.cookie = name + "='" + value + "'";
}


function load_cookie(name) {
    var cookie_str = document.cookie;
    var start_pos = cookie_str.indexOf(name);
    if(start_pos != -1) {
        start_pos = start_pos + name.length + 2;
        var end_pos = cookie_str.indexOf("'", start_pos);
        return cookie_str.substring(start_pos, end_pos);
    } else {
        return false;
    }
}


//
// Scroll functions.
//
function save_scroll_position() {
    save_cookie("scroll_position", f_scrollTop());
}


function load_scroll_position() {
    scroll_pos = load_cookie("scroll_position");
    if(scroll_pos) {
    	window.pageYOffset ? window.pageYOffset = scroll_pos : 0,
    	document.documentElement ? document.documentElement.scrollTop = scroll_pos : 0,
    	document.body ? document.body.scrollTop = scroll_pos : 0
    }
}

