/*
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

function initRollovers() {
	if (!document.getElementById) return

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}

			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function initOneRollover(id) {
	if (!document.getElementById) return
	var preLoad;;
	var sTempSrc;
	var image = document.getElementById(id);
  if (image == null) return false;

	if (image.className == 'imgover') {
		var src = image.getAttribute('src');
		var ftype = src.substring(src.lastIndexOf('.'), src.length);
		var hsrc = src.replace(ftype, '_o'+ftype);

		image.setAttribute('hsrc', hsrc);

		preLoad = new Image();
		preLoad.src = hsrc;

		image.onmouseover = function() {
			sTempSrc = this.getAttribute('src');
			this.setAttribute('src', this.getAttribute('hsrc'));
		}

		image.onmouseout = function() {
			if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
			this.setAttribute('src', sTempSrc);
		}
	}
}


/***************************************************************/
Prototype.preloadImages = function(){
	for(var i = 0, images = []; src = arguments[i]; i++){
		images.push(new Image());
		images.last().src = src;
	}
};
/***************************************************************/
// From: http://cass-hacks.com/articles/code/js_url_encode_decode/
function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

/***************************************************************/

Event.observe(window, 'load', function() {
  initRollovers();

	Prototype.preloadImages(
	  "images/menu-info_o.gif",
	  "images/menu-press_o.gif",
	  "images/menu-tickets_and_schedule_o.gif",
	  "images/menu-funders_and_partners_o.gif",
	  "images/menu-venue_info_o.gif",
	  "images/blip_festival_2008_logo_over.gif",
	  "images/blip_festival_2008_logo_out.gif"
	);

  var logo = $('logo');
  if (logo.tagName == 'A') {
    logo = logo.down();
    logo.observe('mouseover', function(e) {
      logo.src = "images/blip_festival_2008_logo_over.gif";
    });
    logo.observe('mouseout', function(e) {
      logo.src = "images/blip_festival_2008_logo_out.gif";
    });
  }
});
