//------------------------------------------------------------------------------------------
// Methods to do with clippings
//------------------------------------------------------------------------------------------

function ClippingsManager(searchId, callbackObject)
{
    this.searchId = !searchId ? "" : searchId;

    if (callbackObject)
    {
        ClippingsManager.callbackObject = eval(callbackObject);
    }

    // Set the instance
    ClippingsManager.instance = this;
}

/**
 * The one and only instance of ClippingsManager
 */
ClippingsManager.instance = null;

// The display properties that are associated with a clipped article
ClippingsManager.clippedClassName = "clipped";
ClippingsManager.clippedIcon      = getImageDir() + "/icon.checkmark.19x19.gif";
ClippingsManager.clippedAltTitle  = "<<<uninitialized>>>";

// The display properties that are associated with an unclipped article
ClippingsManager.unClippedClassName = "add-clipping";
ClippingsManager.unClippedIcon      = getImageDir() + "/icon.clipping.FFF.25x29.gif";
ClippingsManager.unClippedAltTitle  = "<<<uninitialized>>>";

/**
 * Adds a result to the clippings list.
 * @param e the event object
 */
ClippingsManager.addClipping = function(e)
{
    if (typeof(e.target) == "undefined")
    {
        e.target = e.srcElement;
    }

    var searchId = e.target.getAttribute("searchId");

    if (!searchId)
    {
        searchId = ClippingsManager.instance.searchId;
    }

    var isAlert = (e.target.getAttribute("isAlert") == "true");

    YAHOO.util.Event.stopEvent(e);

    MarkedListConnector.add(searchId, [e.target.value], isAlert, ClippingsManager.callbackObject);

    var move_anim = ClippingsManager.createMoveAnim(e.target, YAHOO.util.Dom.getXY(e.target), YAHOO.util.Dom.getXY("todays-clippings"));

    move_anim.onComplete.subscribe(function(){ClippingsManager.clippingComplete(e.target,"add");});
    move_anim.animate();

    // remove "ugly dotted lines" from Firefox
    e.target.blur();
};

/**
 * Removes the selected result from the clippings list.
 */
ClippingsManager.removeClipping = function(e)
{
    if (typeof(e.target) == "undefined")
    {
        e.target = e.srcElement;
    }

    YAHOO.util.Event.stopEvent(e);

    MarkedListConnector.remove([e.target.value], ClippingsManager.callbackObject);

    var move_anim = ClippingsManager.createMoveAnim(e.target, YAHOO.util.Dom.getXY("todays-clippings"), YAHOO.util.Dom.getXY(e.target));

    move_anim.onComplete.subscribe(function(){ClippingsManager.clippingComplete(e.target,"remove");});
    move_anim.animate();

    // remove "ugly dotted lines" from Firefox
    e.target.blur();
};


/**
 * This method is called when clipping is complete and the element that is being clipped needs to be updated
 * to indicate its state.
 * @param el the element that was clicked on that needs updating
 * @param action the clipping action that is beinf performed, either "add" or "remove"
 */
ClippingsManager.clippingComplete = function(el, action)
{
    var fadeImage  = "";
    var fadeWidth  = "";
    var fadeHeight = "";

    if (action == "add")
    {
        fadeWidth  = "19";
        fadeHeight = "19";
        fadeImage  = ClippingsManager.clippedIcon;
    }
    else if (action == "remove")
    {
        fadeWidth  = "25";
        fadeHeight = "29";
        fadeImage  = ClippingsManager.unClippedIcon;
    }

    // reset position
    el.style.position = "";
    el.style.top      = "";
    el.style.left     = "";

    // fade in to the new image
    el.src = fadeImage;
    var fade_anim = new YAHOO.util.Anim(el, {width : { to : fadeWidth }, height : { to : fadeHeight }}, .5);
    fade_anim.animate();

    ClippingsManager.setClippingAttributes(el, action == "add");
}

/**
 * Sets the display attributes of the clipping icon
 * @param el the element containing the clipping icon
 * @param isClipped true if the result represented by the clipping icon is clipped
 */
ClippingsManager.setClippingAttributes = function(el, isClipped)
{
    var className = "";
    var src       = "";
    var altTitle  = "";
    var eventHandler = null;

    if (isClipped)
    {
        className    = ClippingsManager.clippedClassName;
        src          = ClippingsManager.clippedIcon;
        altTitle     = ClippingsManager.clippedAltTitle;
        eventHandler = ClippingsManager.removeClipping;
    }
    else
    {
        className    = ClippingsManager.unClippedClassName;
        src          = ClippingsManager.unClippedIcon;
        altTitle     = ClippingsManager.unClippedAltTitle;
        eventHandler = ClippingsManager.addClipping;
    }

    el.className = className;
    el.src       = src;
    el.alt       = altTitle;
    el.title     = altTitle;
    YAHOO.util.Event.removeListener(el, "click");
    YAHOO.util.Event.on(el, "click", eventHandler);    
}

/**
 * Creates an element that contains the clipping icon
 * @param i the number that this element will have in its id to make it unique
 * @param restultId the resultId of searchResult.  Used in the value attribute.
 * @param isClipped true if the result is in the marked item list
 */
ClippingsManager.createClippingIcon = function(i, resultId, isClipped)
{
    var clippingIcon = document.createElement("input");
    clippingIcon.type  = "image";
    clippingIcon.id    = "clippingIcon" + i;
    clippingIcon.value = resultId;

    ClippingsManager.setClippingAttributes(clippingIcon, isClipped);

    return clippingIcon;
}

/**
 * Creates a animation that will move from the "from" element to the "to" element
 * @param context the element context in which to create the animation
 * @param from the element that the animation will start from
 * @param to the element that the animation will go to
 */
ClippingsManager.createMoveAnim = function(context, from, to)
{
    return new YAHOO.util.Motion(context,{
        points : { from: from, to : to },
        width : { to : 0 },
        height : { to : 0 }},
        .5, YAHOO.util.Easing.easeOut);
}

/**
 * Sets the value of the clippings count element
 * @param clippingsCount
 */
ClippingsManager.prototype._setClippingsCount = function(clippingsCount)
{
    document.getElementById("clippings-count").innerHTML = clippingsCount;
}
/**
 * Callback method for AJAX calls to set the current clipping count value on the page.
 * @param clippingsCount the current count of clippings
 */
ClippingsManager.setClippingsCount_callback = function(clippingsCount)
{
    ClippingsManager.instance._setClippingsCount(clippingsCount);
};

/**
 * Error handler for errors coming from calls to set the current clippings count
 * @param message the error message
 * @param info the detailed information, including the stack trace
 */
ClippingsManager.setClippingsCount_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("ClippingsManager.setClippingsCount", message, info);
}

/**
 * The object used when making calls to addclipping and removeclipping
 */
ClippingsManager.clippingsCallbackDWRObject = {
  callback:     ClippingsManager.setClippingsCount_callback,
  timeout:      20000,
  errorHandler: ClippingsManager.setClippingsCount_errorHandler
};

/**
 * The callback object that receives from the add and remove clippings.  This can
 * be overridden in the instance constructor
 */
ClippingsManager.callbackObject = ClippingsManager.clippingsCallbackDWRObject;

