﻿(function ($) {

    $.fn.randomize = function (childElem) {
        return this.each(function () {
            var $this = $(this);
            var elems = $this.children(childElem);

            elems.sort(function () { return (Math.round(Math.random()) - 0.5); });

            $this.remove(childElem);

            for (var i = 0; i < elems.length; i++)
                $this.append(elems[i]);

        });
    }
})(jQuery);


/*
* Image preview script 
* powered by jQuery (http://www.jquery.com)
* 
* written by Alen Grakalic (http://cssglobe.com)
* 
* for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
*
*/
this.imagePreview = function () {

    $("a.preview").click(function (e) {
        $("#preview").hide().remove();
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img alt='Image preview' />" + c + "</p>");
        $("#preview img").load(function () {
            var top = ($(window).height() / 2) - ($("#preview").height()) / 2 + ($(window).scrollTop()) - 10;
            var left = ($(window).width() / 2) - ($("#preview").width() / 2) - 10;
            $("#preview")
                .css("top", top + "px")
			    .css("left", left + "px")
                .fadeIn("fast")
                .click(function () { $(this).remove(); });
        });
        $("#preview img").attr('src', this.href);
        return false;
    });
}

/* POPUPWINDOW */

jQuery.fn.popupwindow = function (p) {

    var profiles = p || {};

    return this.each(function (index) {
        var settings, parameters, mysettings, b, a, winObj;

        // for overrideing the default settings
        mysettings = (jQuery(this).attr("rel") || "").split(",");


        settings = {
            height: 600, // sets the height in pixels of the window.
            width: 600, // sets the width in pixels of the window.
            toolbar: 0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
            scrollbars: 0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
            status: 0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
            resizable: 1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
            left: 0, // left position when the window appears.
            top: 0, // top position when the window appears.
            center: 1, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
            createnew: 1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
            location: 0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
            menubar: 0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
            onUnload: null // function to call when the window is closed
        };

        // if mysettings length is 1 and not a value pair then assume it is a profile declaration
        // and see if the profile settings exists

        if (mysettings.length == 1 && mysettings[0].split(":").length == 1) {
            a = mysettings[0];
            // see if a profile has been defined
            if (typeof profiles[a] != "undefined") {
                settings = jQuery.extend(settings, profiles[a]);
            }
        }
        else {
            // overrides the settings with parameter passed in using the rel tag.
            for (var i = 0; i < mysettings.length; i++) {
                b = mysettings[i].split(":");
                if (typeof settings[b[0]] != "undefined" && b.length == 2) {
                    settings[b[0]] = b[1];
                }
            }
        }

        // center the window
        if (settings.center == 1) {
            settings.top = (screen.height - (parseInt(settings.height) + 110)) / 2;
            settings.left = (screen.width -  parseInt(settings.width)) / 2;
        }

        parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left + ",screenX=" + settings.left + ",top=" + settings.top + ",screenY=" + settings.top;

        jQuery(this).bind("click", function () {
            var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
            winObj = window.open(this.href, name, parameters);

            if (settings.onUnload) {
                // Incremental check for window status
                // Attaching directly to window.onunlaod event causes invoke when document within window is reloaded
                // (i.e. an inner refresh)
                unloadInterval = setInterval(function () {
                    if (!winObj || winObj.closed) {
                        clearInterval(unloadInterval);
                        settings.onUnload.call($(this));
                    }
                }, 500);
            }

            winObj.focus();

            return false;
        });
    });

};

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function ($) { $.fn.hoverIntent = function (f, g) { var cfg = { sensitivity: 7, interval: 100, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function (ev) { cX = ev.pageX; cY = ev.pageY; }; var compare = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]); } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function () { compare(ev, ob); }, cfg.interval); } }; var delay = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]); }; var handleHover = function (e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } } if (p == this) { return false; } var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } if (e.type == "mouseover") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function () { compare(ev, ob); }, cfg.interval); } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function () { delay(ev, ob); }, cfg.timeout); } } }; return this.mouseover(handleHover).mouseout(handleHover); }; })(jQuery);
