//$import:Scripts/Core/Core.js
if(typeof(browser)=="undefined") {
	var _BT = function (){
		var cfr = function (s){return navigator.userAgent.indexOf(s)!= -1}
		if(cfr("MSIE")) return "IE";
		if(cfr("Gecko/")) return "Moz";
		if(cfr("Opera/")) return "Op";
		return "";
		}();
	var browser = {type : _BT,
	isIE:(_BT=="IE"),isMoz:(_BT=="Moz"),isOpera:(_BT=="Op"),isNoIE:(_BT!="IE")}
}

function Location(x,y){
    this.x= this.X = x || 0;
    this.y= this.Y = y || 0;
}

Location.get=function(element){
   return  Position.cumulativeOffset(element);
}

Location.prototype.moveAt = function (e){
	var e = $(e);
	e.style.left = this.x;
	e.style.top = this.y;
}

Location.prototype.toString = function (){
	return this.x + "," + this.y;
}
function Size(w,h){
    this.width = this.w = w || 0;
    this.height= this.h = h || 0;
	this.toString = function (){
		return [this.width,this.height].join(",");
	}
}
Size.prototype.equals=function (size){
    if(! (size instanceof Size) ) return  false;
    return  (this.w==size.w) && (this.h==size.h)
}
Size.get=function(element){
    //misura anche gli elementi non visibili;
    element=$(element);
    if($E.visible(element)) return  new Size(element.offsetWidth,element.offsetHeight);
    var els = element.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = '';
    var originalWidth = element.offsetWidth;
    var originalHeight = element.offsetHeight;
    els.display = 'none';
    els.position = originalPosition;
    els.visibility = originalVisibility;
    return new Size(originalWidth, originalHeight);
}

Size.getClientHeight=function (){
        var height = self.innerHeight; // Safari, Opera
        var mode = document.compatMode;
        if ( (mode || browser.isIE) && !browser.isOpera ) { // IE, Gecko
            height = (mode == 'CSS1Compat') ?
                    document.documentElement.clientHeight : // Standards
                    document.body.clientHeight; // Quirks
        }

        return height;
}
Size.getClientWidth = function() {
      var width = self.innerWidth;  // Safari
      var mode = document.compatMode;
      if (mode || browser.isIE) { // IE, Gecko, Opera
          width = (mode == 'CSS1Compat') ?
                  document.documentElement.clientWidth : // Standards
                  document.body.clientWidth; // Quirks
      }
      return width;
}

Size.client =  function () { 
  return  new Size(Size.getClientWidth(),Size.getClientHeight());
}


function Bounds(e){    
    //E' possibile anche costruirlo con 2 argomenti
    //size e location
	this.element = $(e);
    this.location=(arguments.length>1)?arguments[0]:Location.get(e);
    this.size=(arguments.length>1)?arguments[1]:Size.get(e);   
    this.x= this.X = this.left = 0;
    this.y= this.Y = this.top = 0;
    this.width = this.w = 0;
    this.height= this.h = 0;
    this.right= this.r = 0;
    this.bottom = this.b =0;
	this.loadValues();
}

Bounds.prototype.reduce = function (value){
	//riduce il rettangolo
	var l = this.location;
	var s = this.size;
	l.x += value;
	l.y += value;
	s.w -= (value*2);
	s.h -= (value*2);
	this.changeDimensions(l,s);
}
Bounds.prototype.leftTop = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= 0;
	this.location.y= 0;
	this.location.x += $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.rightTop = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= bc.w - this.w;
	this.location.y= 0;
	this.location.x -= $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.leftBottom = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= 0;
	this.location.y= (bc.h - this.h) ;
	this.location.x += $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.rightBottom = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= bc.w - this.w;
	this.location.y= (bc.h - this.h) ;
	this.location.x -= $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.centerBottom = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= (bc.w - this.w) / 2;
	this.location.y= ((bc.h - this.h) / 3) * 2;
	this.location.x += $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.centerTop = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= (bc.w - this.w) / 2;
	this.location.y= (bc.h - this.h) / 3;
	this.location.x += $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
Bounds.prototype.center = function (container){
	var bc;
	if(container instanceof Bounds) bc=container;
	else bc=Bounds.get(container);
	this.location.x= (bc.w - this.w) / 2;
	this.location.y= (bc.h - this.h) / 2;
	this.location.x += $P.scrollLeft();
	this.location.y += $P.scrollTop();
	this.location.x = Math.max(this.location.x,0)
	this.location.y = Math.max(this.location.y,0)
	this.loadValues();
	this.applyLocation(this.element,true);
}
$E.leftTop = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.leftTop(c);	
}
$E.leftBottom = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.leftBottom(c);	
}
$E.rightTop = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.rightTop(c);	
}
$E.rightBottom = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.rightBottom(c);	
}
$E.centerTop = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.centerTop(c);	
}
$E.centerBottom = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.centerBottom(c);	
}
$E.center = function (element,container){
	var c = container || Bounds.window();
	var b = new Bounds(element);
	b.center(c);	
}
Bounds.prototype.applyLocation = function (e){
	with($(e).style){
		position="absolute";
		left = this.x + "px";
		top = this.y + "px";
	}
}
Bounds.prototype.apply = function (e,absolutize){
	with($(e).style){
		if (absolutize) position="absolute";
		width = this.w + "px";
		height = this.h + "px";
		this.applyLocation(e)
	}
}
Bounds.prototype.changeDimensions = function (newLocation,newSize){
	this.location=newLocation;
	this.size=newSize;
	this.loadValues();
}
Bounds.prototype.changeLocation = function (newLocation){
	this.location=newLocation;
	this.loadValues();
}

Bounds.prototype.changeSize = function (newSize){
	this.size=newSize;
	this.loadValues();
}

Bounds.prototype.loadValues = function (){
	//carica i valori da location e size
	this.x= this.X = this.left = this.location.x;
    this.y= this.Y = this.top = this.location.y;
    this.width = this.w = this.size.w;
    this.height= this.h = this.size.h;
    this.right= this.r = this.x + this.w;
    this.bottom = this.b =this.y + this.h;	
}

Bounds.prototype.test =function (){
    var oDiv=$E.createDiv();
    with(oDiv.style){
        border="1px solid red";
        position="absolute";
        left=this.x;
        top=this.y;
        width=this.w;
        height=this.h;
    }
    document.body.appendChild(oDiv);
}

Bounds.combine=function (element1,element2){
    var b1 = Bounds.get(element1);
    var b2 = Bounds.get(element2);
    var X=Math.min(b1.x,b2.x);
    var Y=Math.min(b1.y,b2.y);
    var R=Math.max(b1.r,b2.r);
    var B=Math.max(b1.b,b2.b);
    var loc = new Location(X,Y);
    var size = new Size(R-X,B-Y);
    return  new Bounds(loc,size);
}

Bounds.prototype.toString=function (){
    return  [this.x,this.y,this.w,this.h].toString();
}

Bounds.prototype.contains=function(x,y){
    var containsX=(x>=this.x && x<=this.r);
    var containsY=(y>=this.y && y<=this.b);
   // alert(this.toString() + [containsX,containsY] + [x,y])
    return containsX && containsY ;
}

Bounds.prototype.isContained=function(container){
    var c=container||null;
    var cBounds  = (c==null)? Bounds.window() : Bounds.get(c);
    return  (
        this.x>=cBounds.x &&
        this.y>=cBounds.y &&
        this.r<=cBounds.r &&
        this.b<=cBounds.b
        )
}

Bounds.prototype.interfere=function(container){
    var c=container||null;
    var cBounds  = (c==null)? Bounds.window() : Bounds.get(c);
    var interfereX = !(
    (this.x<cBounds.x && this.r< cBounds.x) ||
    (this.x>cBounds.x && this.r>cBounds.r)
    )
    var interfereY = !(
    (this.y<cBounds.y && this.b< cBounds.y) ||
    (this.y>cBounds.y && this.b> cBounds.b) 
    )
    return  (interfereX && interfereY)
}

Bounds.inWindow = function (element){
    var b=Bounds.get(element);
    return  b.isContained();
}
Bounds.window=function(){ 
    return  new Bounds(new Location(0,0),Size.client());
}
Bounds.windowAll=function(){ 
    return  new Bounds(new Location(0,0),Size.client(false));
}
Bounds.get=function(element){
    element=$(element);
    return  new Bounds(element);
}

/*
OLD:dismessed
Bounds.correctPositionRight=function (element){
    var bw = Bounds.window();
    var be = Bounds.get(element);
    var beX= (be.r > bw.r) ? bw.r - be.w : be.x;
    $P.set(element,new Location(be.x, be.y));
}*/

Bounds.fillPage =function (element){
    var e=$(element);
    var bw = Bounds.window();
    var be = Bounds.get(element);
    var x = be.x;
    var y = be.y;    
    var fillWidth = bw.w - x;
    var fillHeight = bw.h - y;
    var border =0 ;
    try{
        var b = parseInt(e.style.borderWidth,'px')
        border = ((isNaN(b))?0:b) * 2;
        if(e.nodeName.toLower()=='iframe') {
         $P.absolutize(e);
         border += be.x;
        }
    }
    catch(ex){}
        e.style.width=  (fillWidth - border) + "px";
        e.style.height=  (fillHeight - border) + "px";
}
Bounds.fillParentHeight=function (element) {
 var e=$(element);
 var be = Bounds.get(e);
 var bp = Bounds.get(e.parentNode);
 e.style.height= bp.h + "px";
}
/*
allunga l'elemento a occupare il resto della pagina in altezza
bodyPadding è il padding del documento
*/
Bounds.fillHeight=function (element,bodyPadding){
    var e=$(element);
    var bw = Bounds.window();
    var be = Bounds.get(e);
    var y = be.y;    
    var h = bw.h - y - bodyPadding;
	
    var border =0 ;
    try{
        var b = parseInt(e.style.borderWidth,'px')
        border = ((isNaN(b))?0:b) * 2;
        if(e.nodeName.toLower()=='iframe') {
         $P.absolutize(e);
         border += be.x;
        }
    }
    catch(ex){}
        e.style.height=(h - border) + "px";
}
function Displayer(element,target,horizontalAlign,verticalAlign,offsetLeft,offsetTop,overlap){
    this.element=$(element);
    this.target = $(target||document.body);
    this.be = Bounds.get(this.element);
    this.bt = Bounds.get(this.target);
    // posizione in orizzontale:
    // l = a sinistra del target
    // r = a destra del target
    // a = allineato al target
    this.horizontal = (['l','r','a'].contains(horizontalAlign))? horizontalAlign : 'a'; 
    // posizione in verticale:
    // t= sopra il target
    // b= sotto il target
    this.vertical = (['t','b'].contains(verticalAlign)) ? verticalAlign : 'b';
    this.offsetLeft = offsetLeft||0;
    this.offsetTop = offsetTop||0;
    this.overlap = overlap || false; // se l'elemento si deve sovrapporre al target
}

Displayer.prototype.setLocation = function (x,y){
    this.bt.location=new Location(x,y);
    this.bt.x=x;
    this.bt.y=y;
}
Displayer.prototype.display = function (){
    $P.absolutize(this.element);   
    var loc = new Location(0,0);
    //posizione orizzontale;
    if(this.horizontal=='l'){
        loc.x=this.bt.x - this.be.w - this.offsetLeft;
    }
    else if(this.horizontal=='r'){
        loc.x=this.bt.r + this.be.w + this.offsetLeft;
    }
    else if(this.horizontal=='a'){
        loc.x=this.bt.x + this.offsetLeft;
    }
    //posizione verticale;
    if(this.vertical=='t'){
        loc.y=this.bt.b  - this.offsetTop;
         if(!this.overlap) loc.y -=  this.bt.h;
    }   
    else if(this.vertical=='b'){
        loc.y=this.bt.y + this.offsetTop;
        if(!this.overlap) loc.y += this.bt.h;
    }
    $P.set(this.element,loc);
}

var $P = Position = {
  // set to true if needed, warning: firefox performance problems
  // NOT neeeded for page scrolling, only if draggable contained in
  // scrollable elements
  includeScrollOffsets: false,
  moveUnder:function (element,target,offset,show){
    //muove un elemento sotto un altro
    var d=new Displayer(element,target,'a','b',offset,offset,false);
    d.display();
    if(show) $E.show(element);
  },
  set:function (element,location){
    with ($(element).style) {
        left=location.x + "px";
        top=location.y + "px";
    }
  },
  // must be called before calling withinIncludingScrolloffset, every time the
  // page is scrolled
  prepare: function() {
    this.deltaX =  window.pageXOffset
                || document.documentElement.scrollLeft
                || document.body.scrollLeft
                || 0;
    this.deltaY =  window.pageYOffset
                || document.documentElement.scrollTop
                || document.body.scrollTop
                || 0;
  },
  realOffset: function(element) {
    element=$(element);
    var valueT = 0, valueL = 0;
    do {
      valueT += element.scrollTop  || 0;
      valueL += element.scrollLeft || 0;
      element = element.parentNode;
    } while (element);
    return new Location(valueL,valueT);
  },
  cumulativeOffset: function(element) {
    element=$(element);
    var valueT = 0, valueL = 0;
    try{
        do {
          valueT += element.offsetTop  || 0;
          valueL += element.offsetLeft || 0;
          element = element.offsetParent;
        } while (element);
     }
     catch(ex){} 
    return new Location(valueL, valueT);
  },
  positionedOffset: function(element) {
    element=$(element);
    var valueT = 0, valueL = 0;
    do {    
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
      if (element) {
        p = element.style.position;
        if (p == 'relative' || p == 'absolute') break;
      }
    } while (element);   
    return new Location(valueL, valueT);
  },

  offsetParent: function(element) {
    element=$(element);
    if (element.offsetParent) return element.offsetParent;
    if (element == document.body) return element;

    while ((element = element.parentNode) && element != document.body)
      if (element.style.position != 'static')
        return element;

    return document.body;
  },

  // caches x/y coordinate pair to use with overlap
  within: function(element, x, y) {
    element=$(element);
    if (this.includeScrollOffsets)
      return this.withinIncludingScrolloffsets(element, x, y);
    this.xcomp = x;
    this.ycomp = y;

    this.offset = this.cumulativeOffset(element);

    return (y >= this.offset.y &&
            y <  this.offset.y + element.offsetHeight &&
            x >= this.offset.x &&
            x <  this.offset.x + element.offsetWidth);
  },

  withinIncludingScrolloffsets: function(element, x, y) {
  element=$(element);
    var offsetcache = this.realOffset(element);

    this.xcomp = x + offsetcache.x - this.deltaX;
    this.ycomp = y + offsetcache.y - this.deltaY;
    this.offset = this.cumulativeOffset(element);

    return (this.ycomp >= this.offset.y &&
            this.ycomp <  this.offset.y + element.offsetHeight &&
            this.xcomp >= this.offset.x &&
            this.xcomp <  this.offset.x + element.offsetWidth);
  },
  // within must be called directly before
  overlap: function(mode, element) {
    element = $(element);
    if (!mode) return 0;
    if (mode == 'vertical')
      return ((this.offset.y + element.offsetHeight) - this.ycomp) /
        element.offsetHeight;
    if (mode == 'horizontal')
      return ((this.offset.x + element.offsetWidth) - this.xcomp) /
        element.offsetWidth;
  },

  page: function(forElement) {
    var valueT = 0, valueL = 0;

    var element = $(forElement);
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;

      // Safari fix
      if (element.offsetParent==document.body)
        if (element.style.position=='absolute') break;

    } while (element = element.offsetParent);

    element = forElement;
    do {
      valueT -= element.scrollTop  || 0;
      valueL -= element.scrollLeft || 0;
    } while (element = element.parentNode);

    return new Location(valueL, valueT);
  },
  scrollLeft:function (){
    return (document.documentElement.scrollLeft)?document.documentElement.scrollLeft: document.body.scrollLeft;
  },
  scrollTop:function (){
    return (document.documentElement.scrollTop)?document.documentElement.scrollTop: document.body.scrollTop;
  },
  currentPoint : function (){
  	if(isIE) return  new Location(docX + this.scrollLeft(), docY + this.scrollTop());	
 	 return  new Location(docX , docY);	
    },

  absolutize: function(element) {
    element = $(element);
    element.style.position = 'absolute';  
  },
  relativize: function(element) {
    element = $(element);
    element.style.position = 'relative';
  },
  correctPosition : function (e){
  	e = $(e);
  	var bw = Bounds.window(); //grandezza finestra
	var be = Bounds.get(e);
	be.changeLocation(this.currentPoint());
	if(!isIE) {
		bw.right -= 18; //scrollbars in firefox
	}
	if(be.right > bw.right) {
		be.location.x = bw.right - be.w;		
		}
	if(be.x < bw.x) { be.location.x = bw.x}
	if(be.y < bw.y) be.location.y = bw.y;	
	if(be.bottom > bw.bottom) be.location.y = bw.bottom - be.h;			
	be.location.moveAt(e);	
	be = Bounds.get(e);

  }
}

$E.setPosition = function (target,x,y,offsetX,offsetY) {
	$E.style(target,"top:" + (y + offsetY) + "px;left:" + (x + offsetX) + "px");
}
$E.setPositionAtMouse = function (target,offset) {
	$E.show(target);
	$E.setPosition(target,docX + $P.scrollLeft(), docY + $P.scrollTop(),offset,offset);
}
var docX, docY;
function listener( e ){
   if( e ){
      if( typeof( e.pageX ) == 'number' ) {
         docX = e.pageX;
         docY = e.pageY;
      }
      else{
         docX = e.clientX;
         docY = e.clientY;
      }
   }
   else{
      e = window.event;
      docX = e.clientX;
      docY = e.clientY;
      if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ){
         docX += document.documentElement.scrollLeft;
         docY += document.documentElement.scrollTop;
      } 
      else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ) {
         docX += document.body.scrollLeft;
         docY += document.body.scrollTop;
      }
   }
}
try {document.addEventListener("mousemove", listener,true)}
catch(e) {document.attachEvent("onmousemove",listener)}