﻿var previousItem;

$(function() {
    captchaSetup();
});

function captchaSetup() {
    $(".draggables").draggable({
        snap: "#iconContainer",
        snapMode: "inner",
        snapTolerance: 5,
        revert: false,
        containment: "#draggableArea",
        start: function() { 
		resetPreviousItem($(this)) 
		}
    });

    $("#iconContainer").droppable({
        // handles drop action
        drop: function(event, ui) {
            $("#captchaValue").val(ui.draggable.attr("alt"));
            $("#captchaValue").next("div.bubble").animate({ opacity: "hide" }, "slow");
            //resetPreviousItem(ui.draggable);
        }

    });
}

function resetPreviousItem(currentItem) {
    $("#captchaValue").val("");
    if ($("#captchaValue").parent().attr("class") != "bubbleholder") {
        $("#captchaValue").parent().wrapInner("<div class='bubbleholder'></div>");
        $("#captchaValue").after("<div class='bubble'><div>Drop here</div></div>");
    }

    $("#captchaValue").next("div.bubble").css("right", "39px");
    $("#captchaValue").next("div.bubble").css("top", "8px");
    $("#captchaValue").next("div.bubble").animate({ opacity: "show" }, "slow");
    
    
    //if (previousItem != undefined && previousItem.attr("alt") != currentItem.attr("alt")) {
        //alert("Previous : " + perviousItem.attr("alt"));
        // an object has been selected previously
        // reset previous object position
        //previousItem.attr("style", "position : relative;");
    //}
    //previousItem = currentItem;
}