// JavaScript Document

function read_ajax_sub(selektor, pozajax){
//no a nacítat supporting text cez AJAX ....
						$j.ajax({
							async: false,
							url: pozajax,
							type: 'GET',
							dataType: 'html',
							timeout: 1000,
							cache: false,
							error: function(XMLHttpRequest, testStatus, errorThrown){
								var msg = "Sorry ale nastala chyba v AJAX requeste AJAX pre support text: ";
								alert (msg + xhr.status + " " + xhr.statusText);
								}, // koniec error casti
							success: function(text){
								$j(selektor).html(text);
								}, //koniec success casti
							}) //koniec AJAXu
}

$j(function(){

	// funkcia, ktorá sa spustí pokaždé, ked kliknem na niektorý z elementov quicksummary (podmienka trieda = bbq alebo má link "a" s linkom zacínajúcim na hash
	$j('.bbq a[href^=#]').live( 'click', function(e){ //Selects elements of class .bbq and those a elements that have the attribute href with a value beginning with a #.
		var state = {},
      
		id = $j(this).closest( '.bbq' ).attr( 'id' ),		// nacítanie ID atribútu najblicšieho rodicovského elementu s triedou bbq

		url = $j(this).attr( 'href' ).replace( /^#/, '' );	// vytvorenie retazca s cistým url odkazovanej stránky, tak ako je to uvedené v elemente, ktorý spustil funkciu

		state[ id ] = url ;									// premenná pola state, riadok id sa nastaví na text url

		$j.bbq.pushState(state,0);							// ak je to nieco iné, tak to potom iba doplníme nakoniec hash casti (prípad zmeny quickmenu)

		return false;
	});


/* funkcia, ktorá sa spustí pokaždé, ked kliknem na niektorý z elementov lselect - hlavné menu (podmienka trieda = bbq1 alebo má link "a" s linkom zacínajúcim na hash */
	$j('.bbq1 a[href^=#]').live( 'click', function(e){ //Selects elements of class .bbq and those a elements that have the attribute href with a value beginning with a #.
		var state = {},
      
		id = $j(this).closest( '.bbq1' ).attr( 'id' ),		// nacítanie ID atribútu najblicšieho rodicovského elementu s triedou bbq

		url = $j(this).attr( 'href' ).replace( /^#/, '' );	// vytvorenie retazca s cistým url odkazovanej stránky, tak ako je to uvedené v elemente, ktorý spustil funkciu

		state[ id ] = url ;									// premenná pola state, riadok id sa nastaví na text url
		
		// tu treba dorobit ukladanie premennej do konštanty, aby bolo jasné, že sa menilo menu.....
		//$j("#lselect").data('lselect')
		if (url.indexOf("/txt/", 0) >= 0) { // nájde 1 výskyt retazca /txt/ v url. Ak je cast true, ak nie je tak cast else
			$j("#lselect").data('lselect','jeto');	// tu sa nastaví premenná data.lselect = "jeto"
			$j("#lselect").data('noid', id);		// tu sa nastaví premenná data.noid = id, co je vlastne id elementu, ktorý spustil akciu....
			}
			else {										// no a pre prípad, _e akcia bola spustená elementom, ktorý nemal url, ale iba nejaký text tak sa nastaví premenná data nasledovne:
			$j("#lselect").data('lselect','nenito');	// tu sa nastaví premenná data.lselect = "nenito"
			$j("#lselect").data('noid', '');			// tu sa nastaví premenná data.noid = ""
		};

		$j.bbq.pushState(state,0);							// ak je to nieco iné, tak to potom iba doplníme nakoniec hash casti (prípad zmeny quickmenu)

		return false;
	});



/*	toto bude špeciálny prípad pre výber jazyka*/
		$j('.langg').live( 'click', function(e){
			//var url = $(this).attr('href');
			//var coords = $(this).attr('coords').split(',');
			var id = $j(this).attr('id');						// tu dostanem vlastne skratku jazyku...
			var that = $j("#lselect4");							// do that uložíme preferenciu na najblicší kontajner lsel, v ktorom je (pre tento prípad to bude lselect4)
			// Your code here
			// To prevent default action
			// tvar href je: #?dat=main.xml
			
			$j("#lselect").data('lselect','jeto');				// vložíme do premennej data navigacného kontajnera že to je nastavené
			$j(that).data('lang',id);							// do premennej kontajnera výberu jazykov vložíme informáciu o vybranom jazyku
			url = "#lang=" + id;								// vytvorím url retazec, ktorý je tvorený s hash lang = skratka jazyka
			$j.bbq.pushState(url,0);							// nastavím stav
			
			return false;
		});




	$j(window).bind( 'hashchange', function(e) {
/*			var that = $j('#preamble'); // vytvotenie objektu s div 
			var that1 = $j('#explanation'); // vytvorenie objektu s div1
*/			
			urla = $j.bbq.getState( 'lselect' ) || $j('#lselect').find('a').slice(0).attr('href').replace('#','');
			urlb = $j.bbq.getState( 'quickSummary' ) || $j('#quickSummary').find('a').slice(0).attr('href').replace('#','');
			
			alert ("hash change: url a = " + urla);

			jazyk = $j.bbq.getState( 'lang' ) || "sk";
			url = urla + urlb + "&jazyk=" + jazyk;
			
						alert ("hash change: cele url je = " + url);

			vyber = "a[href^=#" + urla + "]";
			ajaxquick="/menu/quickmenu.php?dat=" + $j(vyber).attr("id") + "&jazyk=" + jazyk; // + ' ' + '#navi';

			//ak sa mení hlavné menu, je treba speci ukony.....
			konst = $j("#lselect").data('lselect');
			if (konst=="jeto") {
				//najprv zrušit konst
				$j("#lselect").data('lselect','nenito');
				
				// potom nacítat menu
				$j.ajax({
					async: false,
					url: ajaxquick,
					type: 'GET',
					dataType: 'html',
					timeout: 1000,
					cache: false,
					error: function(XMLHttpRequest, testStatus, errorThrown){
						//alert('Chybicka se vloudila - status: ' + testStatus + ", HTTPReq: " + XMLHttpRequest + ", errthrown: " + errorThrown);
						},
					success: function(qhtml){
							$j("#quickSummary").hide("slow");
							$j("#quickSummary").html(qhtml);
							$j("#quickSummary").show("slow");
						}
				});
			};
			//koniec zmeny quicksummary
			
			// teraz text html stránky.......
			// ajax požiadavku musíme upravit, aby sme do nej dostali aj 1 option z novo vytvoreného quickmenu
			ajreq = url; // + ' ' + '#vrat';
				$j.ajax({
					async: false,
					url: ajreq,
					type: 'GET',
					dataType: 'html',
					timeout: 1000,
					cache: false,
					error: function(XMLHttpRequest, testStatus, errorThrown){
							var msg = "Sorry ale nastala chyba v AJAX requeste AJAX: ";
							alert (msg + xhr.status + " " + xhr.statusText);
						},
					success: function(data){
						//naplnime div preamble .....
						$j( '#preamble' ).html(data);
						
						//precitame kolko ma supplements.....
						h=$j( '#vrat' ).attr('supp');
						//alert ("supporting má pocet textov: " + h);
						//$j('.explan').hide("slow");

						// a nacítat ich
						for (i=1;i<=h;i++) {
							pp = url + '&su=' + i; //+ ' ' + '#vratsup';
							ajreee = pp.replace("index.php","support.php");
							ret = "#explanation"+i;
							read_ajax_sub(ret, ajreee);
							} //koniec for pre i......
						for (j=2;j>h;j--) { //skryjeme nepotrebné div.....
							ret = "#explanation"+j;
							$j(ret).hide("slow");
							} //koniec for pre j......
						for (k=1;k<=h;k++) { //skryjeme nepotrebné div.....
							ret = "#explanation"+k;
							$j(ret).show("slow");
							} //koniec for pre k......
						} // koniec success
				}); //koniec AJAX requestu
			
			//alert("presiel som cely ajax");
	});

  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  
  $j(window).trigger( 'hashchange' );
  
})
