// ==UserScript==
// @name          NoID Webkettes URL-rovidito
// @namespace     http://noid.admc.hu/
// @description   Minden oldalon az összes linket NoID-vel rövidíti, ha tudja
// @include       *
// @exclude       http://noid.admc.hu/ablak.php*
// ==/UserScript==

var linx=document.getElementsByTagName("a");
var dontesprogram="a,e,i,o,u|p,t,r,m,c,f,n,l,d,s,z,g,b,v,j,k,h,ny,gy,ly,sl,zs,ty,sz,cs,st";

var dontesek=dontesprogram.split("|");
for (k in dontesek)
	dontesek[k]=dontesek[k].split(",");

function szambol_szot(szam) {
	if (!(szam*1>-1)) return 0;
	
	if (szam<17438699) {
		szam=(szam*17438) % 17438699;
	} else {
		szam=(((szam-17438699)*17438) % 87438685933)+17438699;
	}

	fonematipusok=dontesek.length;
	eredeti_szam=szam; szo=""; betuk_szama=0;

	this_fonema=szam % fonematipusok;
	szam=Math.floor(szam/fonematipusok);
	if (szam==0) szam=eredeti_szam;
	
	hosszlehet=new Array();
	for (k in dontesek)
		hosszlehet[k]=dontesek[k].length;

	do {
		lehet=dontesek[this_fonema];
		
		if (szam<lehet.length) {
			l= new Array(); l[0]=""; i=1;
			for (k in lehet)
				l[i++]=lehet[k];
			lehet=l;
		}
		
		betu=szam % hosszlehet[this_fonema];
		szam=Math.floor(szam/hosszlehet[this_fonema]);
		if (szam==0) szam=eredeti_szam;
		
		szo+=lehet[betu];
		betuk_szama++;
		
		this_fonema=(this_fonema+1) % fonematipusok;
	} while (szam!=eredeti_szam);
	
	return szo;
}

function rovidebb_url (url) {
	tomb=url.split("?userID=");
	if (tomb.length==2 && (tomb[0].indexOf("wiw.hu/pages/user/userdata.jsp")>-1 || (parent.location.href.indexOf("wiw.hu")>-1 && tomb[0].indexOf("pages/user/userdata.jsp")>-1))) {
		kesszo=szambol_szot(tomb[1]);
		if (kesszo!=0)
			return "http://"+kesszo+".iwiw.noid.admc.hu/";
	}
	
	tomb=url.split("dalszoveg.phtml?szk=");
	if (tomb.length==2 && ((tomb[0].indexOf("zeneszoveg.hu/")>-1 || parent.location.href.indexOf("zeneszoveg.hu")>-1) ||
				(tomb[0].indexOf("zeneszöveg.hu/")>-1 || parent.location.href.indexOf("zeneszöveg.hu")>-1))) {
		kesszo=tomb[1].split("&")[0];
		kesszo=szambol_szot(kesszo);
		
		if (kesszo!=0)
			return "http://"+kesszo+".zeneszoveg.noid.admc.hu/";
	}

	tomb=url.split("?t=");
	if (tomb.length==2 && (tomb[0].indexOf("/Article/showArticle")>-1 || tomb[0].indexOf("/Topic/showTopicList")>-1)) {
		kesszo=tomb[1].split("&")[0];
		kesszo=szambol_szot(kesszo);

		if (kesszo!=0)
			return "http://"+kesszo+".index.noid.admc.hu/";
	}

	tomb=url.split("popup_details.php?doc=");
	if (tomb.length==2 && tomb[0].indexOf("kereso.nava.hu/nava/public")>-1) {
		kesszo=tomb[1].split("&")[0];
		kesszo=szambol_szot(kesszo);

		if (kesszo!=0)
			return "http://"+kesszo+".nava.noid.admc.hu/";
	}

	tomb=url.split("?post_id=");
	if (tomb.length==2 && (tomb[0].indexOf(".blogter.hu/")>-1 || parent.location.href.indexOf(".blogter.hu")>-1)) {
		if (tomb[0].indexOf(".blogter.hu/")==-1) {
			tomb[0]=parent.location.href.replace("http://", "");
			tomb[0]=tomb[0].substr(0, tomb[0].indexOf("/")+1);
		}

		kesszo=szambol_szot(tomb[1]);
		if (kesszo!=0)
			return "http://"+tomb[0].replace("http://", "").replace(".blogter.hu/", "")+".blogter.noid.admc.hu/"+kesszo+"/";
	}

	return url;
}

for (k in linx) {
	if (linx[k].getAttribute) {
		linkcel=linx[k].getAttribute("href");
		if (linkcel) {
			eztet=k;
			linkcel2=rovidebb_url(linkcel);
			if (linkcel!=linkcel2) {
				k=eztet;
				linx[k].setAttribute("href", linkcel2);
			}
		}
	}
}
