/*

Js functions
---------------------------------------------------------------------

Copyright (C) 2005 Marek 'juneau' Klusak, <stopar@tiscali.cz>
Web: http://rs.reality-show.net

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/



/*
Spoluprace s kontrolni otazkou.
*/
var typ_otazky;
if(typ_otazky == 3)
	{
	document.getElementById('k-otazka').style.display = 'none';
	document.getElementById('odp_a').value = '52';
	}




/*
	Tato funkce je prirazena odkazum typu "maly obrazek odkazuje na jeho puvodni velikost".
	Je predavana jen adresa obrazku.
	Jenze drive se predavala cela url (popup-img.php?...) a je nutne to odlisit pro zpetnou kompatibilitu.
*/
function popup(img)
	{
	var re = new RegExp("popup-img\.php");
	var nalez = img.search(re);
	
	if (nalez == -1) {
		// nove napsane
		window.open('/popup-img.php?img='+img,'popup','resizable,statusbar=no');
	} else {
		// stara verze
		window.open(img,'popup','resizable,statusbar=no');		
	}
	return true;
	}



/*
*	Kontrola vyplneni kontrolni otazky
*/
function submitForm()
	{
	if(document.getElementById('otazka-id'))
		{
		if(document.getElementById('odp_a').checked == false && document.getElementById('odp_b').checked == false)
			 {
			 alert('Nezodpověděli jste kontrolní otázku. Vyberte prosím správnou odpověď.');
			 return false;
			 }
		}
	return true;
	}



/*
	Skryvani a odkryvani galerii alb v menu fotoalba
*/

function fotoAlbumHide(id)
	{
    el=document.getElementById(id).style;
    el.display=(el.display == 'block') ? 'none' : 'block';
	}







function vlozSmajla(co,kam) {
	var text = document.getElementById(kam).value;
	document.getElementById(kam).value = text + ' ' + co;
}








// zvyraznovani odstavce
var pole = window.location.href.split("#");

if(pole[1])
{
	if(pole[1].substring(0,9) == "odstavec-")
		{
		var odstavec = pole[1];
		document.getElementById(odstavec).className +=" zvyraznit";
		}
	else if(pole[1].substring(0,5) == "comm-")
		{
		var odstavec = pole[1];
		document.getElementById(odstavec).className +=" komentar-zvyraznit";
		}
	else if(pole[1].substring(0,5) == "post-")
		{
		var odstavec = pole[1];
		document.getElementById(odstavec).className +=" post-blok-zvyraznit";
		}
}

// o tomhle se hezky rozepsal pixy: http://www.pixy.cz/pixylophone/2005_03_archiv.html#1110561223




function reference(cislo,formular) {
	var obsahPole = document.getElementById(formular).value;
	if (obsahPole != '')
		obsahPole += '\n';
	document.getElementById(formular).value = obsahPole + '['+cislo+']';
}




//------------------
//-------------------------------------------------------------------------------------------------------------------------------
//------------------



// zkouska: ke kazdemu lichemu/sudemu komentari se prida odpovidajici CSS trida -> lze formatovat

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}


var pole = getElementsByClassName(document,'div','komentar');

if(pole.length > 0) 
	{
	for(var i = 0; i < pole.length; i++)
		{
		var kom = pole[i];
		if ((i+1) % 2 == 0)
			{
			var classn = 'kom-sudy';
			}
		else
			{
			var classn = 'kom-lichy';
			}
		pole[i].className += ' ' + classn;
		}
	}


//------------------
//-------------------------------------------------------------------------------------------------------------------------------
//------------------
// AJAXovy naseptavac
// http://www.milosnemec.cz/clanek.php?id=157

// při prvním zobrazení stránky našeptávač skryjeme
document.getElementById("naseptavacDiv").style.visibility = "hidden";

function GetKeyCode(e) {
  if (e) {
    return e.charCode ? e.charCode : e.keyCode;
  }
  else {
    return window.event.charCode ? window.event.charCode : window.event.keyCode;
  }
}

function generujNaseptavac(e) {
  var unicode = GetKeyCode(e);
  if (unicode == 40) {
    // šipka dolů
    var naseptavacDiv = document.getElementById("naseptavacDiv");
    if (naseptavacDiv.style.visibility == "visible") {
      var naseptavac = document.getElementById("naseptavac");
      naseptavac.options.selectedIndex = 0;
      naseptavac.focus();
      return;
    }
  }
  var str = document.getElementById("slovo").value;
  if (str != "") {
    // IE/zbytek světa
    if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      httpRequest = new XMLHttpRequest();
    }
    var url = "ajaxovy-naseptavac.php?str=" + encodeURI(str);
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange= function () {processRequest(); } ;
    httpRequest.send(null);
  }
  else {
    document.getElementById("naseptavacDiv").style.visibility = "hidden";
  }
}

function processRequest() {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      var response = httpRequest.responseText;
      if (response == 'EMPTY') {
        document.getElementById("naseptavacDiv").style.visibility = "hidden";
      }
      else {
        document.getElementById("naseptavacDiv").innerHTML = response;
        document.getElementById("naseptavac").size =
          document.getElementById("naseptavac").options.length;
        document.getElementById("naseptavacDiv").style.visibility = "visible";
      }
    }
    else {
      alert("Chyba při načítání stránky"
        + httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}

function getResultClickHandler() {
  var select = document.getElementById("naseptavac");
  var nazev = select.options[select.selectedIndex].innerHTML;
  document.getElementById("slovo").value = nazev;
  document.getElementById("naseptavacDiv").style.visibility = "hidden";
}

function getResultKeyPressHandler(e) {
  if (GetKeyCode(e) == 13) {
    getResultClickHandler();
  }
}
