// JavaScript Document
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getRandom(min, max)
{		
	var randomNum = Math.random() * (max-min); 
    // Round to the closest integer and return it
	return(Math.round(randomNum) + min); 
}
  

$(document).ready(function(){

    var screen_width = $(window).width();
    var screen_height = $(window).height();
    var loaded = 0;
    
    var _images = [ 
        "/img/bollino_newsletter1.gif",
        "/img/bollino_newsletter2.gif",
        "/img/bollino_newsletter3.gif"];

    $.each(_images,function(e) {
        $(new Image()).load(function() {
            //alert($(this).attr('src') + 'has loaded!');
            loaded++;
            if( loaded == _images.length) {
                $(document).oneTime( 2000, function(i) {
                    var xy = getScrollXY();
                    $("#bollino").css({
                        'width' :  $("#bollino img").width(),
                        'height' :  $("#bollino img").height(),
                        'left' :  getRandom(xy[0], (xy[0] + screen_width - $("#bollino img").width())),
                        'top' :  getRandom(xy[1], (xy[1] + screen_height - $("#bollino img").height()))
                    });
                        
                    $(document).everyTime( 6000, function(i) {
    
                        var xy = getScrollXY();
                        
                        $("#bollino img").attr({ 
                            src: "/img/bollino_newsletter" + getRandom( 1, 3) + ".gif",
                            title: "newsletter",
                            alt: "newsletter"
                        });
    
                        $("#bollino").css({
                            'width' :  $("#bollino img").width(),
                            'height' :  $("#bollino img").height(),
                            'left' :  getRandom(xy[0], (xy[0] + screen_width - $("#bollino img").width())),
                            'top' :  getRandom(xy[1], (xy[1] + screen_height - $("#bollino img").height()))
                        });
                    });
                });
            }
        }).attr('src',this);
        
    });
    
    $("#bollino").css( {
        'visibility': 'visible',
        'left': screen_width - $("#bollino").width(),
        'top': 0
    });
});  
