/********************************************************************
 * Služi za rad s horizontalinim MENU koji ima svoje SUBMENU.
 *
 * Sadrži funkcije za rad sa DIV Info u kojega upisujemo informacije,
 * kao i za vizualnu manipulaciju sa standardnim elementima.
 *******************************************************************/
var kaj, to, inf;

/****************
 * I N F O      *
 ****************/

function CreateInfo() {
	inf = document.getElementById( "info" );
	if( inf != null ) return inf;
	
	inf = document.createElement( "DIV" );
	inf.id = "info";
	inf.className = "info neprint";

	var i = document.createElement( "IMG" );
	i.className = "gumbic";
	i.src = "stilovi/x.gif";
	i.alt = "Zatvori";
	i.title = "Zatvori";
	i.onclick = ZatvoriInfo;
	inf.appendChild( i );

	i = document.createElement( "DIV" );
	i.style.clear = "both";
	inf.appendChild( i );
	
	document.getElementById( "glavniStupac" ).appendChild( inf );

	return inf;
}

/*
 * Upisuje dani tekst u DIV Info i prikazuje ga
 */
function Info(x) {
	inf = CreateInfo();
	inf.lastChild.innerHTML = "<h3>"+x+"</h3>";
}
/*
 * Sakriva DIV info
 */
function ZatvoriInfo( ) {
	inf.parentNode.removeChild( inf );
}
/*
 * Funkcija koju aktivira AJAX:
 * 	Ispisuje tekst koji vraća neka PHP skripta.
 * 	Ako se u tekstu nalazi "OK", Info se zatvara.
 */
function OkZatvaraInfo() {
	if( http.readyState == 4 ) {
		uPogonuSam = false;
		inf.lastChild.innerHTML += http.responseText;
		document.getElementsByTagName("BODY")[0].style.cursor = "auto";
		if( http.responseText.indexOf("OK") != -1 ) {
			setTimeout( "ZatvoriInfo()", 2 );
		}
	}
}
/*
 * Funkcija koju aktivira AJAX:
 * 	Ispisuje tekst koji vraća neka PHP skripta.
 * 	Ako se u tekstu ne nalazi "Greška", Info se zatvara.
 */
function BezGreskeZatvaraInfo() {
	if( http.readyState == 4 ) {
		uPogonuSam = false;
		inf = CreateInfo();
		inf.lastChild.innerHTML += http.responseText;
		document.getElementsByTagName("BODY")[0].style.cursor = "auto";
		if( http.responseText.indexOf("Greška") == -1 ) {
			setTimeout( "ZatvoriInfo()", 2 );
			location=location;
		}
	}
}

/************
 * M E N U  *
 ************/

/*
 * Submenu je, u stvari, samo jedan.
 * U njega upisujemo potrebne linkove i
 * njega mičemo lijevo-desno ovisno o potrebi
 */

/*
 * Sakriva Submenu
 */
function SakrijSubMenu() {
	document.getElementById("submenu").style.display = "none";
}

function GdjeSam( b ) { // Y
	if ( b.offsetParent ) {
		curtop = b.offsetTop
		while ( b = b.offsetParent) {
			curtop += b.offsetTop
		}
	}
	return curtop;
}

function GdjeSamX( b ) {
	if ( b.offsetParent ) {
		curleft = b.offsetLeft
		while ( b = b.offsetParent) {
			curleft += b.offsetLeft
		}
	}
	return curleft;
}


/*
 * Pokazuje zahtjevani SubMenu tako da prije ispuni i namjesti.
 */
function PokaziSubMenu( x, th ) {
	if( to !== undefined ) {
		clearTimeout(to);
	}
	var sm = document.getElementById("submenu");
	sm.innerHTML = document.getElementById( "sbm" + x ).innerHTML;
	sm.style.left = GdjeSamX( th ) + "px";
	sm.style.top = ( 20 + GdjeSam( th ) ) + "px";
	sm.style.display = "block";
}

/*
 * Otvara/Zatvara Sadrćaj ovako definirane kućice:
 * -----------------------------------------
 * <div>
 * 	<div>
 * 		<img ... onClick='OZ2(this)' />
 * 		<h3>Naslov kućice</h3>
 * 	</div>
 * 	<div> 
 * 		Sadržaj kućice koji se prikazuje ili nestaje
 * 	</div>
 * </div>
 * -----------------------------------------
 * NAPOMENA: u stvarnosti kućicu treba napraviti bez razmaka između
 * tagova da se u DOM ne uvuku prazni TEXT objekti. (ovaj prikaz je
 * bio samo radi lakšeg snalaženja)
 */
function OZ(x) {
	x = x.parentNode.firstChild;
	var oz = x.parentNode.lastChild;
	if( oz.style.display == "none") {
		oz.style.display = "";
		x.src= "stilovi/zatvori.gif";
		x.title= "Zatvori";
	} else {
		oz.style.display = "none";
		x.src = "stilovi/otvori.gif";
		x.title= "Otvori";
	}
}
/* Otvara/Zatvara dijelove AdminPanela ili drugog sistema formiranog:
 * --------------------------------------------------------
 * <dl>
 * 	<dt><img ... onClick='OZAP(this)' />...</dt>
 * 	<dd>...</dd>
 * 	<dd>...</dd>
 * 	<dd>
 * 		<dl>
 * 			<dt><img ... onClick='OZAP(this)' />...</dt>
 * 			<dd>...</dd>
 * 		</dl>
 * 	</dd>
 * </dl>
 * --------------------------------------------------------
 * NAPOMENA: u stvarnosti treba napraviti bez razmaka između
 * tagova da se u DOM ne uvuku prazni TEXT objekti. (ovaj prikaz je
 * bio samo radi lakšeg snalaženja)
 */
function OZAP(x) {
	var oz = x.parentNode.parentNode.lastChild;
	if( oz.style.display == "none") {
		oz.style.display = "";
		x.src= "stilovi/zatvori.gif";
		x.title= "Zatvori";
	} else {
		oz.style.display = "none";
		x.src = "stilovi/otvori.gif";
		x.title= "Otvori";
	}
}
function Otvori( x ) {
	ul = x.parentNode;
	for( i=0; i<ul.childNodes.length; i++ ) {
		ul.childNodes[i].style.borderBottomStyle="solid";
	}
	x.style.borderBottomStyle="none";
}

function PokaziPrijavu( adresa ) {
	gdje = arguments.length == 2 ? arguments[1] : "pomocniStupac";
	var de = document.getElementById( gdje );

	if( document.getElementById( "Prijava" ) == null ) {

		var d = document.createElement( "DIV" );
		d.className = "plavi neprint";
		d.id = "Prijava";

		var u = document.createElement( "DIV" );
		u.className = "u";

		var i = document.createElement( "IMG" );
		i.className = "gumbic";
		i.title = "Zatvori";
		i.alt = "Zatvori";
		i.src = "stilovi/x.gif";
		i.onclick = function() {
			de.removeChild( de.childNodes[0] );
		}
		u.appendChild( i );

		var h = document.createElement( "H5" );
		var t = document.createTextNode( "PRIJAVITE SE" );
		h.appendChild( t );
		u.appendChild( h );
		d.appendChild( u );

		var f = document.createElement( "FORM" );
		f.className = "uu";
		f.id = "prijava2";
		f.method = "post";
		f.action = adresa + "?login";

		var l = document.createElement( "LABEL" );
		var b = document.createElement( "B" );
		t = document.createTextNode( "Ime: " );
		b.appendChild( t );
		l.appendChild( b );
		i = document.createElement( "INPUT" );
		i.alt = "Ulazno ime";
		i.name = "ulime";
		l.appendChild( i );
		f.appendChild( l );

		l = document.createElement( "LABEL" );
		b = document.createElement( "B" );
		t = document.createTextNode( "Kod: " );
		b.appendChild( t );
		l.appendChild( b );
		i = document.createElement( "INPUT" );
		i.alt = "ulazni kod";
		i.type = "password";
		i.name = "ulkod";
		l.appendChild( i );
		f.appendChild( l );

		l = document.createElement( "P" );
		l.className = "link";
		t = document.createTextNode( "Zaboravili ste podatke?" );
		l.appendChild( t );
		l.onclick = function() {
			PokaziZaborav();
		}
		f.appendChild( l );

		i = document.createElement( "INPUT" );
		i.type = "button";
		i.value = "Odustani";
		i.onclick = function() {
			de.removeChild( de.childNodes[0] );
		}
		f.appendChild( i );

		i = document.createElement( "INPUT" );
		i.type = "submit";
		i.value = "Prijavi >";
		f.appendChild( i );


		d.appendChild( f );
		de.insertBefore( d, de.firstChild );
	}
}

function PokaziZaborav( ) {
	var de = document.getElementById( "pomocniStupac" );

	var d = document.createElement( "DIV" );
	d.className = "plavi neprint";
	d.id = "Zaborav";

	var u = document.createElement( "DIV" );
	u.className = "u";

	var h = document.createElement( "H5" );
	var t = document.createTextNode( "Zaboravljeni podaci" );
	h.appendChild( t );
	u.appendChild( h );
	d.appendChild( u );

	var f = document.createElement( "FORM" );
	f.className = "uu";
	f.id = "zaborav";
	f.method = "post";

	var l = document.createElement( "LABEL" );
	var b = document.createElement( "B" );
	t = document.createTextNode( "E-mail adresa: " );
	b.appendChild( t );
	l.appendChild( b );
	i = document.createElement( "INPUT" );
	i.alt = "E-mail adresa";
	i.name = "email";
	l.appendChild( i );
	f.appendChild( l );

	l = document.createElement( "P" );
	t = document.createTextNode( "Ulazni podaci će biti poslani na upisanu adresu samo ako je registrirana." );
	l.style.font = "11px italic";
	l.appendChild( t );
	f.appendChild( l );

	i = document.createElement( "INPUT" );
	i.type = "button";
	i.value = "Zatraži >";
	f.appendChild( i );
	i.onclick = function() {
		PosaljiPodatke( this.form.email.value );
	}

	d.appendChild( f );
	de.insertBefore( d, de.firstChild );
}

function PosaljiPodatke( email ) {
	de = document.getElementById( "pomocniStupac" );
	if( http ) {
		http.open( "GET", "PosaljiPodatke.php?email="+email );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				alert( http.responseText );
				if( http.responseText.indexOf( "Greška" ) == -1 ) {
				de.removeChild( de.childNodes[0] );
				}
			}
		}
		http.send( null );
	} else {
		alert( "Ne mogu izvršiti zadatak bez Ajaxa" );
	}
}

function onLoad() {
	// Zamjeni link za prijavu
	var p = document.getElementById( "PokaziPrijavu" );
	if( p == null ) {
	} else {
		p.href = "javascript: void(0)";
	}
}
