var ratingFunctionsEnabled = true;
var voteGiven = false;

function createXMLHttpRequest()
{
    ratingFunctionsEnabled = true;

    try { return new XMLHttpRequest(); } catch (e) { }
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }

    ratingFunctionsEnabled = false;
    return null;
}

function DoXMLHttpRequest(requestUrl)
{
    var xmlHttpReq = createXMLHttpRequest();

    if (!ratingFunctionsEnabled)
        return "";
    
    xmlHttpReq.open("GET", requestUrl, false);
    xmlHttpReq.send(null);
    var result = xmlHttpReq.responseText;
    return result;
}

function GetIssueRating(issueId)
{
    //return "3,45|123";//testkod
    var result = DoXMLHttpRequest("/RatingHandler.ashx?action=GET&IssueID=" + issueId);
    return result;
}

function GetMagazineRating(magazineId)
{
    //return "3,45|123"; //testkod
    var result = DoXMLHttpRequest("/RatingHandler.ashx?action=GET&MagazineID=" + magazineId);
    return result;
}

function GetIssuesWithHigestRating(listSize)
{
    //return "FAN200501|3,45|123"; //testkod
    var result = DoXMLHttpRequest("RatingHandler.ashx?action=HIGH2&ListSize=" + listSize);
    return result;
}

function GetMagazinesWithHigestRating(listSize)
{
    //return "KNK01|KAP005|0";//testkod
    var result = DoXMLHttpRequest("RatingHandler.ashx?action=HIGH1&ListSize=" + listSize);
    return result;
}

function GetTwoRandomIssuesWithoutOrFewVotes()
{
    //return "KNK01|KAP005|0";//testkod
    var result = DoXMLHttpRequest("RatingHandler.ashx?action=NONE");
    return result;
}

function AddVote(issueId, rate)
{
    if (voteGiven)
        return;

    var result = DoXMLHttpRequest("RatingHandler.ashx?action=PUT&IssueID=" + issueId + "&Rate=" + rate);
    
    switch (rate)
    {
        case 1:
            vote2.src = 'images/gray.png';
            vote3.src = 'images/gray.png';
            vote4.src = 'images/gray.png';
            vote5.src = 'images/gray.png';
            break;
        case 2:
            vote3.src = 'images/gray.png';
            vote4.src = 'images/gray.png';
            vote5.src = 'images/gray.png';
            break;
        case 3:
            vote4.src = 'images/gray.png';
            vote5.src = 'images/gray.png';
            break;
        case 4:
            vote5.src = 'images/gray.png';
            break;
    }

    voteGiven = true;
    DisplayRating(result);
}

function LoadIssueRating(issueId)
{
    var result = GetIssueRating(issueId);
    
    if (ratingFunctionsEnabled)
    {
        DisplayRating(result);
    }
    else
    {
        HideRateGui();
    }
}

function LoadMagazineRating(magazineId)
{
    var result = GetMagazineRating(magazineId);
    
    if(ratingFunctionsEnabled)
    {
        DisplayRating(result);
    }
    else
    {   
        HideRateGui();
    }
}

function HideRateGui()
{
    var voteAreaObject = document.getElementById("voteArea");

    if (voteAreaObject != null && voteAreaObject.style != null)
        voteAreaObject.style.display = "none";
    
    var rateDisplayAreaObject = document.getElementById("RateDisplayArea");

    if (rateDisplayAreaObject != null && rateDisplayAreaObject.style != null)
        rateDisplayAreaObject.style.display = "none";
}

function DisplayRating(result)
{
    var betyg = parseInt(result.split("|")[0]);
    var betygdisplay = result.split("|")[0];
    var noOfVotes = parseInt(result.split("|")[1]);

    rate1.style.display = "none";
    rate2.style.display = "none";
    rate3.style.display = "none";
    rate4.style.display = "none";
    rate5.style.display = "none";

    if (betyg >= 1)
        rate1.style.display = "inline";
    if (betyg >= 2)
        rate2.style.display = "inline";
    if (betyg >= 3)
        rate3.style.display = "inline";
    if (betyg >= 4)
        rate4.style.display = "inline";
    if (betyg >= 5)
        rate5.style.display = "inline";

    var gradeDisplay = document.getElementById("spanGradeDisplay");
    
    gradeDisplay.style.visibility = "visible";
    gradeDisplay.innerHTML = "Betyg:&nbsp;" + betygdisplay + "&nbsp;/&nbsp;Antal r&ouml;ster:&nbsp;" + noOfVotes;
}

function RateImgSwitchOn(rate)
{

    if (voteGiven)
        return;

    if(rate == 1)
    {
        vote1.src = 'images/red.png';
    }
    else if(rate == 2)
    {
        vote1.src = 'images/orange.png';
        vote2.src = 'images/orange.png';
    }
    else if(rate == 3)
    {
        vote1.src = 'images/yellow2.png';
        vote2.src = 'images/yellow2.png';
        vote3.src = 'images/yellow2.png';
    }
    else if(rate == 4)
    {
        vote1.src = 'images/lightgreen.png';
        vote2.src = 'images/lightgreen.png';
        vote3.src = 'images/lightgreen.png';
        vote4.src = 'images/lightgreen.png';
    }
    else if(rate == 5)
    {
        vote1.src = 'images/green.png';
        vote2.src = 'images/green.png';
        vote3.src = 'images/green.png';
        vote4.src = 'images/green.png';
        vote5.src = 'images/green.png';
    }
}

function RateImgSwitchOff(rate)
{
    if (voteGiven)
        return;

    vote1.src = 'images/white.png';
    vote2.src = 'images/white.png';
    vote3.src = 'images/white.png';
    vote4.src = 'images/white.png';
    vote5.src = 'images/white.png';
}
