﻿ if(typeof(Prototype) == "undefined")
	throw "MouseTrail requires Prototype to be loaded.";
 var MouseTrail = Class.create({
 
    initialize : function(container, zlookup)
    {
        this.offset = null;
        this.frame = null;
        this.body = null;
        this.container = container;
        this.zlookup = false;
        if(zlookup) this.zlookup =zlookup;
         
        Event.observe(window, 'load', this._postInitialize.bindAsEventListener(this));
        this.isShows = false;
        
        this.x = 0;
        this.y = 0;
        
        // binders
        this.mouseMoveBinded = this._recalPositionMove.bindAsEventListener(this);
        this.framePositionMove = this._framePositionMove.bindAsEventListener(this);
    },
 
    show : function()
    {
        if(!this.isShows)
        {
            Event.observe(document, 'mousemove', this.mouseMoveBinded);
            if(this.body == null) this.body = $(this.container).up('body');
            this.body.observe('scroll', this.mouseMoveBinded);
            Event.observe(window, 'scroll', this.mouseMoveBinded);
            
            $(this.container).show();
            this.isShows = true;
        }
    },
    
    hide : function()
    {
        $(this.container).hide();
        
        Event.stopObserving(document, 'mousemove', this.mouseMoveBinded);
        if(this.body == null) this.body = $(this.container).up('body');
        this.body.stopObserving('scroll', this.mouseMoveBinded);
        Event.stopObserving(window, 'scroll', this.mouseMoveBinded);

        this.isShows = false;
    },
    
    setFrame : function(frame)
    {
        this.frame = frame;
    },
    
    _getOffset : function()
    {
        if(!this.offset) this.offset = this.frame.cumulativeOffset();
        return this.offset;
    },
   
    _framePositionMove : function(e)
    {
        
        var x = Event.pointerX(e);
        var y = Event.pointerY(e);
        
        offset = this._getOffset();
        x += offset.left;
        y += offset.top;
         
        this.x = x + 10;
        this.y = y + 10;  
        
        $(this.container).setStyle({top: this.y + 'px'});
        $(this.container).setStyle({left: this.x + 'px'});
    },    
    
    _recalPositionMove : function(e)
    {
        var x = Event.pointerX(e);
        var y = Event.pointerY(e);
        
        this.x = x + 10;
        this.y = y + 10;  
        
        $(this.container).setStyle({top: this.y + 'px'});
        $(this.container).setStyle({left: this.x + 'px'});

    },
    
    _postInitialize : function()
    {
        this.body = $(this.container).up('body');
        $(this.container).hide();
        $(this.container).setStyle({position: 'absolute'});
        var z = 2000;
        if(this.zlookup)
        { 
            z = this._getMaxZIndex(this.body);
            z += 1;
        }
        
        $(this.container).setStyle({zIndex : z});
    },
    
    _getMaxZIndex : function(element)
    {
        var maxIndex = 0;
        var d = $(element).descendants();
        for (var index = 0, len = d.length; index < len; ++index) 
        {
            var item = d[index];
            var zindex = item.getStyle('z-index');
            maxIndex = Math.max(maxIndex,zindex);      
        }
        return maxIndex;
    }
});