// Copyright (c) 2007 Jakub Argasinski (jakub.argasinski@escsa.pl).
// Last modified: śro 20 cze 2007 16:25:49 CEST
// v0.02

if (typeof Vela=="undefined") {	Vela={} }

Vela.agent = Array();
Vela.agent['name'] = navigator.userAgent.toLowerCase();
Vela.agent['major'] = parseInt(navigator.appVersion);
Vela.agent['minor'] = parseFloat(navigator.appVersion);
Vela.agent['ie'] = ((Vela.agent['name'].indexOf("msie") != -1) && (Vela.agent['name'].indexOf("opera") == -1));
Vela.agent['ie5']  = (Vela.agent['ie'] && (Vela.agent['major'] == 4) && (Vela.agent['name'].indexOf("msie 5.0") != -1));
Vela.agent['ie55'] = (Vela.agent['ie'] && (Vela.agent['major'] == 4) && (Vela.agent['name'].indexOf("msie 5.5") != -1));
Vela.agent['ie6']  = (Vela.agent['ie'] && (Vela.agent['major'] == 4) && (Vela.agent['name'].indexOf("msie 6.") != -1));
Vela.agent['ie7']  = (Vela.agent['ie'] && (Vela.agent['name'].indexOf("msie 7.") != -1));
Vela.agent['gecko'] = (Vela.agent['name'].indexOf('gecko') != -1);
Vela.agent['opera'] = (Vela.agent['name'].indexOf("opera") != -1);

//
// Metoda ta pobiera wszystkie obiekty typu <a> (anchor) w dokumencie
// i sprawdza, czy posiadaja one atrybut 'rel' rowny 'external'. Jezeli
// tak, przypisuja atrybutowi 'target' anchora wartosc '_blank',
// powodujac, ze link taki otwiera sie w nowym oknie przegladarki.
//
// @return    true, jesli znaleziono jakies anchory, w przeciwnym razie
//            false.
//
Vela.externalAnchors = function() {
	var anchors = document.getElementsByTagName("a");
	if (!anchors.length) return false;
	for (var i=0; i < anchors.length; i++) {
		var a = anchors[i];
		if (a.getAttribute("href") && a.getAttribute("rel") == "external") a.target = "_blank";
	}
	return true;
};

Vela.blurAnchors = function() {
	var anchors = document.getElementsByTagName("a");
	if (!anchors.length) return false;
	for (var i=0; i < anchors.length; i++)
		anchors[i].onfocus = function () { this.blur(); }
	return true;
};

Vela.focusAnchors = function() {
	var anchors = document.getElementsByTagName("a");
	if (!anchors.length) return false;
	for (var i=0; i < anchors.length; i++)
		anchors[i].onfocus = function () { this.focus(); }
	return true;
};



Vela.addEventListener = function(event, func) {
    // Gecko, Opera8
    if (typeof window.addEventListener != "undefined") {
        window.addEventListener(event, func, false);
    // Opera 7
    } else if (typeof document.addEventListener != "undefined") {
        document.addEventListener(event, func, false);
    // IE, Opera7 and older
    } else if (typeof window.attachEvent != "undefined") {
        window.attachEvent('on'+event, func);
    }
};

Vela.removeEventListener = function(event, func) {
    // Gecko, Opera8
    if (typeof window.removeEventListener != "undefined") {
        window.removeEventListener(event, func, false);
    // Opera 7
    } else if (typeof document.addEventListener != "undefined") {
        document.removeEventListener(event, func, false);
    // IE, Opera7 and older
    } else if (typeof window.attachEvent != "undefined") {
        window.detachEvent('on'+event, func);
    }
};


//
// Throughout, whitespace is defined as one of the characters
//  "\t" TAB \u0009
//  "\n" LF  \u000A
//  "\r" CR  \u000D
//  " "  SPC \u0020
//
// This does not use Javascript's "\s" because that includes non-breaking
// spaces (and also some other characters).
//


//
// Determine whether a node's text content is entirely whitespace.
//
// @param nod  A node implementing the |CharacterData| interface (i.e.,
//             a |Text|, |Comment|, or |CDATASection| node
// @return     True if all of the text content of |nod| is whitespace,
//             otherwise false.
//
Vela.isAllWhitespace = function(nod) {
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
};


//
// Determine if a node should be ignored by the iterator functions.
//
// @param nod  An object implementing the DOM1 |Node| interface.
// @return     true if the node is:
//                1) A |Text| node that is all whitespace
//                2) A |Comment| node
//             and otherwise false.
//

Vela.isIgnorable = function(nod) {
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && this.isAllWhitespace(nod) ); // a text node, all ws
};

Vela.parentNode = function(nod) {
	return nod.parentNode;
};

Vela.getObject = function(name) {
	return document.getElementById(name);
};


Vela.dumpObject = function(o, indent) {
	var l = new String();
	if (typeof indent == 'undefined') indent = 0;
	if (indent > 3) return;
	var i = new String();
	for (k=0; k < indent; k++) i += ' ';
	if (typeof o != 'string') {
		for (var k in o) {
			switch (typeof o[k]) {
				case 'array':
				case 'object': l += i+k+': '+o[k]+'\n'+this.dumpObject(o[k], indent+2); break;
				case 'function': l += i+'function '+k+'()\n'; break;
				case 'string': l += i+k+': \''+o[k]+'\'\n'; break;
				
				default: l += i+k+': '+o[k]+'\n';
			}
		}
	} else {
		l += i+o+'\n';
	}
	return l;
};


//
// Version of |previousSibling| that skips nodes that are entirely
// whitespace or comments.  (Normally |previousSibling| is a property
// of all DOM nodes that gives the sibling node, the node that is
// a child of the same parent, that occurs immediately before the
// reference node.)
//
// @param sib  The reference node.
// @return     Either:
//               1) The closest previous sibling to |sib| that is not
//                  ignorable according to |velaIsIgnorable|, or
//               2) null if no such node exists.
//
Vela.previousSibling = function(sib) {
  while ((sib = sib.previousSibling)) {
    if (!this.isIgnorable(sib)) return sib;
  }
  return null;
};

//
// Version of |nextSibling| that skips nodes that are entirely
// whitespace or comments.
//
// @param sib  The reference node.
// @return     Either:
//               1) The closest next sibling to |sib| that is not
//                  ignorable according to |Vela.isIgnorable|, or
//               2) null if no such node exists.
//
Vela.nextSibling = function(sib) {
  while ((sib = sib.nextSibling)) {
    if (!this.isIgnorable(sib)) return sib;
  }
  return null;
};

//
// Version of |lastChild| that skips nodes that are entirely
// whitespace or comments.  (Normally |lastChild| is a property
// of all DOM nodes that gives the last of the nodes contained
// directly in the reference node.)
//
// @param sib  The reference node.
// @return     Either:
//               1) The last child of |sib| that is not
//                  ignorable according to |Vela.isIgnorable|, or
//               2) null if no such node exists.
//
Vela.lastChild = function(par) {
  var res=par.lastChild;
  while (res) {
    if (!this.isIgnorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
};

//
// Version of |firstChild| that skips nodes that are entirely
// whitespace and comments.
//
// @param sib  The reference node.
// @return     Either:
//               1) The first child of |sib| that is not
//                  ignorable according to |Vela.isIgnorable|, or
//               2) null if no such node exists.
//
Vela.firstChild = function(par) {
  var res=par.firstChild;
  while (res) {
    if (!this.isIgnorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
};
//
// Version of |data| that doesn't include whitespace at the beginning
// and end and normalizes all whitespace to a single space.  (Normally
// |data| is a property of text nodes that gives the text of the node.)
//
// @param txt  The text node whose data should be returned
// @return     A string giving the contents of the text node with
//             whitespace collapsed.
//
Vela.getData = function(txt) {
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
};

Vela.getStyle = function (el,styleProp) {
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
};


Vela.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

Vela.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

Vela.eraseCookie = function(name) {
	this.createCookie(name,"",-1);
};

Vela.getBrowserName = function() {
	if (Vela.agent['ie6']) return "IE6";
	if (Vela.agent['gecko']) return "Gecko";
	if (Vela.agent['ie7']) return "IE7";
	if (Vela.agent['opera']) return "Opera";
	if (Vela.agent['ie5']) return "IE5";
	if (Vela.agent['ie55']) return "IE55";
	return null;
};

Vela.getBrowserVersion = function() {
	return Vela.agent['major']+'.'+Vela.agent['minor'];
};


