﻿/**
* Checks whether a certain element exists.
*
* @name     exists
* @author   Rudy S. Dahbura
* @type     jQuery
* @example  $("#myelement").exists();
*
*/
$.fn.exists = function() {
    return $(this).length > 0;
}

/**
* Preloads images.
*
* @name     preloadImages
* @author   Rudy S. Dahbura
* @type     jQuery
* @example  $.preloadImages("img1", "img2", "img3");
*
*/
$.fn.preloadImages = function() {
    for (var i = 0; i < arguments.length; i++) {
        $("<img>").attr("src", arguments[i]);
    }
}

/**
* Adds menu behavior to an unordered list with nested elements.
*
* @name     scmenu
* @author   Rudy S. Dahbura
* @type     jQuery
* @example  $("#myelement").scmenu();
*
*/
$.fn.scmenu = function() {
    $("li", this)
        .hover(
		    function() {
		        $(this).addClass("ui-state-hover");
		    },
		    function() {
		        $(this).removeClass("ui-state-hover");
		    });

    return this;
}

/**
* Adds button effects for various events.
*
* @name     scbutton
* @author   Rudy S. Dahbura
* @type     jQuery
* @example  $("#id").scbutton();
*
*/
$.fn.scbutton = function() {
    $(this)
        .addClass('ui-state-default')
        .addClass('ui-corner-all')
        .blur(
            function() {
                $(this).removeClass('ui-state-focus');
            })
        .focus(
            function() {
                $(this).addClass('ui-state-focus');
            })
        .hover(
            function() {
                $(this).addClass('ui-state-hover');
            },
            function() {
                $(this).removeClass('ui-state-hover ui-state-active');
            })
        .mousedown(
            function() {
                $(this).addClass('ui-state-active');
            })
        .mouseup(
            function() {
                $(this).removeClass('ui-state-active');
            });

    return $(this);
}

function showError(error) {
    $('<div style="text-align:left;"/>')
        .html('The server returned the following message:\n' + error)
        .dialog({
            buttons: { 'Ok': function() { $(this).dialog('close'); } },
            draggable: false,
            resizable: false,
            title: 'Error'
        });
}
