var colorTable = ['300', '330', '360', '390', '3C0', '3F0', '6F0', '6C0', '690', '660', '630', '600', 'F00', 'F30', 'F60', 'F90', 'FC0', 'FF0', '303', '333', '363', '393', '3C3', '3F3', '6F3', '6C3', '693', '663', '633', '603', 'F03', 'F33', 'F63', 'F93', 'FC3', 'FF3', '306', '336', '366', '396', '3C6', '3F6', '6F6', '6C6', '696', '666', '636', '606', 'F06', 'F36', 'F66', 'F96', 'FC6', 'FF6', '309', '339', '369', '399', '3C9', '3F9', '6F9', '6C9', '699', '669', '639', '609', 'F09', 'F39', 'F69', 'F99', 'FC9', 'FF9', '30C', '33C', '36C', '39C', '3CC', '3FC', '6FC', '6CC', '69C', '66C', '63C', '60C', 'F0C', 'F3C', 'F6C', 'F9C', 'FCC', 'FFC', '30F', '33F', '36F', '39F', '3CF', '3FF', '6FF', '6CF', '69F', '66F', '63F', '60F', 'F0F', 'F3F', 'F6F', 'F9F', 'FCF', 'FFF', '00F', '03F', '06F', '09F', '0CF', '0FF', '9FF', '9CF', '99F', '96F', '93F', '90F', 'C0F', 'C3F', 'C6F', 'C9F', 'CCF', 'CFF', '00C', '03C', '06C', '09C', '0CC', '0FC', '9FC', '9CC', '99C', '96C', '93C', '90C', 'C0C', 'C3C', 'C6C', 'C9C', 'CCC', 'CFC', '009', '039', '069', '099', '0C9', '0F9', '9F9', '9C9', '999', '969', '939', '909', 'C09', 'C39', 'C69', 'C99', 'CC9', 'CF9', '006', '036', '066', '096', '0C6', '0F6', '9F6', '9C6', '996', '966', '936', '906', 'C06', 'C36', 'C66', 'C96', 'CC6', 'CF6', '003', '033', '063', '093', '0C3', '0F3', '9F3', '9C3', '993', '963', '933', '903', 'C03', 'C33', 'C63', 'C93', 'CC3', 'CF3', '000', '030', '060', '090', '0C0', '0F0', '9F0', '9C0', '990', '960', '930', '900', 'C00', 'C30', 'C60', 'C90', 'CC0', 'CF0', '000', '111', '222', '333', '444', '555', '666', '777', '888', '999', 'AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'F80', 'AC4'];

var firstTime = true;
function getRandomPastel()
{
    var palette = ['0', '8', 'f'];
    var color = "";

    color = colorTable[Math.floor(Math.random()*colorTable.length)];
    if (firstTime)
    {
        firstTime = false;
        return 'f80';
    }
    else
        return color;
}

var ToolBoxPluginPlaces = Class.create();
ToolBoxPluginPlaces.prototype = Object.extend(new ToolBoxPlugin, {
    initialize: function(parent, name, map, color, sequence)
    {
        this.basic(parent, name);
        var random_color = getRandomPastel();
        if (color != null)
          random_color = color;
        this.source_marker = new GIcon();
        this.source_marker = Object.extend(this.source_marker, {
            image: '/markers/' + random_color + '.png',
            shadow: '/markers/s.png',
            iconSize: new GSize(20,34),
            shadowSize: new GSize(36,34),
            iconAnchor: new GPoint(10,34),
            infoWindowAnchor: new GPoint(0,1)
        });
        this.source_marker_route = new GIcon();
        this.source_marker_route = Object.extend(this.source_marker_route, {
            image: '/markers/small/' + random_color + '.png',
            shadow: '/markers/small/s.png',
            iconSize: new GSize(12,20),
            shadowSize: new GSize(22,20),
            iconAnchor: new GPoint(6,20),
            infoWindowAnchor: new GPoint(5,1)
        });
        
        this.map = map;
        this.list = new Array();
        this.itemsCounter = 1;
        this.sequence = sequence;
        this.colorInput = random_color.toLowerCase();
        this.itemsRouteCounter = 1;
        this.activeItem = -1;
        this.pluginDiv = document.createElement('div');
        this.pluginDiv.className = 'toolBoxContent';
        // places
        this.listDiv = document.createElement('div');
        this.listDiv.className = 'toolBoxContent2 c00 nomargins';
        this.pluginDiv.appendChild(this.listDiv);
        this.updateList();
        this.oncolor();
    },
    onresize: function(height)
    {
        this.pluginDiv.style.height = height + "px";
    },
    oncolor: function()
    {
      var temp = this.colorInput;
      temp += '.png';
      var temp_route = '/markers/small/' + temp;
      temp = '/markers/' + temp;
      this.source_marker.image = temp;
      this.source_marker_route.image = temp_route;
      this.color = "#" + this.colorInput;
      this.toolBoxColorDiv.style.backgroundColor = this.color;
      for (var i=0; i<this.list.length; i++)
          this.list[i].refreshMarker();
    },
    updateList: function()
    {
        clearInnerHTML(this.listDiv);
        if (this.list.length == 0)
            this.listDiv.innerHTML = '<center><i>' + lang[22] +'</i></center>';
        else
        {
            for (var i=0; i<this.list.length; i++)
            {
                this.listDiv.appendChild(this.list[i].getDiv());
            }
        }
    },    
    getSelected: function()
    {
        for (var i=0; i<this.list.length; i++)
        {
            if (this.list[i].selected == true)
                return this.list[i];
        }
        return null;
    },
    selectItem: function(no, centerOnIt)
    {
        this.parent.selectByPlugin(this);
        if ((no >= 0)==false)
            no = this.list.indexOf(no);
        var item = this.list[no];
        if (!item.selected)
        {
            this.deselectAllItems();
            item.select(true);
            if (centerOnIt) item.centerMapOnIt();
        }
        this.activeItem = no;
    },
    deselectAllItems: function()
    {
        this.resetMapToolBar();
        this.list.each( function(item, index){
            item.select(false);
        });
    },
    addNewItem: function()
    {
        var selected = this.getSelected();
        //if (selected == null || (selected != null && selected.initialized()))
        {
            this.list.reverse();
            this.list.push(new Place(this, lang[23] + ' ' + this.itemsCounter, ''));
            this.list.reverse();
            this.selectItem(0, false);
            this.updateList();
            this.itemsCounter++;
        }// else alert('Po prostu kliknij na mapę...');
    },
    addNewItemRoute: function()
    {
        var selected = this.getSelected();
        //if (selected == null || (selected != null && selected.initialized()))
        {
            this.list.reverse();
            this.list.push(new Route(this, lang[24] + ' ' + this.itemsRouteCounter, '', false));
            this.list.reverse();
            this.selectItem(0, true);
            this.updateList();
            this.itemsRouteCounter++;
        }// else alert('Po prostu kliknij na mapę...');
    }, 
    onDeselect: function()
    {
        this.deselectAllItems();
    },
    centerMapOnSelected: function()
    {
        this.getSelected().centerMapOnIt();
    },
    resetMapToolBar: function()
    {
    },
    getFullColor: function()
    {
        var c = this.color;
        return "#" + c.substr(1,1) + '0' + c.substr(2,1) + '0' + c.substr(3,1) + '0';
    }
});
