/**

   Inflate/deflate effect

					José Paulo Leal
					zp@dcc.fc.up.pt
					August 2009

   All elements of class "flate" are set with event handlers that
   automatically inflate an element when the mouse enters it, and 
   deflates its when it leaves. You can assing an initial font size 
   to all elements in this class using the CSS rule

   .flate {
   	font-size: 50%;
	}

   When the cursor approaches the borders of the navigator screen
   them the page is automatically scrolled to avoi using the scrollbar  	


   Tested with:		Browser			Version		OS
			Mozilla Firefox		3.0.3		Linux (Mandriva)
			Mozilla Firefox		3.5.2		Windows Vists
			Microsoft Explorer	7.0.6		Windows Vists
			Google Chrome		2.0.172.39	Windows Vista
			Apple Safari		4.0.3		Windows Vista
			Opera 			9.64		Linux (Mandriva)
			Mozilla SeaMonkey	1.1.17		Linux (Mandriva)
			Gnome Epiphany		2.26.1		Linux (Mandriva)
 **/
	
var flateRate       =   20;
var flateUpperLimit =  100;
var flateLowerLimit =   70;
var flateInterval   =  100;
var flateIdCounter  =    0;

/**
  Initialize all HTML elements belonging to class "flate"
 **/
function init() {

   var flateElements = getElementsByClass("flate");

   for(var idx=0; idx < flateElements.length; idx++) {

      var elem = flateElements[idx];
		
      if(elem.id == "")
           elem.id = "flate" + ( ++flateIdCounter);

      elem.style.fontSize = flateLowerLimit + "%";
      elem.onmouseover = function() { inflate(this); }; 
      elem.onmouseout  = function() { deflate(this); }; 
   }

}

/**
 Start inflation of given element
 **/
function inflate(elem) {

   elem.flateRate     = flateRate;
   elem.flateLimit    = flateUpperLimit;
   elem.flateInterval = flateInterval;

   flate(elem.id);

   elem.onmousemove = checkScroll;

}

/**
 Start deflation of given element
 **/
function deflate(elem) {

   elem.flateRate     = -flateRate;
   elem.flateLimit    = flateLowerLimit;
   elem.flateInterval = flateInterval;

   flate(elem.id);

   elem.onmousemove = null;
}

/**
 (In/de)flate  element with given id
 (object references cannot by passed as arguments in timeouts)
 **/
function flate(id) {

   var elem = document.getElementById(id);

   if(elem == null) {
     alert("unknown element with id: "+id)
     return 
   }

   var size = parseInt(elem.style.fontSize);
   if(size * elem.flateRate < elem.flateLimit * elem.flateRate) {
	size += elem.flateRate;
   	elem.style.fontSize = size + "%"
        setTimeout( "flate('"+id+"')",elem.flateInterval);
    }
}

/**
 Check cursor position using events to scroll page automatically
 **/
function checkScroll(event) {
    var scrollX = 0, scrollY = 0;
    var clientX, clientY;

    if(event == undefined) {
	// this is probabbly IE and it processes events differently
	clientX = window.event.clientX;
	clientY = window.event.clientY;
    } else {
	// this is for all the others: Firefox, Chrome, Opera, Safari
	clientX = event.clientX;
	clientY = event.clientY;
    } 
    var deltaX = 100 * (screen.width  - clientX) / screen.width;
    var deltaY = 100 * (screen.height - clientY) / screen.height;

    if(deltaX < 40)
	scrollX = 10;

    if(deltaX > 60)
	scrollX = -10;


    if(deltaY < 40)
	scrollY = 10;

    if(deltaY > 60)
	scrollY = -10;

    scrollBy(scrollX,scrollY);
}


// By DustinDiaz http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


