/********************************
    Funzioni per la grafica  
          MarcoESSE powered
********************************/

/**
 * coordinate
 * @param {int,int,int,int} x,y,w,h 
 */
function Coordinate(x,y,w,h) 
{
	this.x = eval(x);
	this.y = eval(y);
  this.w = eval(w);
  this.h = eval(h)
  ;
	this.top = this.y;
	this.left = this.x;
  this.width = this.w;
  this.height = this.h;
  
}

/*******
  Se iexplorer è TRUE allora il browser è MSIE altrimenti no
******/
var iexplorer = (navigator.userAgent.toLowerCase().indexOf('msie')!=-1);


/*******
  Variabili per avere sempre sott'occhio le coordinate del mouse
******/
var mouseX = 0;
var mouseY = 0;

/*****************************************************
 Funzione per recupearre le dimensioni di un oggetto
*****************************************************/
function getDimension(element)
{
    var x=-1;
    var y=-1;
    var w=0;
    var h=0;
    if (element==document)
    {
        x=element.body.scrollLeft;
        y=element.body.scrollTop;
        w=element.body.clientWidth;
        h=element.body.clientHeight;
    }
    else if (element!=null)
    {
        var e=element;
        var left=e.offsetLeft;
        while ((e=e.offsetParent)!=null) 
            left+=e.offsetLeft;

        var e=element;
        var top=e.offsetTop;
        while((e=e.offsetParent)!=null) 
            top+=e.offsetTop;

        x=left;
        y=top;
        w=element.offsetWidth;
        h=element.offsetHeight;
    }
    return new Coordinate(x,y,w,h);
}

/*****************************************************
 Funzione per recupearre le dimensioni della finestra
    Return:
        {XOffset,YOffset,Width,Height}
*****************************************************/
function getWindowDimension()
{
  w = 0;
  h = 0;
  x = 0;
  y = 0;
  if(iexplorer)
  {
    w = document.body.clientWidth;
    h = document.body.clientHeight;
    x = document.body.scrollLeft;
    y = document.body.scrollTop;
  }else
  {
    w = window.innerWidth;
    h = window.innerHeight;
    x = window.pageXOffset;
    y = window.pageYOffset;
  }
  return new Coordinate(x,y,w,h);
}

/*****************************************************
 Funzione per recupearre il centro della finestra
    Return:
        {x,y}
*****************************************************/
function getWindowCenter()
{
  g = getWindowDimension();
  return new Coordinate((g.w/2) + g.x,(g.h/2) + g.y, 0, 0);
}

/*****************************************************
 Funzione per impostare le coordinate del mouse
 nelle variabili mouseX e mouseY
*****************************************************/
function getMouse(e)
{
    if(!e) 
      var e=window.event;
      
    if(e.pageX||e.pageY)
    {
      mouseX=e.pageX;
      mouseY=e.pageY;
    }
    else if(e.clientX||e.clientY)
    {
      mouseX = e.clientX + document.documentElement.scrollLeft;
      mouseY = e.clientY + document.documentElement.scrollTop;
    }
}

/*****************************************************
 Funzione per il centramento di un oggetto nella
 finestra
*****************************************************/
function centerInWindow(element, offsetLeft, offsetTop)
{
  if(!element)
    return;
  
  var ol = 0;
  var ot = 0;

  if(offsetLeft)
    ol = offsetLeft;
  if(offsetTop)
    ot = offsetTop;
    
  var g = getWindowCenter();
  var d = getDimension(element);
  element.style.left = (g.w - (d[2]/2)) - ol;
  element.style.top  = (g.h - (d[3]/2)) - ot;
}

