window.addEvent('domready', function() {

	// POPUP NO INTURSIVO
	// Capturamos todos los popup no intrusivos marcados con la clase "no-intrusive" del documento
	var no_intrusive_links = $$('a.no-intrusive');
	
	no_intrusive_links.each(function(item){
		
		// Creamos id aleatorio para cada item
		iframe_id = getRandomString(16,0,1,1,1,0,1,1,1,0);
		iframe_url = item.get('href');
		iframe_size = String.split(item.rel,',');
		if(item.getPosition().x > document.getSize().x/2) {
			iframe_pos = item.getPosition().x - parseInt(iframe_size[0]) + item.getSize().x;
		} else {			
			iframe_pos = item.getPosition().x;
		}
		iframe_pos_y = (item.getPosition().y+17);
				
		// Creamos un iframe dentro del enlace y cargamos la url del mismo
		(new IFrame({
			id: iframe_id,
			src: iframe_url,
			frameborder: '0px',
			styles: {
				'position': 'absolute',
				'visibility': 'hidden',
				'z-index': '1000',
				'width': iframe_size[0]+'px',
				'height': iframe_size[1]+'px',
				'left': iframe_pos+'px',
				'right': 'auto',
				'top': iframe_pos_y+'px',
				'bottom': 'auto',
				'border': '0px',				
				'margin': '0px'
			}
		})).inject(item);
		
		// Abrir/cerrar el iframe
		$(document.html).addEvent('click', function(e) {
			if(e.target == item)
				item.getFirst('iframe').setStyle('visibility',item.getFirst('iframe').getStyle('visibility')=='hidden'?'visible':'hidden');	
			else
				item.getFirst('iframe').setStyle('visibility','hidden');
		});
		
	});
		
});


// Obtener un string aleatorio de caracteres
var getRandomNum = function(lbound, ubound) { return (Math.floor(Math.random() * (ubound - lbound)) + lbound); }
var getRandomChar = function(number, lower, upper, other, extra) {
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnopqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	if (number == true) charSet += numberChars;
	if (lower == true) charSet += lowerChars;
	if (upper == true) charSet += upperChars;
	if (other == true) charSet += otherChars;
	return charSet.charAt(getRandomNum(0, charSet.length));
}
var getRandomString = function(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther) {
	var rc = "";
	if (length > 0) rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
	for (var idx = 1; idx < length; ++idx) {
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
	}
	return rc;
}


var replaceLatinChar = function(mystr){
	return output = mystr.replace(/ñ|ý|á|é|í|ó|ú|ä|ë|ï|ö|ü|à|è|ì|ò|ù|â|ê|î|ô|û/ig,function(str,offset,s) {
		var str = str=="ñ"?"n":str=="ý"?"y":str;
		str = str=="á"?"a":str=="é"?"e":str=="í"?"i":str=="ó"?"o":str=="ú"?"u":str;
		str = str=="ä"?"a":str=="ë"?"e":str=="ï"?"i":str=="ö"?"o":str=="ü"?"u":str;
		str = str=="à"?"a":str=="è"?"e":str=="ì"?"i":str=="ò"?"o":str=="ù"?"u":str;
		str = str=="â"?"a":str=="ê"?"e":str=="î"?"i":str=="ô"?"o":str=="û"?"u":str;
		return (str);
	});
};

var text2url = function (name) {
	name = name.toLowerCase(); // lowercase
	name = name.replace(/^\s+|\s+$/g, ''); // remove leading and trailing whitespaces
	name = name.replace(/\s+/g, '-'); // convert (continuous) whitespaces to one -
	name = replaceLatinChar(name);
	name = name.toLowerCase(); // lowercase again
	name = name.replace(/[^a-z|0-9|_|-]/g, ''); // remove everything that is not [a-z][0-0][-][_]
	name = name.replace('--', '-'); // remove repeat spacer
	return name;
}

// Formularios
var submitForm = function(f) {
	(new Element('input', {
		type: 'hidden',
		name: 'checksubmitform',
		value: '1'
	})).inject(f);
	f.submit();
}

var resetForm = function(f) {
	(new Element('input', {
		type: 'hidden',
		name: 'checkresetform',
		value: '1'
	})).inject(f);
	f.submit();
}

var submitFormWithVar = function(f,v) {
	(new Element('input', {
		type: 'hidden',
		name: 'checksubmitform',
		value: v
	})).inject(f);
	f.submit();
}

