/*
    Shaun The Sheep Sudoku (Kids Remix)
    v1.0 - Richard Davey (richard.davey@aardman.com)
    18th October 2007
*/

    $(document).ready(function()
    {

        function setCounterPlacement (c, x, y)
        {
            this.counterXY[c] = new Array(x, y);
        }
        
        function setCounterValue (c, v)
        {
            this.counterValue[c] = Number(v);
        }

        function getCounterOrigins (c)
        {
            return this.counterXY[c];
        }

        function setGridValue (x, v)
        {
            this.contents[x] = Number(v);
        }

        function getGridValue (x)
        {
            return Number(this.contents[x]);
        }
        
        function getCounterInGridValue (c)
        {
            var v = this.contents[c];
            
            if (v == 0)
            {
                return 0;
            }
            else
            {
                return Number(this.counterValue[v]);
            }
        }

        function getCounterInGrid (c)
        {
            for (i = 1; i <= 16; i++)
            {
                if (this.contents[i] == c)
                {
                    return i;
                }
            }
            
            return false;
        }
        
        function setGridOrigins ()
        {
            g = 1;
            offset_x = 59;
            offset_y = 174;
            
            for (r = 0; r < 4; r++)
            {
                gy = 80 * r;
                
                for (c = 0; c < 4; c++)
                {
                    gx = 80 * c;
                    
                    this.gridX[g] = gx + offset_x + 15;
                    this.gridY[g] = gy + offset_y + 15;
                    g++;
                }
            }
            
            //console.log(this.gridX);
            //console.log(this.gridY);
        }

        function getGridOriginsX (g)
        {
            return this.gridX[g];
        }

        function getGridOriginsY (g)
        {
            return this.gridY[g];
        }

        function gameBoard ()
        {
            this.contents = new Array();
            this.counterXY = new Array();
            this.counterValue = new Array();
            this.gridX = new Array();
            this.gridY = new Array();

            this.initGrid = setGridOrigins;
            this.init = setCounterPlacement;
            this.initValue = setCounterValue;
            
            this.set = setGridValue;
            this.get = getGridValue;
            this.getV = getCounterInGridValue;
            this.getCounter = getCounterInGrid;
            this.getCounterXY = getCounterOrigins;
            this.getGridX = getGridOriginsX;
            this.getGridY = getGridOriginsY;
            
        }
        
        function placeFixedCounter (grid_ref, counter, counter_value)
        {
            //  Mark it in the grid array
            gb.set(grid_ref, counter);
            gb.initValue(counter, counter_value);

            //  Position it
            $("#counter" + counter).css("left", gb.getGridX(grid_ref) + "px");
            $("#counter" + counter).css("top", gb.getGridY(grid_ref) + "px");
        }

        function checkGridOccupied (grid_obj)
        {
            var counter = currentPiece.piece;
            var grid = $(grid_obj).attr("id").substring(4);

            //console.log("counter" + counter + " was dropped into grid position " + grid);

            //  Grid space occupied?
            if (gb.get(grid) > 0)
            {
                //  Yes, so reset the counter location
                resetCounterLocation(counter);
            }
            else
            {
                gb.set(grid, counter);
                smoothCounterIntoGrid(grid, counter);
                //  Set the current X and Y to be the counters new XY (so it moves back on an invalid move, rather than home)
            }

            //  Detect if they've won the game + update the ticks
            checkForWin();
        }
        
        function smoothCounterIntoGrid(grid, counter)
        {

            $("#counter" + counter).animate({
                left: gb.getGridX(grid),
                top: gb.getGridY(grid)
            }, 500 );
        
            //console.log("smoothing to:");
            //console.log(gb.getGridX(grid));
            //console.log(gb.getGridY(grid));
        
        }
        
        function resetCounterLocation (counter)
        {
            var xy = gb.getCounterXY(counter);
            
            $("#" + currentPiece.id).animate({
                left: xy[0],
                top: xy[1]
            }, 500 );
        }
        
        function checkRow (r1, r2, r3, r4, tick_id)
        {
            var cell = new Array(gb.getV(r1), gb.getV(r2), gb.getV(r3), gb.getV(r4));
            
            var r1 = false;
            var r2 = false;
            var r3 = false;
            var r4 = false;
            
            //console.log("Cells: " + cell);

            for (i = 0; i < 4; i++)
            {
                switch(cell[i])
                {
                    case 1:
                        r1 = true; break;
                    case 2:
                        r2 = true; break;
                    case 3:
                        r3 = true; break;
                    case 4:
                        r4 = true; break;
                }
            }
            
            if (r1 == true && r2 == true && r3 == true && r4 == true)
            {
                $("#tick" + tick_id).removeClass("tick");
                $("#tick" + tick_id).addClass("tick_on");
                //console.log("tick on:" + tick_id);
                return true;
            }
            else
            {
                $("#tick" + tick_id).removeClass("tick_on");
                $("#tick" + tick_id).addClass("tick");
                return false;
            }
            
        }
        
        function checkForWin ()
        {
            row1 = checkRow(1, 2, 3, 4, 1);
            row2 = checkRow(5, 6, 7, 8, 2);
            row3 = checkRow(9, 10, 11, 12, 3);
            row4 = checkRow(13, 14, 15, 16, 4);
            col1 = checkRow(1, 5, 9, 13, 5);
            col2 = checkRow(2, 6, 10, 14, 6);
            col3 = checkRow(3, 7, 11, 15, 7);
            col4 = checkRow(4, 8, 12, 16, 8);
            
            if (row1 == false || row2 == false || row3 == false || row4 == false || col1 == false || col2 == false || col3 == false || col4 == false)
            {
                //console.log("win check failed");
                return false;
            }
            
            $("#gamewin").show("slow");
            
            return true;
        }








        //  Set-up our game board (4x4 grid)

        var gb = new gameBoard();
        gb.initGrid();
        
        for (i = 1; i <= 16; i++)
        {
            gb.set(i, 0);
        }
        
        var currentPiece = function() {
            this.id = false;
            this.piece = 0;
            this.x = 0;
            this.y = 0;
        };

        //  The X&Y of the counter container
        var c_width = 55;
        
        //  Now set all of the counters in two strips
        for (c = 1; c <= 12; c++)
        {
            if (c <= 6)
            {
                var cx = (c_width * c) - c_width;
                var cy = 0;
            }
            else
            {
                var cx = (c_width * (c - 6)) - c_width;
                var cy = 56;
            }
            
            cx = cx + 392;
            cy = cy + 58;

            $("#counter" + c).css("position", "absolute");
            $("#counter" + c).css("display", "block");
            $("#counter" + c).css("left", cx + "px");
            $("#counter" + c).css("top", cy + "px");
            
            //console.log("cx: " + cx + " top:" + cy);
            
            gb.init(c, cx, cy);
            
            //  And the counter value
            if (c % 4 == 0)
            {
                gb.initValue(c, 4);
            }
            else
            {
                gb.initValue(c, c % 4);
            }
        }
        
        //  Place the fixed counters
        switch (sudoku_level)
        {
            case 1:
                placeFixedCounter(1, 13, 1);
                placeFixedCounter(4, 14, 2);
                placeFixedCounter(13, 15, 3);
                placeFixedCounter(16, 16, 4);
                break;
                
            case 2:
                placeFixedCounter(1, 13, 1);
                placeFixedCounter(7, 14, 2);
                placeFixedCounter(12, 15, 3);
                placeFixedCounter(14, 16, 4);
                break;
                
            case 3:
                placeFixedCounter(2, 13, 1);
                placeFixedCounter(7, 14, 2);
                placeFixedCounter(13, 15, 3);
                placeFixedCounter(16, 16, 4);
                break;
                
            case 4:
                placeFixedCounter(3, 15, 3);
                placeFixedCounter(5, 13, 1);
                placeFixedCounter(9, 14, 2);
                placeFixedCounter(14, 16, 4);
                break;
                
            case 5:
                placeFixedCounter(2, 16, 4);
                placeFixedCounter(15, 14, 2);
                placeFixedCounter(10, 15, 3);
                placeFixedCounter(7, 13, 1);
                break;
                
            default:
                placeFixedCounter(1, 13, 1);
                placeFixedCounter(4, 14, 2);
                placeFixedCounter(13, 15, 3);
                placeFixedCounter(16, 16, 4);
        }
        
        //  Make pieces dragable
        $('.piece').draggable({
            containment: '#gameboard',
            start: function(e, ui)
            {
                currentPiece.piece = $(this).attr("id").substring(7);
                currentPiece.id = $(this).attr("id");

                //  Make it hover above other elements
                $(this).css("z-index", "100");
                
                //  If this piece was already in the grid, we need to reset that
                var current_grid = gb.getCounter(currentPiece.piece);

                if (current_grid !== false)
                {
                    gb.set(current_grid, 0);
                }
            },
            stop: function(e, ui)
            {
                //  Lower it back down again
                $(this).css("z-index", "5");
            }
        });
        
        //  Make grid droppable
        $(".grid").droppable({
            accept: '.piece',
            tolerance: 'intersect',
            hoverClass: 'grid_hover',
            drop: function(e, ui)
            {
                checkGridOccupied(this);
            }
        });

    });


