var ETest = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var LTest = /^([a-z0-9\.\-])+$/;

function rejestracja1()
{
   error = '';
   
   if (document.getElementById('loginr').value.length<5) error += "Login musi składać się z minimum 5 znaków!\n";
   if (!LTest.test(document.getElementById('loginr').value)) error += "Login może składać się z małych liter (bez polskich znaków), cyfr lub myślnika!\n";
   if (document.getElementById('pass1').value != document.getElementById('pass2').value)
      error += "Wprowadzone hasła różnią się od siebie!\n";
   else if (document.getElementById('pass1').value.length<5) error += "Hasło musi składać się z minimum 5 znaków!\n";
   if (!ETest.test(document.getElementById('email').value)) error += "Podaj poprawny adres e-mail (np. nazwa@domena.pl)!\n";
   if (document.getElementById('city').value.length<2) error += "Podaj miasto!\n";
   if (document.getElementById('passkod').value.length!=6) error += "Przepisz kod z obrazka!\n";
   if (document.getElementById('regulamin').checked != 1) error += "Brak akceptacji regulaminu!\n";

   if (error != '')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function rejestracja2()
{
   error = '';

   if (document.getElementById('typbudynku').value == 0) error += "Wybierz typ budynku!\n";
   if (document.getElementById('rodzajbudynku').value == 0) error += "Wybierz rodzaj budynku!\n";
   if (document.getElementById('technologia').value == 0) error += "Wybierz technologię!\n";
   if (document.getElementById('project_city').value == 0) error += "Wpisz miejsce budowy!\n";

   if (error != '')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function rejestracjakonto1()
{
   error = '';
   
   if (document.getElementById('nick').value.length<3) error += "Nick musi składać się z minimum 3 znaków!\n";
   if (!ETest.test(document.getElementById('email').value)) error += "Podaj poprawny adres e-mail (np. nazwa@domena.pl)!\n";
   if (document.getElementById('pass1').value != document.getElementById('pass2').value)
      error += "Wprowadzone hasła różnią się od siebie!\n";
   else if (document.getElementById('pass1').value.length<5) error += "Hasło musi składać się z minimum 5 znaków!\n";
   if (document.getElementById('passkod').value.length!=6) error += "Przepisz kod z obrazka!\n";
   if (document.getElementById('regulamin').checked != 1) error += "Brak akceptacji regulaminu!\n";

   if (error != '')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function zatwierdz(strona)
{
  error = '';

   if (document.getElementById('login').value.length<3) error += "Podaj login!\n";
   if (document.getElementById('pass').value.length<3) error += "Podaj hasło!\n";

   if (error != '') {
      alert(error);
      return false;
   }
   else
   {
	if (!ETest.test(document.getElementById('login').value)) document.getElementById('forma').action = "http://www."+document.getElementById('login').value+"."+strona;
	else document.getElementById('forma').action = "http://"+strona;	
      return true;
   }
}

function zatwierdz2(strona)
{
  error = '';

   if (document.getElementById('login1').value.length<3) error += "Podaj login!\n";
   if (document.getElementById('pass1').value.length<3) error += "Podaj hasło!\n";

   if (error != '') {
      alert(error);
      return false;
   }
   else
   {
	if (!ETest.test(document.getElementById('login1').value)) document.getElementById('forma1').action = "http://www."+document.getElementById('login1').value+"."+strona;
	else document.getElementById('forma1').action = "http://"+strona;	
      return true;
   }
}

function zatwierdz3(strona)
{
  error = '';

   if (document.getElementById('login3').value.length<3) error += "Podaj login!\n";
   if (document.getElementById('pass3').value.length<3) error += "Podaj hasło!\n";

   if (error != '') {
      alert(error);
      return false;
   }
   else
   {
	if (!ETest.test(document.getElementById('login3').value)) document.getElementById('forma3').action = "http://www."+document.getElementById('login3').value+"."+strona;
	else document.getElementById('forma3').action = "http://"+strona;	
      return true;
   }
}



function sprawdzenie_komentarz()
{
   error = '';
   if (document.getElementById('tresc').value.length<2) error += "Wpisz treść komentarza!\n";
   if (!document.getElementById('reg').checked) error += "Brak zgody na publikację!\n";

   if (error != '')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function przypomnienie()
{
   error = '';
   if (document.getElementById('loginp').value.length<4) error += "Wpisz poprawny login!\n";
   if (!ETest.test(document.getElementById('mailp').value)) error += "Podaj poprawny adres e-mail!\n";
      
   if (error != '')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function mail_do_uzytkownika() {
	
	error = '';
	
	text = document.getElementById('tresc').value;
	
	if(text == '' || text == 'Wpisz wiadomość') error += 'Wpisz treść wiadomości!\n';
	
	if (error != '') {
		alert(error);
		return false;
	} else return true;
}

function showhide(co)
{

   if (document.getElementById(co).style.display == 'none') document.getElementById(co).style.display = 'block'; else document.getElementById(co).style.display = 'none';

   return false;
}

function nowe_okno(adres)
{
   window.open(adres,'','');

}

function new_window_zdjecie(adres)
{
   window.open(adres, 'Zdjecie', 'resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=no, width=600, height=600');
}

function new_window_img(adres)
{
   window.open('powiekszenie.php?plik='+adres, 'Zdjecie', 'resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,status=no,width=820,height=700');
}

function new_window(adres)
{
   window.open(adres, 'Pomoc', 'resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=no, width=600, height=600');
}

function wstawkolor(pole, wartosc)
{
      document.kolory.getElementById(pole).value = wartosc;	
      return true
}

function reklamastyle()
{

element = document.getElementById("reklama");

  element.style.fontSize = "14px";
  element.style.fontWeight = "bold";
  element.style.color = "red";

        x = document.body.scrollWidth;
        y = document.body.scrollHeight;

//alert((y-100));

  element.style.right = ((x-980)/2)+'px';
//  element.style.bottom = ((y-150))+'px';
}


function zamknij() {
  widoczne=false;
}

function rusz() {
  if (window.scrollX>=0) { // przesunięcie okna dla Netscape
    x = window.scrollX; y = window.scrollY;
  } else { // dla IE, Opery, Firefox
    x = document.body.scrollLeft;
    //y = document.body.scrollTop; // nie dziala w IE 7
    y = document.documentElement.scrollTop;
  }
  docelowax = pozycjax + x;
  doceloway = pozycjay + y;

  yd = (doceloway - parseInt(obj.style.top)) / 5;
  xd = (docelowax - parseInt(obj.style.left)) / 5;

  obj.style.left = parseInt(obj.style.left) + xd + "px";
  obj.style.top = parseInt(obj.style.top) + yd + "px";

  if (widoczne) { setTimeout("rusz()", 50); }
  else { obj.style.visibility = "hidden"; }

}

function changeContent(id) {
	
	var divsNames = new Array('postsDiv','buildDiv','firmDiv','observDiv');
	var linksNames = new Array('postsLink','buildLink','firmLink','observLink');

	var display;
	var className;
	 
	 for(var i = 0; i < divsNames.length; i++){
		 
	  if(id == i){
		  
		  className = 'selected';
		  display = 'block';
	  }
	  else{
	
		  if(i == 3) className = 'unselected_main_table';
		  else className = 'unselected';
		  
		  display = 'none';
	  }
	  
	  document.getElementById(divsNames[i]).style.display = display;
	  document.getElementById(linksNames[i]).className = className;
	 }
}


function wstecz() {
 history.back();
}





function showHideProject() {
	if(document.getElementById('check_project').checked == true){
		document.getElementById('projectTR').style.display = '';
		document.getElementById('autorTR').style.display = '';
		document.getElementById('project_id').disabled = true;
	}	
	else {
		document.getElementById('projectTR').style.display = 'none';
		document.getElementById('autorTR').style.display = 'none';
		document.getElementById('project_id').disabled = false;
	}
}

/* v - wartość wpisu lub komentarza */

function setItemId(v, from){
	 
	 unsetItemId();
	
	 document.getElementById('co').value = from;

	 switch(from) {
	 
	 	case 'raczki_wpisy':
		case 'ulubiony':
			
			document.getElementById('wpis_id').value = v;
			document.getElementById('kom_id').value = '';
			
		break;
		 
		case 'raczki_komentarze':
			
			document.getElementById('kom_id').value = v;
			document.getElementById('wpis_id').value = '';
			
		break;
	 }
}

function setItemId2(v,from) {
	
	unsetItemId();
	
	document.getElementById('wpis_id').value = v;
	
	if(from == 'wpis' || from == 'zdjecie') document.getElementById('komentarz').value = from;
	else document.getElementById('ulubione').value = from;
}


function unsetItemId() {

	document.getElementById('ulubione').value = '';
	document.getElementById('komentarz').value = '';
	document.getElementById('co').value = '';
	document.getElementById('wpis_id').value = '';
	document.getElementById('kom_id').value = '';
}

function zatwierdzSzukaj()
{
  error = '';

   if (document.getElementById('project_or_blog_name').value.length < 3) error += "Za krótka fraza do wyszukania\n";

   if (error != '') {
      alert(error);
      return false;
   }
   else return true;
}

function getTop(event) {
	  
	  var scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;

	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		
	  }
	  
	  return scrOfY;
}

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

var photos_paths = new Array();

var item_id = 0;

function show_gallery(arr, photo_id, event, _item_id) {
	
	item_id = _item_id;
	
	photos_paths = arr.slice(0);
	
	if(typeof photo_id == 'string') {
		
		var i = 0;
		
		for(; i < photos_paths.length; i++) {
			
			if(photo_id.substr(1) == photos_paths[i].path) {
				
				photo_id = i;
				break;
			}
		}
		
		if(i == photos_paths.length) photo_id = 0;
	}
	
	selected_photo = parseInt(photo_id) % perpage + 1;
	
	selected_page = parseInt(photo_id / perpage);

	show_form(1, 2, event);
}

/*
	show_form(1,1,event); - pokaz login
	show_form(0,1,event); - ukryj login
	
	show_form(1,3,event); - pokaz login przy dodawaniu komentarza
	show_form(0,3,event); - ukryj login
*/

function show_form(v, z, event){
	
	// escape dla loginu i galerii
	
	if(v == 1) {
		
		document.onkeydown = function(evt) {
		    
			evt = evt || window.event;	// ie
		    if (evt.keyCode == 27) {
		        
		    	v = 'hidden';
		    	
		    	log_op = document.getElementById('login_opacity');
		    	log_f = document.getElementById('login_form_v');
		    	
		    	gal_op = document.getElementById('gallery_opacity');
		    	gal_f = document.getElementById('gallery_form');
		    	
		    	if(log_op.style.visibility == 'visible' && gal_op.style.visibility == 'visible') {
		    		
		    		log_op.style.visibility = v;
		        	log_f.style.visibility = v;
		    	}
		    	else {
		    		
			    	log_op.style.visibility = v;
			    	log_f.style.visibility = v;
			    	gal_op.style.visibility = v;
			    	gal_f.style.visibility = v;
		    	}
		    }
		};
	}
	else {
		
		document.onkeydown = '';	
	}
	
	if(z == 3) {
		
		opacity = 'login_opacity';
		form = 'login_form_v2'
	}
	else if(z == 1) {
		
		opacity = 'login_opacity';
		form = 'login_form_v'
			
		// uzupelnienie formularzu logowania:
		// user jest w powiększeniu zdjęć i chce się zalogować
		
		if(choose == 'main' && ( document.getElementById('gallery_form').style.visibility == 'visible' )) {
			
			document.getElementById('galeria').value = 1;
			
			big_photo = document.getElementById('main_img').src;

			index_path = big_photo.indexOf('/pliki/');
			
			document.getElementById('zdjecie_sciezka').value = big_photo.substr(index_path);
	
			document.getElementById('item_id').value = item_id;
			
			t = document.getElementById('gallery_form').style.top;
			
			document.getElementById('wysokosc').value = t.substr(0, t.length - 2);
		
		}
		else if(choose == 'main') {
			
			document.getElementById('galeria').value = 0;
		}
		
	}
	else {
		
		opacity = 'gallery_opacity';
		form = 'gallery_form';
	
		if(v == 1) {
			
			if(typeof event == 'number') { // powrót z logowania
				
				document.getElementById(form).style.top = event + 'px';
			}
			else document.getElementById(form).style.top = getTop(event) + 'px';
			
			init_gallery();
		}
		else {
			
			selected_photo = 0;
			selected_page = 0;
		}
	}
	
	if(v == 1) t = 'visible';
	else t = 'hidden';

	document.getElementById(opacity).style.height = getDocHeight();
	
	document.getElementById(opacity).style.visibility = t;
	document.getElementById(form).style.visibility = t;

	return false;
}


var photos = 0;
var perpage = 0;
var page_last = 0;
var current_page = 0;
var current_photo = 1;

var selected_photo = 0;
var selected_page = 0;

function photo(id, path, width, height) {
	
	this.id = id;
	this.path = path;
	this.width = width;
	this.height = height;
}

function init_gallery() {

	if(choose == 'gallery') {
		
		_hash = document.location.hash;
		
		if(_hash != '' && _hash.indexOf(',') != -1) {
			
			ind1 = _hash.indexOf(',');
			
			selected_page = parseInt( _hash.substr(1, ind1)) - 1;
			
			selected_photo = parseInt( _hash.substr(ind1 + 1));
		}
	}
	
	if(selected_photo != 0) {
		
		current_page = selected_page;
		current_photo = selected_photo;
	}
	
	photos = photos_paths.length;
	
	page_last = parseInt(photos / perpage);
	
	if(photos % perpage != 0) page_last += 1;
	
	prev_next();
	
	show_photos(0, false);
	
	document.getElementById('photos_all').innerHTML = photos;
	
	prev_next_photo();	// załadowanie strzałek na odpowiednim miejscu
}

/*
Logowanie w galerii, nie na stronie bloga 
*/

function login(v) {

	if(v == 1) {
		
		t = 'visible';
		
		window.onscroll = function() {
		
			window.scrollTo(0,0);
		
			document.getElementById('hash').value = hash_c;
			
			window.location.hash = hash_c;
		}
	}
	else {
		
		t = 'hidden';
		
		window.onscroll = function() {
		}
	}
	document.getElementById('opacity_window').style.visibility = t;
	document.getElementById('login_form').style.visibility = t;
}



function createRequest() {

	try {
	
		request = new XMLHttpRequest();
	
	} catch(tryMS) {
		
		try {
		
			request = new ActiveXObject("Msxml2.XMLHTTP");
		
		} catch(otherMS) {
		
			try {
			
				request = new ActiveXObject("Microsoft.XMLHTTP");
			
			} catch (failed) {
			
				request = null;
			}
		}
	}
	
	return request;
}

var cat_ajax = 'ajax_files/';

function getLoadPage(id) {
	
	id_ = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	file = 'load_page.php';
	str = 'id=' + id;
	str += '&id2=' + photos_paths[id_].id;
	
	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 1){
		
			// document.getElementById(id).innerHTML = '<img src="loader.gif"/>'; // blad ie
		}
		else if(request.readyState == 4 && request.status == 200) {

			text = request.responseText;
			
			if(( ast_pos = text.indexOf('*')) > 0) {
				
				num = text.substr(ast_pos + 1);
				
				selectOption(num);
				
				text = text.substr(0, ast_pos);
			}
			
			document.getElementById('favourite_photo').onclick = function () {
				
				if(text.charAt(0) == '-') login(1);
				else getFavouritePhoto(document.getElementById('favourite_photo'));
			}
			
			plus_pos = text.indexOf('+');
			
			bird_pos = text.indexOf('^');
			
			if(text.charAt(0) == '-') s = '';
			else s = text.substr(0, plus_pos);
			
			
			
			document.getElementById('delete_photo').innerHTML = s;
			
			document.getElementById('favourite_photo').innerHTML = text.substr(plus_pos + 1, bird_pos - plus_pos - 1);
			
			document.getElementById('komentarze').innerHTML = text.substr(bird_pos + 1);
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}

/* Zaznaczenie wyświetlanych */

function selectOption(op) {
	
	if(document.getElementById('add1') != null && document.getElementById('add2') != null &&
		document.getElementById('add3') != null) {
		
		switch(parseInt(op)) {
		
			case 3:
				document.getElementById('add3').innerHTML = '[x] wnętrze';
				document.getElementById('add2').innerHTML = '[ ] zdjęcie';
				document.getElementById('add1').innerHTML = '[ ] nic';
			break;
			
			case 2:
				document.getElementById('add3').innerHTML = '[ ] wnętrze';
				document.getElementById('add2').innerHTML = '[x] zdjęcie';
				document.getElementById('add1').innerHTML = '[ ] nic';
			break;
			
			case 1:
			default:
				document.getElementById('add3').innerHTML = '[ ] wnętrze';
				document.getElementById('add2').innerHTML = '[ ] zdjęcie';
				document.getElementById('add1').innerHTML = '[x] nic';
		}
	}
}

/* Dodanie do wyświetlanych */

function getAddPhoto(op) {
	
	str = 'id=' + photos_paths[parseInt(document.getElementById('photo_id').innerHTML) - 1].id;
	
	str += '&galeria=' + op;
	
	file = 'add_photo.php';

	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {	
			
			text = request.responseText.substr();
			
			if(text.substr(0,4) != 'Błąd') {
			
				selectOption(op);
				
				document.getElementById('komunikaty').innerHTML = '<span class="info2">' + text + '</span>';
			}
			else {
				document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
			}
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}


/*
	Pobieranie komentarzy przy dodaniu nowego komentarza
	- główna różnica taka, że mogš wystšpić jakie komunikaty zwrotne, które trzeba przekazać w inne miejsce
	- 
*/

function getCommentsPlus() {

	if(isValidCommentForm()) {

		file = 'add_comment.php';
	
		str = '';
		/*
		  czy pola id i komentarza są już wykorzystane ?  
		 */
		if(document.getElementById('id_formularz').value != '') str += 'id=' + document.getElementById('id_formularz').value;
	
		if(document.getElementById('komentarz_formularz').value != '') str += '&komentarz=' + document.getElementById('komentarz_formularz').value;
		
		if(document.getElementById('tytul2').value != '') str += '&tytul='+ document.getElementById('tytul2').value;	
	
		str += '&tresc='+ document.getElementById('tresc2').value;
		
		request = createRequest();
		
		if(request == null) { alert('Unable to create request'); return;}

		request.onreadystatechange = function() {
	  
			if(request.readyState == 4 && request.status == 200) {	

				text = request.responseText;
				
				if(text.substr(0,4) == 'Błąd') {
					
					document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
				}
				else {
					
					document.getElementById('komunikaty').innerHTML = '<span class="info2">Komentarz został dodany</span>';
				
					document.getElementById('komentarze').innerHTML = text;
					
				}
			}
		}
				
		request.open('GET', server + cat_ajax + file +'?'+str, true);
		
		request.setRequestHeader('Content-type', 'text/html; charset=iso-8859-2');
		
		request.send();
		
		show_comment_form(1, '', '', 'dodaj');
	}
}

/*
	Walidacja formularza dodawania komentarzy
*/

function isValidCommentForm() {

	error2 = '';
	
	if(document.getElementById('tresc2').value == '') error2 += 'Wpisz treść komentarza!\n';

	if(!document.getElementById('reg2').checked) error2 += 'Brak zgody na publikację!\n';

	if(error2 !=  '') { alert(error2); return false;}
	return true;
}

/*
	obsługa blokowania, odblokowywania komentarzy
*/

function getActiveComment(obj) {
	
	str = 'id=' + obj.id;
	
	if(obj.innerHTML == 'odblokuj') blok = 1;
	else blok = 0; 
	
	str += '&blok=' + blok;
	
	file = 'active_comment.php';

	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {	
			
			text = request.responseText.substr();
			
			if(text.substr(0,4) != 'Błąd') {
				
				if(blok == 0) {
					
					obj.innerHTML = 'odblokuj';
					
					document.getElementById('k_' + obj.id).style.backgroundColor = '#FCB69C';
				}
				else {
					obj.innerHTML = 'zablokuj';
					
					document.getElementById('k_' + obj.id).style.backgroundColor = 
					document.getElementById('kolor_komentarzy').style.backgroundColor;

				}
				
				document.getElementById('komunikaty').innerHTML = '<span class="info2">' + text + '</span>';
			}
			else {
			
				document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
			}
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}

/*
Sprawdzenie czy jest możliwość edycji komentarza przez użytkownika
*/

function getCheckEditComment(obj) {
	
	id = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	str = 'id=' + photos_paths[id].id;
	
	str += '&komentarz=zdjecie';
	
	str += '&kom_id=' + obj.id;
	
	file = 'check_edit_comment.php';

	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {	
			
			text = request.responseText.substr();
			
			if(text.substr(0,2) != 'ok') {
				
				document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
				
				obj.innerHTML = '';
			}
			else {
			
				document.getElementById('komunikaty').innerHTML = '<span class="info2">Czas na edycję komentarza to 30 minut.</span>';
			
				document.getElementById('id_edytowanego_komentarza').innerHTML = obj.id;
				
				if(document.getElementById('comment_title') != null) {
					
					title = document.getElementById('comment_title').innerHTML;
				} else title = '';
				
				
				show_comment_form(0, title, document.getElementById('comment_desc').innerHTML,'edytuj');
			}
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}


function getEditComment() {

	id = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	str = 'id=' + photos_paths[id].id;
	
	str += '&komentarz=zdjecie';

	kom_id = document.getElementById('id_edytowanego_komentarza').innerHTML;
	
	str += '&kom_id=' + kom_id;

	
	str += '&tresc=' + document.getElementById('tresc2').value;
	
	str += '&tytul=' + document.getElementById('tytul2').value;
	
	file = 'edit_comment.php';

	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {	
			
			text = request.responseText;
			
			if(text.substr(0,4) == 'Błąd') {
				
				document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
			}
			else {
			
				document.getElementById('komunikaty').innerHTML = '<span class="info2">Komentarz został zmieniony.</span>';
			
				document.getElementById('komentarze').innerHTML = text;
			
				show_comment_form(0, '', '', 'dodaj');
			}
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	
	request.setRequestHeader('Content-type', 'text/html; charset=iso-8859-2');
	
	request.send();
}


/*
	dodanie do ulubionych
*/

function getFavouritePhoto(obj) {
		
	_id = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	str = 'id=' + photos_paths[_id].id;
	
	if(obj.innerHTML.substr(0,5) == 'dodaj') action =  'add';
	else action = 'delete';
	
	str += '&action=' + action;
	
	file = 'favourite_photo.php';

	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {	
			
			text = request.responseText;
			
			if(text.substr(0,4) == 'Błąd') {
				
				document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
			}
			else {
			
				if(action == 'add'){
					
					fav1 = 'Zdjęcie zostało dodane do ulubionych.';
					fav2 = 'usuń zdjęcie z ulubionych&nbsp;|&nbsp;';
				}
				else {
					
					fav1 = 'Zdjęcie zostało usunięte z ulubionych.';
					fav2 = 'dodaj zdjęcie do ulubionych&nbsp;|&nbsp;';
				}
				
				document.getElementById('komunikaty').innerHTML = '<span class="info2">' + fav1 + '</span>';
				
				obj.innerHTML = fav2;
			}
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}

/*
	Usuń zdjęcie, jeśli nie ma go we wpisie
*/

function getDeletePhoto(obj) {
	
	if(obj.innerHTML.substr(0,4) == 'Usuń') {
	
		_id = parseInt(document.getElementById('photo_id').innerHTML) - 1;
		
		str = 'id=' + photos_paths[_id].id;
		
		file = 'delete_photo.php';
	
		request = createRequest();
		
		if(request == null) { alert('Unable to create request'); return;}
	
		request.onreadystatechange = function() {
	  
			if(request.readyState == 4 && request.status == 200) {	
				
				text = request.responseText;
				
				if(text.substr(0,4) == 'Błąd') {
					
					document.getElementById('komunikaty').innerHTML = '<span class="error2">' + text + '</span>';
				}
				else {

					show_photos(1, false);
					
					document.getElementById('komunikaty').innerHTML = '<span class="info2">Zdjęcie zostało usunięte.</span>';
					
					obj.innerHTML = ''
				}
			}
		}
				
		request.open('GET', server + cat_ajax + file +'?'+str, true);
		request.send();
	}
}


/* onMouseOver - najechanie na miniaturkę */

function on_mouse_over(obj) {
	
	if(previous_photo_id != obj.id) {
		
		obj.className='opacity100'
	}
}

/*
	Pokazanie lub ukrycie przycisków poprzedni i następny
*/

function prev_next() {	

	if(current_page == 0) document.getElementById('prev_arrow').style.display = 'none';
	else document.getElementById('prev_arrow').style.display = 'inline-block';
	
	if(current_page == page_last - 1) document.getElementById('next_arrow').style.display = 'none';
	else document.getElementById('next_arrow').style.display = 'inline-block';
}

/*
Dodaje #numer strony,numer zdjecia na stronie
*/

var hash_c = '';

function addHashToAddress(page, photo_id) {
	
		_hash = '#' + page + ',' + photo_id;
	
		window.location.hash = _hash;
		hash_c = _hash;
}

/*
Zmiana klasy miniaturki i jej zdarzeń 
*/

var previous_photo_id = 0; 

function change_mini_class(idk) {
	
	document.getElementById(idk).className = 'img_selected';

	document.getElementById(idk).onmouseout = '';
	
	if(previous_photo_id != 0 && previous_photo_id != idk) {
		
		document.getElementById(previous_photo_id).onmouseout = function () { this.className='opacity50' };
		
		document.getElementById(previous_photo_id).className = 'opacity50';
	}

	previous_photo_id = idk;
}

function show_main_photo(idk) {
	
	id_ = current_page * perpage + parseInt(idk.substr(3)); // numer zdjęcia
	
	document.getElementById('photo_id').innerHTML = id_;
	
	document.getElementById('main_img').src = photos_paths[id_ - 1].path; 
		
	change_mini_class(idk);

	
	show_comment_form(1, '', '', 'dodaj');
	
	photo_id2 = document.getElementById('id'+(idk).substr(3)).innerHTML;
	
	
	document.getElementById('id_formularz').value = photo_id2;	// id w formularzu
	
	document.getElementById('komunikaty').innerHTML = ''; // czyszczenie komunikatu z poprzedniej strony
	
	if(choose == 'gallery') {
		getLoadPage(photo_id2);
	
		addHashToAddress(current_page + 1, parseInt(idk.substr(3)));
	}
	else getLoadPageMain(photo_id2);

	if(current_page == 0 && parseInt(document.getElementById('photo_id').innerHTML) == 1) document.getElementById('prev_arrow_photo').style.display = 'none';
	else document.getElementById('prev_arrow_photo').style.display = 'inline-block';
	
	if(document.getElementById('photos_all').innerHTML == document.getElementById('photo_id').innerHTML) last_photo = true; else last_photo = false;
	
	if(current_page == page_last - 1 && last_photo == true) document.getElementById('next_arrow_photo').style.display = 'none';
	else document.getElementById('next_arrow_photo').style.display = 'inline-block';
	
	setMainImgOpacity();
}

/*
	Podanie cieżek do zdjęć, które mają być pokazane.
	- popraw we wszystkich wywołaniach drugi parametr
*/

function show_photos(_delete_photo, _previous) {

	if(_delete_photo == 1) {
	
		photos_paths.splice(id_ - 1, 1);
		
		photos --;
		
		document.getElementById('photos_all').innerHTML = photos;
	}

	from = perpage * current_page;

	if(current_page == page_last - 1) { // ostatnia strona
	
		to = photos;
	
	} else {
	
		to = perpage * (current_page + 1);
	}
		
	for(var i = from, j = 1; i < to; i++, j++) {
		
		document.getElementById('img'+j).style.display = 'inline';
		document.getElementById('img'+j).src = server + 'photo.php?size=100&zdjecie=' + server + photos_paths[i].path;

		document.getElementById('id'+j).innerHTML = photos_paths[i].id;
	}
	
	
	if(current_page == page_last - 1) {
		
		rest = photos % perpage;
		
		if(rest != 0) {
		
			end = photos + (perpage - (rest));
			
			for(var i = photos, j = rest + 1; i < end; i++, j++) {
			
				document.getElementById('img'+j).style.display = 'none';
			}
		}
	}
	
	if(current_photo == 0) {
	
		if(_previous){
			
			_k = from + perpage - 1;
			_kk = perpage;
		}
		else {
			_k = from;
			_kk = 1;
		}
		
		document.getElementById('main_img').src = server + photos_paths[_k].path;

		document.getElementById('photo_id').innerHTML = _k + 1;
		
		photo_id1 = photos_paths[_k].id;
	
		if(choose == 'gallery') addHashToAddress(current_page + 1, _kk);
	}
	else {
		
		if(choose == 'gallery') addHashToAddress(current_page + 1, current_photo);
		
		current_photo = current_photo + perpage * current_page - 1;
		
		document.getElementById('main_img').src = server + photos_paths[current_photo].path;

		document.getElementById('photo_id').innerHTML = current_photo + 1;
		
		photo_id1 = photos_paths[current_photo].id;
	
		current_photo = 0;
	}
	
	document.getElementById('id_formularz').value = photo_id1; // id w formularzu
	
	if(choose == 'gallery') getLoadPage(photo_id1);
	else getLoadPageMain(photo_id1);
	
	if(selected_photo != 0) { // galeria na blogach, zaciemnienie wybranego zdjęcia
		
		change_mini_class(document.getElementById('img'+selected_photo).id);
		
		selected_photo = 0;
		selected_page = 0;
	}
	else {
		
		if(_previous) _wh = 'img' + perpage; else _wh = 'img1';

		change_mini_class(document.getElementById(_wh).id);		
	}
	document.getElementById('komunikaty').innerHTML = '';	// czyszczenie komunikatu z poprzedniej strony
}

/*
	Zmiana bieżšcej strony ( Przyciski poprzedni, następny )
*/

function page_num(num, _previous) {
	
	if(num == -2) current_page ++;
	if(num == -1) current_page --;

	if(num > -1 && num < page_last) current_page = num;
	
	if(document.getElementById('userSelectPageId') != null) {
		
		document.getElementById('userSelectPageId').selectedIndex = current_page;
	}
	
	prev_next();
	
	show_photos(0, _previous);
	
	prev_next_photo();	// bedzie sie uruchamiac dwa razy gdy to ostatnia strona i zostala zmieniona przez klikniecie w przycisk
}

/* Zmiana bieżącego zdjęcia przez strzałkę */

function photo_num(num) {
	
	_id = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	_idk = _id - current_page * perpage + 1;
	
	if( (_idk == 1 && num == -1)  || (_idk == perpage && num == -2) ) {
		
		if(_idk == 1 && num == -1) page_num(num, true);
		else page_num(num, false);
	}
	else {
	
		if(num == -2) _idk += 1; else _idk -= 1;
		
		show_main_photo('img' + _idk);
	}

	prev_next_photo();
}


function prev_next_photo() {
	
	if(current_page == 0 && parseInt(document.getElementById('photo_id').innerHTML) == 1) document.getElementById('prev_arrow_photo').style.display = 'none';
	else document.getElementById('prev_arrow_photo').style.display = 'inline-block';
	
	if(document.getElementById('photos_all').innerHTML == document.getElementById('photo_id').innerHTML) last_photo = true; else last_photo = false;
	
	if(current_page == page_last - 1 && last_photo == true) document.getElementById('next_arrow_photo').style.display = 'none';
	else document.getElementById('next_arrow_photo').style.display = 'inline-block';
	
	setMainImgOpacity();
}

var photoSide = 0;

function setMainImgOpacity() {

	_num = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	_h = photos_paths[_num].height + 'px';
	
	_width = photos_paths[_num].width;
	
	document.getElementById('next_arrow_photo').style.height = _h;
	document.getElementById('prev_arrow_photo').style.height = _h;
		
	_left_offset = ( document.getElementById('main_image').offsetWidth - _width ) / 2; 
	
	_op = document.getElementById('main_img_opacity');
	
	_op.style.left =  _left_offset + 'px';
	_op.style.width = _width + 'px';
		
	photoSide = 0;
}

function onMouseMovePrevPhoto() {
	
	if(photoSide == 0 || photoSide == 2) {
		
		document.getElementById("next_arrow_photo").className = 'next_arrow_photo_hidden';
		document.getElementById("prev_arrow_photo").className = 'prev_arrow_photo_visible';
		
		photoSide = 1;
	}
}

function onMouseMoveNextPhoto() {
	
	if(photoSide == 0 || photoSide == 1) {
		
		document.getElementById("next_arrow_photo").className = 'next_arrow_photo_visible';
		document.getElementById("prev_arrow_photo").className = 'prev_arrow_photo_hidden';

		photoSide = 2;
	}
}

function onMouseOutMainImgOpacity() {
	
	photoSide = 0;
	
	document.getElementById("next_arrow_photo").className = 'next_arrow_photo_hidden';
	document.getElementById("prev_arrow_photo").className = 'prev_arrow_photo_hidden';
}


function show_comment_form(t, title, desc, from){
	
	com = document.getElementById('dodaj_komentarz');

	if(com.style.display == 'block' || t == 1) com.style.display = 'none';
	else com.style.display = 'block';
	
	document.getElementById('tytul2').value = title;
	document.getElementById('tresc2').value = desc;
	
	if(from == 'dodaj') {

		document.getElementById('submitBtn').onclick = function() {getCommentsPlus(); }
		document.getElementById('submitBtn').value = 'dodaj';
	}
	else {
		
		document.getElementById('submitBtn').onclick = function() {getEditComment(); } // edytuj
		document.getElementById('submitBtn').value = 'edytuj';
	}
}

/* Funkcja do obsługi powiększania zdjęć. Wczytuje komentarze, dodanie do ulubionych. */

function getLoadPageMain(id) {
	
	id_ = parseInt(document.getElementById('photo_id').innerHTML) - 1;
	
	file = 'load_page_main.php';
	str = 'id=' + id;
	str += '&path=' + photos_paths[id_].path;
	
	request = createRequest();
	
	if(request == null) { alert('Unable to create request'); return;}

	request.onreadystatechange = function() {

		if(request.readyState == 4 && request.status == 200) {

			text = request.responseText;
			
			if(( ast_pos = text.indexOf('*')) > 0) {
				
				num = text.substr(ast_pos + 1);
				
				selectOption(num);
				
				text = text.substr(0, ast_pos);
			}
			
			document.getElementById('favourite_photo').onclick = function () {
				
				if(text.charAt(0) == '-') show_form(1,1);
				else getFavouritePhoto(document.getElementById('favourite_photo'));
			}

			bird_pos = text.indexOf('^');
			
			if(text.charAt(0) == '-') start_pos = 1;
			else start_pos = 0;
			
			document.getElementById('favourite_photo').innerHTML = text.substr(start_pos, bird_pos - start_pos);
			
			document.getElementById('komentarze').innerHTML = text.substr(bird_pos + 1);
		}
	}
			
	request.open('GET', server + cat_ajax + file +'?'+str, true);
	request.send();
}

function userSelectPage(t) { // this
	
	page_num(parseInt(t.value), false);
}
