/**
* Returns DOM-node(s) by id(s)
* Example:
*   var obj = $('divMsg');
*   obj.innerHTML = 'Hello World!';
*/
if (typeof($) == "undefined") {
	$ = function(id) { // Strong Recommended
		if (arguments.length > 1) {
			for (var i = 0, elements = [], length = arguments.length; i < length; i++)
				elements.push($(arguments[i]));
			return elements;
		}
		if (typeof id == 'string')
			element = document.getElementById(id);
		return element;
	}
}
/**
* Returns array of DOM-nodes by tag name
* Example:
*   var objBody = $$('body')[0];
*/
if (typeof($$) == "undefined") {
	$$ = function(tagName) {
		return document.getElementsByTagName(tagName);
	}
}
// backward capability, 
function byid(id) {// Not recommended
  var obj = document.getElementById(id);
  return !obj ? false : obj;
}
function layerShow(id) {
    with ($(id).style) {display = "block"; visibility = "visible"; }
}
function layerHide(id) {
	with ($(id).style) {display = "none"; visibility = "hidden"; }
}

/**
* showBox is layerShow & layerHide in one
* optional boolShow, objArrowImg is interchangeable
*   showBox('divInfo');
*   showBox('myDiv','myArrowPic');
*   showBox('myDiv', false);
*   showBox('myDiv','myArrowPic',true);
*/
function showBox(obj, boolShow, objArrowImg) {
	if (typeof(obj) == "string") obj = $(obj);
	var show = img = false;
	show = typeof(boolShow) == "boolean" ? boolShow : typeof(objArrowImage) == "boolean" ? objArrowImg : (obj.style.display == "none");
	img = typeof(boolShow) == "string" ? $(boolShow) :
		typeof(boolShow) == "object" ? boolShow :
		typeof(objArrowImg) == "string" ? $(objArrowImg) :
		typeof(objArrowImg) == "object" ? objArrowImg :
		false;
	if (show) {
		obj.style.display = "";
		if (img) img.src = "images/arr_down.png"; // ?
	} else {
		obj.style.display = "none";
		if (img) img.src = "images/arr_right.png";
	}
}
/**
 * Override window "onload" function
 * Example:
 *   addLoadEvent(myOnLoad);
 *   function myOnLoad() { alert("Page loaded!"); }
 */
function addLoadEvent(func) { // Strong recommended
  if (window.addEventListener) 
    window.addEventListener("load", func, false);
  else if (window.attachEvent) 
    window.attachEvent("onload", func);
  else window.onload = func;
}
/**
 * Checks all checkbox on form (exc. "allbox")
 */
function checkAll(form, name) {
  if (typeof(form) == "string") form = $(form);
  for (var element = 0; element < form.elements.length; element++) {
    var checkbox = form.elements[element];
    if (checkbox.type == 'checkbox' && checkbox.name.substr(0, 6) != 'allbox' && (!name || (name && checkbox.name.substr(0, name.length) == name)))
	{
      checkbox.checked = !checkbox.checked;
    }
  }
}
/*-------------------- JavaScript includes functions -----------------------*/

/**
* Returns basepath for current script (dirname(__FILE__).'/');
*/
function getPath() {
  // Get last script element
  var objContainer = document.body;
  if (!objContainer) {
    objContainer = document.getElementsByTagName("head")[0];
    if (!objContainer)  objContainer = document;
  }
  var objScript = objContainer.lastChild;
  // Get path
  var path = "";
  var strSrc = objScript.getAttribute("src");
  if (strSrc) {
    var arrTokens = strSrc.split("/");
    // Remove last token
    arrTokens = arrTokens.slice(0, -1);
    if (arrTokens.length)
	  path = arrTokens.join("/") + "/";
  }
  return path;
}

/**
 * Simply writes script tag to the document. Checks if specified JS file is
 * already loaded unless boolForce argument is true.
 *
 * @param {string} strSrc Src attribute value of the script element
 * @param {boolean} boolForce Optional. Force reload if it is already loaded
 */
var isLoadedJS = [];
function include(strSrc, boolForce) { // Recommended
  // Check if it is already loaded
  if (!boolForce && isLoadedJS[strSrc]) {
    return;
  }
  // Include file
  document.write('<s' + 'cript type="text/javascript" src="' + strSrc +
   '"></s' + 'cript>');
  // Add this URL to the list of loaded
  isLoadedJS[strSrc] = true;
};

/**
 * Includes JS file into the page. Allows URLs from foreign domains.
 * Notice that script is loaded asynchronously.
 *
 * @param {string} strSrc Src attribute value of the script element
 * @param {boolean} boolForce Optional. Force reload if it is already loaded
 */
function includeJS(strSrc) {
  var objContainer = document.body;
  if (!objContainer) {
    objContainer = document.getElementsByTagName('head')[0];
    if (!objContainer) {
      objContainer = document;
    }
  }
  var objScript = document.createElement('script');
  objScript.type = 'text/javascript';
  objScript.src = strSrc;
  // It's important for Safari to assign attributes before appending
  objContainer.appendChild(objScript);
  loadedJS[strSrc] = true;
};
/**
 * Includes CSS file into the page. Allows URLs from foreign domains.
 *
 * @param {string} strHref Href attribute value of the link element
 */
function includeCSS(strHref) {
  // May appear only inside head
  var objContainer = document.getElementsByTagName('head')[0];
  if (!objContainer) {
    return;
  }
  var objLink = document.createElement('link');
  objLink.setAttribute('rel', 'stylesheet');
  objLink.setAttribute('type', 'text/css');
  objLink.setAttribute('href', strHref);
  objContainer.appendChild(objLink);
}

/*---------------- Window functions ----------------*/
// v2.0 Refresh window, ex. refreshWin();
function refreshWin(win) {
  if (typeof(win) == "undefined") win = window;
  var url = win.location.href;
  var hash_pos = url.indexOf("#");
  if (hash_pos > 0) url = url.substring(0, hash_pos);
  win.location.href = url;
}
// Go to URL, ex. redirect('http://www.google.com');
function redirect(url) {
  document.location.href = url;
}
// Open popup window, ex. openWin("index2.html");
var popUpWin=0;
function openWin(URLStr, width, height) {
  if (popUpWin) {
    if(!popUpWin.closed) popUpWin.close();
  }
  width = (typeof(width) == "undefined") ? 400 : width;
  height = (typeof(height) == "undefined") ? 300 : height;
  x = Math.round((screen.width - width)/2);
  y = Math.round((screen.height - height)/2);
  popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,width='+width+',height='+height+',left='+x+',top='+y);
  popUpWin.focus();
  return popUpWin;
}

//----- UTIL
/**
* Returns element visual info
*/
function getSizePos(id) {
	var el = $(id);
	var p = getAbsolutePos(el); // see below
	return {
		x: p.x,
		y: p.y,
		w: el.offsetWidth,
		h: el.offsetHeight
	};
}
function getAbsolutePos(el, scrollOff) {
	if (typeof(el) == "string") el = document.getElementById(el);
	var SL = 0, ST = 0;
	if (!scrollOff) {
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
	}
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};
if (!window.console) window.console = {log: function(x) {}};