/// Script Library functions
/// uses X, partially included below, cross-browser library (cross-browser.com)
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

function getFormElementByName(name)
{
	var form = document.forms[0];
	return form != null ? form.elements[name] : null;
}

function hasClass(el,c) 
{
	var re = new RegExp('\\b'+c+'\\b', 'i');
	var el = xGetElementById(el);
    return el && re.test(el.className);
}

function removeClassName(el,c)
{
	el = xGetElementById(el);
	if ( el && c != null && c.length > 0 && el.className.length > 0 )
	{
		var re = new RegExp('\\b'+c+'\\b', 'ig');
		el.className = el.className.replace(re, "").trim();
	}
}

function appendClassName(el,c) 
{
	el = xGetElementById(el);
	if ( !hasClass(el) ) { el.className = (el.className + " " + c).trim();}
}

function toggleClassName(el, c)
{
	el = xGetElementById(el);
	if ( hasClass(el, c) ) 
		removeClassName(el, c);
	else
		appendClassName(el, c);
}

/// Adds class name on hover & focus for IE that doesn't have :hover, :focus
function addHoverClassName(el, c, focus)
{
	el = xGetElementById(el);
	if ( el && el.attachEvent )
	{
		el.attachEvent("onmouseover",function(){appendClassName(el,c);});
		el.attachEvent("onmouseout",function(){removeClassName(el,c);});
		if ( focus )
		{
			el.attachEvent("onfocus",function(){appendClassName(el,c);});
			el.attachEvent("onblur",function(){removeClassName(el,c);});
		}
	}
}

function multipleAddHoverClassName(parent,tag,className)
{
	parent = xGetElementById(parent);
	if ( parent && parent.attachEvent)
	{
		var items = xGetElementsByTagName(tag,parent);
		map(items, function(el) { addHoverClassName(el, className); })
	}
}

function getAttribute(el, name)
{
    var attr = el.getAttribute(name);
    return attr ? attr : el[name];
}

function setOpacity(el,val) {
    if (el.filters) { try { el.filters['alpha'].opacity = val*100; } catch(e){} } 
    else if (el.style.opacity) { el.style.opacity = val; }
}

function addQueryString(url,name,val)
{
	// QueryString is empty
	if ( url.indexOf('?') < 0 )
		return url + "?" + name + "=" + val;
	
	// Check if name exists
	if ( url.indexOf("?"+name+"=") < 0 && url.indexOf("&"+name+"=") < 0 )
		return url + "&" + name + "=" + val;

	var replaceName = url.indexOf("?"+name+"=") < 0 ? "&"+name+"=" : "?"+name+"=";

	var start	= url.indexOf(replaceName);
	var end		= url.indexOf('&', start + replaceName.length);

	if (end < 0) // Value was empty 
		return url.slice(0, start + replaceName.length) + val;

	return url.slice(0, start + replaceName.length) + val + url.slice(end); 
}

function getFirstElementByTagName(t,p)
{
	var arr = xGetElementsByTagName(t,p);
	return arr != null && arr.length > 0 ? arr[0] : null;
}

/**************************************
/* PROTOTYPES
/**************************************/
if (!Array.prototype.push) Array.prototype.push = function() 
{
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

if (!Array.prototype.find) Array.prototype.find = function(value, start) 
{
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

if (!Array.prototype.has) Array.prototype.has = function(value) 
{
    return this.find(value)!==-1;
}

if (!String.prototype.trim) String.prototype.trim = function() { 
	return this.replace( /^\s+|\s+$/, "" ); 
}

/**************************************
/* FUNCTIONAL
/**************************************/
function map(arr, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < arr.length; i++) result.push(func(arr[i], i, arr));
    return result;
}

function filter(arr, func) {
    var result = [];
    func = func || function(v) {return v};
    map(arr, function(v) { if (func(v)) result.push(v) } );
    return result;
}

/* x.js compiled from X 4.0 with XC 0.27b. Distributed by GNU LGPL. For copyrights, license, documentation and more visit Cross-Browser.com */
// globals, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
var xVersion = "4.0";

var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xNN4,xGecko,xUA=navigator.userAgent.toLowerCase();
if(window.opera){
  var i=xUA.indexOf('opera');
  if(i!=-1){
    var v=parseInt(xUA.charAt(i+6));
    xOp7Up=v>=7;
    xOp6Dn=v<7;
  }
}
else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1){
  xIE4Up=parseFloat(navigator.appVersion)>=4;
  xIE4=xUA.indexOf('msie 4')!=-1;
  xIE5=xUA.indexOf('msie 5')!=-1;
}
else if(document.layers){xNN4=true;}
xMac=xUA.indexOf('mac')!=-1;

xGecko=xUA.indexOf('gecko')!=-1;


// xDef, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

// xGetElementById, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementById(e)
{
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

// xGetElementsByAttribute, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementsByAttribute(sTag, sAtt, sRE, fn)
{
  var a, list, found = new Array(), re = new RegExp(sRE, 'i');
  list = xGetElementsByTagName(sTag);
  for (var i = 0; i < list.length; ++i) {
    a = list[i].getAttribute(sAtt);
    if (!a) {a = list[i][sAtt];}
    if (typeof(a)=='string' && a.search(re) != -1) {
      found[found.length] = list[i];
      if (fn) fn(list[i]);
    }
  }
  return found;
}

// xGetElementsByClassName, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementsByClassName(c,p,t,f)
{
  var found = new Array();
  var re = new RegExp('\\b'+c+'\\b', 'i');
  var list = xGetElementsByTagName(t, p);
  for (var i = 0; i < list.length; ++i) {
    if (list[i].className && list[i].className.search(re) != -1) {
      found[found.length] = list[i];
      if (f) f(list[i]);
    }
  }
  return found;
}

// xGetElementsByTagName, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementsByTagName(t,p)
{
  var list = null;
  t = t || '*';
  p = p || document;
  if (xIE4 || xIE5) {
    if (t == '*') list = p.all;
    else list = p.all.tags(t);
  }
  else if (p.getElementsByTagName) list = p.getElementsByTagName(t);
  return list || new Array();
}

// xPreventDefault, Copyright 2004,2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xPreventDefault(e)
{
  if (e && e.preventDefault) e.preventDefault();
  else if (window.event) window.event.returnValue = false;
}

function xStopPropagation(evt)
{
  if (evt && evt.stopPropagation) evt.stopPropagation();
  else if (window.event) window.event.cancelBubble = true;
}
