// --------------------------------------------------------------------------------
// Momus
// --------------------------------------------------------------------------------
if(typeof Prototype == 'undefined' || !Prototype.Version.match("1.6"))
  throw("Momus requires Prototype library >= 1.6.0");

var Momus = {
	Version: '2.0',
	
	includeLibrary: function(librarySource) {
		document.write('<script type="text/javascript" src="' + librarySource + '"></script>');
	},
	
	includeStyle: function(src) {
		var h = document.getElementsByTagName("head")[0];
		var link = $E({tag:'link', type:'text/css', rel:'stylesheet', media:'all', href:src});
		h.appendChild(link);
	},
	
	load: function() {
		$A(document.getElementsByTagName("script")).findAll( function(s) {
			return (s.src && s.src.match(/momus\.js(\?.*)?$/))
		}).each( function(s) {
			var path = s.src.replace(/momus\.js(\?.*)?$/,'');
			var includes = s.src.match(/\?.*load=([a-z,]*)/);
			(includes ? includes[1] : 'momusStyles,momusEvents').split(',').each(function(include) {Momus.includeLibrary(path + include + '.js')});
		});
	},
	
	expandCollapse: function(handler) {
		var id = handler.id.split('-')[0];
		var content = $(id + '-content');
		
		if (handler && content) {
			if (handler.hasClassName('collapse')) {
				handler.removeClassName('collapse');
				if (content)
					Effect.BlindUp(content, {duration:0.2});
			} else {
				handler.addClassName('collapse');
				if (content)
					Effect.BlindDown(content, {duration:0.2});
			}
		}
	},
	
	flashFix: function(o) {
		document.write(o);
	},
	
	getViewportSize: function() {
		var size = [0, 0];
		
		if (typeof window.innerWidth != 'undefined') {
			size = [
				window.innerWidth,
				window.innerHeight
				];
		}
		else if (typeof document.documentElement != 'undefined'
			&& typeof document.documentElement.clientWidth !=
			'undefined' && document.documentElement.clientWidth != 0) {
			size = [
				document.documentElement.clientWidth,
				document.documentElement.clientHeight
			];
		}
		else {
			size = [
				document.getElementsByTagName('body')[0].clientWidth,
				document.getElementsByTagName('body')[0].clientHeight
			];
		}

		return size;
	}
};

//Momus.load();

/**
 * document.createElement convenience wrapper
 *
 * The data parameter is an object that must have the "tag" key, containing
 * a string with the tagname of the element to create.  It can optionally have
 * a "children" key which can be: a string, "data" object, or an array of "data"
 * objects to append to this element as children.  Any other key is taken as an
 * attribute to be applied to this tag.
 *
 * Available under an MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @param {Object} data The data representing the element to create
 * @return {Element} The element created.
 */
function $E(data) {
    var el;
    if ('string' == typeof data) {
        el = document.createTextNode(data);
    } else {
        //create the element
        el = document.createElement(data.tag);
        delete(data.tag);

        //append the children
        if ('undefined'!=typeof data.children) {
            if ('string'==typeof data.children ||
                'undefined'==typeof data.children.length
            ) {
                //strings and single elements
                el.appendChild($E(data.children));
            } else {
                //arrays of elements
                for (var i=0, child=null; 'undefined'!=typeof (child=data.children[i]); i++) {
                    el.appendChild($E(child));
                }
            }
            delete(data.children);
        }

        //any other data is attributes
        for (attr in data) {
            el[attr]=data[attr];
        }
    }

    return el;
}
