	/** ---------------------------------------- *
	 * Start strony - onload document
	 * ----------------------------------------- */
	
	$(document).ready(function() {

			// obsluga zewnatrznych linkow
			$('a.external').click( function(){	window.open(this.href,'_blank'); return false;})
			
			// zlikwidowanie kropek wokol linkow
			$('a, label').bind('focus',function(){if(this.blur)this.blur();});
			
			// zmiana wysokosci tla flasha
			fsetDocumentHeight();
			
			// srodkowanie ulubionych
			setCenterFavourities();
			
			// zamiana liter w submenu  na obrazki
			changeSubmenuLetters('pojSubMenu');
			
			// centrowanie pagowania
			fSetCenterAlign();
			
			// pobiera tla
			tryCache('.pojInvisible', '');		
			
			// rozszerzanie tla do konca w ie6
			expandBg();
			
			// zaladowanie tla dla loadaera axjaxa
			$('body').append('<div id="AjaxCache" class="cache"></div>'); 
	});	
	
	/**
	 * rozszerzanie tla w ie6
	 */
	
	function expandBg() {
		// rozszerzanie tla do konca w ie6
		if ($.browser.msie && (parseInt($.browser.version)<=6)) {
				$('#kbg').css('height', $('#pojFlashTlo').css('height'));
				$('#kbg').attr('height', '100%');
		}
	}

	$(window).resize(function() {
		fsetDocumentHeight();
	});
	
	$('#pojCalosc').resize(function() {
		fsetDocumentHeight();
	});

	/* ---------------------------------------------------------------- */

	/**
	 * semafor dt. udostepniania mozliwosci uruchomienia funkcji
	 */

	var iSemaphore = -2;
	
	/**
	 * czy uzywane
	 */

	var iUsed = 0;
	
	/**
	 * czy strona glowna
	 */
	
	var bMainPage = false;
	
	/**
	 * czy strony konkursowe
	 */
	
	var bContest = false;
	
	/**
	 * @desc skalowanie warstwy z flashem
	 */
	function fsetDocumentHeight(){
		
		
		var iCorrection = ($.browser.msie) ? 1 : 0;
		var iWindowHeight = parseInt($(window).height());
		
		if ($.browser.opera) {
			iWindowHeight = window.innerHeight;
		}
		
		var iHeight = 0;
	
		if ($('#pojCalosc').height() > iWindowHeight) {
				iHeight = parseInt($('#pojCalosc').height());

				
		} else {
				iAllHeight = parseInt($('#pojCalosc').height());
				if ((iWindowHeight - iAllHeight) > 30) 
					iHeight = iWindowHeight - 30;
				else
					iHeight = iWindowHeight - (iHeight - iAllHeight);
		}

		
		
		//if( ($('#pojFlashTlo').height()<=iHeight) )
		iWysokosc = iHeight-iCorrection+30;
		/*if (iWysokosc<900)
			iWysokosc = 900;*/
		if ($('#pojFlashTlo').length > 0) {
			if (bMainPage && iWysokosc<767)
				iWysokosc=767;
			$('#pojFlashTlo').css('height', iWysokosc + 'px');
			if (!bMainPage) setFlashContentHeight();
		}
		
	}
	
	/**
	 * @desc zagadnaie do flasha
	 * 
	 * @param {String} nazwa akcji we flashu
	 * @param {Vars} oVars
	 */
	function fInvokeFlashBroadcaster( oVars ){
			var oFlashDispatcher =  document.getElementById('broadcaster');
			
			/*
			var bRun = true;
			while (bRun) {	// zeby nie generowalo bledow zanim sie zaladuje bradcaster
				try {
					oFlashDispatcher.callFlash(oVars);
					bRun = false;
				} 
				catch (e) {}
			}
			*/
			
			try {
				oFlashDispatcher.callFlash(oVars);
			} 
			catch (e) {
				setTimeout(oFlashDispatcher.callFlash(oVars),1500);
			}
			
	}
	
	/**
	 * @desc funkcja uruchamiana gdy broadcaster jest gotowy
	 */
	function fBroadcasterIsReady(){
		setFlashContentHeight();
	}
	/**
	 * @desc ustawienie wysokosci pojemnika z tlem flashowym
	 */
	function setFlashContentHeight(){
		//console.log(iSemaphore);
			iSemaphore ++;
			if (iSemaphore == 0) {
				if (iUsed == 0) {
					//iUsed = 1;
				}
				else {
					return false;
				}
			} else {
				if (iSemaphore>0)
					iSemaphore --;
				return false;
			}
			if (!bContest) {
				$('#pojContent').css('height', 'auto');
				
				iZawartosc = $('#pojCalosc').height() - $('#pojMenu').height() - $('#pojStopka').outerHeight() - 150; // to 150 to padding-bottom pojContent
				var iHeight = iZawartosc + 164;
				if (iHeight < 686) {
					$('#pojContent').css('height', '522px');
					iHeight = 686;
				}
				if (bMainPage) {
					iHeight += 130;
				}
			}
			
			var oVars = {				
					szMethodName:'setBgHeight',
					oExtVars: iHeight				
			}
			fInvokeFlashBroadcaster( oVars);
			fsetDocumentHeight();
		
	}
	
	function fTalkBottle (sText) {
			var aArray = new Array();
			aArray[0] = new Array(0, sText);
			var oVars = {				
					szMethodName:'showPoints',
					oExtVars: aArray				
			}
			fInvokeFlashBroadcaster( oVars);
	}
	
	function fNavigateToUrlViaFlash(sUrl){
			var oVars = {
					szMethodName:'navigateToUrl',
					oExtVars: sUrl
			}
			fInvokeFlashBroadcaster( oVars);
	}
	
	function fShowLoginFormInFlash(){
			var oVars = {
					szMethodName:'openLogging'
			}
			fInvokeFlashBroadcaster( oVars);
	}
	
	/**
	 * wysrodokuje cyferki stron w ulubionych
	 */	
	function setCenterFavourities() {
		// centrowanie cyferek w ulubionych w stronach
		if ($("#pojFavNumberBar #pojEndDots").length > 0 || $("#pojFavNumberBar #pojStartDots").length > 0) {
			if ($.browser.msie && parseInt($.browser.version)<=6) {		// dla IE6 mamy specjalne metody liczenia szerokosci
				iWidth = 0;
				$("#pojFavNumberBar *").each(function(){
					if ($(this).attr('id')!='' && $(this).css("display")!='none')		// zliczamy tylko widoczne i nazwane przedmioty
						iWidth += $(this).width();
				});
				$("#pojFavNumberBar").css("width", iWidth + 50);
			}
			else {
				$("#pojFavNumberBar").css("width", "auto");
				$("#pojFavNumberBar").css("float", "left");
				$("#pojFavNumberBar").css("width", $("#pojFavNumberBar").width() + 10);
				$("#pojFavNumberBar").css("float", "none");
			}
		}
		
		if ($.browser.msie && parseInt($.browser.version) <= 6) { // ustalanie minimalnej wysokosci dla IE6
			if ($("#pojFavList").height() < parseInt($("#pojFavList").css("min-height"))) 
				$("#pojFavList").height($("#pojFavList").css("min-height"));
				
			if ($("#pojFavTop").height() < parseInt($("#pojFavTop").css("min-height"))) 
				$("#pojFavTop").height($("#pojFavTop").css("min-height"));
		}
	}
	
	/**
	 * zamiana literek na obrazkowe
	 */
	
	function changeSubmenuLetters(sContenerName) {
		aSubMenu = $('#'+sContenerName+' a');
		if (aSubMenu.length == 0) aSubMenu = $('#'+sContenerName+'Width a');
		aSubMenu.each(function() {
			if ($('#'+sContenerName+'').css("color").charAt(0) == '#') { // eliminujemy znaczek # z poczatku ciagu rgb
				sColor = $('#'+sContenerName+'').css("color").substr(1);
				if (sColor.length == 3) {
					sR = parseInt('0x' + sColor.substr(0, 1));
					sG = parseInt('0x' + sColor.substr(1, 1));
					sB = parseInt('0x' + sColor.substr(2, 1));
				} else {
					sR = parseInt('0x' + sColor.substr(0, 2));
					sG = parseInt('0x' + sColor.substr(2, 2));
					sB = parseInt('0x' + sColor.substr(4, 2));
				}
				//console.log("url(submenu,"+$(this).text()+","+sR+","+sG+","+sB+".gif)");
			} else if ($('#'+sContenerName+'').css("color").substr(0,3).toLowerCase() == 'rgb') {		// jezeli jest zapisane w postaci rgb(rrr,ggg,bbb)
				aColor = $('#'+sContenerName+'').css("color").substr(4, $('#'+sContenerName+'').css("color").length-5).split(",");
				sR = parseInt(aColor[0]);
				sG = parseInt(aColor[1]);
				sB = parseInt(aColor[2]);
			}
			else 
				return false;
			iLinesCount = $(this).attr("rel").split("%3B%3B").length;		// ilosc linii
			if ($('#pojRankingsSubMenu').length>0) {	// rankingi maja inny kolor literek
				$(this).css("background-image", "url(submenu-rankings,"+$(this).attr("rel")+","+sR+","+sG+","+sB+".gif)");
			} else {
				$(this).css("background-image", "url(submenu,"+$(this).attr("rel")+","+sR+","+sG+","+sB+".gif)");
			}
			$(this).addClass("txtNW");	
			if (iLinesCount>1)				// dodanie obslugi wielu linii
					$(this).addClass("poj"+iLinesCount+"Lines");
			return true;
		}); 
		
		return aSubMenu.length;
	}
	
	/**
	 * probuje "cachowac" tla na stronie
	 * @param string sJQuery ciag identyfikujacy
	 * @param string sParams parametry
	 */
	
	function tryCache(sJQuery, sParams) {
		//$(sJQuery).addClass('ticketed');
		$(sJQuery).show();
		$(sJQuery).hide();
		
	}
	


	/** ---------------------------------------- *
	 * Operacje na marchewce
	 * ----------------------------------------- */

	
	/**
	 * rozszerza pojemnik Marchewki dla logowania
	 */
	function fSetCarrotsSizeForLogin(){
			$('#pojMarchewka').removeClass();
			$('#pojMarchewka').addClass('LoginBox');
			if ($.browser.msie && parseInt($.browser.version) <= 6) {
				$('#pojMarchewka').css('width', '765px');
				$('#pojMarchewka').css('height', '336px');
				$('#pojMarchewka').css('left', '160px');
			}
	}

	/**
	 * rozszerza pojemnik Marchewki dla logowania w gore
	 */
	function fSetCarrotsSizeForTopLogin(){
			$('#pojMarchewka').removeClass();
			$('#pojMarchewka').addClass('LoginTopBox');
			if ($.browser.msie && parseInt($.browser.version) <= 6) {
				$('#pojMarchewka').css('width', '765px');
				$('#pojMarchewka').css('height', '335px');
				$('#pojMarchewka').css('left', '160px');
				$('#pojMarchewka').css('top', '0px');
			}
	}

	/**
	 * rozszerza pojemnik Marchewki dla komunikatow
	 */
	function fSetCarrotsSizeForInfo(){
			$('#pojMarchewka').removeClass();
			$('#pojMarchewka').addClass('InfoBox');
			if ($.browser.msie && parseInt($.browser.version) <= 6) {
				$('#pojMarchewka').css('width', '375px');
				$('#pojMarchewka').css('height', '336px');
			}
	}

	/**
	 * wraca do normalnej postaci marchewki
	 */
	function fSetCarrotsNormalSize(){
			$('#pojMarchewka').removeClass('InfoBox');
			$('#pojMarchewka').removeClass('LoginBox');
			$('#pojMarchewka').removeClass('LoginTopBox');
			if ($.browser.msie && parseInt($.browser.version) <= 6) {
				$('#pojMarchewka').css('width', '375px');
				$('#pojMarchewka').css('height', '155px');
				$('#pojMarchewka').css('left', '550px');
				$('#pojMarchewka').css('top', '117px');
			}
	}



/** -------------------------------------------
 * Operacje na liscie pagowanej
 * --------------------------------------------*/

	var iNaviWidth = 0;

	function fSetCenterAlign(){
			
			/* mozliwosc centrowania gdy prev i next maja inne niz standard dlugosci */		
			if (iNaviWidth == 0)
				iNaviWidth = 200;
			$('ul.pojNaviList').each( function(){
					$(this).css('padding-left', parseInt( (($(this).parent('div').width() - iNaviWidth)/2) - ( $(this).children('li').size() * 10 ) ));
			})
	}


	/**
	 * @desc pokazanie formularza przypomnienia hasla
	 */
	function fShowRemindLoginForm(){		
			fLoadAjaxContent( 'remind.html', 'pojUserInfoBox');	
	}

	/**
	 * @desc pokazanie formularza logowania
	 */
	function fShowLoginForm(){		
			fLoadAjaxContent( 'login.html', 'pojUserInfoBox');	
	}
	
	/**
	 * @desc wyslanie formularza logowania
	 */
	function fSendLogin(){
			var oVar = {
					oFunction: function(){
							fInvokeFlashBroadcaster({szMethodName: 'userIsLogged', oExtVars: null});
					}
			}; 
			fSendFormAjax( 'formLogowanie', 'pojUserInfoBox', oVar);		
	}
	
	
	/**
	 * @desc wyslanie formularza przypomnienia haslo
	 */
	function fSendLoginRemind(){
			fSendFormAjax( 'formRemind', 'pojUserInfoBox');
	}
	
	/**
	 * @desc funkcja zapamietauje uzytkownika
	 */
	function fSetUserRemember(){
			$.ajax({
					type: 'GET',
					url: 'index.php?sModul=mLogin&sAction=setLoginRemember',
					dataType : 'xml',
					success: function(oXml){}
			});
		
	}
		
	
	
	/**
	 * @desc wyslanie formularza poprzez ajax
	 * @param {string} sFormId - identyfikator formularza
	 * @param {string} sContentId - identyfikator pojemnika do ktorego ladowana ma byc tresc
	 * @param {object} oVars - dodatkowe parametry wowolania
	 */
	function fSendFormAjax(sFormId, sContentId, oVars){
		var sQueryString = $('#' + sFormId).formSerialize();
		
		sUrl = 'index.php';
		
		$('#AjaxWait').show();
		if (oVars) {
			if(oVars.sUrl) 
				sUrl = oVars.sUrl;
		}
			
			$.ajax({
					type: 'POST',
					url: sUrl,
					dataType : 'xml',
					data: sQueryString,
					success: function(oXml){
							//iSemaphore --;
							
							
							var iStatus = $(oXml).find('iStatus').text();
							
							if (iStatus == 1){
									if ( $(oXml).find('Redirect') && $(oXml).find('Redirect').text() != '' ){
											location.href = $(oXml).find('Redirect').text();
									}
									$('#'+sContentId).html($(oXml).find('Content').text());
									if (oVars) {
									  	if (oVars.oFunction)
									  			oVars.oFunction();
								  }
								
							} else {
									
									alert( $(oXml).find('sMessage').text() );
							}
							
							$('#AjaxWait').fadeOut('slow');
							fSifrRender();
							setTimeout("iSemaphore--; setFlashContentHeight(); expandBg();", 1000);
					}
			});

	}	
	
	
	/**
	 * @desc wysylanie formularza
	 * @param {string} sFormId - identyfikator formularza
	 */
	function fSendForm(sFormId){
			document.forms[sFormId].submit();
	}

	
	/**
	 * @desc zaladowanie contantu poprzez ajax
	 * @param {string} sUrl - adres
	 * @param {string} sContentId - identyfikator pojemnika do ktorego ladowana ma byc tresc
	 */
	function fLoadAjaxContent( sUrl, sContentId, oVars){		
			
			$('#AjaxWait').show();
			
			$.ajax({
					type: 'GET',
					url: sUrl,
					dataType : 'xml',
					success: function(oXml){
							$('#'+sContentId).html($(oXml).find('Content').text());
							if (oVars) {
								  	if (oVars.oFunction)
								  			oVars.oFunction();
							}
							$('#AjaxWait').fadeOut('slow');
							fSifrRender();
							fsetDocumentHeight();
							setTimeout("iSemaphore--; setFlashContentHeight(); expandBg();", 1000);
					}
			});
			
	}		
	
	/**
	 * zmienia strone ulubionych
	 * @param {int} iSetPage - strona ktora ma byc wyswietlona 
	 */
	
	function goToPage(iSetPage) {
		$("#pojFav li").hide();
		for (i=(iSetPage-1)*10;i<iSetPage*10;i++) {
			$("#pojFav li").eq(i).show();	// pokazywanie odpowiednich pozycji
		}
		if (iPages > 4) {			// ustalanie wyswietlania odpowiednich stron
			$(".pojFavPages").hide();
			if (iSetPage == 1) {		// pierwsza strona
				for (i = 2; i < 4; i++) 
					$("#pojFavPage" + i).show();
			}
			else {						
				if (iSetPage == iPages) {		// ostatnia strona
					for (i = iPages; i > iPages - 3; i--) 
						$("#pojFavPage" + i).show();
				}
				else {							// ani pierwsza ani ostatnia
					for (i = iSetPage - 1; i <= iSetPage + 1; i++) 
						$("#pojFavPage" + i).show();
				}
				
				if (iSetPage -1 >2)				// wstawianie kropek pomiedzy 1 ... n
					$('#pojStartDots').show();
				else
					$('#pojStartDots').hide();
				
				if (iSetPage +2 < iPages)		// wstawianie kropek na koncu n ... iPages
					$('#pojEndDots').show();
				else
					$('#pojEndDots').hide();
			}
		}
		
		// ustalanie szerokosci do wysrodkowania numerow stron w uulubionych
		setCenterFavourities();
		
		$("#pojFavPage"+iPage).removeClass('pojFavPageSel');
		$("#pojFavPage"+iSetPage).addClass('pojFavPageSel');
		
		$("#pojFavPage"+iSetPage).show();
		iPage = iSetPage;
	}
	
	/**
	 * wyswiela kolejna strone ulubionych
	 */
	
	function goToNextPage() {
		if (iPage != iPages)
			goToPage(iPage+1);
	}
	
	/**
	 * wyswiela poprzednia strone ulubionych
	 */
	
	function goToPrevPage() {
		if (iPage != 1)
			goToPage(iPage-1);
	}
	
	/**
	 * dodaje strone do ulubionych
	 * @param {string} sCode
	 */
	
	function addFav(sCode) {
		document.location = "addfav.html?sCode="+sCode+"&sRedirect="+document.location;
	}
	
	/**
	 * dodaje gre do ulubionych
	 * @param {int} iId
	 */
	
	function addFavGame(iId) {
		document.location = "addfavgame.html?iId="+iId+"&sRedirect="+document.location;
	}
	
	/**
	 * wysyla quiz
	 */	
	function fSendQuiz() {
		if ($('#formQuiz').length>0) {
			$("#sRedirect").attr('value',document.location);
			$('#formQuiz').submit();
		}
	}
	
	/**
	 * dostosowuje sciezke zeby znow mozna bylo wyslac quiz
	 */
	
	function clearErrorQuiz() {
		sUrl = '';
		aUrl = document.location.href.split("?");
		aNewQuery = new Array();
		if (aUrl.length>1) {
			aQueryString = aUrl[1].split("&");
			for (i = 0; i < aQueryString.length; i++) {
				aValue = aQueryString[i].split("=");
				if (aValue[0] != 'iQuizError') 
					aNewQuery[aNewQuery.length] = aQueryString[i];
			}
			sUrl = aUrl[0] + '?' + aNewQuery.join("&");
			
		} else {
			sUrl = document.location.href;
		}
		document.location = sUrl;
	}
	
	
	/**
	 * pokazanie stopki
	 */
	function fShowFooter(){
			$('#pojStopka').show();
			$('#pojMenu').show();
	}
	
