var artistHoverInProgress = 0;

function delayRetrieveArtistHoverInfo(artistHoverDivID, artistJSONRequestURL, artistLandingURL) {

    var pattern = /\_.*$/;
    var temp = artistHoverDivID.replace(pattern, "");

    hoverobject = document.getElementById(temp);
    var retrieveArtistHoverInfoTimer = retrieveArtistHoverInfo.delay(900, null, [artistHoverDivID, artistJSONRequestURL, artistLandingURL]);
    hoverobject.addEvent('mouseleave', function() {
        if($defined(retrieveArtistHoverInfoTimer))
        {
            $clear(retrieveArtistHoverInfoTimer);
        }
    });
    hoverobject.addEvent('click', function() {
        if($defined(retrieveArtistHoverInfoTimer))
        {
            $clear(retrieveArtistHoverInfoTimer);
        }
    });
}

function retrieveArtistHoverInfo(artistHoverDivID, artistJSONRequestURL, artistLandingURL)
{
    // Do check to see if a call is in progress.
    if (artistHoverInProgress != 1)
    {
        var req = new Request.JSON(
        {
            method: 'get',
            url: artistJSONRequestURL,
            onRequest: function () { artistHoverInProgress = 1; },
            onComplete: function(response)
            {
                
                artistFormattedHtml = '<p><img src="' + response.imageUrl
                    + '" alt = "' + response.artistName
                    + '" hspace="4" vspace="4" border="0" align="left" /><strong>'
                    + response.artistName + '</strong><br /><br />';

                artistFormattedHtml += '<strong>Most Recent Movie:</strong><br />';

                if (response.latestTitle.length >34)
                {
                    response.latestTitle = response.latestTitle.substring(0, 33) + '...';
                }

                if (response.latestTitle.length > 17)
                {
                    artistFormattedHtml += response.latestTitle + ' (' + response.latestMovieYear + ')';
                    artistFormattedHtml += '<br /><br /><br /><strong>Most Popular Movies:</strong><br />';
                }
                else
                {
                    artistFormattedHtml += response.latestTitle + ' (' + response.latestMovieYear + ')';
                    artistFormattedHtml += '<br /><br /><br /><br /><strong>Most Popular Movies:</strong><br />';
                }
                
                top5count=1;
                while (response.topFive[top5count-1])
                {
                    if (response.topFive[top5count-1].length > 55)
                    {
                        response.topFive[top5count-1] = response.topFive[top5count-1].substring(0,54) + '...';
                    }
                    artistFormattedHtml += '<strong>' + top5count + '. </strong>'
                        + response.topFive[top5count-1] + '<br />';

                    top5count++;
                }

                artistFormattedHtml += '<br /><strong>Movies made from '
                    + response.earliestMovieYear + ' to '
                    + response.latestMovieYear + '</strong><br /><br />';

                artistFormattedHtml += '</p>' + '<ul>'
                    + '<li><a href="' + artistLandingURL + '">More Info ...</a></li>'
                    + '<li><a href="http://imdb.com/name/' + response.IMDbId + '" target="_blank">Go to IMDb</a></li>'
                    + '</ul>';

                document.getElementById(artistHoverDivID).innerHTML = artistFormattedHtml;
                artistHoverInProgress = 0;
            }
        }).send();
    }
}

