//##############################################################################
var textMeasurementDiv = null;

//------------------------------------------------------------------------------
function TextItem(text, fontSize, align)
{ 
    Item.apply(this);
    
    if(typeof(align) == 'undefined')
        align = 'center';
    
    this.fontSize = fontSize;
    this.text = '';
    this.linkURL = '';
    
    var div = document.createElement('div');
    div.onmouseover = albumMouseOver;
    div.onmouseout = albumMouseOut;
    div.style.position = 'absolute';   
    div.style.visibility = 'hidden';
    div.opacityMaster = 0;
    div.innerHTML = text;
    div.style.color = '#ffffff';
    div.style.textAlign = align;
    div.style.zIndex = '100';
    div.style.cursor = 'default';
    // div.style.border = '1px solid #ffffff';    
    div.onmousedown = dragStart;         
    div.style.whiteSpace = 'nowrap';

    div.mdItem = this;
    this.div = div;

    if(text)
        this.setText(text);
    else
    {
        this.sizeX = this.fontSize * 10;
        this.sizeY = this.fontSize;
        div.style.backgroundImage = 'url(img/word' + getRandomInt(17) + '.png)';
        div.style.backgroundRepeat = 'no-repeat';
        div.style.backgroundPosition = 'center';
    }
    
    this.attached = false;
        
    needsUpdate = true;    
}

TextItem.prototype = new Item();

//------------------------------------------------------------------------------
TextItem.prototype.isLoaded = function()
{
    return true;
}

//------------------------------------------------------------------------------
TextItem.prototype.getIdentifier = function()
{
    return this.text.toLowerCase();
}

//------------------------------------------------------------------------------
TextItem.prototype.addToWorld = function()
{   
    if(this.attached)
        return;
        
    var baseElement = document.getElementById('content');

    if(this.div)
    {
        this.div.style.visibility = 'hidden';
        baseElement.appendChild(this.div);
    }
    
    this.attached = true;
}
    
//------------------------------------------------------------------------------
TextItem.prototype.removeFromWorld = function()
{   
    if(!this.attached)
        return;
        
    var baseElement = document.getElementById('content');

    if(this.div)
        baseElement.removeChild(this.div);
    
    this.attached = false;
}
    
//------------------------------------------------------------------------------
TextItem.prototype.setText = function(text)
{
    this.text = text;
    var fixedText = this.text.replace(/ /g, '&nbsp;');
    
    if(this.linkURL)
	{
		this.div.className = 'textItem';
		this.div.innerHTML = '<a href="' + this.linkURL + '" target="_blank">' + fixedText + '</a>';
	}
	else
	{
		this.div.className = '';
		this.div.innerHTML = fixedText;
	}
	
    this.div.style.backgroundImage = '';
    
    var width = 0;
    var lines = text.split('<br>');
    var a;
    for(a = 0; a < lines.length; a++)
    {
        if(lines[a].length > width)
            width = lines[a].length;
    }
    
    this.sizeY = this.fontSize * 1.2 * lines.length;

	if(!textMeasurementDiv)
	{
		textMeasurementDiv = document.createElement('div');
		textMeasurementDiv.style.fontSize = '100px';
		textMeasurementDiv.style.position = 'absolute';
		textMeasurementDiv.style.visibility = 'hidden';
		textMeasurementDiv.style.whiteSpace = 'nowrap';
		var baseElement = document.getElementById('content');
		baseElement.appendChild(textMeasurementDiv);
	}

	textMeasurementDiv.innerHTML = fixedText;
	this.sizeX = ((textMeasurementDiv.offsetWidth * this.fontSize) / 100) * 1.05;
}

//------------------------------------------------------------------------------
TextItem.prototype.setLink = function(url)
{
	this.linkURL = url;
	this.setText(this.text);
}

//------------------------------------------------------------------------------
TextItem.prototype.setColor = function(color)
{
    this.div.style.color = color;
}

