floaterFx = false;
splashCt = 0;
splashRunLoop = true;
splashCurCt = -1;
function initSplash() {
  bx = $('splashBox').getCoordinates();
  xmultiplier = bx['width'];
  imgArray = new Array();
  linkArray = new Array();
  i = 0;
  $$('ul#splash li').each(function(item){
    item.set('rel',i); item.set('id','sb'+i); i++;
    if (item.getElement('img')) {
      thisImg = item.getElement('img').get('src');
      imgArray.push(thisImg);
      thisLink = false;
      if (item.getElement('a')) {
		thisLink = item.getElement('a').get('href');
      }
      linkArray.push(thisLink);
      linkArray.push(thisLink);
      item.addEvent('click',function(){
        splashRun(item);
        /*
        imgIndex = item.get('rel');
        if (floaterFx) { floaterFx.cancel(); }
        gc = item.getCoordinates();
        trgLeft = gc['left'];
        gc = $('splash').getCoordinates();
        trgLeft = (trgLeft - gc['left']);
        var floaterFx = new Fx.Tween($('floater'), {duration: 500, transition:'quad:in:out', unit:'px'}).start('left',trgLeft);
        var floaterFx = new Fx.Tween($('xImages'), {duration: 500, transition:'quad:in:out', unit:'px'}).start('left',(0 - (xmultiplier * imgIndex)));
        */
      });
    }
    else {
      item.destroy();
    }
  });
  sBox = new Element('ul').set('id','xImages').setStyle('width',(xmultiplier * i)+'px');
  new Element('li').set('id','floater').inject($('splash'));
  for (i=0; i<imgArray.length; i++) {
    newLi = new Element('li').set('id','x'+i);
    if (linkArray[i]) {
      newHref = new Element('a').set('href',linkArray[i]);
      new Element('img').set('src',imgArray[i]).inject(newHref);
      newHref.inject(newLi);
    }
    else {
      new Element('img').set('src',imgArray[i]).inject(newLi);    
    }
    newLi.inject(sBox);
  }
  sBox.inject($('splashBox'));
  sBox.addEvent("mouseover",function(){ splashRunLoop = false; });
  sBox.addEvent("mouseout",function(){ splashRunLoop = true; });
  splashCt = (imgArray.length - 1);
  splashLoop();
}
function splashLoop() {
  if (splashCt) {
    if (splashRunLoop) {
      splashCurCt++;
      if (splashCurCt > splashCt) {
        splashCurCt = 0;
      }
      splashRun($('sb'+splashCurCt));
    }
    setTimeout("splashLoop();",4000);
   }
}
function splashRun(item) {
  imgIndex = item.get('rel');
  splashCurCt = imgIndex;
  if (floaterFx) { floaterFx.cancel(); }
  gc = item.getCoordinates();
  trgLeft = gc['left'];
  gc = $('splash').getCoordinates();
  trgLeft = (trgLeft - gc['left']);
  var floaterFx = new Fx.Tween($('floater'), {duration: 500, transition:'quad:in:out', unit:'px'}).start('left',trgLeft);
  var floaterFx = new Fx.Tween($('xImages'), {duration: 500, transition:'quad:in:out', unit:'px'}).start('left',(0 - (xmultiplier * imgIndex)));
}

function URLEncode(str) {
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";	// x-www-urlencoded, rather than 
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert("Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
}

function rn() {
  return Math.floor(Math.random()*999999999);
}

function toggleChk(elem) {
  elem = $(elem);
  if (elem) {
    if (elem.checked) { elem.checked = false; }
    else { elem.checked = true; }
  }
}

function toggleElem(elem) {
  elem = $(elem);
  if (elem) {
    if (elem.style.display == "none") {
      elem.setStyle('display','block');
    }
    else {
      elem.setStyle('display','none');
    }
  }
}

