﻿var currentOption;
var currentCaptchaID;

var labels = false;
var objects = false;
var modalContainer;

var isModalDisplayed = false;

var previousParent, childToMove;

var waitUrl = "StaticResource.ashx/images/spinner.gif";

//function $get(id)
//{
//    return document.getElementById(id);
//}

function initWC()
{
    labels = 
        {
            name: $get('wc_name'),
            miniName: $get('wc_miniName'),
            hometown: $get('wc_hometown'),
            pilot: $get('wc_pilot'),
            crew: $get('wc_crew')
        };        
        
    //labels.hometown.style.display = labels.pilot.style.display = labels.crew.style.display = 'none';
    
    objects = 
        {
            miniDisplay: $get('miniDisplay'),
            fullDisplay: $get('wc_previewImage')
        };
        

    
    if(objects != null && options != undefined)
		objects.miniDisplay.src = options[0].miniSketch;
}
    
var captchaImage;

// option structure:
/*
options.push({
    id: '00000000-0000-0000-0000-000000000000',
    name: 'Halo 2 Ghostbike',
    tinySketch: 'StaticResource.ashx/Assets/Portland2008/Halo2Ghostbike-option.gif',
    miniSketch: 'StaticResource.ashx/Assets/Portland2008/Halo2Ghostbike-mini.gif',
    mainSketch: 'StaticResource.ashx/Assets/Portland2008/Halo2Ghostbike-main.gif',
    hugeSketch: 'StaticResource.ashx/Assets/Portland2008/Halo2Ghostbike-huge.gif',
    hometown: 'El Mirage, AZ',
    pilot: 'David Oppliger',
    crew: '<br />- Joe Montoya<br />- John Toblerone<br />- Brian Faulkner<br />- Ronnie Gablestein',
    });
*/    

function swapMini(optionID)
{
    if (!labels)
        initWC();
    
    objects.miniDisplay.src = options[optionID].miniSketch;
    objects.miniDisplay.alt = options[optionID].name;
}

function next()
{
    currentOption++;
    if (currentOption >= options.length)
        currentOption = 0;
        
    displayBigSketch(currentOption);
}

function previous()
{
    currentOption--;
    if (currentOption < 0)
        currentOption = options.length - 1;
    
    displayBigSketch(currentOption);
}

function revealModal(divID, idOfChildToShow)
{
    if (!isModalDisplayed)
    {
		modalContainer = $get('modalContainer');
    
        window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
        document.getElementById(divID).style.display = "block";
        document.getElementById(divID).style.top = document.body.scrollTop;

        childToMove = $get(idOfChildToShow);
        if (childToMove)
        {
            previousParent = childToMove.parentNode;
            previousParent.removeChild(childToMove);
            
            modalContainer.appendChild(childToMove);
        }

        isModalDisplayed = true;
    }
}

function hideModal(divID)
{
    document.getElementById(divID).style.display = "none";
    modalContainer.removeChild(childToMove);
    previousParent.appendChild(childToMove);
    
    isModalDisplayed = false;
}
   

function displayVote(optionIndex)
{
    if (!labels)
        initWC();
        
    if (optionIndex > options.length)
        return false;
        
    currentOption = optionIndex;
    
    var option = options[optionIndex];
    captchaImage = $get('voteOnlyCaptcha');
    requestNewCaptcha();
    setLabelText(labels.miniName, option.name);
        
    revealModal('modalPage', 'voteOnlyModal');
} 

function displayBigSketch(optionIndex)
{
    if (!labels)
        initWC();
        
    // display the sketch
    // display the vote
    // get a new captcha image
    // display the team info
    
    if (optionIndex > options.length)
        return false;
    
    var option = options[optionIndex];
    currentOption = optionIndex;    
    captchaImage = $get('bigCaptchaImage');    
    requestNewCaptcha();
    setLabelText(labels.name, option.name);
    setLabelText(labels.hometown, "Hometown: " + option.hometown);
    setLabelText(labels.pilot, "Pilot: " + option.pilot);
    setLabelText(labels.crew, "Crew: " + option.crew);
    objects.fullDisplay.style.backgroundImage = 'url(' + option.mainSketch + ')';
    
    revealModal('modalPage', 'enlargeModal');
}

function displayBiggestSketch()
{
    var win = window.open(options[currentOption].hugeSketch, "sketchPopup", "toolbar=0,directories=no,location=no,menubar=no,resizable=yes,width=1200,height=790,statusbar=no,status=no,scrollbars=yes", true);
    win.focus();
}

function hideVoteWindow()
{
    hideModal('modalPage');
}

function hideLargeWindow()
{
    hideModal('modalPage');
}

function requestNewCaptcha()
{
    var svc = new WildcardV2();
    captchaImage.src = waitUrl;
    svc.GetRandomCaptcha(requestNewCaptchaCallback_Success, requestNewCaptchaCallback_Failure);
}

function requestNewCaptchaCallback_Success(result)
{
    currentCaptchaID = result;
    captchaImage.src = 'CaptchaImage.ashx?guid=' + result;
}

function requestNewCaptchaCallback_Failure(error)
{
    alert('Failed to request a new captcha.');
}

function getCaptchaResponse(useLargeWindow)
{
    var input = useLargeWindow ? $get('mainCaptchaInput') : $get('miniCaptchaInput');
    return input.value;
}

function sendVote(useLargeWindow)
{
    var svc = new WildcardV2();
    svc.Vote(options[currentOption].id.toString(), currentCaptchaID, getCaptchaResponse(useLargeWindow), sendVoteCallback_Success, sendVoteCallback_Failure);
}

function sendVoteCallback_Success(result)
{
    if (result.Success)
    {
        hideModal('modalPage');
        drawVotePercentages(result.Scores);
        document.getElementById('wildCardMaster').className = 'wildCard voted';
    }
    else
    {
        captchaImage.src = waitUrl;
        captchaImage.src = 'CaptchaImage.ashx?guid=' + result.NextCaptchaID;
        currentCaptchaID = result.NextCaptchaID;
        alert(result.Message);
    }
}

function sendVoteCallback_Failure(result, error)
{
    alert('Vote failed.');
}

function drawVotePercentages(votePercentagesByTeam)
{
    var width = 280;
    
    var indexOfBiggest = 0;
    var biggest = 0;
    for (var i = 0; i < votePercentagesByTeam.length; i++)
    {
        if (votePercentagesByTeam[i] > biggest) 
        {
            biggest = votePercentagesByTeam[i];
            indexOfBiggest = i;
        }
    }
    
    for (var i = 0; i < options.length; i++)
    {
        var bar = $get('voteResult_option' + i.toString());
        bar.style.width = (votePercentagesByTeam[i] * width / biggest).toString() + 'px';
        
        var label = $get('voteResultTxt_option' + i.toString());
        var numAsInt = parseInt(votePercentagesByTeam[i].toString());
        setLabelText(label, numAsInt.toString() + '%');
    }
}

function setLabelText(element, text) 
{
    element.innerHTML = text;
    if (!text || text.length == 0)
    {
        element.style.display = 'none';
    }
    else
    {
        element.style.display = '';
    }
}



