//##############################################################################
//------------------------------------------------------------------------------
function Word()
{    
    Entity.apply(this);

	this.isWord = true;
    this.text = '';
    this.dataPage = 0;
    this.loading = false;
}

Word.prototype = new Entity();

//------------------------------------------------------------------------------
Word.prototype.startSearches = function(priority)
{
    if(this.didSearch)
        return;
        
    var args = 'method=flickr.photos.search&sort=interestingness-desc&content_type=1&per_page=20&tags=' + this.text;
    
    this.loading = true;
    getXML(flickrPath + '?args=' + escape(args), parseFlickr, this, priority);

    this.didSearch = true;
    this.dataPage = 1;
}

//------------------------------------------------------------------------------
Word.prototype.loadMore = function()
{       
	if(this.loading || !this.didSearch)
		return;
		
    this.dataPage++;
    var args = 'method=flickr.photos.search&sort=interestingness-desc&content_type=1&per_page=20'
            + '&page=' + this.dataPage + '&tags=' + this.text;
    
    this.item.div.style.color = '#00797D';
    
    this.loading = true;
    getXML(flickrPath + '?args=' + escape(args), parseFlickr, this, 1);
}

//##############################################################################
//------------------------------------------------------------------------------
function parseFlickr(http_request, requester)
{
	requester.loading = false;
	
	if(hoverItem == requester.item)
		requester.item.div.style.color = tagHighlightColor;
	else	
		requester.item.div.style.color = '#ffffff';
	
    var file = http_request.responseXML;
    if(file)
    {
        var velocityRange = 40 * requester.childScale;
        var photos = file.getElementsByTagName('photo');
        var count = photos.length;
        var a;
        for(a = 0; a < count; a++)
        {
            var photo = photos[a];
            var farm = getXMLAttribute(photo, 'farm');
            var server = getXMLAttribute(photo, 'server');
            var secret = getXMLAttribute(photo, 'secret');
            var id = getXMLAttribute(photo, 'id');
            var owner = getXMLAttribute(photo, 'owner');
            var title = getXMLAttribute(photo, 'title');
            if(!title)
            	title = '(untitled)';
                    
            var urlBase = 'http://farm' + farm + '.static.flickr.com/' + server + '/' 
                    + id + '_' + secret;
                    
            var linkURL = 'http://www.flickr.com/photos/' + owner + '/' + id + '/'; 
            
            var item = new ImageItem(urlBase + '_t.jpg');
            item.bigURL = urlBase + '.jpg';
            item.linkURL = '';
            item.size = 30 * requester.childScale;
            item.velocityX = 0;
            item.velocityY = 0;
            item.relativePosX = (Math.random() * velocityRange) - (velocityRange / 2);
            item.relativePosY = (Math.random() * velocityRange) - (velocityRange / 2);;
            item.posX = requester.item.posX + item.relativePosX;
            item.posY = requester.item.posY + item.relativePosY;
            item.parent = requester.item;
            
            var photo = new Photo();
            photo.childScale = requester.childScale * entityScaleFactor;
            photo.id = id;
            photo.secret = secret;
            photo.item = item;
            item.entity = photo;
            
            photo.label = new Label(item, title, 5 * photo.childScale, linkURL, owner);

            if(requester.itemsAdded && requester.item == controller.focusItem)
                doc.addItem(item);

            requester.items.push(item);
            requester.entities.push(photo);
        }
    }
    
   	controller.onSearchComplete(requester);
}