/*
 * Copyright IPVision, Inc 2003 - 2006.   All rights reserved.
 */


function UIMapThumbnails(entityMap, scale, thumbnailUriFcn, contextMenu)
{
	if(arguments.length == 1 && arguments[0] == UI_PROTO)
		return;

	this.uiMapThumbnailsInit(entityMap, scale, thumbnailUriFcn, contextMenu);
}

// Inherit member functions and default member values from base class
UIMapThumbnails.prototype = UiPrototype(UIElement);

// Initialize default member values
UIMapThumbnails.prototype.classId = "uiMapThumbnails";

UIMapThumbnails.prototype.uiMapThumbnailsInit = function(entityMap, scale, thumbnailUriFcn, contextMenu)
{
	this.uiElementInit();

	this.entityMap = entityMap ? entityMap : {};
	this.legendGroup = entityMap.LegendGroup ? entityMap.LegendGroup : [];
	this.scale = scale;
	this.thumbnailUriFcn = thumbnailUriFcn;   
    this.contextMenu = contextMenu;
}

UIMapThumbnails.prototype.activateContextMenu = function(event, itemNumber)
{
    if(this.contextMenu)
        this.contextMenu.activateAtClick(event, [itemNumber]);
    UiCancelEvent(event);
}

UIMapThumbnails.prototype.build = function()
{
	if(null == this.thumbnailUriFcn)
		return;

    this.addHTML('<table cellpadding="4"><tr><td colspan="4" align="center">');
    this.addHTML('Click on thumbnail image to select MapAnalysis.');
    if(this.contextMenu)
        this.addHTML(' Right-click for other options.');
    this.addHTML('</td>');

    var legendsDisabled = true;
    for(var k = 0; k < this.legendGroup.length; k++) {
        if(this.legendGroup[k].isDisabled != "true") {
            legendsDisabled = false;
            break;
        }
    }
    // show 1st thumnail with all legends disabled
    var uri = this.thumbnailUriFcn(this.scale, GC.LEGEND_SELECT_NONE);
    var border = legendsDisabled ? 'style="border: solid 3px red;"' : "";
    var onClick = ' onClick="doAction(\'legendSelect\',' + GC.LEGEND_SELECT_NONE + ')"';
    var onContextMenu = ' onContextMenu="GetUiElem(' + this.id + ').activateContextMenu(event,' + GC.LEGEND_SELECT_NONE + ')"';
    this.addHTML('</tr><tr><td height="10"></td></tr><tr>');
    this.addHTML('<td align="center"><table ' + border + '>');
    this.addHTML('<tr><td align="center"><img src="' + uri + '"' + onClick + onContextMenu + '"/></td></tr>');
    this.addHTML('<tr><td align="center">Base Map, without analysis</td></tr></table></td>');

	for(var k = 0; k < this.legendGroup.length; k++)
	{
        var legend = this.legendGroup[k];
        uri = this.thumbnailUriFcn(this.scale, k);
        border = (legend.isDisabled != "true") ? 'style="border: solid 3px red;"' : "";
        onClick = ' onClick="doAction(\'legendSelect\',' + k + ')"';
        onContextMenu = ' onContextMenu="GetUiElem(' + this.id + ').activateContextMenu(event,' + k + ')"';
        if(k % 4 == 3)
			this.addHTML('</tr><tr><td height="10"></td></tr><tr>');

		this.addHTML('<td align="center"><table ' + border + '>');
		this.addHTML('<tr><td align="center"><img src="' + uri + '"' + onClick + onContextMenu + '"/></td></tr>');
		this.addHTML('<tr><td align="center">' + legend.title + '</td></tr></table></td>');
	}
    this.addHTML('</tr></table>');
}
