// From the facebooker project
function $(element) {
	if (typeof element == "string") {
		element=document.getElementById(element);
	};
	if (element)
		extend_instance(element,Element);
	return element;
};

function extend_instance(instance,hash) {
	for (var name in hash) {
		instance[name] = hash[name];
	};
};

var Element = {
	"hide": function () {
		this.setStyle("display","none")
	},
	"show": function () {
		this.setStyle("display","block")
	},
	"visible": function () {
		return (this.getStyle("display") != "none");
	},
	"toggle": function () {
		if (this.visible) {
			this.hide();
		} else {
			this.show();
		}
	}
};

var Form = {};
Form.serialize = function(form_element) {
	return $(form_element).serialize();
};

Ajax.Updater = function (container,url,options) {
	this.container = container;
	this.url=url;
	this.ajax = new Ajax();
	this.ajax.requireLogin = 1;
	if (options["onSuccess"]) {
		this.ajax.responseType = Ajax.JSON;
		this.ajax.ondone = options["onSuccess"];
	} else {
		this.ajax.responseType = Ajax.FBML;
		this.ajax.ondone = function(data) {
			$(container).setInnerFBML(data);
		}
	}
	if (options["onFailure"]) {
		this.ajax.onerror = options["onFailure"];
	}

	this.ajax.post(url,options['parameters']);
	if (options["onLoading"]) {
		options["onLoading"].call()
	}
};
Ajax.Request = function(url,options) {
	Ajax.Updater('unused',url,options);
};

PeriodicalExecuter = function (callback, frequency) {
        setTimeout(callback, frequency *1000);
        setTimeout(function() { new PeriodicalExecuter(callback,frequency); }, frequency*1000);
};

// Got the following from http://pastie.org/256150

/*
 * Applies block to all elements of an array
 */
function map(array, block){ 
  results = [];
  for (var i=0,l=array.length;i<l;i++){
    results.push(block(array[i]));
  }
  return results;
}

/*
 * Collects all elements under 'element' that 'matcher' returns true for
 */
function collect(element, matcher) {
    collection = [];
    var recurse = function(subelement){
        var nodes = subelement.getChildNodes();
        map(nodes, function(node){
            if (matcher(node)) {
                collection.push(node);
            }
            if (node.getFirstChild()) {
                recurse(node);
            }
        });
    };
    recurse(element);
    return collection;
}

function getElementsByName(elementName) {
    var matcher = function(element) {
        return (element.getName() === elementName);
    };
    return collect(document.getRootElement(), matcher);
}
function getElementsByClass(classname) {
    var matcher = function(element) {
        return (' ' + element.getClassName() + ' ' === ' ' + classname + ' ');
    };
    return collect(document.getRootElement(), matcher);
}
function getElementsByTagName(tagName) {
    tagName = tagName.toUpperCase();
    var matcher = function(element) {
        return (element.getTagName() === tagName);
    };
    return collect(document.getRootElement(), matcher);
}
