function addElEvent(el, ev, func) {
  var oldFunc = el[ev];
  if (typeof el[ev] != 'function') {
    el[ev] = func;
  }
  else {
    el[ev] = function() {
      oldFunc(arguments);
      func(arguments);
    }
  }
}

function attachInputLabels() {
	var inputs = document.getElementsByTagName('input');
	for (var i=0;input=inputs[i];i++) {
	  if (hasClass(input, 'embed-label') && input.value == input.title) input.value = '';
	  if (hasClass(input, 'embed-label') && input.title && !input.value) {
	  	removeClass(input, 'embed-label');
	  	addClass(input, 'input-label');
      input.onfocus = inputFocus.bind(input);
      input.value = input.title;
	  }
	}
}

function attachPortalThumbs() {
	var imgs = document.getElementsByTagName('IMG');
	var portals = [];
	
	for (i = 0; img = imgs[i]; i++) {
  	if (img && hasClass(img, 'image-portal')) {
  		portals.push(img);
  	}
	}
	
//	portals.sort(randOrd);
	
	if ($('frontpage-logo')) {
	  var start = 2300;
	}
	else {
	  var start = 0;
	}
	
	for(i = 0; portal = portals[i]; i++) {
		var opacity = new fx.Opacity(portal, {duration: 3000});
		
		setTimeout(opacity.toggle.bind(opacity), start + (i*500));
		
		var load = new Image;
		load.src = states[portal.src];
	}
}

function attachToolTips() {
	var links = document.getElementsByTagName('A');
	
  for (i = 0; link = links[i]; i++) {
  	if (link && hasClass(link, 'tooltip')) {
  		var text = link.title;
  		
  		link.title = "";
  		
  		var sp = document.createElement('span');
  		sp.innerHTML = text;

			sp.style.position = "absolute";
			sp.style.display = "none";

			sp.className = "tooltip-text";
			
			img = link.getElementsByTagName('IMG')[0];
			imgX = null;
			imgY = null;
			
			if (img && hasClass(img, 'image-portal')) {
	  		var imgObj = {};
  		
  			imgObj.offState = img.src;
  			if (typeof(states) != 'undefined') imgObj.onState = states[img.src];
  			imgObj.imgEl = img;

				addElEvent(img, 'onmouseover', (function() { this.imgEl.src = this.onState; }).bind(imgObj));
				addElEvent(sp, 'onmouseover', (function() { this.imgEl.src = this.onState; }).bind(imgObj));
				
				addElEvent(img, 'onmouseout', (function() { this.imgEl.src = this.offState; }).bind(imgObj));
				addElEvent(sp, 'onmouseout', (function() { this.imgEl.src = this.offState; }).bind(imgObj));
				
				pos = findPos(img);
				imgX = pos[0] + (img.offsetWidth * 0.5);
				imgY = pos[1] + (img.offsetHeight * 0.5);
				
				sp.setAttribute('posx', imgX);
				sp.setAttribute('posy', imgY);
  		}
  		
			addElEvent(link, 'onmouseover', tooltipOver.bind(sp));
			addElEvent(sp, 'onmouseover', tooltipOver.bind(sp));
			
			addElEvent(link, 'onmouseout', (function() { this.style.display = "none"; }).bind(sp));
			addElEvent(sp, 'onmouseout', (function() { this.style.display = "none"; }).bind(sp));
  		
  		document.body.appendChild(sp);
  	}
  }
}

function tooltipOver(e) {
//	if (!this.placed || (this.placed + 700) < new Date().getTime()) {
  posx = this.getAttribute('posx');
  posy = this.getAttribute('posy');
	if (!this.placed || (this.placed + 700) < new Date().getTime()) {
	  if (!posx) var posx = mouseX;
	  if (!posy) var posy = mouseY;

		if (posx > (document.body.clientWidth/2) && false) {
			this.style.right = (document.body.clientWidth - posx - 10) + "px";
		}
		else if (posx > 0)  {
			this.style.left = (posx - 10) + "px";
		}
		
		if (posy > 0) {
		  this.style.top = (posy - 10) + "px";
		}
		this.placed = new Date().getTime();
	}

	this.style.display = "block";
}

function inputFocus() {
  this.value = "";
  this.onfocus = null;
  removeClass(this, 'input-label');
}

addLoadEvent(attachInputLabels);
addLoadEvent(attachToolTips);
addLoadEvent(attachPortalThumbs);
//addLoadEvent(attachPortalRollover);


  if(document.styleSheets[0].addRule ) { //IE
   	document.styleSheets[0].addRule( '.image-portal', "{ visibility: hidden; }" ); 
  }
  if( document.styleSheets[0].insertRule ) { //Mozilla
  	document.styleSheets[0].insertRule('.image-portal { visibility: hidden; }', document.styleSheets[0].cssRules.length );
  }


/**
 * Misc. Functions
 */

function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}  

  return true
}

/**
 * Recreate the prototype bind function with the necessary sub functions
 */
var $A = function(iterable) {
  if (!iterable) return [];
  
    var results = [];
    for (var i = 0, length = iterable.length; i < length; i++)
      results.push(iterable[i]);
    return results;
}

if(window.opera){
  Array.prototype.concat = function(){
    var array = [];
    for(var i = 0, length = this.length; i < length; i++) array.push(this[i]);
    for(var i = 0, length = arguments.length; i < length; i++) {
      if(arguments[i].constructor == Array) {
        for(var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
          array.push(arguments[i][j]);
      } else {
        array.push(arguments[i]);
      }
    }
    return array;
  }
}

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

function randOrd(){ return (Math.round(Math.random())-0.5); }
