// ==UserScript==
// @name           suma4firefox
// @description    Adapta SUMA a Firefox, dado que la gente de SUMA es un poco racista con nuestro navegador por ser negro...
// @include        https://suma.um.es/*
// ==/UserScript==


// si se define usuario@alu.um.es y su contraseña, el script cubrirá los campos por ti :)
var user="";
var pass="";
var ibase="http://www.inertinc.org/suma4firefox/"; // no lo toques o no funcionará xD



// autologin
if (location.href=="https://suma.um.es/suma/sumav2/entrada.html"
	|| location.href=="https://suma.um.es/sumav2/entrada.html") {
	if (user && pass) {
		document.getElementsByName("mailSinArroba")[0].value=user.substring(0,user.indexOf("@"));
		document.getElementsByName("mail")[0].value=user;
		document.getElementsByName("clv")[0].value=pass;
		//document.getElementsByName("form")[0].submit();
	}
	return;
}


// obtener parámetros de suma
try {
	oper=location.href.split("?")[1].split("&")[0].split("=")[1];
} catch(e) {
	// si falla, lo típico xDDD
	oper="";
}

// función para cambiar el CSS "on the fly"
function changecss(rule,element,value) {
	var i,j,css,found=false;
	// test object (some works in IE, ...)
	if (document.all) css="rules";
	else if (document.getElementById) css="cssRules";
	// locate rule in style sheets
	var ts=0;
	for (i=0;i<document.styleSheets.length;i++)
		for (j=0;j<document.styleSheets[i][css].length;j++)
			if (document.styleSheets[i][css][j].selectorText==rule) {
				document.styleSheets[i][css][j].style[element]=value;
				found=true;
			}
	// if not found, add it to last style sheet at first pos
	if (!found && i) {
		i--;
		// test if standard or a fucking-IE
		if (document.styleSheets[i].insertRule) {
			document.styleSheets[i].insertRule(rule+" { "+element+": "+value+" }",0);
		} else {
			document.styleSheets[i].addRule(rule,element+": "+value);
		}
	}
}


//alert(oper);


// modificar cada pantalla deacuerdo a NUESTRA religión xD
switch (oper.split(".")[0]) {
case "inicio":

	// exclusiones
	switch (oper) {
	case "inicio.OnLinePage":
	case "inicio.cargaApplet":
	case "inicio.muestraPagina":
	case "inicio.loadApplet":
	case "inicio.altaSuma":
		return;
	default:
	}

	document.body.innerHTML
		=document.body.innerHTML.replace(/\|/g,"")
		;
	document.body.innerHTML=document.body.innerHTML.replace(/<br>/g,"<br style='clear:both;'>");
	document.body.innerHTML=document.body.innerHTML.replace(/&nbsp;/g,"");

	styles=document.getElementsByTagName("style")
	for (i=0;i<styles.length;i++) {
		styles[i].parentNode.removeChild(styles[i]);
		i--;
	}

	style=document.createElement("style");
	style.appendChild(document.createTextNode("\
		body,td{ font-family:Arial; font-size:12px; }\
		body{ margin:0px;background-color:#FFF; }\
		.clase, .clase1 { display:block; float:left; color: padding:1px 5px 1px 5px; }\
		a .clase, a .clase1 { display:block; float:left; padding:0px; }\
		a { display:block; float:left; padding:1px 5px 1px 5px; color:#448; text-decoration:none; }\
		a:hover{ color:#FFF; background-color: #000; text-decoration:none; -moz-border-radius:4px; }\
		td[align='right'] { white-space: nowrap; }\
		td[align='right'] a { display: inline; float: none; }\
		td[align='right'] a:hover { background-color: #FFF; }\
		"));
	document.body.appendChild(style);

	imgs=document.getElementsByTagName("img");
	for (i=0;i<imgs.length;i++) {
		try {
			if (imgs[i].alt && !imgs[i].parentNode.title) {
				imgs[i].parentNode.title=imgs[i].alt;
				imgs[i].src=ibase+imgs[i].src.substring(37);
			}
		} catch(e) {}
	}

	a=document.createElement("a");
	a.setAttribute("target","_blank");
	a.setAttribute("href","http://www.inertinc.org/");
	a.setAttribute("title","mod v0.1 by mr.xkr at inertinc Industries");
	a.innerHTML="<img src='"+ibase+"inertinc.gif' border=0 width=63 height=30 alt='~ inertinc Industries'>";
	tds=document.getElementsByTagName("TD");
	for (i=0;i<tds.length;i++) {
		if (tds[i].getAttribute("valign")=="top" &&
				tds[i].getAttribute("align")=="right") {
			tds[i].appendChild(a);
		}
	}

	tag=document.getElementsByTagName("font")
	for (i=0;i<tag.length;i++) {
		tag[i].removeAttribute("face");
		tag[i].removeAttribute("size");
	}

	// altura barra superior
	parent.document.body.rows=document.body.offsetHeight+",*";

	break;

default:

	switch (oper) {
	case "noticias.mapaNoticias":

		// modificación de página principal
		document.body.innerHTML=document.body.innerHTML.replace(/<b>/g,"<B style='display:block;'>");
		document.body.innerHTML=document.body.innerHTML.replace(/<\/a>\&nbsp\;\n\t<\/td>/g,"</a></td>");

		// estilo decente xD
		document.getElementsByTagName("link")[0].href=ibase+"estiloSumaDecente.css";

		/*changecss("a","position","relative");
		changecss("a","font-size","20px");
		changecss("a","padding","1px 3px 1px 3px");
		changecss("a:hover","padding","1px 3px 1px 3px");
		changecss("a","margin","1px 3px 1px 3px");
		changecss("a:hover","margin","1px 3px 1px 3px");

		changecss("a","border","1px solid #FFF");
		changecss("a:hover","border","1px solid #CCC");*/

		style=document.createElement("style");
		style.appendChild(document.createTextNode("\
			body { background-color: #888; }\
			td[bgcolor='#10739C'] img,\
			td[bgcolor='#EFDEA5'] img,\
			td[bgcolor='#F72900'] img,\
			td[bgcolor='#BDBD00'] img\
			{ float: right; }\
			td[bgcolor='#FFFFFF'] a { padding: 1px 3px 1px 3px; border: 1px solid #FFF; text-decoration: none; -moz-border-radius: 4px; }\
			td[bgcolor='#FFFFFF'] a:hover { background-color: #F0FCFF; border: 1px solid #88F; text-decoration: none; }\
			"));
		document.body.appendChild(style);

		fonts=document.getElementsByTagName("font")
		for (i=0;i<fonts.length;i++) {
			fonts[i].removeAttribute("size");
			//fonts[i].setAttribute("style","color:#0F0;");
			//fonts[0].parentNode.replaceChild(div,fonts[0]);
		}

		a=document.getElementsByTagName("a")
		for (i=0;i<a.length;i++) {
			a[i].setAttribute("style","");
		}

		br=document.getElementsByTagName("br")
		for (i=0;i<br.length;i++) {
			br[i].parentNode.removeChild(br[i]);
			i--;
		}

		center=document.getElementsByTagName("center")
		for (i=0;i<center.length;i++) {
			center[i].parentNode.removeChild(center[i]);
			i--;
		}

		imgs=document.getElementsByTagName("img")
		for (i=0;i<imgs.length;i++) {
			/*try {
			switch (imgs[i].src) {
			case "https://suma.um.es/sumav2/img/logoServicios3.gif":
				imgs[i].setAttribute("style","display:block;float;left;");
				break;
			}
			} catch(e) {}*/
			switch (imgs[i].height) {
			case 60: imgs[i].parentNode.removeChild(imgs[i]); i--; break;
			}

		}

		break;

	case "docente/tablon.DetalleMensajeTablon":

		var div=document.createElement("div");
		var textarea=document.getElementsByName("texto")[0];
		div.setAttribute("style","margin:16px;font-weight:bold;");
		div.innerHTML=textarea.innerHTML;
		textarea.parentNode.replaceChild(div,textarea);


	default:

		// por defecto para el resto de páginas
		try {
			document.getElementsByTagName("link")[0].href=ibase+"estiloSumaDecente.css";
		} catch(e) {}

		// iconos con descripciones
		imgs=document.getElementsByTagName("img");
		for (i=0;i<imgs.length;i++) {
			try {
				if (imgs[i].alt && !imgs[i].parentNode.title) {
					imgs[i].parentNode.title=imgs[i].alt;
				}
			} catch(e) {}
		}


	}

}
