function displayBlock(name, cnt, id) {
    for(i=1; i<=cnt; i++) {
       document.getElementById(name+i).className='block hide';
       if(i==id) {
          document.getElementById(name+i).className='block';
       }
    }
}

function displaySubMenu(name, cnt, id, styl) {
    for(i=1; i<=cnt; i++) {
       document.getElementById(name+i).className='subCategories hide';
       if(i==id) {
          document.getElementById(name+i).className='subCategories '+styl;
       }
    }
}
function activeMenu(name, cnt, id, styl) {
   for(i=1; i<=cnt; i++) {
      document.getElementById(name+i).className='';
      if(i==1) {
         document.getElementById(name+i).className='first';
      }
	  if(i==cnt) {
         document.getElementById(name+i).className='last';
      }
      if(i==id && i==cnt) {
         document.getElementById(name+i).className='last ' + styl;
      }
	  else if(i==id) {
         document.getElementById(name+i).className=styl;
      }	  
   }
}

function hideMenu(name) {
	styl = document.getElementById(name).className;
	if ( styl == 'hide' ) {
    	document.getElementById(name).className='';
	} else {
		document.getElementById(name).className='hide';
	}
}
function zwin(id){
document.getElementById(id).className=((document.getElementById(id).className=='zwiniete')?(''):('zwiniete'));
}

function active(id, styl) {
    document.getElementById(id).className=styl;
}
function display(section, styl) {
    document.getElementById(section).className=styl;
}

function ankK(x) {
	switch(x) {
		case '1': return 'Musisz być zalogowany, aby oddać głos!'; break;
		case '2': return 'Zapisano głos!'; break;
		case '3': return 'Już oddano głos w tej sondzie!'; break;
		case '4': return 'Sonda nie jest aktywna!'; break;
		case '5': return 'Nieprawidłowe dane głosowania.'; break;
	}
}

function glosuj(sonda,glos) {
	advAJAX.get({
		url: "./ajax.php",
		a: "vote",
		sonda: sonda,
		glos: glos,
		onInitialization : function() { document.getElementById('votingBox').innerHTML = "Zapisywanie odpowiedzi..."; },
		onSuccess : function(obj) {
			var spl = obj.responseText.split("|");
			document.getElementById('votingBox').innerHTML = ankK(spl[1]);
			if(spl[0] == 1) {
				upRes(sonda);
				displayBlock('ank', 2, 2);
			}
		},
		onError : function(obj) { document.getElementById('votingBox').innerHTML = "Bł?d: " + obj.status; }
	});
}

function upRes(sonda) {
	advAJAX.get({
		url: "./ajax.php",
		a: "res",
		sonda: sonda,
		onSuccess : function(obj) {	document.getElementById('ankWyniki').innerHTML = obj.responseText; },
		onError : function(obj) { document.getElementById('ankWyniki').innerHTML += "<br />Bł?d: " + obj.status; }
	});
}

function getRadioVal(el) {
	for (var i=0; i < el.length; i++) {
		if(el[i].checked) {
			return el[i].value;
		}
	}
}

function img_fs(id) {
    var imgWin = window.open('http://www.valhalla.pl/img.php?id='+id, 'imgWin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=850,height=670');
    if (!imgWin.opener) {
		imgWin.opener = self;
	}
	imgWin.focus();
}

function sondaWyniki(id) {
    var sondaWin = window.open('/sonda.php?id='+id, 'sondaWin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=300,height=470');
    if (!sondaWin.opener) {
		sondaWin.opener = self;
	}
	sondaWin.focus();
}

function komentarz(tid,topic) {
    var komWin = window.open('http://www.valhalla.pl/post.php?tid='+tid+'&temat='+topic, 'komWin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=500,height=370');
    if (!komWin.opener) {
		komWin.opener = self;
	}
	komWin.focus();
}

function doDni(obj,dni) {
	obj.length = 0;
	var i = 0;
	for(i=0;i<dni;i++) {
		obj.options[i] = new Option(i+1,i+1);
	}
	obj.options[0].selected = true;
}

function dni(miesiac,rok) {
	rok = rok.value;
	if((rok%4==0 && rok%100!=0) || rok%400==0) {
		var miesiace = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	} else {
		var miesiace = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	}
	return miesiace[miesiac];
}

function votK(x) {
	switch(x) {
		case '1': return 'Musisz być zalogowany, aby oddać głos!'; break;
		case '2': return 'Zapisano głos!'; break;
		case '3': return 'Już oddano głos na tę grę!'; break;
		case '4': return 'Nieprawidłowe dane!'; break;
	}
}


function ocengre(gra,glos) {
	advAJAX.get({
		url: "./ajax.php",
		a: "votegra",
		gra: gra,
		glos: glos,
		onInitialization : function() { document.getElementById('gamevoting').innerHTML = "Zapisywanie głosu..."; },
		onSuccess : function(obj) {
			var spl = obj.responseText.split("|");
			document.getElementById('gamevoting').innerHTML = votK(spl[1]);
		},
		onError : function(obj) { document.getElementById('gamevoting').innerHTML = "Bł?d: " + obj.status; }
	});
}

function popitup(url)
{
	newwindow=window.open(url,'name','height=650,width=600,scrollbars=no');
	if (window.focus) {newwindow.focus()}
	return false;
}

function popNews(url)
{
	newwindow=window.open(url,'news','width=547,scrollbars=yes');
}

function zwijacz(el,obrazek,img1,img2) {
	if(document.getElementById(el).style.display == 'none') {
		document.getElementById(el).style.display = 'block';
		document.getElementById(obrazek).src = img1;
		setCookie(el+'Display','block',365);
	} else {
		document.getElementById(el).style.display = 'none';
		document.getElementById(obrazek).src = img2;
		setCookie(el+'Display','none',365);
	}
}

function hajd(el) {
	if(document.getElementById(el).style.display == 'none') {
		document.getElementById(el).style.display = 'block';
	} else {
		document.getElementById(el).style.display = 'none';
	}
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function showJumpForm( objectId )
{
	if(document.getElementById(objectId).style.display=="block")
	{
		document.getElementById(objectId).style.display="none";
	}
	else
	{
		var d = new Date();
		var t = d.getTime().toString();
		document.getElementById(objectId).style.display="block";
		document.getElementById(objectId).innerHTML="<p class=\"go\">idź do strony</p><form action=\"#\" method=\"get\" onsubmit=\"goToPage(this.page.value); return false;\"><div><input type=\"text\" size=\"5\" name=\"page\" class=\"page\" /><input type=\"image\" src=\"i/butIdz.gif\" class=\"submit\" /></div></form>";
	}
	return false;
}




function showBigImg(id) {
	for (i=0;i<5;i++) {
		if (i == id) {
			document.getElementById('img_big_'+i).style.visibility = 'visible';
		} else {
			document.getElementById('img_big_'+i).style.visibility = 'hidden';
		}
	}
}


function minisonda_vote(sonda,odp) {
	advAJAX.get({
		url: "./ajax.php",
		a: "minisonda_vote",
		sonda: sonda,
		odp: odp,
		onInitialization : function() {  },
		onSuccess : function(obj) {
			var spl = obj.responseText.split("|");
			document.getElementById('minisonda_info').innerHTML = ankK(spl[1]);
			if(spl[0] == 1) {
				advAJAX.get({
					url: "./ajax.php",
					a: "minisonda_res",
					sonda: sonda,
					onSuccess : function(obj) {	document.getElementById('minisonda_results').innerHTML = obj.responseText; },
					onError : function(obj) { document.getElementById('minisonda_info').innerHTML += "<br />Bł?d: " + obj.status; }
				});
				var votecount = parseInt(document.getElementById('minisonda_votecount').innerHTML)+1;
				document.getElementById('minisonda_votecount').innerHTML = votecount;
				document.getElementById('minisonda_vote').style.display = "none";
				document.getElementById('minisonda_results').style.display = "block";
			}
		},
		onError : function(obj) { document.getElementById('minisonda_info').innerHTML = "Bł?d: " + obj.status; }
	});
	
}

function quotePost(pid) {
	if(document.forms['commentForm'].tresc.value=='Wpisz swój komentarz...') document.forms['commentForm'].tresc.value = '';
	advAJAX.get({
		url: "./ajax.php",
		a: "getquote",
		pid: pid,
		onSuccess : function(obj) {
			//document.forms['commentForm'].tresc.value += '[cytat]'+obj.responseText+'[/cytat]';
			document.forms['commentForm'].tresc.value += obj.responseText;
			document.forms['commentForm'].tresc.focus();
		}
	});
}

function recommendTopic(tid) {
	advAJAX.get({
		url: "./ajax.php",
		a: "recommendTopic",
		tid: tid,
		onSuccess : function(obj) {
			if(obj.responseText == "1") {
				document.getElementById('recommendBox').innerHTML = parseInt(document.getElementById('recommendBox').innerHTML)+1;
			}
		}
	});
}

function $(id) {
	return document.getElementById(id);
}

function sortPosts(tid,po) {
	advAJAX.get({
		url: "./ajax.php",
		a: "sortPosts",
		tid: tid,
		po: po,
		onInitialization : function() {
			document.getElementById('forumPosts').style.display = "none";
			document.getElementById('forumPostsLoading').style.display = "block";
			
		},
		onSuccess : function(obj) {
			document.getElementById('forumPosts').innerHTML = obj.responseText;
			document.getElementById('sortBtn1').className = '';
			document.getElementById('sortBtn2').className = '';
			document.getElementById('sortBtn3').className = '';
			document.getElementById('sortBtn4').className = 'last';
			switch(po) {
				case 1: document.getElementById('sortBtn1').className = 'active'; break;
				case 2: document.getElementById('sortBtn2').className = 'active'; break;
				case 3: document.getElementById('sortBtn3').className = 'active'; break;
				case 4: document.getElementById('sortBtn4').className = 'active last'; break;
			}
			document.getElementById('forumPostsLoading').style.display = "none";
			document.getElementById('forumPosts').style.display = "block";
		}
	});
}

function recommendPost(pid,value) {
	if(value == true) {
		var plusminus = 1;
		advAJAX.get({
			url: "./ajax.php",
			a: "recommendPost",
			pid: pid,
			plusminus: plusminus,
			onSuccess : function(obj) {
				if(obj.responseText == "1") {
					if(plusminus == 0) {
						document.getElementById('recommendBoxPid'+pid).innerHTML = parseInt(document.getElementById('recommendBoxPid'+pid).innerHTML)-1;
					} else {
						document.getElementById('recommendBoxPid'+pid).innerHTML = parseInt(document.getElementById('recommendBoxPid'+pid).innerHTML)+1;
					}
					if(parseInt(document.getElementById('recommendBoxPid'+pid).innerHTML)>0) {
						document.getElementById('recommendBoxPid'+pid).innerHTML = "+"+document.getElementById('recommendBoxPid'+pid).innerHTML;
					}
				}
			}
		});
	} else {
		dissuadePostForm(pid);
	}
}

function dissuadePostForm(pid) {
	var formtext = '<br /><br /><input type="hidden" id="save_post_content" name="save_post_content" value="" /><b>Podaj powód, dla którego negatywnie oceniasz ten komentarz:</b><br /><input type="text" size="45" id="dissuadeReason' + pid + '" /> <input type="button" value="Zapisz" onclick="dissuadePost(' + pid + ');" /> <input type="button" value="Anuluj" onclick="unDel(' + pid + ');" />';
	var posttext = document.getElementById('post_tresc_'+pid).innerHTML;
	document.getElementById('post_tresc_'+pid).innerHTML += formtext;
	document.getElementById('save_post_content').value = posttext;
}

function dissuadePost(pid) {
	if(document.getElementById('dissuadeReason'+pid).value.length == 0 || document.getElementById('dissuadeReason'+pid).value == null) {
		alert('Nie podano powodu!');
	} else {
		advAJAX.get({
			url: "./ajax.php",
			a: "dissuadePost",
			pid: pid,
			reason: document.getElementById('dissuadeReason'+pid).value,
			onSuccess : function(obj) {
				if(obj.responseText == "1") {
					document.getElementById('recommendBoxPid'+pid).innerHTML = parseInt(document.getElementById('recommendBoxPid'+pid).innerHTML)-1;
					if(parseInt(document.getElementById('recommendBoxPid'+pid).innerHTML)>0) {
						document.getElementById('recommendBoxPid'+pid).innerHTML = "+"+document.getElementById('recommendBoxPid'+pid).innerHTML;
					}
				}
			}
		});
		unDel(pid);
	}
}


function storeCaret(text) {
	if (typeof(text.createTextRange)!="undefined") text.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret(ta,txt) {
	if(ta.value == 'Wpisz swój komentarz...') {
		ta.value = '';
	}
	ta.value += txt;
	ta.focus();
}

function bbAtCaret(textarea,kod) {
	var text1 = '['+kod+']';
	var text2 = '[/'+kod+']';
	if(kod == "url") {
		var url = prompt("Podaj adres URL:","http://");
		text1 = "[url="+url+"]";
	}
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	} else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	} else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}
function V18() {
    var vWin = window.open('v18.html', 'vWin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=880,height=700');
    if (!vWin.opener) {
		vWin.opener = self;
	}
	vWin.focus();
}

function editForm(pid) {
	var formtext = '<input type="hidden" id="save_post_content" name="save_post_content" value="" /><textarea style="width: 420px; height: 160px;" id="post_edit_field"></textarea><br /><input type="button" value="zapisz zmiany" onclick="editPost(' + pid + ');" /> <input type="button" value="anuluj" onclick="unDel(' + pid + ');" />';
	var posttext = document.getElementById('post_tresc_'+pid).innerHTML;
	document.getElementById('post_tresc_'+pid).innerHTML = formtext;
	document.getElementById('save_post_content').value = posttext;
	document.getElementById('post_edit_field').focus();
	document.getElementById('post_edit_field').value += "Pobieranie danych...";
	advAJAX.get({
		url: "./ajax.php",
		a: "getkom",
		pid: pid,
		onSuccess : function(obj) {
			document.getElementById('post_edit_field').value = "";
			document.getElementById('post_edit_field').value += obj.responseText;
		}
	});
}

function unDel(pid) {
	document.getElementById('post_tresc_'+pid).innerHTML = document.getElementById('save_post_content').value;
}

function editPost(pid) {
	advAJAX.post({
		url: "./ajax.php?a=editkom",
		pid: pid,
		tresc: document.getElementById('post_edit_field').value,
		onInitialization : function() { document.getElementById('post_tresc_'+pid).innerHTML = "Zapisywanie..."; },
		onSuccess : function(obj) {
			document.getElementById('post_tresc_'+pid).innerHTML = obj.responseText;
		},
		onError : function(obj) { document.getElementById('post_tresc_'+pid).innerHTML = "Błąd: "+obj.status; }
	});
}
function getLogins(pattern,boxId,fieldId) {
	var box = document.getElementById(boxId);
	if(pattern.length<4) {
		box.style.display="none";
		return false;
	}

	box.style.display="block";
	advAJAX.get({
		url: "./ajax.php",
		a: "getLogins",
		query: pattern,
		onSuccess : function(obj) {
			var x = obj.responseText.split(',');
			if(x[0]=='') x='';
			box.innerHTML = "";
			var len=x.length;
			if(len!=0) {
				if(len>25) len=25;
				for(var i=0;i<len;i++) {
					box.innerHTML += '<a href="#" onclick="loginsUpdate(\''+x[i]+'\',\''+boxId+'\',\''+fieldId+'\');return false;"><p>'+x[i]+'</p></a>';
				}
			} else {
				box.style.display="none";
			}
		}
	});
}


function loginsUpdate(txt,boxId,fieldId) {
	document.getElementById(fieldId).value='';
	document.getElementById(fieldId).value=txt;
	document.getElementById(boxId).style.display='none';
	document.getElementById(fieldId).focus();
}

function getRecommendedTopics(d) {
	advAJAX.get({
		url: "./ajax.php",
		a: "recommendedTopics",
		d: d,
		onInitialization : function() {
			//document.getElementById('recommendedTopicsList').style.display = "none";
			document.getElementById('topicsLoading').style.display = "block";
			
		},
		onSuccess : function(obj) {
			document.getElementById('recommendedTopicsList').innerHTML = obj.responseText;
			document.getElementById('sortBtn1').className = '';
			document.getElementById('sortBtn7').className = '';
			document.getElementById('sortBtn30').className = '';
			document.getElementById('sortBtn365').className = '';
			document.getElementById('sortBtn'+d).className = 'active';
			document.getElementById('topicsLoading').style.display = "none";
			//document.getElementById('recommendedTopicsList').style.display = "block";
		}
	});
}

jQuery.noConflict();
jQuery(document).ready(function ($) {
	$('div.gamesList').each(function () {
		var e = $(this);
		e.find('.more a').mouseover(function () {
			e.find('ul').not(":first").show()
			e.find('.down').text('Zwiń');
			e.find('div.list').bind("mouseleave", function () {
				e.find('ul').not(":first").hide();
				e.find('.down').text('Rozwiń');
			})
		});
	});
	$("#popup").hide().find("div.close a").click(function () { $("#popup").hide(); return false; });
	$("a.tellAFriend").click(function() { tellAFriend(); return false; });
	$("#popup div.error").hide();
	$("#tellAFriendSend").click(function() {
		$("#popup div.error").hide();
		$.ajax({
			type: "POST",
			url: "ajax.php?a=tellAFriend",
			data: ({	tellFrom: $("#tellFrom").val(),
						tellTo: $("#tellTo").val(),
						tellLink: $("#tellLink").val(),
						tellText: $("#tellText").val()
						}),
			dataType: "html",
			success: function(msg) {
				if(msg == "error") {
					$("#popup div.error").show();
				} else {
					$("#popup").fadeOut();
				}
			}
		});
		return false;
	});
	
});
function tellAFriend() {
	var $ = jQuery
	if ($('#popup').is(":visible")) return

	var top = (function centerVert(h) {
		var wh = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight)
		var p = (wh - h < 0) ? 0 : wh - h;
		var s = (window.scrollY ? window.scrollY : document.documentElement.scrollTop)
		return (s + (p / 2));
	})(300);

	$('#popup').css({ top: top }).show()
}