﻿/*-------------------------- 
* jQuery.Mtk
* versão 2.1
* Metatheke - Software
*--------------------------*/
(function ($) {
    // add native array reverse() to jQuery arrays
    //--------------------------------------------------
    $.fn.reverse = [].reverse;

    // change images on over/out
    //--------------------------------------------------
    $.fn.hoverSrc = function (hover, out) {
        return this.each(function () {
            $(this).hover(function () {
                $(this).attr({ src: "/imgs/" + hover })
            }, function () {
                $(this).attr({ src: "/imgs/" + out })
            });
        });
    }

    // handle "enter" keypress
    //--------------------------------------------------
    $.fn.onEnterPress = function (target) {
        return this.each(function () {
            $(this).keypress(function (e) {
                if (e.which == 13 && !(e.srcElement && (e.srcElement.tagName.toLowerCase() == "textarea"))) {
                    if ($.isFunction(target)) {
                        target();
                        return;
                    }
                    var targetBtt = document.getElementById(target);
                    if (typeof (targetBtt.click) != "undefined") {
                        targetBtt.click();
                        e.cancelBubble = true;
                        if (e.stopPropagation) e.stopPropagation();
                        return false;
                    }
                    if (typeof (targetBtt.href) != "undefined") {
                        eval(unescape(targetBtt.href.substr(11)));
                        e.cancelBubble = true;
                        if (e.stopPropagation) e.stopPropagation();
                        return false;
                    }
                } return true;
            });
        });
    }

    // set textboxes default value (hint)
    //--------------------------------------------------
    $.fn.defaultVal = function () {
        var args = arguments, c = 0;
        return (this.each(function () {
            var me = $(this), dv = args[c++];
            me.val(dv).focus(function () {
                if (me.val() == dv) me.val("");
                me.blur(function () {
                    if (me.val() == "") me.val(dv);
                });
            });
        }));
    }

    // add panning capabilities to image
    //--------------------------------------------------
    $.fn.panImage = function (width, height) {
        var obj = $(this), vpWidth = width, vpHeight = height, mover, viewport, offset, draggingThing;
        if (obj.length == 0) return;
        obj.each(function () {
            this.ondragstart = rfalse; // for IE (dragging halts)
            this.onmousedown = rfalse; // at least for opera (continuous scroll outside boundaries)
        });
        mover = obj.wrapAll("<div class='panviewport'><div class='panmover'></div></div>") // wrap all items in the movable object
                   .parent().mousedown(startDrag); // set the drag handler to the viewport
        viewport = mover.parent().get(0);

        function rfalse() { return false; } // helper method

        function startDrag(e) {
            mover.addClass('grab'); // change cursor
            draggingThing = mover.get(0);
            offset = { 'x': e.clientX, 'y': e.clientY }; // initial offset
            document.body.onmousemove = moveDrag; // handler
            document.body.onmouseup = endDrag; // handler
            document.onselectstart = function () { };
        }

        function moveDrag(e) {
            e = e || event;
            if (draggingThing) {
                if (e.clientX < viewport.offsetLeft || e.clientX > viewport.offsetLeft + vpWidth || e.clientY < viewport.offsetTop) {// || e.clientY > viewport.offsetTop + vpHeight) {
                    endDrag(); // terminate if outside boundaries
                    return false
                }
                viewport.scrollLeft = viewport.scrollLeft - (e.clientX - offset.x); // scroll horizontally
                viewport.scrollTop = viewport.scrollTop - (e.clientY - offset.y); // scroll vertically
                offset.x = e.clientX; offset.y = e.clientY; // update offset
                return true;
            }
        }
        function endDrag(e) {
            mover.removeClass('grab'); // change cursor
            draggingThing = null; // dump dragg
            document.body.onmousemove = null; // handler
            document.body.onmouseend = null; // handler
            document.onselectstart = null;
        }

        function initial() {
            $(viewport).width(vpWidth).height(vpHeight);
        }

        initial();
        return obj;
    }
})(jQuery);

