String.prototype.substr_count = function(s) {
	var p = 0, c = 0;
	while ((p = this.indexOf(s, p)) >= 0) { c++; p++; }
	return c;
};

String.prototype.explode = function(s, c) {
	if (c == undefined) return this.split(s);
	var l = this.split(s);
	for (var n = l.length - 2; n >= c - 1; n--) l[n] += s + l[n + 1];
	l.length = c;
	return l;
};

String.prototype.basename = function() {
	return this.substr(this.lastIndexOf('/') + 1);
};

function $(id) {
	if (id.substr(0, 1) == '#') return document.getElementById(id.substr(1));
	return document.getElementById(id);
}

function processSearchSubmit(form) {
	var text = form.search.value;
	document.location.href = '/encontrar/' + text + '.php';
	return false;
}

var CategoryListTimerList = {};
var CategoryListLastVisible;
var CategoryListWidth = 220;
var CategoryListHeight = 90;

function getPosition(obj) {
	var top = obj.offsetTop, left = obj.offsetLeft;
	while (obj.offsetParent) if (obj = obj.offsetParent) { top += obj.offsetTop; left += obj.offsetLeft; }
	return  { 'left' : left, 'top' : top };
}

function CategoryListShowRequest(id) {
	if (document.all) return;

	var obj = $('CategoryList' + String(id));

	var pos = getPosition($('CategoryListBase' + String(id)));

	obj.style.position = 'absolute';
	obj.style.top = pos.top + 'px';
	obj.style.left = pos.left + 'px';

	//alert($('CategoryListBase' + String(id)).clientY);

	if (CategoryListLastVisible) CategoryListLastVisible.style.display = 'none';

	clearInterval(CategoryListTimerList[id]);

	if (!obj || obj.style.display == 'inline') return;

	obj.style.display = 'inline';

	CategoryListTimerList[id] = setTimeout(function() {
		CategoryListTimerList[id] = setInterval(function() {
			if (true) {
				var width = parseFloat(obj.style.width);
				if (!width && width !== 0) width = 0;

				var height = parseFloat(obj.style.height);
				if (!height && height !== 0) height = 0;

				width += 30;
				height += 8;

				if (width >= CategoryListWidth) width = CategoryListWidth;
				if (height >= CategoryListHeight) height = CategoryListHeight;
				if (width >= CategoryListWidth && height >= CategoryListHeight) clearInterval(CategoryListTimerList[id]);

				obj.style.width = String(width) + 'px';
				obj.style.height = String(height) + 'px';
			} else {
				if (!obj.style.opacity && obj.style.opacity !== 0) obj.style.opacity = 0;
				obj.style.opacity = parseFloat(obj.style.opacity) + 0.2;
				if (obj.style.opacity  >= 1) { obj.style.opacity = 1; clearInterval(CategoryListTimerList[id]); }
			}
		}, 25);
	}, 200);

	CategoryListLastVisible = obj;
}

function CategoryListHideRequest(id) {
	if (document.all) return;

	var obj = $('CategoryList' + String(id));

	clearInterval(CategoryListTimerList[id]);

	if (!obj || obj.style.display == 'none') return;

	CategoryListTimerList[id] = setTimeout(function() {
		CategoryListTimerList[id] = setInterval(function() {
			if (true) {
				var width = parseFloat(obj.style.width);
				if (!width && width !== 0) width = 0;

				var height = parseFloat(obj.style.height);
				if (!height && height !== 0) height = 0;

				width -= 40;
				height -= 12;

				if (width <= 0) width = 0;
				if (height <= 0) height = 0;
				if (width <= 0 && height <= 0) clearInterval(CategoryListTimerList[id]);

				obj.style.width = String(width) + 'px';
				obj.style.height = String(height) + 'px';
			} else {
				if (!obj.style.opacity && obj.style.opacity !== 0) obj.style.opacity = 1;
				obj.style.opacity = parseFloat(obj.style.opacity) - 0.2;
				if (obj.style.opacity <= 0) { obj.style.opacity = 0; clearInterval(CategoryListTimerList[id]); obj.style.display = 'none'; }
			}
		}, 16);
	}, 200);
}

// Obtenemos el objeto de Ajax
function getAjaxObject() {
	var xmlhttp;

	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				xmlhttp = undefined;
			}
		}
	}

	return xmlhttp;
}

// Obtenemos los datos de una direcci??, al terminar llamamos a una
// funci??allback.
function httpGetData(url, callback, relative) {
	var xmlhttp = getAjaxObject();
	
	if (relative === undefined || relative) url = 'http://' + window.location.host + '/' + url;

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				if (callback) callback(xmlhttp.responseText);
			} else {
				if (callback) callback(undefined);
			}
		}
	};
	
	try {		
		xmlhttp.open('GET', url, true);
		xmlhttp.send('');
	} catch (e) {
		alert('Error al abrir url: ' + url + "\n" + String(e));
	}
};

function httpPostData(url, data, callback, relative) {
	var xmlhttp = getAjaxObject();

	if (relative === undefined || relative) url = 'http://' + window.location.host + '/' + url;

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && callback) callback((xmlhttp.status == 200) ? xmlhttp.responseText : undefined);
	};
	
	xmlhttp.open('POST', url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Content-length", data.length);
	xmlhttp.setRequestHeader("Connection", "close");	
	xmlhttp.send(data);
};

function httpGetJson(url, callback) {
	httpGetData(url, function(cdata) {
		var data = undefined;
		try {
			eval('data=' + cdata + ';');
		} catch (e) {
			alert('Invalid json: ' + cdata + "\n" + String(e));
		}
		if (callback) callback(data);
	});
};

function httpPostJson(url, data, callback) {
	httpPostData(url, data, function(cdata) {
		var data = undefined;
		try {
			eval('data=' + cdata + ';');
		} catch (e) {
			alert('Invalid json: ' + cdata + "\n" + String(e));
		}
		if (callback) callback(data);
	});
};

// Realizamos una acci?? mostramos el resultado mediante un alert
function performActionAndShow(action, params) {
	httpGetData('opajax.php?action=' + action + '&' + params, alert);
}

// Cargamos el resultado de una petici??n un objeto HTML
function loadInto(url, id, callback, dvalue) {
	httpGetData(url, function(data) { if (!data) data = dvalue; if (id && data) document.getElementById(id).innerHTML = data; if (callback) callback(data); });
}

// Validamos un email
function validateEmail(email) {
	var re = /^[a-z0-9!#$%&*+-=?^_`{|}~]+(\.[a-z0-9!#$%&*+-=?^_`{|}~]+)*@([-a-z0-9]+\.)+([a-z]{2,3}|info|arpa|aero|coop|name|museum)$/i;
	if (typeof(email) != 'string') return false;
	return re.test(email);
} validate_email = validateEmail;

// Definimos una cookie
function setCookie(name, value, expires, path, domain, secure) {
	var domain = document.location.host;
	domain = (domain.substr_count('.') >= 2) ? ('.' + domain.explode('.', 2)[1]) : ('.' + domain);
	
	document.cookie = 
		name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "")
	;
}

// Definimos una cookie que no expira
function setCookieForever(name, value) {
	setCookie(name, value, new Date((new Date()).getFullYear() + 10, 01, 01), '/');
}

// Obtenemos una cookie
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";

	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);

	if (end == -1) end = dc.length;

	return unescape(dc.substring(begin + prefix.length, end));
}

// Borramos una cookie
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = (
			name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT"
		);
	}
}

// Obtenemos la lista de posibles conexiones y sus bps
function getConnectionList() {
	return {
		'56 kbps'   : 56000   ,
		'128 Kbps'  : 128000  ,
		'512 Kbps'  : 512000  ,
		'1 Mega'    : 1024000 ,
		'2 Megas'   : 2048000 ,
		'3 Megas'   : 3072000 ,
		'4 Megas'   : 4096000 ,
		'20 Megas'  : 20480000
	};
} get_connection_list = getConnectionList;

//function get_connection_list() { return getConnectionList(); }

// Obtenemos el tiempo necesario para descargar el archivo
function getTime(size, speed) { return (size * 8) / speed; };
get_time = getTime;

// Obtenemos el texto en horas, minutos y segundos
function getTimeText(time) {
	var div = 3600, ctime = Math.ceil(time), value;
	var ret = '';
	while (true) {
		value = parseInt(ctime / div).toString(); ctime %= div;
		if (value.length == 1) value = '0' + value;
		ret += value + ':';
		if (div == 1) break; div /= 60;
	}
	return ret.substr(0, ret.length - 1);
} get_time_text = getTimeText;

// Enfocamos la barra de b?da
function focusevent() {
	var searchobj = document.getElementById('TopSearch');
	if (!searchobj) searchobj = document.getElementById('fullsearch');
	if (!searchobj) return;
	//searchobj.focus();
	//searchobj.select();
}

function SistemaOperativo() {
	if (navigator.userAgent.indexOf('IRIX') != -1) {var SO = "Irix" }
	else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('98') != -1)) {var SO= "Windows 98"}
	else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1)) {var SO= "Windows 95"}
	else if (navigator.appVersion.indexOf("16") !=-1) {var SO= "Windows 3.1"}
	else if (navigator.userAgent.indexOf("NT 5.1") !=-1) {var SO= "Windows XP"}
	else if (navigator.userAgent.indexOf("NT 5.2") !=-1) {var SO= "Windows Server 2003"}
	else if (navigator.userAgent.indexOf("NT 5") !=-1) {var SO= "Windows 2000"}
	else if (navigator.userAgent.indexOf("NT 6") !=-1) {var SO= "Windows Vista"}
	else if (navigator.appVersion.indexOf("NT") !=-1) {var SO= "Windows NT"}
	else if (navigator.appVersion.indexOf("SunOS") !=-1) {var SO= "SunOS"}
	else if (navigator.appVersion.indexOf("Linux") !=-1) {var SO= "Linux"}
	else if (navigator.userAgent.indexOf('Mac') != -1) {var SO= "Macintosh"}
	else if (navigator.appName=="WebTV Internet Terminal") {var SO="WebTV"}
	else if (navigator.appVersion.indexOf("HP") !=-1) {var SO="HP-UX"}
	else {var SO= "No identificado"}
	return SO;
}

function portableAttachEvent(nevent, callback) {
	(document.all) ?
		window.attachEvent('on' + nevent, callback) :
		window.addEventListener(nevent, callback, true)
	;
}

function loadScript(url, callback) {
	var s = document.createElement('script');
	if (callback !== undefined)
	s.onload = callback;
	s.onreadystatechange = function(v) { if (s.readyState == 'loaded') s.onload(); };
	s.type = 'text/javascript';
	s.src = url;
	document.getElementsByTagName('head')[0].appendChild(s);
}

portableAttachEvent('load', focusevent);