/*
function $(d){
	return document.getElementById(d);
}
*/

function W_CzytajDiv(obj){
	var div=document.getElementById(obj);
	if(div){
		var tresc=div.innerHTML;
		if(tresc)
			return tresc;
	}
}

function W_PiszDiv(obj, tresc){
	var div=document.getElementById(obj);
	div.innerHTML=tresc;
}
//Parametry 
//adres - adres serwra sond,
//idw - węzeł, 
//div - div do którego wczytane będą wyniki, 
//szer_tab - ustawai szerokosc tabel,
//wys_pkt - wysokosc w px kropki
//p - formularz z pytaniem,
//s - id sondy
//rodzaj_wyn - rodzaj wysw wynikow p: procenty
//sonda - w: pokazuje wynik, f: pokazuje formularz

function W_PobierzSonde(idw, div, szer_tab, wys_pkt,rodz_wyn, sonda, wyn_text, glos_text, sonda_text, ankieta_text, p, s){
	var pyt=0;
	if(p!=undefined){
		var form_sonda=document.getElementById(p);
		for(i=0; i<form_sonda.glos.length; ++i){
			if(form_sonda.glos[i].checked){
				pyt=i+1;
			}
		}
	}	
	var obraz=new Image();
	obraz.src="/szablon/sonda_loader.gif";
	var wpis="<img id='w_img_sonda_load' src='"+obraz.src+"' align='middle' alt='' />";
	W_PiszDiv(div, wpis);
	var oXmlHttp=zXmlHttp.createRequest();
	oXmlHttp.open("get","/sondy/sondy_serwer.php?idw="+idw+"&p="+pyt+"&s="+s, true);	
	oXmlHttp.onreadystatechange=function(){
		if(oXmlHttp.readyState==4){
			if(oXmlHttp.status==200){
				var odpowiedz=oXmlHttp.responseText.split("||");
				var idx_obr;
				var glos;
				var liczw=0;
				var liczp=0;
				var nazwa="";
				var opis="";
				var ids="";
				var wyniki=Array();
				var pytania=Array();
				for(x in odpowiedz){
					if(x<=3){
						if(x==0)
							nazwa=odpowiedz[x];
						else if(x==1)
							opis=odpowiedz[x];
						else if(x==2)
							ids=odpowiedz[x];
						else
							glos=odpowiedz[x];							
					}
					else{
						if(x%2==0){
							pytania[liczw]=odpowiedz[x];
							++liczp;
						}
						else{
							wyniki[liczw]=odpowiedz[x];
							++liczw;
						}
					}
				}
				var i=0;
				var zaw="<h4>"+ankieta_text+"</h4><div class='sonda1'>";
				if(glos==1 || sonda=="w"){
					zaw+="<h5>"+nazwa+"</h5><table width='"+szer_tab+"'>";					
					var suma=0;
					var max=0;
					for(i=0; i<wyniki.length; ++i){
						suma+=parseInt(wyniki[i]);
						if(parseInt(wyniki[i])>max)
							max=parseInt(wyniki[i]);
					}
					var szer=0;
					for(i=0; i<wyniki.length; ++i){
						szer=Math.floor((wyniki[i]/max)*100);	
						szer=Math.floor(szer*szer_tab/100);
						if(rodz_wyn=="p"){
							ilosc=Math.floor(wyniki[i]/suma*100);
							ilosc+="%";
						}
						else{
							ilosc=wyniki[i];
						}
						zaw+="<tr><td align='left'>"+pytania[i]+" ("+ilosc+")</td></tr>";
						zaw+="<tr><td align='left'><img src='/szablon/point.gif' width='"+szer+"' height='"+wys_pkt+"'></td></tr>";
						zaw+="<tr><td align='left'> </td></tr>";
					}
					if(glos!=1){
						zaw+="<tr><td align='left'><span class='wiecej_sonda'><a href=\"javascript:W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"', 'f', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"')\">"+sonda_text+"</a></td></tr>";
					}
					zaw+="</table>";
				}
				else{
					zaw+="<h5>"+nazwa+"</h5><form id='sonda_form_"+ids+"'><table width='"+szer_tab+"'>";
					for(i=0; i<pytania.length; ++i){
						var nrp=i+1;
						zaw+="<tr><td align='left' valign='top'><input type='radio' name='glos' value='"+nrp+"'></td><td>"+pytania[i]+"</td></tr>";
					}
					zaw+="<tr><td align='right' colspan='2'><input type='button' value='"+glos_text+"' onclick=\"W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"','"+sonda+"', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"' ,'sonda_form_"+ids+"',"+ids+");\"></td></tr>";
					zaw+="<tr><td align='left' colspan='2'><span class='wiecej_sonda'><a href=\"javascript:W_PobierzSonde("+idw+",'"+div+"',"+szer_tab+","+wys_pkt+",'"+rodz_wyn+"','w', '"+wyn_text+"', '"+glos_text+"', '"+sonda_text+"', '"+ankieta_text+"')\">"+wyn_text+"</a></td></tr>";
					zaw+="</table></form>";
				}
				zaw+="</div>";
				if(ids>0)
					W_PiszDiv(div, zaw);
				else
					W_PiszDiv(div, "");
			}
			else{
			}
		}
	}
	oXmlHttp.send(null);
	
}



function W_ColapseObiekty(obj, rodzaj){
	var d=document.getElementById(obj);
	l=d.getElementsByTagName('div');
	wc_cont=[];
	wc_head=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){
			wc_cont.push(h);
		}
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			wc_head.push(h);
		}
	}	
	if(rodzaj=="h")
		return wc_head;
	else
		return wc_cont;
}


function W_Colapse(obj, id){
	if(id==null)
		id=0;
	var d=document.getElementById(obj);
	l=d.getElementsByTagName('div');
	//l=$(obj).getElementsByTagName('div');	
	wc_cont=[];
	wc_head=[];
	for(i=0;i<l.length;i++){
		h=l[i].id;
		if(h.substr(h.indexOf('-')+1,h.length)=='content'){
			wc_cont.push(h);
		}
		if(h.substr(h.indexOf('-')+1,h.length)=='header'){
			wc_head.push(h);
		}
	}
	
	for(j=0;j<wc_cont.length;j++){
		if(j==id){
			var el = document.getElementById(wc_cont[j]);
			el.style.display = '';
		}
		else{
			var el1 = document.getElementById(wc_cont[j]);
			el1.style.display = 'none';
		}
	}
	
	//zdarzenie onclick
	for(i=0;i<wc_head.length;i++){
		h=document.getElementById(wc_head[i]);
		h.onclick = function(){
			var wct_head=W_ColapseObiekty(this.parentNode.id, "h");
			var wct_cont=W_ColapseObiekty(this.parentNode.id, "c");
			var j=0;
			for(j=0;j<wct_cont.length;j++){
				if(this.id==wct_head[j]){
					var el = document.getElementById(wct_cont[j]);
					if ( el.style.display != "none" ) {
						var idx=j+1;
						var adr=W_CzytajDiv("dadr-"+idx);
						if(adr){
							location.href=adr;
						}
						else{
							el.style.display = 'none';
						}
					}
					else {
						el.style.display = '';
					}
				}
				else{
					var el1 = document.getElementById(wct_cont[j]);
					el1.style.display = 'none';
				}
			}
		}
	}
/*
	var el = document.getElementById(obj);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
*/
}



var odpowiedz;
function W_Ajax_ShowPicture(id, id_obr, rozmiar_obrazu, adres){
	var width=800;
	var height=600;
	idobr=id_obr;
	NewWindow1=window.open(adres+'/galeria_ajax/galeria_mini_ajax.php?id_f='+id+'&id_obr='+id_obr+'&rozm='+rozmiar_obrazu, '','width='+width+',height='+height+',toolbar=no,directories=no,menubar=no,locations=no,status=no,scrollbars=yes,resizable=yes,fullscreen=no');
	NewWindow1.focus();
}

function W_Ajax_PobierzNumeryObrazow(id, id_obr, rozmiar_obrazu, adres){
	var oXmlHttp=zXmlHttp.createRequest();
	oXmlHttp.open("get",adres+"/galeria_ajax/galeria_mini.php?id_f="+id+"&rozm="+rozmiar_obrazu, true);
	oXmlHttp.onreadystatechange=function(){
		if(oXmlHttp.readyState==4){
			//var dane=oXmlHttp.responseText;
			if(oXmlHttp.status==200){
				odpowiedz=oXmlHttp.responseText.split("||");
				var idx_obr;
				for(x in odpowiedz){
					var p=odpowiedz[x].split("_");
					if(p[0]==id_obr)
						idx_obr=x;
				}
				W_Ajax_ZmienObraz(idx_obr);
			}
			else{
			}
		}
	}
	oXmlHttp.send(null);
}

function W_Ajax_ZmienRozmiarOkna(){
	var img_ax=document.getElementById("img_cms_ga");
	var width=img_ax.width+40;
	var height=img_ax.height+140;
	window.resizeTo(width,height);
}

function W_Ajax_ZmienObraz(index){
	var licznik=0;
	var element=odpowiedz[index];
	var ilosc=odpowiedz.length;
	var new_div_txt="";
	for(i in odpowiedz){
		++licznik;
		if(i!=index){
			new_div_txt=new_div_txt+"<a href='javascript:W_Ajax_ZmienObraz("+i+");'>"+licznik+"</a>";
		}
		else{
			new_div_txt=new_div_txt+licznik;
		}
		if(licznik<ilosc){
			new_div_txt+=" | ";
		}
	}
	var div_nav=document.getElementById("div_ax_nav1");
	var div_opis=document.getElementById("div_ax_nav2");
	var nav="";
	if(index>0){
		nav="<a href='javascript:W_Ajax_ZmienObraz("+(index-1)+");'> < Poprzednie | </a> ";
	}
	if(index<ilosc-1){
		nav=nav+"<a href='javascript:W_Ajax_ZmienObraz("+(index+1)+");'> | Nastepne > </a> ";
	}
	div_nav.innerHTML=nav;
	div_opis.innerHTML=new_div_txt;
	var img_ax=document.getElementById("img_cms_ga");
	img_ax.src=sciezka_ajax_grm+element;
}









function W_ShowPicture(sciezka, width, height, ramkaw, ramkah, nazwa, klasa_table, klasa_img){
	if(ramkaw!=1){
		width=parseInt(width);
		ramkaw=parseFloat(ramkaw);
		var w=Math.floor(width+width*ramkaw);		
	}
	else{
		var w=Math.floor(width);
	}
	if(ramkah!=1){
		height=parseInt(height);
		ramkah=parseFloat(ramkah);
		var h=Math.floor(height+height*ramkah);
	}
	else{
		var h=Math.floor(height);
	}
	NewWindow=window.open('', '','width='+w+',height='+h+',toolbar=no,directories=no,menubar=no,locations=no,status=no,scrollbars=yes,resizable=yes,fullscreen=no');
	NewWindow.document.open();
	NewWindow.document.writeln("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><title>Powiększenie<\/title><\/head>");
	NewWindow.document.writeln("<body style='background: none !important; margin:0 !important; padding: 0 !important; overflow:hidden !important;'>");
	NewWindow.document.writeln("<div class='"+klasa_table+"'>");
	NewWindow.document.writeln("<a href=\"javascript:window.close();\"><img style='border: none;' class='"+klasa_img+"' src='"+sciezka+"' alt='' width='"+width+"' height='"+height+"' \/><\/a>");
	NewWindow.document.writeln("<\/div>");
	NewWindow.document.writeln("<\/body><\/html>");
	NewWindow.document.close(); 
	NewWindow.focus();
}



//Pokazuje podkategorie dla danej kategorii
function W_PokazPodKategorie(id_kat, id_pkat, kom_wszystko, kom_brak){
	var select_kat=document.getElementById(id_kat);
	var select_pkat=document.getElementById(id_pkat);
	var index_kat=select_kat.selectedIndex;
	var id_akt_kat=select_kat.options[index_kat].value;
	var ilosc_opcji=select_pkat.options.length-1;
	for (var i=ilosc_opcji; i>=0; i--){
		//select_pkat.options[0]=null;
		//opcja=null;
		select_pkat.remove(i);
	}	
	var licznik=0;
	if(id_akt_kat!=0){
		var opcja=new Option(kom_wszystko, 0);
		select_pkat.options[0]=opcja;
		licznik=1;
		for(var i in podkat[id_akt_kat]){
			var tekst=podkat[id_akt_kat][i];
			var opcja=new Option(tekst, i);
			//select_pkat.add(opcja, licznik);
			select_pkat.options[licznik]=opcja;
			++licznik;
		}
	}
	else{
		var opcja=new Option(kom_brak, 0);
		select_pkat.options[0]=opcja;
	}
}


//Uruchamia duży obrazek w trybie modal
function ShowBigPicture(picture_index, tytul, tab_obrazy, tab_width, tab_height){
	wybrany_div='divobraz'+picture_index;
	var wybrany_img='obr_mod'+picture_index;
	document.images[wybrany_img].src=tab_obrazy[picture_index].src;
	//picture=sciezka;
	var w=Math.floor(tab_width[picture_index])+8;
	var h=Math.floor(tab_height[picture_index])+4;
	var parametry="width="+w+"px,height="+h+"px,center=1";
	var obraz_div="obraz_modal"+picture_index;
	PokazModal(obraz_div, 'div', wybrany_div, tytul, parametry, 'divflash', 'recal');
}


function PokazModal(uniqueID, contenttype, contentsource, title, attributes, nazwa_flash, recalonload){	
	//UkryjElement(nazwa_flash);	
	obraz=dhtmlmodal.open(uniqueID, contenttype, contentsource, title, attributes, recalonload)
	obraz.onclose=function(){
		//PokazElement(nazwa_flash);
		
		//dhtmlwindow.cleanup
		//obraz.cleanup()
		//var dht=document.getElementById("dhtmlwindowholder");
		//dht.InnerHTML="<span style='display:none'>.</span></div>";
		return true;
	}
}



//Nie skończony rozmiar
function PokazDuzyObraz(width, height, sciezka){
	picture=sciezka;				
	NewWindow=window.open('', '','width='+width+',height='+height+',toolbar=no,directories=no,menubar=no,locations=no,status=no,scrollbars=yes,resizable=yes,fullscreen=no');
	NewWindow.document.open();
	NewWindow.document.writeln("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><link rel='stylesheet' href='/style.css' type='text/css' /><title>Powiększenie<\/title><\/head>");
	NewWindow.document.writeln("<body>");
	NewWindow.document.writeln("<table class='zwykla'>");
	NewWindow.document.writeln("<tr valign='middle'><td align=center><a href=\"javascript:window.close();\"><img class='img_duze' src="+picture+" border=0 alt='' \/><\/a><\/td><\/tr>");
	NewWindow.document.writeln("<tr><td align=center><a href=\"javascript:window.close();\">zamknij okno<\/a><\/td><\/tr>");
	NewWindow.document.writeln("<\/table>");
	NewWindow.document.writeln("<\/body><\/HTML>");
	NewWindow.document.close(); 
	NewWindow.focus();
	return;		
}

function okno_param(url, name, param){
	window.open(url,name,param)
}

function okno(url, name){
	window.open(url,name,'align=center,toolbar=no,status=no,location=no,directories=no,resizable=no,scrollbars=yes,width=300,height=600,menubar=no')
}

function UkryjElement(element){
	var obiekt=document.getElementById(element);
	obiekt.style.display = 'none';
}

function PokazElement(element){
	var obiekt=document.getElementById(element);
	obiekt.style.display = '';
}

function PokazUkryj(element){
	if(element.style.display == 'none'){
		element.style.display = '';
	}
	else{
		element.style.display = 'none';
	}
}

function UkryjDiv(elementID){
	var obj = document.getElementById(elementID);
        obj.style.visibility = "hidden";
}

function PokazDiv(elementID){
	var obj = document.getElementById(elementID);
        obj.style.visibility = "visible";
}

function PokazUkryjDiv(element){
	if(element.style.visibility == 'hidden')
		element.style.visibility = 'visible';
	else
		element.style.visibility = 'hidden';
/*
	if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
 */
}

