/*
window.onerror = null;
var topMargin = 100;
var slideTime = 1200;
var ns6 = (!document.all && document.getElementById);
var ie4 = (document.all);
var ns4 = (document.layers);
*/

function FloatObject() {

    this.A = null;
    this.B = null;
    this.C = null;
    this.D = null;
    
    this.id = null;
    this.currentY = null;
    this.startY = null;
    this.style = null;
    this.targetY = null;
    this.newTargetY = null;
}

var nomi_oggetti_fluttuanti=new Array();
nomi_oggetti_fluttuanti[0] = 'oggettistica_mappa';
nomi_oggetti_fluttuanti[1] = 'adwords';

var floatObjects=new Array();

function layerSetup() {
    for(i=0 ; i < nomi_oggetti_fluttuanti.length; i++){
        var object = new FloatObject();
        object.id = nomi_oggetti_fluttuanti[i];
        
        if (ns4) {
            object.startY = document.layers[object.id].top;
        } else if(ns6) {
            object.startY = parseInt(document.getElementById(object.id).style.top);
        } else if(ie4) {
            object.startY = document.all[object.id].style.pixelTop;
       }
       
        object.B = Math.PI / ( 2 * this.slideTime );

        floatObjects.push( object );
    }
    window.setInterval("main()", 10);
}

function floatObject() {
    if (ns4 || ns6) {
        findHt = window.innerHeight;
    } else if(ie4) {
        findHt = document.body.clientHeight;        
    }
} 

function main() {
    for(i=0 ; i < nomi_oggetti_fluttuanti.length; i++){
        if (ns4) {
            floatObjects[i].currentY = document.layers[floatObjects[i].id].top;
            this.scrollTop = window.pageYOffset;
        } else if(ns6) {
            floatObjects[i].currentY = parseInt(document.getElementById(floatObjects[i].id).style.top);        
            this.scrollTop = window.scrollY;
        } else if(ie4) {
            floatObjects[i].currentY = document.all[floatObjects[i].id].style.pixelTop;
            this.scrollTop  = document.documentElement.scrollTop;        
       }
   }
   mainTrigger();
}

function mainTrigger() {
    
    for(i=0 ; i < nomi_oggetti_fluttuanti.length; i++){
    
        //if(this.scrollTop>150)
        if(this.scrollTop>200)
            //floatObjects[i].newTargetY = this.scrollTop + this.topMargin;        125
            //floatObjects[i].newTargetY = this.scrollTop + 5;
            floatObjects[i].newTargetY = this.scrollTop + floatObjects[i].startY-200;
            
            /*if(floatObjects[i].newTargetY>1300) {
                floatObjects[i].newTargetY = 1300;
                //return;
            }*/
        else
            floatObjects[i].newTargetY = floatObjects[i].startY;
    
       //floatObjects[i].newTargetY = this.scrollTop + 5;
       
       if ( floatObjects[i].currentY != floatObjects[i].newTargetY ) {
            var now = new Date();
            if ( floatObjects[i].newTargetY != floatObjects[i].targetY ) {            
                floatObjects[i].targetY = floatObjects[i].newTargetY;
                floatObjects[i].A = floatObjects[i].targetY - floatObjects[i].currentY;
                floatObjects[i].C = now.getTime();
                
                if (Math.abs(floatObjects[i].A) > this.findHt) {
                    floatObjects[i].D = floatObjects[i].A > 0 ? floatObjects[i].targetY - this.findHt : floatObjects[i].targetY + this.findHt;
                    floatObjects[i].A = floatObjects[i].A > 0 ? this.findHt : -this.findHt;        
                } else {
                    floatObjects[i].D = floatObjects[i].currentY;
                }
            }
            
            var newY = floatObjects[i].A * Math.sin( floatObjects[i].B * ( now.getTime() - floatObjects[i].C ) ) + floatObjects[i].D;
            
            newY = Math.round(newY);
            if (( floatObjects[i].A > 0 && newY > floatObjects[i].currentY ) || (floatObjects[i].A < 0 && newY < floatObjects[i].currentY )) {
                if ( ie4 )document.all[floatObjects[i].id].style.pixelTop = newY;
                if ( ns4 )document.layers[floatObjects[i].id].top = newY;
                if ( ns6 )document.getElementById(floatObjects[i].id).style.top = newY+"px";                
            }
        }

    }
}

function start() {
    if(ns6||ns4) {
        pageWidth   = innerWidth;
        pageHeight  = innerHeight;        
    } else if(ie4) {        
        pageWidth   = document.body.clientWidth;
        pageHeight  = document.body.clientHeight;        
    }
    floatObject();
    layerSetup(); 
}