/**
 * tooltip.js
 * PB 13/03/08
 * A basic tooltip framework which inherits base functionality from window.js. 
 * All project specific functionally such as tooltip content is provided by 
 * the props object supplied to the constructor. Project specific functionality 
 * is then mapped onto methods in this suppied object via tooltip's parent 
 * (window) getContent() method.
 *
 * Dependancies:
 * ============================================================================
 * base.js      http://dean.edwards.name/weblog/2006/03/base/
 * event.js     http://dean.edwards.name/weblog/2005/10/add-event2/
 * window.js    parent object, provides base functionality;  
 */

var Tooltip = Window.extend({

    tags: ["a", "img", "label", "h1", "h2", "h3", "h4", "h5", "h6"], /* supported elements */
    
    /**
     * @param   props{Object}  object providing project specific functionality;
     */
	constructor: function( props ){
	   // equivilent to calling super, or 
	   // the parent class constructor;
	   this.base( props );
	   
	   this.setType( "tooltip" );
	   this.build();
	   this.bind( this.setTarget() );
	   this.dom.addClass( this.frame, "hide" );

	   return this;
	},
	
	setTarget: function(){
	    var elems, 
	        target = this.props.target;

	    for( var i = 0; i<this.tags.length; i++ ){
	        if( this.tags[i] === target ){
	            elems = this.dom.getTags( target );
	        };
	        if( ! elems ){
	            elems = this.dom.getClass( target );
	        }
	    };
        return elems;
	},
	
	bind: function( elems ){
	    for( var i = 0; i<elems.length; i++ ){
            addEvent( elems[i], "mouseover", this.handler( this, this.show ) );
            addEvent( elems[i], "mouseout", this.handler( this, this.hide ) ); 
        };
	},
	
	handler: function( callee, func ){ 
	    return function( e ){
            func.call( callee, e, this );
	    };
	},
	
	show: function( evt, elem ){
	    var x = this.dom.getMouseX(evt) + this.props.offsetX;
        var y = this.dom.getMouseY(evt) + this.props.offsetY;
        
        this.dom.removeClass( this.frame, "hide" ); 
        this.setText( "text", elem );
        this.move( x, y );
        
        return stopDefault(evt);
	},
	
	hide: function(){
	    this.dom.addClass( this.frame, "hide" );
	},
	
	getWrap: function( target ){
	    return this.dom.getClass( target, this.frame )[0];
	},
    
    setText: function( type, el ){
        var body, elem, text;
        
        body = this.dom.getClass( "body", this.frame )[0];
        this.removeChildren( body );
        
        text = this.getContent( type, el ); 
        elem = this.dom.add( "p", null, text );
        return this.dom.append( elem, body );
    }
});