﻿var _currentTool = 1;
var _currentColor = 1;
var _cellElements = new Array();
var _border = false;

function InitFloorDesigner()
{
    var table = document.getElementById('tblFloorDesigner').childNodes[0];
    
    for (var row = 0; row < _length; row++) // The rows
    {
        for (var cell = 0; cell < _width; cell++)
        {
            var index = (row *_width) + cell;
            _cellElements[index] = table.childNodes[row + 1].childNodes[cell + 1];
            if (_cellColorIndexes[index])
                applyTile(_cellElements[index],_cellColorIndexes[index]);
            else    
                applyTile(_cellElements[index],0);
        }
    }
    UpdateStatusLabel();
}
//function ApplyBorder()
//{
//    var oldTool = _currentTool;
//    var oldColor = _currentColor;
//    
//    _currentTool = 3;
//    applyTool(null,0,0);
//    applyTool(null,_length,_length);
//    _currentTool = 2;
//    applyTool(null,0,0);
//    applyTool(null,_width,1);
//    _currentTool = oldTool;
//    
//}
function SetHiddenValues(element)
{
    var indexString = _cellColorIndexes.join(',');
    
    document.getElementById(element).value = indexString;
}
function OnCellOver(cell)
{
    
}
function OnCellClick(e,index,row)
{
    applyTool(e,index,row,_currentTool);
}
function colorChange(sender, eventArgs)
{
    _currentColor = getColorIndex(sender.get_selectedColor());
    if (_border)
        ApplyBorder();
}
function getColorIndex(color)
{
    for (var i = 0;i < _cellColors.length; i++)
    {
        if (_cellColors[i] == color)
        {
            return i;
        }
    }
    return 0;
}
function UpdateStatusLabel()
{
    var lbl = document.getElementById(_summaryLabel);
    var summary = "";
    var colorTotals = new Array();
    var summaryTotals = new Array();

    for (var i = 0; i < _cellColorIndexes.length; i++)
    {
        var index = parseInt(_cellColorIndexes[i]);
        var colorName = _colorAliases[_cellColorIndexes[i]];
        if (!colorTotals[index])
            colorTotals[index] = 0;    
        colorTotals[index] += 1;
        summaryTotals[index] = '<b><span style="color:' + _cellColors[index] + '">' + colorTotals[index] + " " + colorName + "</span></b>&nbsp;Tiles.<br/>";
    }
    
    for (var i = 0; i< _cellColors.length; i++)
    {
        if (!summaryTotals[i]) continue;
        summary += summaryTotals[i];
    }
    lbl.innerHTML = summary;
}
function applyTool(e,index,row,tool)
{
    if (tool == 1)
    {
        applyTile(_cellElements[index],_currentColor,index);
        UpdateStatusLabel();
    }
    else if (tool == 2)
    {
       var rowIndex = index - (row * _width);
       for (var i = 0; i < _length;i++)
       {
            applyTile(_cellElements[(i * _width) + rowIndex],_currentColor,(i * _width) + rowIndex);
       }
       UpdateStatusLabel();
        
    } else if (tool == 3)
    {
        
       var rowStart = (row * _width);
       var rowMax = rowStart + _width;

       for (var i = rowStart; i < rowMax;i++)
       {
            applyTile(_cellElements[i],_currentColor,i);
       }
       UpdateStatusLabel();
    
    }
}
function applyTile(cell,colorIndex,index)
{
    
     cell.style.backgroundColor = _cellColors[colorIndex];
     _cellColorIndexes[index] = colorIndex;
     SetHiddenValues(_hiddenFieldId);
}
