/********************************************************************
 * 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;

/************
 * 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 = ( 27 + 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";
	}
}

function PokaziFormu( id, title, action ) {
	f = document.getElementById( id );
	if( f != null ) return false;

	document.getElementById( "sjena" ).style.display = "block";
	var f = document.createElement( "FORM" );
	f.id = id;
	f.method = "post";
	f.action = action;
	f.className = "formica";

	var i = document.createElement( "IMG" );
	i.className = "gumbic";
	i.title = "Zatvori";
	i.alt = "Zatvori";
	i.src = "slike/x.gif";
	i.onclick = function() {
		de.removeChild( f );
		if( de.childNodes.length == 0 ) {
			document.getElementById( "sjena" ).style.display = "none";
		} else {
			de.lastChild.style.display = "block";
		}
	}
	f.appendChild( i );

	var h = document.createElement( "H3" );
	h.appendChild( document.createTextNode( title ) );
	f.appendChild( h );

	var de = document.getElementById( "nadSjenom" );
	de.insertBefore( f, de.firstChild );

	return f;
}

function PokaziPrijavu( adresa ) {
	var f = PokaziFormu( "prijava", "PRIJAVITE SE", adresa + "?login" );

	var l = document.createElement( "LABEL" );
	l.appendChild( document.createTextNode( "E-mail: " ) );
	i = document.createElement( "INPUT" );
	i.alt = "E-mail";
	i.name = "email";
	l.appendChild( i );
	f.appendChild( l );

	l = document.createElement( "LABEL" );
	t = document.createTextNode( "Kod: " );
	l.appendChild( document.createTextNode( "Lozinka: " ) );
	i = document.createElement( "INPUT" );
	i.alt = "ulazni kod";
	i.type = "password";
	i.name = "ulkod";
	l.appendChild( i );
	f.appendChild( l );

	l = document.createElement( "LABEL" );
	l.className = "link";
	l.appendChild(  document.createTextNode( "Zaboravili ste podatke?" ) );
	l.onclick = function() {
		f.style.display = "none";
		PokaziZaborav();
	}
	f.appendChild( l );

	l = document.createElement( "LABEL" );
	i = document.createElement( "INPUT" );
	i.className = "but";
	i.type = "submit";
	i.value = "Prijavi >";
	l.appendChild( i );
	f.appendChild( l );

}

function PokaziZaborav( ) {
	var f = PokaziFormu( "zaborav", "ZABORAVLJENI PODACI", "javascript:void(0)" );
	if( f == false ) return;

	var l = document.createElement( "LABEL" );
	var b = document.createElement( "B" );
	t = document.createTextNode( "E-mail: " );
	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 );

	l = document.createElement( "LABEL" );
	i = document.createElement( "INPUT" );
	i.type = "button";
	i.value = "Zatraži >";
	i.className = "but";
	i.onclick = function() {
		PosaljiPodatke( this.form.email.value, f );
	}
	l.appendChild( i );
	f.appendChild( l );
}

function Greska( tekst ) {
	var f = PokaziFormu( "greska", "GREŠKA", "javascript:void(0)" );
	if( f== false ) return;
	f.className += " greskica";
	f.appendChild( document.createTextNode( tekst ) );
}

function PosaljiPodatke( email, f ) {
	if( http ) {
		http.open( "GET", "sadrzaj/PosaljiPodatke.php?email="+email );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				alert( http.responseText );
				if( http.responseText.indexOf( "Greška" ) == -1 ) {
					f.parentNode.lastChild.style.display = "block";
					f.parentNode.removeChild( f );
				}
			}
		}
		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)";
	}

}

function SpremiKorisnika( f, tka ) {
	var tk;
	if( tka ) {
		tk = document.getElementById( "trenutniKod" );
	}
	
	var gs = document.getElementById( "gumbSpremi" );
	if( !puno( f.ulime.value ) ) {
		Greska( "Morate upisati ulazno ime i sve podatke!" );
		return;
	}
	if( !puno( f.ime.value ) || !puno( f.prezime.value ) || !jeMail( f.email.value ) ) {
		Greska( "Morate upisati ime, prezime i email" );
		return;
	}

	if( tka ) {
		if( ( puno( f.ulko2.value ) || puno( f.ulko1.value ) ) && f.ulko1.value != f.ulko2.value ) {
			Greska( "Kodove morate upisati identično" );
			return;
		}
		if( tk.style.display == "none" ) {
			f.insertBefore( tk, gs );
			tk.style.display = "block";
			return;
		}
		if( !puno( f.ulkod.value ) ) {
			Greska( "Morate upisati svoju trenutnu lozinku radi provjere" );
			return;
		}
	}
	f.submit();
}

function IdiNaStranicu( id, broj ) {
	var f = document.getElementById( id );
	f.poc.value = broj * f.kol.value/* + 1*/;
	f.submit();
	return false;
}

// Stranice u Leadu
var _stranica = new Array; 

function PrikaziStranicu( adresa, broj ) {
	document.getElementById( "stranica_" + adresa + "-" + _stranica[adresa] ).style.display = "none";
	document.getElementById( "stranica_" + adresa + "-" + broj ).style.display = "block";
	document.getElementById( "link_" + adresa + "-" + _stranica[adresa] ).className = "link";
	document.getElementById( "link_" + adresa + "-" + broj ).className = "bold";
	_stranica[adresa] = broj;
}

function Viewport() { 
	this.windowX = window.innerWidth || (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; 
	this.windowY = window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; 
}

function PokaziSliku( path, slika, slike ) {
	alert( path + "/" + slika );
	slike = slike.split('###');
	alert( slike );
	/**/
	var ivs = document.getElementById( "sjena" );
	var dvs = document.getElementById( "nadSjenom" );
	dvs.lastChild.innerHTML = "Učitava se";
	var vp = new Viewport();
	var img;

	ivs.style.display = "block";
	ivs.style.width = vp.windowX + "px";
	dvs.style.display = "block";
	ivs.style.height = Math.max( vp.windowY, ( document.getElementsByTagName( "BODY" )[0].offsetHeight ) ) + "px";

	if( img === undefined ) {
		img = document.createElement( "IMG" );
	}
	if( dvs.firstChild.tagName == "IMG" ) {
		dvs.removeChild( dvs.firstChild );
	}
	img.src=src;
	dvs.insertBefore( img, dvs.firstChild );
	img.onload = function() {
		dvs.lastChild.innerHTML = "Kliknite da biste zatvorili sliku.";
	}

	dvs.style.top = Math.ceil( ( vp.windowY - h ) / 2 ) - 10 + "px";
	dvs.style.left = Math.ceil( ( vp.windowX - w ) / 2 ) - 10 + "px";
	dvs.style.width= w + "px";
	dvs.style.height = ( h + 15 ) + "px";
	/**/
}

function SakrijSliku( ) {
	var dvs = document.getElementById( "divVelikeSlike" );
	dvs.style.display = 'none';
	dvs.removeChild( dvs.firstChild );
	document.getElementById( "ispodVelikeSlike" ).style.display = 'none';
}

function OdaberiUAnketi( f ) {
	odg = 0;
	if( f.odg[0] && f.odg[0].checked ) odg=1;
	if( f.odg[1] && f.odg[1].checked ) odg=2;
	if( f.odg[2] && f.odg[2].checked ) odg=3;
	if( f.odg[3] && f.odg[3].checked ) odg=4;
	if( f.odg[4] && f.odg[4].checked ) odg=5;
	if( !odg ) return;
	document.cookie = "anketa" + f.anketa.value + "=" + odg;

	if( http ) {
		http.open( "GET", "http://www.kognosko.hr/sadrzaj/anketa.php?akcija=DodajOdgovor&odgovor="  + odg +"&anketa=" + f.anketa.value );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				var rez = http.responseText.split("###");
				alert( rez[0] );
				document.getElementById( "tijeloAnkete" ).innerHTML = rez[1].split('<div id="tijeloAnkete">')[1].split("</div>")[0];
			}
		}
		http.send( null );
	} else {
		alert( "Ne mogu izvršiti zadatak bez Ajaxa" );
	}
}

