/// Dropit Script Library functions
/// uses X, partially included below, cross-browser library (cross-browser.com)
///
///	Include this with: UI.ClientScriptUtil.RegisterFoundationLibrary(this.Page);

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);
}

function addHoverClassName(el, c)
{
	el = xGetElementById(el);
	if ( el )
	{
		xAddEventListener(el,"mouseover",function(){appendClassName(el,c);});
		xAddEventListener(el,"mouseout",function(){removeClassName(el,c);});
	}
}

function getAttribute(el, name)
{
    var attr = el.getAttribute(name);
    return attr ? attr : el[name];
}

function unselectable(el)
{
	el = xGetElementById(el);
	if (el)
	{
		el.style.MozUserSelect = "none";
		el.style.KhtmlUserSelect = "none";
		el.style.userSelect = "none";
		el.unselectable = "on"; 
	}
}

function setOverflow(el, y, x)
{
	el = xGetElementById(el);
	if (el)
	{
		if (y&&x)
		{
			el.style.overflow = "auto";
		}
		else if (y)
		{
			if (xGecko) el.style.overflow = "-moz-scrollbars-vertical";
			el.style.overflowY = "auto";
			el.style.overflowX = "hidden";
		}
		else if (x)
		{
			if (xGecko) el.style.overflow = "-moz-scrollbars-horizontal";
			el.style.overflowX = "auto";
			el.style.overflowY = "hidden";
		}
		else 
		{
			el.style.overflow = "hidden";
		}
	}
}

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 dispatchEvent(el, name, bubbles, cancelable) 
{
	bubbles = bubbles || true;
	cancelable = cancelable || true;
	
	if ( document.createEvent && el.dispatchEvent ) 
	{
		var e = document.createEvent("HTMLEvents");
		e.initEvent(name, bubbles, cancelable); 
		el.dispatchEvent(e); 
	} 
	else if (el.fireEvent)  
	{ 
		var e = document.createEventObject();
		e.cancelBubble = bubbles;
		el.fireEvent("on" + name, e); 
	}
	else if ( eval("el.on" + name) ) 
	{ 
		eval("el.on" + name + "()"); 
	}
}


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); 
}

/**************************************
/* ARRAY EXTENSIONS
/**************************************/
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;
}


/**************************************
/* STRING EXTENSIONS
/**************************************/
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;
}

/**************************************
/* POP-UP FUNCTIONS
/**************************************/
var _POPUP_FEATURES = "";

// pops up a window containing url optionally named target, optionally having features
function raw_popup(url, target, features) {
   	// Set parent window name - for easy reference from popup
   	if ( window.name.length <= 0 )
	   	window.name = "WindowOpener";

    if ( !xDef(features) ) {
		features = _POPUP_FEATURES;
	}
    if ( !xDef(target) ) {
		target   = "_blank";
	}
	if ( xDef(currentSite) )
		url = addQueryString(url,"site",currentSite);
		
    var popupWindow = window.open(url, target, features);
    if ( popupWindow != null )
	    popupWindow.focus();
    return popupWindow;
}

function link_popup(src, features) {
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}


function event_popup(e) 
{
    link_popup( findParentLink(e.target || event.srcElement) );
    xPreventDefault(e);
}

function event_popup_features(features) 
{
    return function(e) { link_popup( findParentLink(e.target || event.srcElement), features); xPreventDefault(e); };
}

function findParentLink(el)
{
	if (el == null) return null;
	if ( el.tagName == "A" ) return el;
	return findParentLink(el.parentNode);
}

function linksAsPopup(target, features) 
{
	var callback = function(el) { xAddEventListener(el, "click", event_popup_features(features)); };
	xGetElementsByAttribute("a", "target", "^" + target + "$", callback);
}

/**************************************
/* POSITION FUNCTIONS
/**************************************/

function absoluteLeft(el)
{
	return el ? xOffsetLeft(el) + absoluteLeft(el.offsetParent) : 0;
}

function absoluteTop(el)
{
	return el ? xOffsetTop(el) + absoluteTop(el.offsetParent) : 0;
}

/* 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;

// xAddEventListener, 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 xAddEventListener(e,eT,eL,cap)
{
  if(!(e=xGetElementById(e))) return;
  eT=eT.toLowerCase();
  if((!xIE4Up && !xOp7Up) && e==window) {
    if(eT=='resize') { window.xPCW=xClientWidth(); window.xPCH=xClientHeight(); window.xREL=eL; xResizeEvent(); return; }
    if(eT=='scroll') { window.xPSL=xScrollLeft(); window.xPST=xScrollTop(); window.xSEL=eL; xScrollEvent(); return; }
  }
  var eh='e.on'+eT+'=eL';
  if(e.addEventListener) e.addEventListener(eT,eL,cap);
  else if(e.attachEvent) e.attachEvent('on'+eT,eL);
  else eval(eh);
}
// called only from the above
function xResizeEvent()
{
  if (window.xREL) setTimeout('xResizeEvent()', 250);
  var cw = xClientWidth(), ch = xClientHeight();
  if (window.xPCW != cw || window.xPCH != ch) { window.xPCW = cw; window.xPCH = ch; if (window.xREL) window.xREL(); }
}
function xScrollEvent()
{
  if (window.xSEL) setTimeout('xScrollEvent()', 250);
  var sl = xScrollLeft(), st = xScrollTop();
  if (window.xPSL != sl || window.xPST != st) { window.xPSL = sl; window.xPST = st; if (window.xSEL) window.xSEL(); }
}

// xClientHeight, 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 xClientHeight()
{
  var h=0;
  if(xOp6Dn) h=window.innerHeight;
  else if(document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientHeight)
    h=document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    h=document.body.clientHeight;
  else if(xDef(window.innerWidth,window.innerHeight,document.width)) {
    h=window.innerHeight;
    if(document.width>window.innerWidth) h-=16;
  }
  return h;
}

// xClientWidth, 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 xClientWidth()
{
  var w=0;
  if(xOp6Dn) w=window.innerWidth;
  else if(document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientWidth)
    w=document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    w=document.body.clientWidth;
  else if(xDef(window.innerWidth,window.innerHeight,document.height)) {
    w=window.innerWidth;
    if(document.height>window.innerHeight) w-=16;
  }
  return w;
}


// 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;
}

// xDeleteCookie, 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 xDeleteCookie(name, path)
{
  if (xGetCookie(name)) {
    document.cookie = name + "=" +
                    "; path=" + ((!path) ? "/" : path) +
                    "; expires=" + new Date(0).toGMTString();
  }
}

// xDisplay, Copyright 2003,2004,2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xDisplay(e,s)
{
  if(!(e=xGetElementById(e))) return null;
  if(e.style && xDef(e.style.display)) {
    if (xStr(s)) e.style.display = s;
    return e.style.display;
  }
  return null;
}

// xGetCookie, 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 xGetCookie(name)
{
  var value=null, search=name+"=";
  if (document.cookie.length > 0) {
    var offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      value = unescape(document.cookie.substring(offset, end));
    }
  }
  return value;
}

// 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();
}

// xInnerHtml, 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 xInnerHtml(e,h)
{
  if(!(e=xGetElementById(e)) || !xStr(e.innerHTML)) return null;
  var s = e.innerHTML;
  if (xStr(h)) {e.innerHTML = h;}
  return s;
}

// xNum, 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 xNum()
{
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}

// 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;
}

// xSetCookie, 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 xSetCookie(name, value, expire, path)
{
  document.cookie = name + "=" + escape(value) +
                    ((!expire) ? "" : ("; expires=" + expire.toGMTString())) +
                    "; path=" + ((!path) ? "/" : path);
}

function xStopPropagation(evt)
{
  if (evt && evt.stopPropagation) evt.stopPropagation();
  else if (window.event) window.event.cancelBubble = true;
}

// xStr, 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 xStr(s)
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}

