
//-----------------------------------------
// HOME PAGE
//-----------------------------------------

var ItemScroller = Class.create();
ItemScroller.prototype = {  
      
  initialize: function(element) {
    var options = Object.extend( {
      speed: 1000,
      skipclass: null
    }, arguments[1] || {} );

    this.element = $(element);
    this.options = options;
    
    this.eventMouseOver = this.mouseOver.bindAsEventListener(this);
    this.eventMouseOut  = this.mouseOut.bindAsEventListener(this);
    this.eventMouseMove = this.mouseMove.bindAsEventListener(this);
    
    Event.observe( this.element, "mousemove",  this.eventMouseMove);          
  },

  scroll: function( arr ) {
    this.collection = $A(arr);
    
    var byID = $H()
    for( var i=0; i<this.collection.length; i++) { 
        if( this.collection[i].entryID != null ) {
            byID[this.collection[i].entryID] = this.collection[i];
        }
        // preload the images...
        if( this.collection[i].imageURL ) {
        	new Image().src = this.collection[i].imageURL;
        }
    }
    
    images = new Array();
    var allimgs = this.element.getElementsByTagName("img"); 
    for( var i=0; i<allimgs.length; i++) { 
        if( this.options.skipclass && Element.hasClassName( allimgs[i], this.options.skipclass ) ) {
            continue;   
        }
        images.push( allimgs[i] );
        
        var xx = byID[allimgs[i].name];
        if( xx ) {
            allimgs[i].item = xx;
            byID[allimgs[i].name] = null; // remove it?
        }
        else {
            alert("can not find: "+allimgs[i].name );   
        }
        
        // observe the mouseover/out...
        Event.observe(allimgs[i], "mouseover", this.eventMouseOver);
        Event.observe(allimgs[i], "mouseout",  this.eventMouseOut);                                                     
    }
    
    var hidden = $A();
    var vvv = byID.values();
    for( var i=0; i<vvv.length; i++) { 
        if( vvv[i] != null ) {
            hidden.push( vvv[i] );
        }
    }
    
    //alert( "IMAGES:"+images + "\nALL:" +byID.inspect()+"\nIN:"+arr+"\nCOL:"+this.collection+"\nHIDDEN:"+hidden   );    
    
    if( this.looper ) {
        clearInterfal( this.looper );
    }
    var loopID = 0;
    var scroller = this;
    
    var rand = null;
    var lastrand = null;
    
    this.looper = setInterval( function() {
        for( var i=0; i<5; i++ ) {  // only try 5 times
            rand = images[ Math.floor( Math.random()*images.length ) ]; 
            if( rand && (rand != lastrand) && rand != scroller.overElement ) 
            {
                //alert( "RAND: "+rand + " : " + rand.name + " : " + rand.type );
                    
                lastrand = rand;
                
                if( rand.item ) {
                    var temp = rand.item;
                    rand.item = hidden[loopID];
                    rand.parentNode.href = rand.item.showURL;
                    hidden[loopID] = temp;
                    
                    if( rand.item.imageURL ) {
                        new Effect.Fade( rand, { to:0.05, afterFinish: function() { 
                          rand.src = rand.item.imageURL;
                          new Effect.Appear( rand );
                        } } );
                    }  
                    loopID = (loopID+1)%hidden.length;
                }
                else {
                    alert( "no item: "+rand + " : " + rand.name + " : " + rand.type );
                }
                return;
            }
        }
    }, this.options.speed );
  },
  
  
  mouseOver: function(event) {
     this.overElement = Event.element( event );
     if( this.overElement.item ) {
        if(!this.overlay ) {        
            this.overlay = Builder.node( 'div',  { style:"position:absolute; border:1px solid black; background:#FFFFFF; padding: 5px; visibility: hidden;" }, "hello!" );
            document.body.appendChild( this.overlay );      
        }
        var item = this.overElement.item;
        this.overlay.innerHTML = '<b>'+item.name+'</b><br />'+item.author;
        
        this.mouseMove( event );
        this.overlay.style.visibility ='visible';
     }
  },
  
  mouseOut: function(event) {
     this.overElement = null;
     
     this.overlay.style.visibility ='hidden';
  },
  
  mouseMove: function(event) {
    if( this.overlay ) {
        this.overlay.style.top  = (Event.pointerY(event)+10)+"px";
        this.overlay.style.left = (Event.pointerX(event)+10)+"px";
    }
  }
}
