/*
 * Copyright (C) 2009 Tildeslash Ltd. All rights reserved.
 */


if (typeof MMONIT=="undefined" || !MMONIT) { var MMONIT={}; }


/**
 * Declare the MMONIT namespace with common methods, variables and
 * define a few useful prototype functions.
 */
 
 /** Dom manipulation methods */
MMONIT.dom = function() {
     return {
         /**
         * Get element from DOM
         * @param String/HTMLElement | el : Accepts a string to use as an ID for getting a DOM reference, or an actual DOM reference
         * @return a DOM node
         */
         get: function(el) {
             if (typeof el === 'string') {
                 return document.getElementById(el);
             } else {
                 return el;
             }
         },
         /**
         * Add element to dest
         * @param String/HTMLElement | el : the Node to add
         * @param String/HTMLElement | dest : the target Node to append el to
         */
         add: function(el, dest) {
             var el = this.get(el);
             var dest = this.get(dest);
             dest.appendChild(el);
         },
         /**
         * Remove el from DOM
         * @param String/HTMLElement | el : the Node to remove
         */
         remove: function(el) {
             var el = this.get(el);
             el.parentNode.removeChild(el);
         },
         /**
         * Find first parent of refNode with nodeName = nName
         * @param HTMLElement | refNode : the DOM Node to start searching from
         * @param String | nName : the nodeName to search for
         */
         findParent: function(refNode, nName) {
             var t = refNode.parentNode;
             while (t) {
                 if (t.nodeName.toLowerCase() == nName.toLowerCase()) break;
                 t = t.parentNode;
             }
             return t;
         }
     };
}();


/** Layer methods */
MMONIT.layer = function() {
    return {
        /**
         * Toggle element visibility using the display attribute. 
         * Hide element if visible otherwise show element
         * @param String/HTMLElements: DOM nodes to toogle visibility of
         */
         toggle : function() {
             for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                 el.style.display = (el.style.display != 'none' ? 'none' : '' );
             }
         },
        /**
         * Show element using the display attribute
         * @param String/HTMLElements: DOM nodes to show
         */
        show :  function() {
            for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                el.style.display = '';
            }
        },
        /**
         * Hide element using the display attribute
         * @param variable String/HTMLElements: DOM nodes to hide
         */
        hide :  function() {
            for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                el.style.display = 'none';
            }
        },
        /**
         * Toggle element visibility using the visibility property. 
         * Hide element if visible otherwise show element
         * @param String/HTMLElements: DOM nodes to toogle visibility of
         */
        vtoggle : function() {
            for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                el.style.visibility = (el.style.visibility != 'hidden' ? 'hidden' : 'visible' );
            }
        },
        /**
         * Show elements using the visibility property
         * @param String/HTMLElements: DOM nodes to make visible
         */
        vshow : function() {
            for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                el.style.visibility = 'visible';
            }
        },
        /**
        * Hide elements using the visibility property
        * @param String/HTMLElements: DOM nodes to hide
         */
        vhide : function() {
            for (var i=0, el; el = MMONIT.dom.get(arguments[i]); i++) {
                el.style.visibility = 'hidden';
            }
        }
    };
}();


/** 
 * Animation effects. NOTE, clients should include YUI animation 
 */
MMONIT.effects = function() {
    return {
        /**
         * Author: Dustin Diaz, http://www.dustindiaz.com/
         * License http://creativecommons.org/licenses/by-sa/2.5/
         * @param String/HTMLElement | oEl : Accepts a string to use as an ID for getting a DOM reference, or an actual DOM reference
         * @param Int | iOffset : The unit (in 'px') that the element will be shaken 'by'
         * @param Int | iNum : The number of times the motion will iterate
         * @param Int | iSpeed : The speed at which the motion will animate
         */
        shake : function(oEl, iOffset, iNum, iSpeed) {
            var xy = YAHOO.util.Dom.getXY(oEl);
            var left = xy[0]-iOffset;
            var right = xy[0]+iOffset;
            (function(type, args, count) {
                if ( count >= iNum ) {
                    var a = {
                        points : {
                            to : xy
                        }
                    };
                    var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
                    anim.animate();
                    return;
                }
                else if ( count % 2 ) {
                    var c = count+1;
                    var a = {
                        points : {
                            to : [right, xy[1]]
                        }
                    };
                    var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
                    anim.onComplete.subscribe(arguments.callee, c);
                    anim.animate();
                }
                else {
                    var c = count+1;
                    var a = {
                        points : {
                            to : [left, xy[1]]
                        }
                    };
                    var anim = new YAHOO.util.Motion(oEl, a, iSpeed);
                    anim.onComplete.subscribe(arguments.callee, c);
                    anim.animate();
                }
                })(null, null, 1);
            }
        };
}();


/* ------------------------------------------------------------ Prototypes */


/**
 * A String trim function available to all String objects 
 */
String.prototype.trim = function() {
    return this.replace(/^(\s*)|(\s*)$/g,'');
};


/**
 * Returns true if String starts with str
 */
String.prototype.startsWith = function(str) {
    return (this.indexOf(str) == 0);
};


/**
 * Test for membership operator available to all Array objects
 */
Array.prototype.contains = function(el) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == el) {
            return true;
        }
    }
    return false;
}

