﻿var xiKey = 1;

function xiInsertPuzzle(req, flag) {
    var bShowAnswers = true;
    var bHold = false;

    if (typeof (flag) != 'undefined') {
        bHold = (flag == 'hold');
        bShowAnswers = (flag != false);
    }

    var key = req.replace('/', '-').replace('/', '-') + xiKey++;
    document.write("<div id='" + key + "Title' class='xiTitle'></div>");
    document.write("<div id='" + key + "Author' class='xiAuthor'></div>");
    document.write("<div id='" + key + "Notepad' style='line-height:normal; text-align:left' class='xiNotepad'></div>");
    document.write("<table id='" + key + "Table' class='xiTable' style='line-height:normal' cellpadding='0' cellspacing='0'></table>");
    document.write("<div id='" + key + "Copy' class='xiCopy'></div>");
    document.write("<div id='" + key + "Outer' style='margin-top:1px; height:40px'><div id='" + key + "Info' class='xiNotepad' style='line-height:normal; text-align:left; z-index:201'></div></div>");
    var qstring = "mini=true&track=true&date=" + req + "&key=" + key + "&show=" + bShowAnswers + "&hold=" + bHold + "&callback=?";
    $.getJSON("http://www.xwordinfo.com/JSON/Data.aspx", qstring, xiDisplayPuzzle);
}

function xiDisplayPuzzle(puzzle) {
    var xiTitleId = '#' + puzzle.key + 'Title';
    var xiAuthorId = '#' + puzzle.key + 'Author';
    var xiTableId = '#' + puzzle.key + 'Table';
    var xiCopyId = '#' + puzzle.key + 'Copy';
    var xiNotepadId = '#' + puzzle.key + 'Notepad';
    var xiInfoId = '#' + puzzle.key + 'Info';
    var xiOuterId = '#' + puzzle.key + 'Outer';
    $(xiTitleId).html(puzzle.hold ? 'Click the grid to reveal answers' : puzzle.title);
    $(xiAuthorId).html(puzzle.hold ? puzzle.title : 'by ' + puzzle.author);

    var n = 0;
    var grid = $(xiTableId)[0];
    for (var row = 0; row < puzzle.size.rows; row++) {
        var thisrow = grid.insertRow(row);
        for (var col = 0; col < puzzle.size.cols; col++) {
            var cell = thisrow.insertCell(col);
            var val = puzzle.grid[n];
            if (val == '.') {
                cell.className = 'xiBlack';
                val = ' ';
            }
            else {
                if (puzzle.circles != null && puzzle.circles[n] == 1)
                    cell.className = (val.length > 1) ? 'xiCircle xiRebus' : 'xiCircle';
                else if (val.length > 1)
                    cell.className = 'xiRebus';
            }
            cell.innerHTML = (puzzle.hold) ? ' ' : val;
            n++;
        }
    }

    $(xiTableId).css('position', 'relative'); // IE8 needs this
    $(xiTableId).css('z-index', 100);         // IE8

    if (puzzle.shadecircles)
        $(xiTableId + ' .xiCircle').css('background-color', '#e0e0e0');

    var wTable = grid.clientWidth;
    $(xiTitleId).css('width', wTable);
    $(xiAuthorId).css('width', wTable);
    $(xiNotepadId).css('width', wTable - 6);
    $(xiOuterId).css('width', wTable);

    if (puzzle.notepad) {
        $(xiNotepadId).css('display', 'block');
        $(xiNotepadId).html(puzzle.notepad);
    }

    $(xiCopyId).html('&copy; ' + puzzle.copyright);

    $(xiTableId).bind("mouseenter", function (event) { $('body').css('cursor', 'pointer'); $(xiInfoId).slideDown(300); });
    $(xiTableId).bind("mouseleave", function (event) { $('body').css('cursor', 'default'); $(xiInfoId).slideUp(300); });

    if (puzzle.downmap) {
        $(xiTableId + ' td').bind("mouseover", function (event) {
            var row = this.parentNode.rowIndex;
            var col = this.cellIndex;
            var n = (row * puzzle.size.cols) + col;
            if (puzzle.grid[n] == '.') {
                $(xiInfoId).html('&nbsp;<br />&nbsp;');
            }
            else {
                var across = "undefined";
                var down = "undefined";
                if (puzzle.acrossmap[n] != -1) across = puzzle.clues.across[puzzle.acrossmap[n]].replace('.', ' Across: ');
                if (puzzle.downmap[n] != -1) down = puzzle.clues.down[puzzle.downmap[n]].replace('.', ' Down: ');
                $(xiInfoId).html(across + '<br />' + down);
            }
        });
    }
    else
        $(xiTableId).attr('title', 'Click grid to reveal answers.');

    $(xiTableId).bind('click', function (event) {
        if (puzzle.hold) {
            if (puzzle.revealtime) {
                $(xiTitleId).html('Solution not yet available');
                $(xiAuthorId).html('Try again in ' + puzzle.revealtime);
            }
            else {
                $(xiTitleId).html(puzzle.title);
                $(xiAuthorId).html('by ' + puzzle.author);
            }

            var n = 0;
            var grid = $(xiTableId)[0];
            for (var row = 0; row < puzzle.size.rows; row++)
                for (var col = 0; col < puzzle.size.cols; col++) {
                    val = puzzle.grid[n++];
                    grid.rows[row].cells[col].innerHTML = (val == '.') ? ' ' : val;
                }

            $(xiTableId).attr('title', '');
            var wTable = grid.clientWidth;
            $(xiTitleId).css('width', wTable);
            $(xiAuthorId).css('width', wTable);
            $(xiNotepadId).css('width', wTable - 6);
            $(xiOuterId).css('width', wTable);
            puzzle.hold = false;
        }
        else
            document.location.href = 'http://www.xwordinfo.com/ShowPuzzle.aspx?date=' + puzzle.date;
    });
}