/**
 * @author Slawomir Koklowski {@link http://www.kurshtml.boo.pl}
 * @copyright NIE usuwaj tego komentarza! (Do NOT remove this comment!)
 */

function Indeks(id, ramka)
{
	this.id = id;
	if (typeof ramka == 'undefined') this.ramka = self;
	else if (typeof ramka == 'string')
	{
		if (ramka == '_blank') this.ramka = '';
		else if (ramka == '_self') this.ramka = self;
		else if (ramka == '_parent') this.ramka = parent;
		else if (ramka == '_top') this.ramka = top;
		else this.ramka = ramka;
	}
	else if (!ramka) this.ramka = self;
	else this.ramka = ramka;
	
	this._szukaj = {
		html: '',
		haslo: '',
		i: 0
	};
}

Indeks.prototype.wstaw = function(hasla, adres_bazowy, rozmiar, sortuj)
{
	if (typeof sortuj != 'undefined' && sortuj)
	{
		hasla.sort(
			function(a, b)
			{
				if (a[0] == b[0]) return 0;
				return a[0].compare() < b[0].compare() ? -1 : 1;
			}
		);
	}
	if (typeof sortuj != 'undefined' && sortuj < 0)
	{
		document.write("<pre>");
		for (var i = 0; i < hasla.length; i++)
		{
			document.write("['" + hasla[i][0].addSlashes() + "','" + hasla[i][1].addSlashes() + "']" + (i < hasla.length - 1 ? ",\n" : ""));
		}
		document.write("</pre>");
	}
	else
	{
		document.write(
			'<form id="' + this.id + '" action="javascript:void(0)" onsubmit="' + this.id + '.wyswietl(); return false">' +
				'<input type="text" name="haslo" size="15" onkeyup="' + this.id + '.zaznacz()" class="text" />' +
				
				'<div><select name="hasla" size="' + (typeof rozmiar != 'undefined' && rozmiar ? rozmiar : 5) + '" ondblclick="' + this.id + '.wyswietl()">'
		);
		for (var i = 0; i < hasla.length; i++)
		{
			document.write('<option value="' + ((typeof adres_bazowy == 'undefined' || !adres_bazowy ? '' : adres_bazowy) + hasla[i][1]).htmlSpecialChars(true) + '"' + (i == 0 ? ' selected="selected"' : '') + '>' + hasla[i][0].htmlSpecialChars() + '</option>');
		}
		document.write(
				'</select></div>' +
				'<input type="submit" value="Wyswietl" class="button" onclick="this.form.submit()" /> ' +
			'</form>' +
			'<div id="' + this.id + '_szukaj"></div>'
		);
		
		var matches = window.location.search.match(new RegExp('[?&]' + this.id + '=([^&]+)'));
		if (matches && typeof matches[1] != 'undefined')
		{
			document.forms[this.id].elements['haslo'].value = unescape(matches[1]);
			this.zaznacz();
			this.szukaj();
		}
	}
}



Indeks.prototype.wyszukaj = function(adres, haslo)
{
	var search = adres.match(/#.*/);
	adres = adres.replace(/#.*/, '').replace(new RegExp('[&?]+' + this.id + '=[^&]*', 'g'), '');
	if (adres.indexOf('&') >= 0 && adres.indexOf('?') < 0) adres = adres.replace(/&/, '?');
	this.wyswietl(adres + (adres.indexOf('?') < 0 ? '?' : '&') + this.id + '=' + escape(haslo) + (search ? search : ''));
}

Indeks.prototype.wyswietl = function(adres)
{
	if (typeof adres == 'undefined') adres = document.forms[this.id].elements['hasla'].value;
	if (typeof this.ramka == 'string')
	{
		var okno = window.open(adres, this.ramka, 'menubar=yes,toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes');
		if (okno) okno.focus();
		else window.location.href = adres;
	}
	else this.ramka.location.href = adres;
}

Indeks.prototype.zaznacz = function()
{
	var haslo = document.forms[this.id].elements['haslo'].value.toLowerCase();
	for (var i = 0; i < document.forms[this.id].elements['hasla'].options.length; i++)
	{
		if (document.forms[this.id].elements['hasla'].options[i].text.toLowerCase().indexOf(haslo) == 0)
		{
			document.forms[this.id].elements['hasla'].options[i].selected = true;
			break;
		}
	}
}




String.prototype.htmlSpecialChars = function(attribute)
{
	var str = this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	if (typeof attribute != 'undefined' && !attribute) str = str.replace(/"/g, '&quot;');
	return str;
}
String.prototype.addSlashes = function(limiter)
{
	if (typeof limiter == 'undefined') limiter = "'";
	var regExp = new RegExp(limiter, 'g');
	return this.replace(/\\/g, '\\\\').replace(regExp, '\\' + limiter);
}
if (!String.prototype._toLowerCase)
{
	String.prototype._toLowerCase = String.prototype.toLowerCase;
	String.prototype.toLowerCase = function()
	{
		return this._toLowerCase().replace(/[ACELNÓSZZ]/g,
			function(str)
			{
				if (str == 'A') return 'a';
				if (str == 'C') return 'c';
				if (str == 'E') return 'e';
				if (str == 'L') return 'l';
				if (str == 'N') return 'n';
				if (str == 'Ó') return 'ó';
				if (str == 'S') return 's';
				if (str == 'Z') return 'z';
				if (str == 'Z') return 'z';
				return str;
			}
		);
	}
}
String.prototype.compare = function()
{
	return this.toLowerCase().replace(/[acelnószz]/g,
		function(str)
		{
			if (str == 'a') return 'az';
			if (str == 'c') return 'cz';
			if (str == 'e') return 'ez';
			if (str == 'l') return 'lz';
			if (str == 'n') return 'nz';
			if (str == 'ó') return 'oz';
			if (str == 's') return 'sz';
			if (str == 'z') return 'zz';
			if (str == 'z') return 'zzz';
			return str;
		}
	);
}
