var podswietlenia;

var kod_img;
var kod_xml;
var kod_ico;

var image_x;
var image_y;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function wczytaj_podswietlenia(XMLHttp) {
	var container;
	var podswietlenie;
	var elements;
	var i;

	if(XMLHttp.readyState == 4 || XMLHttp.readyState == "complete") {
		var xmldoc = XMLHttp.responseXML.documentElement;
		if(xmldoc != null) {


			// pobierz element, do ktorego beda wstawiane podswietlenia
			container = window.frames["podglad"].document.getElementById('container');

			elements = xmldoc.getElementsByTagName('box');

			for(i = 0; i < elements.length; i++) {
				// utworz nowy element podswietlenia
				podswietlenie = window.frames["podglad"].document.createElement('div');

				podswietlenie.id = 'ramka_' + i;

				// przygotuj style
				podswietlenie.style.position = 'absolute';
				podswietlenie.style.backgroundColor = 'gold';
				podswietlenie.style.border = '1px solid yellow';

				podswietlenie.style.filter = 'alpha(opacity=30)';	// IE
				podswietlenie.style.opacity = 0.30;			// inne

				podswietlenie.style.top = 0;
				podswietlenie.style.left = 0;
				podswietlenie.style.width = 0;
				podswietlenie.style.height = 0;
				container.appendChild(podswietlenie);

				// zapamietaj pozycje
				podswietlenia.push(new Array(	elements[i].attributes.getNamedItem('top').value, 
								elements[i].attributes.getNamedItem('left').value, 
								elements[i].attributes.getNamedItem('width').value, 
								elements[i].attributes.getNamedItem('height').value));

			}
		}

		// ukryj info o ladowaniu podswietlen
		document.getElementById('ladowanie_podswietlen_info').style.display = 'none';

		skaluj();
	}
} // function wczytaj_podswietlenia

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function zaladuj_podswietlenia() {
	var XMLHttp = GetXMLHttpObject();

	XMLHttp.open('POST', 'wyslij_plik_xml.php', true);
	XMLHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	XMLHttp.onreadystatechange =	function() {
						return function(obj) {
							wczytaj_podswietlenia(obj);
						} (XMLHttp);
					};
	XMLHttp.send('kod=' + kod_xml);

	// pokaz info o ladowaniu podswietlen
	document.getElementById('ladowanie_podswietlen_info').style.display = 'block';
} // function zaladuj_podswietlenia

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function usun_podswietlenia() {
	var i;
	var obj;

	podswietlenia = new Array();

	i = 0;
	obj = window.frames["podglad"].document.getElementById('ramka_' + i);
	while(obj != null) {
		obj.parentNode.removeChild(obj);
		i++;
		obj = window.frames["podglad"].document.getElementById('ramka_' + i);
	}

}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function pokaz_obraz(XMLHttp) {
	if(XMLHttp.readyState == 4 || XMLHttp.readyState == "complete") {
		// ukryj info o ladowaniu obrazu
		document.getElementById('ladowanie_obrazu_info').style.display = 'none';

		usun_podswietlenia();

		var xmldoc = XMLHttp.responseXML.documentElement;
		if(xmldoc != null) {
			var image_info = xmldoc.getElementsByTagName('image')[0];
			var obraz = window.frames["podglad"].document.getElementById('obraz');
			var src = image_info.attributes.getNamedItem('src').value;

			if(src != '') {
				obraz.src = src;

				image_x = image_info.attributes.getNamedItem('size_x').value;
				image_y = image_info.attributes.getNamedItem('size_y').value;

				skaluj();

				zaladuj_podswietlenia();
			}
			else
				alert(document.getElementById('plik_nie_istnieje_info').value);
		}
	}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function zaladuj_obraz() {
	var XMLHttp = GetXMLHttpObject();

	XMLHttp.open('POST', 'przegladarka_prasy_daj_plik.php', true);
	XMLHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	XMLHttp.onreadystatechange = 	function() {
						return function(obj) {
							pokaz_obraz(obj);
						} (XMLHttp);
					};
	XMLHttp.send('kod=' + kod_img);

	// pokaz info o ladowaniu obrazu
	document.getElementById('ladowanie_obrazu_info').style.display = 'block';
} // function zaladuj_podswietlenia

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function skaluj() {
	var zoom = document.getElementById('zoom').value;
	var obj;
	var i;

	// skaluj DIV zewnetrzny
	obj = window.frames["podglad"].document.getElementById('container');
	if(obj != null) {
		obj.style.width = (image_x * zoom) + 'px';
		obj.style.height = (image_y * zoom) + 'px';
	}

	// skaluj obraz
	obj = window.frames["podglad"].document.getElementById('obraz');
	if(obj != null) {
		obj.style.width = (image_x * zoom) + 'px';
		obj.style.height = (image_y * zoom) + 'px';
	}

	// skaluj podswietlenia
	i = 0;
	obj = window.frames["podglad"].document.getElementById('ramka_' + i);
	while(obj != null && i < podswietlenia.length) {
		obj.style.top = (podswietlenia[i][0] * zoom) + 'px';
		obj.style.left = (podswietlenia[i][1] * zoom) + 'px';
		obj.style.width = (podswietlenia[i][2] * zoom) + 'px';
		obj.style.height = (podswietlenia[i][3] * zoom) + 'px';
		i++;
		obj = window.frames["podglad"].document.getElementById('ramka_' + i);
	}

} // function skaluj

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function zoom_in() {
	var zoom_obj = document.getElementById('zoom');

	if(zoom_obj.selectedIndex < zoom_obj.options.length - 1) {
		zoom_obj.selectedIndex+= 1;
		skaluj();
	}
} // function zoom_in

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function zoom_out() {
	var zoom_obj = document.getElementById('zoom');

	if(zoom_obj.selectedIndex > 0) {
		zoom_obj.selectedIndex-= 1;
		skaluj();
	}
} // function zoom_out

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function page_next() {
	var strona_obj = document.getElementById('strona');

	if(strona_obj.selectedIndex < strona_obj.options.length - 1) {
		strona_obj.selectedIndex+= 1;
		init();
	}
} // function page_next

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function page_prev() {
	var strona_obj = document.getElementById('strona');

	if(strona_obj.selectedIndex > 0) {
		strona_obj.selectedIndex-= 1;
		init();
	}
} // function page_prev

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function init() {
	var strona_obj = document.getElementById('strona');
	podswietlenia = new Array();

	if(strona_obj.selectedIndex >= 0) {
		var kody = strona_obj.options[strona_obj.selectedIndex].value;
		var kody_tmp = kody.split(':');

		kod_img = kody_tmp[0];
		kod_xml = kody_tmp[1];
		kod_ico = kody_tmp[2];

		// zaznacz miniature aktywnej strony
		var i = 0;
		var ico = window.frames["ikony"].document.getElementById('miniatura_' + i);
		while(ico != null) {
			if(i == strona_obj.selectedIndex)
				ico.className = 'icon_selected';
			else
				ico.className = 'icon';

			i++;
			ico = window.frames["ikony"].document.getElementById('miniatura_' + i);
		}

		zaladuj_obraz();
	}
} // function init

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function init_icons() {
	var strona_obj = document.getElementById('strona');
	var container_doc = window.frames["ikony"].document;
	var container = container_doc.getElementById('container');
	var img;
	var kod_ico;

	for(var i = 0; i < strona_obj.length; i++) {
		kod_ico = strona_obj.options[i].value.split(':')[2];
		img = container_doc.createElement('img');
		img.id = 'miniatura_' + i;
		img.src = 'wyslij_plik_prasy3.php?on&kp=' + kod_ico;
		img.onclick =	function(index) {
					return function() {
						window.top.set_page(index);
					}
				} (i);
		img.title = i + 1;
		if(i == 0)
			img.className = 'icon_selected';
		else
			img.className = 'icon';

		container.appendChild(img);
	}
} // function init_icons

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function set_page(page) {
	document.getElementById('strona').selectedIndex = page;
	init();
} // function set_page

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
