/**
 * Create an AJAX object for sending AJAX requests and receiving given responses.
 *
 * Part of the Scout Portal Toolkit
 * Copyright 2004 Internet Scout Project
 * http://scout.wisc.edu
 *
 * @author Tim Baumgard
 */
function ResourcePreview() {

    /**
     * @var object This reference to this (used in timeout functions)
     * @var object Ajax AJAX request object
     * @var object Delay holds delay timeout objects
     * @var mixed Param1 used to pass first parameter to timeout functions
     * @var mixed Param2 used to pass second parameter to timeout functions
     */
    var This;
    var Ajax;
    var Delay;
    var DelayTime;
    var Param1;
    var Param2;

    /**
     * --- PUBLIC METHODS ---
     */

    /**
     * Fetch data and display preview.
     *
     * @author Tim Baumgard
     */
    function showInfoAux() {
        // Get parameters, return if element or parent is not an object
        var element = Param1;
        var parent = Param2;
        if ("object" !== typeof element || "object" !== typeof parent) {
            return;
        }

        // Set new parent
        This.setParent(parent);

        // Get resource info and put it in the previewer
        var resourceId = $(element).attr("resourceId");
        $.ajax({
            "url": "index.php?P=AMSER--ResourcePreview",
            "data": {"resourceId": resourceId},
            "dataType": "json",
            "success": function(data, status){
                if (data.status.state == "OK") {
                    var image = (data.data.imageLocation !== "") ?
                        "<img src='"+data.data.imageLocation+"'" + " alt='"+data.data.title+"' style='float:left;" +
                        "background:#FFF;margin: 0 5px 0px 0;padding:5px;' />" : "";
                    var mime = (data.data.mimetypeImage != null) ? data.data.mimetypeImage : " ";

                    // set content and title
                    var title = data.data.title.replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
                    var description = data.data.description.replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
                    This.setTitle("<span style='font-weight:bold;'>"+title+"</span> "+mime);
                    This.addContent("<div style='width: 440px;'><p style='clear:none;background: " +
                        "#F0F0F0;padding:10px;border: 1px solid #CCC;text-align:left;clear:left;'>"+
                        image+" "+description+"<br /></p></div>", true);

                    // show the resource previewer
                    This.show();
                }
            }
        });
    }

    /**
     * Parent function for showInfoAux which causes a delay in info fetching/display execution.
     *
     * @author Tim Baumgard
     * @param element element the element calling this method
     * @param element parent element to use in positioning
     */
    this.showInfo = function (element, parent) {
        // Stop waiting to display if already waiting
        clearTimeout(Delay);

        // Set params, call timeout
        Param1 = element;
        Param2 = parent;
        Delay = setTimeout(showInfoAux, DelayTime);
    }

    /**
     * Run closing actions.
     *
     * @author Tim Baumgard
     */
    this.close = function() {
        // Make sure to stop any timeouts to display content
        clearTimeout(Delay);
        this.hide();
    }

    /**
     * --- PRIVATE METHODS ---
     */

    /**
     * Constructor: set up the Previewer object.
     *
     * @author Tim Baumgard
     */

        Previewer.call(this, document.body, "");
        This = this;
        this.setPointer(
            "SPTUI--AMSER/images/pointer_left.gif",
            "SPTUI--AMSER/images/pointer_right.gif"
        );
        this.useEffects();
        DelayTime = 225;

}

// Allocate resource preview. needed for global scope
var ResourcePreview;

// Apply mouseover/out events
$("document").ready(function(){

    ResourcePreview = new ResourcePreview();

    $(".ResourcePreview").mouseover(function() {
        ResourcePreview.showInfo(this, this);
    });

    $(".ResourcePreviewShort").mouseover(function() {
        ResourcePreview.showInfo(this, this.parentNode);
    });

    $(".ResourcePreview, .ResourcePreviewShort").mouseout(function() {
        ResourcePreview.close();
    });

});

