﻿function beginRequestHandler(sender, args) {
    var elemId = $(args.get_postBackElement()).attr("id");
    if (elemId.indexOf('btnPlay') > -1) {
        showProgress(0);
    }
}

function endRequestHandler(sender, args) {
    hideProgress();
    placePreviewMarkers();
    //$('#compImage').height(Math.min(Math.max(cImg.height, 400), 600));

}
function overToolbox(e) {
    if (e != null) {
        try {
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
        } catch (e) { }
    }
}
function getOrdStr(position) {
    var pos = position.toString();
    var end = pos[pos.length - 1];
    if (position >= 4 && position <= 20)
        return pos + 'th';
    else if (end == '1')
        return pos + 'st';
    else if (end == '2')
        return pos + 'nd';
    else if (end == '3')
        return pos + 'rd';
    else return pos + 'th';

}
function centerPopup(popId) {
    $('#' + popId).css("margin-left",
            ($('#' + popId).width() / 2 * -1) + "px");
    $('#' + popId).css("margin-top",
            ($('#' + popId).height() / 2 * -1) + "px");
}
var useLater = $.flash.create({
    swf: '../how_to_2.swf',
    height: 589,
    width: 830
});
function showHowItWorks() {
    $('#popupInstr').css("display", "none");
    $('#hiwPopup').find('.popupcontent').html(useLater);
    $('#hiwPopup').css("display", "block");
    return false;
}
function hidePopups(isPlay) {
    $('#popupOverlay').css("display", "none");
    $('#popupInstr').css("display", "none");
    $('#popupCompDetail').css("display", "none");
    $('#hiwPopup').css("display", "none");
    if (isPlay)
        showTimedPopup('popClickToPlay');
    return false;
}
function showTimedPopup(id) {
    $('#' + id).stop();
    $('#' + id).css("display", "block")
        .css("opacity", "1");
    $('#' + id).animate({
        opacity: 0.2
    }, 4000, function () {
        $('#' + id).css("display", "none");
    }
    );
}
function showInstructions(e) {
    if (e != null) {
        try {
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
        } catch (e) { }
    }
    $('#popupOverlay').css("display", "block");
    $('#popupInstr').css("display", "block");
    return false;
}
function showPrizeDetails(e) {
    if (e != null) {
        try {
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
        } catch (e) { }
    }
    $('#popupOverlay').css("display", "block");
    $('#popupInstr').css("display", "none");
    $('#popupCompDetail').css("display", "block");
}
function popup2Click(e) {
    $('.popup2').css("display", "none");
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}
function bindOverImageEvents(elem) {
    $(elem).click(function (e) {
        dostuff($('#compImage').eq(0)
                , e);
        try {
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
        } catch (e) { }
    });

    $(elem).bind("mousemove", function (e) {
        doOverStuff($('#compImage').eq(0), e);
        try {
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
        } catch (e) { }
    });
}
function startupStuff() {
    bindOverImageEvents($('#overCurs').eq(0));
    bindOverImageEvents($('.popup3').eq(0));
    images = new Array();
    if (isChange) {
        positionPreviewPixel();
    }
    else {
        for (var mI = 0; mI < 1; mI++) {
            images[mI] = $("<img style=\"display:none\" />").appendTo("#compImage");
            $(images[mI]).attr("src", "/images/competition/mark_" + (mI + 1) + ".png");

            bindOverImageEvents(images[mI]);
        }
    }
    $("*[id^='ctime_']").each(function () {
        writeTime($(this).attr("id"), $(this).attr("id").split('_')[1]);
    });
}
function resetMarkers() {
    for (var mI = 0; mI < images.length; mI++) {
        $(images[mI]).css({ "display": "none" });
    }
}

function getCoords(obj, ev) {
    var xcor, ycor;
    if (typeof ev.offsetX == 'number') { xcor = ev.offsetX; ycor = ev.offsetY; } // IE
    else { xcor = ev.pageX - $(obj).position().left; ycor = ev.pageY - $(obj).position().top; } // Other
    return [Math.min(Math.max(parseInt(xcor), 0), $('#compImage').width()), Math.min(Math.max(parseInt(ycor), 0), $('#compImage').height())];
}

function doOverStuff(obj, ev) {
    var coords = getCoords(obj, ev);
    $('#xcu').html(padZero(coords[0], 3));
    $('#ycu').html(padZero(coords[1], 3));

    if (!(parseInt(coords[0]) < $('#compTools').width() && parseInt(coords[1]) < $('#compTools').height())) {
        $('#overCurs').css("display", "block");
        $('#overCurs').css("left", (coords[0] - 38) + "px");
        $('#overCurs').css("top", (coords[1] - 38) + "px");
    }
}

function dostuff(obj, ev) {
    var coords = getCoords(obj, ev);
    placeMarker(coords, 1);
    showTimedPopup('popPressPlay');
}

function placePreviewMarker(x, y) {
    var img = $('<img style="position:absolute;margin:0px;left:' + (parseInt(x) - 4) + 'px;top:' + (parseInt(y) - 4) + 'px;" src="/images/mypix.gif" />');
    bindOverImageEvents(img.eq(0));
    img.appendTo("#compImage")
}
function placePreviewMarkers() {
    if (typeof (previewImgs) != "undefined") {
        for (var i = 0; i < previewImgs.length; i++) {
            placePreviewMarker(previewImgs[i].split(',')[0], previewImgs[i].split(',')[1]);
        }
    }
}

function placeMarker(coords, imgIndex) {
    $('.popup2').css("display", "none");
    imgIndex = 0;
    $(images[imgIndex]).css({ "position": "absolute", "left": coords[0] - 38, "top": coords[1] - 38, "display": "" });

    var x = padZero(coords[0], 3);
    var y = padZero(coords[1], 3);
    $('#crsdiv').css({ "display": "block" }); $('#xcr').html(x); $('#ycr').html(y); $('input[name="xcr_value"]').val(x); $('input[name="ycr_value"]').val(y);
}

function positionPreviewPixel() {
    images[0] = $('<img style="display:none;" src="/images/competition/mark.gif" />').appendTo("#competition");
    placeMarker(previewCoords, 0);
}

function padZero(input, len) {
    while (input.toString().length < len)
        input = '0' + input;
    return input;
}


