/*+----------------------------------------+*\
  | floatwin.js - CRIO FLOATING-WIN SCRIPT |
  |   Created by Kapoeklizz vers. 08-05    |
\*+----------------------------------------+*/
//---------- Check Browser
var nn=document.layers?true:false;
var DOM2=document.getElementById?true:false;
var NN6=DOM2&&navigator.userAgent.toLowerCase().indexOf("gecko")!=-1;
//---------- DivMenu Class Definition
function DivMenu(id,l,t,w,h,z){
    function moveDiv(mov){
        if (DOM2&&NN6){
            div.style.left = left = mov.pageX-shift_x;
            div.style.top = top = mov.pageY-shift_y;
        }
        else{
            div.style.left = left = document.body.scrollLeft+event.clientX-shift_x;
		    div.style.top = top = document.body.scrollTop+event.clientY-shift_y;
	    }
    }
    function minimizer(){
        if(!minim){
            div.style.height=bottomBarSize;
            content.style.height=minsize;
            minim=1;
        }  
        else{
            div.style.height=height;
            content.style.height=height-bottomBarSize;
            minim=0;
        }
    }
    function resizeDiv(mov){
        if (DOM2&&NN6){
            width=mov.pageX - left + shiftr_x ;
            height=mov.pageY - top + shiftr_y + bottomBarSize;
        }
        else{
            width = document.body.scrollLeft+event.clientX - left + shiftr_x
		    height = document.body.scrollTop+event.clientY - top + shiftr_y + bottomBarSize
	    }
	    if (width<150) width=150;
	    if (height<50) height=50;
	    div.style.width=width;
        div.style.height=height;
        content.style.width=width-(2-minsize);
        content.style.height=height - bottomBarSize;
    }
    function startDiv(){
       if(div==null) div=document.getElementById("div"+id);
       if(content==null) content=document.getElementById("content"+id);
       div.style.left = left
       div.style.top = top
       div.style.width = width
       div.style.height = height
       div.style.zIndex = zIndex
       content.style.width = width-(2-minsize)
       content.style.height = height-bottomBarSize
    }
    function getStyle(){
       return left+" "+top+" "+width+" "+height+" "+zIndex+" "
    }
    function setzIndex(){
       zIndex=div.style.zIndex
    }
    // Private Variables
    var x_start;
    var y_start;
    var id=id
    var movement=0;
    var minim=0;
    var height=h;
    var width=w;
    var top = t
    var left = l
    var zIndex = z
    var div
    var content
    // Methods
    this.moveDiv = moveDiv
	this.minimizer = minimizer
	this.resizeDiv = resizeDiv
	this.startDiv = startDiv
	this.getStyle = getStyle
	this.setzIndex = setzIndex
}
//------------------- Function
function setCookie(){
    var valori=""
    for(x=0;x<totdiv;x++)
        valori=valori+arrDivs[x].getStyle()
    var expdate = new Date();
    expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
    document.cookie = "valori="+valori+"; expires=" + expdate.toGMTString()
}
function readCookie(){
    inizio = document.cookie.indexOf("valori=");
    if (inizio != -1) {
	    inizio += 7;
		fine = document.cookie.indexOf(";", inizio);
		fine = (fine==-1) ? document.cookie.length : fine;
		valori=unescape(document.cookie.substring(inizio, fine))

		if (document.cookie.indexOf("undefined")!=-1)
		    return null
		else{
		    return(unescape(document.cookie.substring(inizio, fine)));
		}
    }
    else return null
}
function setMov(id){
	if (div2move==undefined)
	    div2move=id;
	else{
	    div2move=undefined
	    setCookie()
	}
	if (div2resize!=undefined){
	    div2resize=undefined;
	}
}
function setResize(id){
	if (div2resize==undefined)
	    div2resize=id;
	else{
        div2resize=undefined;
        setCookie()
	}
}
function movediv(mov){
	if (div2move!=undefined)
	    arrDivs[div2move].moveDiv(mov);
	if (div2resize!=undefined)
	    arrDivs[div2resize].resizeDiv(mov);
}
function setOnTop(id){
    //if (id==1) then
    div=document.getElementById('div'+id);
    if (div.style.zIndex!=topIndex){
        div.style.zIndex=topIndex+1;
        arrDivs[id].setzIndex();
        topIndex++;
        setCookie();
    }
}
function setPos(id){
    if (id==1)
	string=null;
    else
    	string=readCookie();
    
    //string=null;
 
    if(string==null)
        arrValue=arrPosDiv
    else{
        arrValue=string.split(" ");
        indici=ordina(arrValue)
        for(x=0;x<totdiv;x++)
            arrValue[x*5+4]=indici[x];
    }
    //document.write(arrValue)
    for(x=0;x<totdiv;x++){
        arrDivs[x]=new DivMenu(x,arrValue[x*5],arrValue[x*5+1],arrValue[x*5+2],arrValue[x*5+3],arrValue[x*5+4])
    }
    for(x=0;x<totdiv;x++){
        arrDivs[x].startDiv();
    }
    if (id==1)
    	  setCookie();
}
function ordina(arr){
    var indici=new Array()
    max=0
    ind=0
    topM=1000000
    for(x=0;x<totdiv;x++){
        for(c=0;c<totdiv;c++){
            if (arr[c*5+4]> max && arr[c*5+4]<topM) {
                max=arr[c*5+4];
                ind=c;
            }
        }
        indici[ind]=totdiv-x;
        topM=max;
        max=0;
    }
    return indici
}
//+----------------------- Editable Variables
var totdiv=5;
var shiftr_x=-35
var shiftr_y=-30
var bottomBarSize=26
var shift_x=18
var shift_y=12
if (screen.availWidth>=1024)
	//var arrPosDiv = [0,511,944,91,5,215,27,522,274,1,785,4,156,465,2,-1,4,168,460,4,216,317,521,178,3]; // > 1024x768
	var arrPosDiv = [1,529,945,86,4,0,3,522,274,1,570,3,374,272,5,0,293,412,218,3,462,292,482,219,2];
else
	var arrPosDiv = [475,303,266,154,3,222,12,520,272,1,2,6,167,277,2,3,303,208,152,5,259,305,166,150,4]; // < 1024x768
//------------------------ Uneditable Variables
var arrDivs=new Array()
var posDivs
var div2move;
var div2resize;
var topIndex=totdiv
var st=0
var minsize
if (DOM2&&NN6) minsize=0
 else minsize=1
 
//-------------------capture Event
nn?window.captureEvents(Event.MOUSEMOVE):null;
document.onmousemove = movediv;