var TooltipManager = {
    CreateTooltip: function(id, props)
    {
        var div = document.createElement('div');
        div.id = id;
        div.className = "TooltipContainer";
        
        div.innerHTML = content;
        document.body.appendChild(div);
        return div;
    }
}

var Tooltip = Class.create();
Tooltip.prototype = {
	margin:
	{
		top:1,
		right:1,
		bottom:1,
		left:1
	},
	
	visibility: 'visible',
	
    initialize: function(id, props)
    {
        this.id = id;
        this.container = document.body;
        if (props.container != null)
        {
            var container = props.container;
            if (typeof(container) == "string")
                container = $(container);
            if (container)
            {
                if (container.appendChild)
                {
                    var div = document.createElement('div');
                    div.className = 'DC_TooltipContainer';
                    container.appendChild(div);
                    this.container = div;
                }
            }
        }
        
        var div = document.createElement('div');
        this.element = div;
        div.id = this.id;
        div.className = "DC_Tooltip";
        /*div.style.position = 'absolute';
        div.style.zIndex = '9000';
        div.style.background = '#BACC6E';
	    div.style.border = 'solid 1px #4D6000';
	    div.style.color = '#4D6000';
	    div.style.padding = '2px';
	    div.style.font = 'normal normal bold 10pt Tahoma';*/
        
        this.container.appendChild(div);
        this.setProperties(props);
        
    },
    
    setProperties: function(props)
    {
    	if (props == null)
    		return false;
		this.props = props;
		
        
        if (this.props.reference != null)
        {
            this.setReference(this.props.reference);
        }
        
        if (this.props.text != null)
        	this.setText(this.props.text);
        	
    	if (this.props.width != null && this.props.height != null)
    		this.setDimension(this.props.width, this.props.height);
    		
		if (this.props.top != null && this.props.left != null)
    		this.setPosition(this.props.left, this.props.top);
    		
		if (this.props.cssClass != null)
			this.setCSSClass(this.props.cssClass);
			
		if (this.props.triggers != null)
		{
			var triggers = this.props.triggers;
			if (triggers.element == null)
			{
				if (this.reference != null)
					triggers.element = this.reference;
			}
			else
			{
				switch (typeof(triggers.element))
				{
					case 'string':
						if ($(triggers.element))
							triggers.element = $(triggers.element);
						else
							triggers.element = null;
						break;
					case 'object':
						if (!triggers.element.id && !$(triggers.element.id))
							triggers.element = null;
						break;
					case 'number':
						triggers.element = null;
						break;
				}

			}
			if (triggers.element != null)
			{
				if (triggers.hide)
					this.setTrigger(triggers.element, triggers.hide, 'hide');
				if (triggers.show)
					this.setTrigger(triggers.element, triggers.show, 'show');
			}
		}
		
		if (this.props.align != null)
		{
			this.setAlign(this.props.align);
		}
    },
    
    setPosition: function(x, y)
    {
        var validFilter = (/(^[0-9]+$)|(^[0-9]+(px)$)/);
        if (!validFilter.test(x) || !validFilter.test(y))
            return false;
        var digitFilter = (/^[0-9]+$/);
        /*if (parseInt(x) + this.element.offsetWidth > document.body.offsetWidth)
		    x = (document.body.clientWidth - this.element.offsetWidth - 10) + 'px';
		if (parseInt(y) + this.element.offsetHeight > document.body.offsetHeight)
		    y = (document.body.clientHeight - this.element.offsetHeight - 10) + 'px';*/
        this.element.style.left = (digitFilter.test(x)) ? x + 'px' : x;
        this.element.style.top = (digitFilter.test(y)) ? y + 'px' : y;
        /*alert('left: ' + x + 
            '\nwidth: ' + this.element.offsetWidth + 
            '\ndocument.body.offsetWidth: ' + document.body.offsetWidth +
            '\ndocument.body.offsetHeight: ' + document.body.offsetHeight);*/
        return true;
    },
    
    setAlign: function(align)
    {
    	if (this.reference == null)
    		return false;
    	if (align == null)
    		return false;
    	if (typeof(align) != 'string')
    		return false;
		var offset = Position.cumulativeOffset(this.reference);
		var top, left;
		switch(align)
		{
			case 'topleft':
				left = offset[0] + this.margin.left;
				top = offset[1] - this.element.offsetHeight - this.margin.bottom;
				if (top < 0)
					top = 0;
				break;
			case 'topright':
				left = offset[0] + this.reference.offsetWidth - this.element.offsetWidth + this.margin.left;
				top = offset[1] - this.element.offsetHeight - this.margin.bottom;
				if (top < 0)
					top = 0;
				break;
			case 'lefttop':
				left = offset[0] - this.reference.offsetWidth - this.margin.right;
				if (left < 0)
					left = 0;
				top = offset[1] + this.margin.top;
				break;
			case 'leftbottom':
				left = offset[0] - this.reference.offsetWidth - this.margin.right;
				if (left < 0)
					left = 0;
				top = offset[1] + this.reference.offsetHeight + this.margin.top;
				break;
			case 'righttop':
				left = offset[0] + this.reference.offsetWidth - this.margin.left;
				top = offset[1] + this.margin.top;
				break;
			case 'rightbottom':
				left = offset[0] + this.reference.offsetWidth - this.margin.left;
				top = offset[1] + this.reference.offsetHeight + this.margin.top;
				break;
			case 'bottomleft':
				left = offset[0] + this.margin.left;
				top = offset[1] + this.reference.offsetHeight + this.margin.top;
				break;
			case 'bottomright':
				left = offset[0] + this.reference.offsetWidth + this.margin.left;
				top = offset[1] + this.reference.offsetHeight - this.element.offsetWidth + this.margin.top;
				break;
			default:
				align = null;
				break;
		}
		if (align == null)
			return false;
		
		this.setPosition(left, top);
		return true;
    },
    
    setText: function(text)
    {
    	if (text == null)
    		return;
    	this.text = text;
    	this.element.innerHTML = text;
    },
    
    setDimension: function(width, height)
    {
    	var validFilter = (/(^[0-9]+$)|(^[0-9]+(px)$)/);
    	if (width == null || height == null)
    		return false;
		if (!validFilter.test(width) || !validFilter.test(height))
			return false;
		var digitFilter = (/^[0-9]+$/);
		this.element.style.width = (digitFilter.test(width)) ? width + 'px' : width;
		this.element.style.height = (digitFilter.test(height)) ? height + 'px' : height;
		this.width = width;
		this.height = height;
		return true;
   	},
   
	setCSSClass: function(cssClass)
	{
		if (cssClass == null)
			return false;
		if (typeof(cssClass) != 'string')
			return false;
		this.element.className = cssClass;
	},
   
   	setTrigger: function(elem, trigger, event)
   	{
		if (event == null || trigger == null || elem == null)
			return false;
		switch (event)
		{
			case 'hide':
				event = this.hide;
				break;
			case 'show':
				event = this.show;
				this.hide();
				break;
			default:
				event = null;
				break;
		}
		if (event == null)
			return false;
		switch (trigger)
		{
			case 'mouseover':
				elem.onmouseover = event.bindAsEventListener(this);
				break;
			case 'mouseout':
				elem.onmouseout = event.bindAsEventListener(this);
				break;
			case 'click':
				elem.onclick = event.bindAsEventListener(this);
				break;
			default:
				elem = null;
				break;
		}
		if (elem == null)
			return false;
		return true;
	},
   
	hide: function()
	{
		Element.hide(this.element);
		this.visibility = 'hidden';
	},
   
	show: function()
	{
   		Element.show(this.element);
   		this.visibility = 'visible';
   	},
   
   	setReference: function(reference)
   	{
        if (typeof(reference) == "string")
    		reference = $(reference);
		if (reference != null && typeof(reference) == 'object')
        {
        	this.reference = reference
            var pos = Position.realOffset(reference);
            this.setPosition(pos[0] + reference.offsetWidth + 5, pos[1]);
        }
   	},
   	
   	setMargin: function(top, right, bottom, left)
   	{
   		var validFilter = (/(^[0-9]+$)|(^[0-9]+(px)$)/);
   		var digitFilter = (/^[0-9]+$/);
   		if (validFilter.test(top))
   		{	
			this.element.style.marginTop = (digitFilter.test(top)) ? top + 'px' : top;
   			this.margin.top = parseInt(top);
   		}
   		if (validFilter.test(right))
   		{	
			this.element.style.marginRight = (digitFilter.test(right)) ? right + 'px' : right;
   			this.margin.right = parseInt(right);
   		}
   		if (validFilter.test(bottom))
   		{	
			this.element.style.marginBottom = (digitFilter.test(bottom)) ? bottom + 'px' : bottom;
   			this.margin.bottom = parseInt(bottom);
   		}
   		if (validFilter.test(left))
   		{	
			this.element.style.marginLeft = (digitFilter.test(left)) ? left + 'px' : left;
   			this.margin.left = parseInt(left);
   		}
   	}
}