/*******************************************/
/* javascript by gonchuki - (c)soho - 2007 */
/****************************************************/
/* CEOjs: modular abstraction layer for javascript  */
/****************************************************/

var CEO = CEO || {};

CEO.dom = {
	/* addEvent function by John Resig */
	addEvent: function ( obj, type, fn ) {
		if (obj.addEventListener)
			obj.addEventListener( type, fn, false );
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
		}
	},
	
	/* stopEvent extracted and adapted from mootools */
	stopEvent: function(e) {
		if (e.preventDefault) e.preventDefault();
		else e.returnValue = false;
		return e;
	},
	
	/* functions by Robert Nyman from ej library */
	/* getElementsByAttribute modified to include className extraction and curried the standard getElementsByClassName */
	getElementsByAttribute: function (oElm, strTagName, strAttributeName, strAttributeValue) {
		var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		
		var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
		var oCurrent;
		var oAttribute;
		for(var i=0; i<arrElements.length; i++){
			oCurrent = arrElements[i];
			oAttribute = (oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName)) || (strAttributeName == 'class' ? oCurrent.className : null);
			if(typeof oAttribute == "string" && oAttribute.length > 0){
				if (typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
					arrReturnElements.push(oCurrent);
				}
			}
		}
		return arrReturnElements;
	},
	
	getElementsByClassName: function (oElm, strTagName, strClassName){
		return (this.getElementsByAttribute(oElm, strTagName, 'class', strClassName));
	},

	addClassName: function (oElm, strClassName){
		var strCurrentClass = oElm.className;
		if(!new RegExp(strClassName, "i").test(strCurrentClass)){
			oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
		}
	},
	
	removeClassName: function (oElm, strClassName){
		var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
		oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
	}, 
	
	/* $: short alias for getElementById */
	$: function (el) {
		return document.getElementById(el);
	},
	
	/* $$: short alias for getElementsByClassName on the whole document */
	$$: function (className) {
		return (this.getElementsByClassName(document, '*', className));
	},
	
	/* $type: short version from mootools */
	$type: function (obj) {
		if (obj.htmlElement) return 'element';
		var type = typeof obj;
		if (type == 'object' && obj.nodeName){
			if (obj.nodeType == 1) return 'element';
		}
		if (type == 'object' || type == 'function'){
			if (obj.constructor == Array) return 'array';
			if (typeof obj.length == 'number'){
				if (obj.item) return 'collection';
			}
		}
		return type;
	},
	
	find_parent: function (element, name) {
		var ret = element.parentNode;
		name = name.toLowerCase();
		while (ret && ret.nodeName.toLowerCase() != name) {
			ret = ret.parentNode;
		}
		return(ret);
	},
	
	find_sibling: function (element, name) {
		var ret = element.nextSibling;
		name = name.toLowerCase();
		while (ret && ret.nodeName.toLowerCase() != name) {
			ret = ret.nextSibling;
		}
		return(ret);
	}
}

/* popup: unobtrusive javascript popups via late binding */
CEO.popup = {
	width: 0,
	height: 0,
	/* standard capturing method */
	captureByRel: function(parent, atrName, width, height) {
		this.capture(CEO.dom.getElementsByAttribute(parent, 'a', 'rel', atrName + '(\\[\\d+,\\s*\\d+\\])*'), width, height);
	},
	
	/* el can be either a DOM element, an Array of elements or a className */
	capture: function(el, width, height) {
		if ((width != undefined) && (height != undefined)) {
			this.width = width;
			this.height = height;
		}
		
		switch (CEO.dom.$type(el)) {
			case 'element':
				this.add_pop_to(el);
				break;
			case 'string':
				el = CEO.dom.$$(el);
			case 'array':
				for (var i in el) this.add_pop_to(el[i]);
				break;
		}
		
		this.width = null;
		this.height = null;
	},
	
	/* add_pop_to: works only on a single DOM element */
	add_pop_to: function(el) {
		var self = this;
		CEO.dom.addEvent(el, 'click', function(e){ CEO.dom.stopEvent(e); self.popup(el); });
		
		var size = el.getAttribute('rel').match(/\[(\d+),\s*(\d+)\]/) || ['', this.width, this.height];
		
		if (size[1]) el.setAttribute('popupprops', 'width=' + size[1] + ', height=' + size[2] );
	},
	
	popup: function(link) {
		props = link.getAttribute('popupprops') || '';
		window.open(link.href, '', props+',scrollbars=1');
	}
}

/* ---------------- */
CEO.dom.addEvent(window, 'load', function () {
	
	/* capture popups */
	CEO.popup.captureByRel(document, 'popup', '620', '460');
	
});