var numNewComments = 0;
var undefined;


function showEmbedWindow(inEmbedURL)
{
        var embedWindow = window.open(inEmbedURL, "embedWindow", "width=400,height=460,status=no,toolbar=no,resizable=yes,scrollbars=no,location=yes");
        embedWindow.focus();
}

function showSpotPlayer(inSpotID)
{
        var spotWindow = window.open("/spot/play/" + inSpotID, "spotWindow", "width=200,height=25,status=no,toolbar=no,resizable=yes,scrollbars=no,location=yes");
        spotWindow.focus();
}

function showMessageSender(inMessageURL)
{
        var messageWindow = window.open(inMessageURL+"?popup=1", "messageWindow", "width=400,height=460,status=no,toolbar=no,resizable=yes,scrollbars=no,location=yes");
        messageWindow.focus();
}

function showDemo(inURL)
{
        var demoWindow = window.open(inURL, "demoWindow", "width=740,height=145,status=no,toolbar=no,resizable=yes,scrollbars=no,location=yes");
        demoWindow.focus();
}

function hideToDo(inXML)
{
    setTimeout('switchoff(\"todo\")', 50);
}

function updateAddComment(inXML)
{
        var table = document.getElementById("commentTable");
        var text = document.getElementById("text").value;

    numNewComments++;
    table.innerHTML += "<div style=\"display:none\" id=\"newcomment" + numNewComments + "\">" + text + "</div>" + "<hr>";

    setTimeout('appear(\"newcomment'+numNewComments+'\")', 50);
}

function addComment()
{
    var keys = new Array(2);
    keys[0] = "text";
    keys[1] = "spotID";
    var values = new Array(2);
    values[0] = document.getElementById('text').value;
    values[1] = document.getElementById('spotID').value;
    ajaxPostMultiple("/spotme/EditComment", keys, values, updateAddComment);
}

function lightStar(bitarray, spotid, suffix)
{
//    alert("lightStar(" + bitarray + ", " + suffix + ")");
    if (suffix == undefined) suffix = "";
    for (var i = 0; i < bitarray.length; i++) {
        var source = "/images/unlitstar.gif";
        var imageid = "star_" + spotid + "_" + (i+1) + suffix;
        if (bitarray.charAt(i) == '1') {
            source = "/images/litstar.gif";
        }
        var theimage = document.getElementById(imageid);
        theimage.src = source;
    }
}

function updateClickStar(inXML)
{
    var responseElement = inXML.getElementsByTagName("result")[0];
    var spotid = responseElement.getAttribute("spotID");
    var numVotes = responseElement.getAttribute("numRatings");
    var ratingsSpanID = "numRatingsSpan" + spotid;
    var ratingsSpan = document.getElementById(ratingsSpanID);
    ratingsSpan.innerHTML = numVotes + " ratings";
    var userSpanID = "userRatingSpan" + spotid;
    var userRatingSpan = document.getElementById(userSpanID);
    userRatingSpan.innerHTML = "Thank you for voting!";
    var spotArray = responseElement.getAttribute("spotRating");
    lightStar(spotArray, spotid, "_avg");
}

function clickStar(spotid, rating)
{

    var text = ""+rating;

    var keys = new Array();
    var values = new Array();
    keys[0] = "id";
    values[0] = spotid;
    keys[1] = "rating";
    values[1] = rating;

    ajaxPostKeysAndValues("/spot/rate", keys, values, updateClickStar);
}

function updateSaveQuote(inXML)
{
}

function restoreQuoteFields()
{
    hideElement('editquote');
    showElement('quote');
    showElement('quotelink');
}

function cancelQuote()
{
    restoreQuoteFields();
}
function saveQuote()
{
    var text = document.getElementById('quotefield').value;

    ajaxPost("/spotme/ajax/EditQuote", text, updateSaveQuote);

    var quoteSpan = document.getElementById('quote');
    quoteSpan.innerHTML = "<em>&quot;" + text + "&quot;</em>";

    restoreQuoteFields();
}

function restoreSpotTitleFields()
{
    hideElement('edittitle');
    showElement('title');
    showElement('titlelink');
}

function cancelSpotTitle()
{
    restoreSpotTitleFields();
}

function saveSpotTitle()
{
        var keys = new Array(2);
    keys[0] = "title";
    keys[1] = "spotID";
    var values = new Array(2);
    values[0] = document.getElementById('titlefield').value;
    values[1] = document.getElementById('spotID').value;

    ajaxPostMultiple("/spotme/ajax/EditSpotTitle", keys, values, updateSaveQuote);

    var titleSpan = document.getElementById('title');
    titleSpan.innerHTML = document.getElementById('titlefield').value;

    restoreSpotTitleFields();
}

function restoreSpotTagsFields()
{
    hideElement('edittags');
    showElement('tags');
    showElement('tagslink');
}

function cancelSpotTags()
{
    restoreSpotTagsFields();
}

function saveSpotTags()
{
        var keys = new Array(2);
    keys[0] = "tags";
    keys[1] = "spotID";
    var values = new Array(2);
    values[0] = document.getElementById('tagsfield').value;
    values[1] = document.getElementById('spotID').value;

    ajaxPostMultiple("/spotme/ajax/EditSpotTags", keys, values, updateSaveQuote);

    var tagsSpan = document.getElementById('tags');
    tagsSpan.innerHTML = document.getElementById('tagsfield').value;

    restoreSpotTagsFields();
}

function setSignupHelp(inHelp)
{
    var helpDiv = document.getElementById('signuphelp');
    helpDiv.innerHTML = inHelp;
}
