var Scheda = new Class({

    mark:'',

    row1:[0,0,0, 0,0,0, 0,0,0],
    row2:[0,0,0, 0,0,0, 0,0,0],
    row3:[0,0,0, 0,0,0, 0,0,0],

    ranges: [[1,9],[10,19],[20,29],[30,39],[40,49],[50,59],[60,69],[70,79],[80,90]],

    initialize: function(mark){

        this.mark = mark

        var $this = this

        var ranges = []
        var random = 0

        ranges = this.randomRanges()
        ranges.each(function(item, index){
            random = 0
            while( this.row1.contains(random) || this.row2.contains(random) || this.row3.contains(random) ){
                random=this.randomNumber(item[0],item[1])
            }
            this.row1[ this.rangeIndex(item[0]) ] = random
        }.bind(this))

        ranges = this.randomRanges()
        ranges.each(function(item, index){
            random = 0
            while( this.row1.contains(random) || this.row2.contains(random) || this.row3.contains(random) ){
                random=this.randomNumber(item[0],item[1])
            }
            this.row2[ this.rangeIndex(item[0]) ] = random
        }.bind(this))

        ranges = this.randomRanges()
        ranges.each(function(item, index){
            random = 0
            while( this.row1.contains(random) || this.row2.contains(random) || this.row3.contains(random) ){
                random=this.randomNumber(item[0],item[1])
            }
            this.row3[ this.rangeIndex(item[0]) ] = random
        }.bind(this))

        for(var i=0;i<9;i++){
            this.sortRow(i)
        }

    },

    randomNumber: function(min,max) {
        //return Math.floor(Math.random()*(max-min+1)+min);
        return phpRandom(min,max);
    },

    randomRanges: function(){
        var clone = Array.clone(this.ranges)
        for(var i=0;i<4;i++){
            clone.splice(this.randomNumber(0, clone.length -1),1)
        }
        return clone
    },

    rangeIndex: function(rangeStart){
        switch(rangeStart){
            default:
            case 1:
                return 0;
                break;
            case 10:
                return 1;
                break;
            case 20:
                return 2;
                break;
            case 30:
                return 3;
                break;
            case 40:
                return 4;
                break;
            case 50:
                return 5;
                break;
            case 60:
                return 6;
                break;
            case 70:
                return 7;
                break;
            case 80:
                return 8;
                break;
        }
    },

    render: function(root){
        var $this=this
        var row1 = this.renderRow(this.row1)
        var row2 = this.renderRow(this.row2)
        var row3 = this.renderRow(this.row3)
        var table = new Element('table',{
            'class':'scheda'
        })
        var tableHeader = new Element('caption',{
            html:'TombolAmica - '+$this.mark
            } )
        tableHeader.inject(table)
        row1.inject(table)
        row2.inject(table)
        row3.inject(table)
        table.inject(root)
    },

    renderRow: function(row){        
        var html = ''
        for(var i=0;i<9;i++){
            html += '<td>'+(row[i]>0?row[i]:'&nbsp;')+'</td>'
        }
        var tr = new Element('tr',html)
        Elements.from(html).inject(tr)
        return tr
    },

    sortRow: function(n){
        var aux=0
        if(this.row1[n]!=0 && this.row2[n]!=0 && this.row1[n]>this.row2[n]){
            aux = this.row2[n]
            this.row2[n] = this.row1[n]
            this.row1[n] = aux
        }
        if(this.row2[n]!=0 && this.row3[n]!=0 && this.row2[n]>this.row3[n]){
            aux = this.row3[n]
            this.row3[n] = this.row2[n]
            this.row2[n] = aux
        }
        if(this.row1[n]!=0 && this.row3[n]!=0 && this.row1[n]>this.row3[n]){
            aux = this.row3[n]
            this.row3[n] = this.row1[n]
            this.row1[n] = aux
        }
        if(this.row1[n]!=0 && this.row2[n]!=0 && this.row1[n]>this.row2[n]){
            aux = this.row2[n]
            this.row2[n] = this.row1[n]
            this.row1[n] = aux
        }
    }


});
