/**
*   @version $Id$
*	@package banner
*	@author piotrj
*/
/**--------------------------------------------------------------------
*	tu bannery
*---------------------------------------------------------------------*/

	var eBanReturnData = Array( '' );
	var eBanTimeoutHit = Array( 0 );
	var eBanAjaxClient = Array( null );
	var eBanObj = Array( null );
	var eBanObjCount = -1;

	/**
	 * funkcja tworzy obiekt XMLHttpRequest w zaleznosci od przegladarki
	 * @author piotrj
	 */
	function eBanCreateXMLHttpRequest()
	{
		if( typeof XMLHttpRequest != "undefined" )
		{
			return new XMLHttpRequest();
		}
		var xhrVersion = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp" ];
		var xhrObj = null;
		for( var i = 0; i < xhrVersion.length; i++ )
		{
			try
			{
				if( xhrObj = new ActiveXObject( xhrVersion[i] ) )
				{
					return xhrObj;
				}
			}
			catch( e )
			{
			}
		}
		return null;
	};

	/**
	 * klasa uzywana do zarzadzania bannerami
	 * @author piotrj
	 * @param string type1 - czy dokument (0), modul (1), grupa (2)
	 * @param string type2 - rodzaj modulu (zgodnie z przyjetymi w cms i frontend typami)
	 * @param string type3 - id grupy (dla type1 = 2), lub dokumentu (dla type1 = 0)
	 */
	function EbBannerObj( type1, type2, type3, placeToInsert, lang, opts )
	{
		this.type1 = type1;						// typ1 atrybutu
		this.type2 = type2;						// typ2 atrybutu
		this.type3 = type3;						// typ3 atrybutu
		this.placeToInsert = placeToInsert;		// gdzie bedzie wstawiony banner (0-gora, 1-dol, 2-lewa kolumna, 3-prawa kolumna, 4-popup)
		this.lang = lang;						// jezyk
		this.opts = opts;						// dodatkowe opcje (jeszcze nie wiem jakie)
		this.state = 0;							// aktualny stan pobierania banerow 0 - w trakcie, 1 - zakonczono sukcesem, < 0 - bledy wszelakie
	}

	/**
	 * metoda laczy sie z plikiem php i pobiera odpowiedni dla wywolania banner
	 * @author piotrj
	 * @param integer objNum - numer objektu, ktory ma byc uzyty
	 */
	function EbBannerObj_getData( objNum )
	{
		var qStr = "type1="+eBanObj[objNum].type1+"&type2="+eBanObj[objNum].type2+"&type3="+eBanObj[objNum].type3+"&placeToInsert="+eBanObj[objNum].placeToInsert+"&lang="+eBanObj[objNum].lang+"&opts="+eBanObj[objNum].opts+"";
		eBanAjaxClient[objNum] = eBanCreateXMLHttpRequest();
		if( eBanAjaxClient[objNum] != null )
		{
			try
			{
				eBanAjaxClient[objNum].onreadystatechange = function()
				{
					try
					{
						if( ( eBanAjaxClient[objNum].readyState == 4 ) && ( eBanAjaxClient[objNum].status == 200 ) )
						{
							if( ( eBanAjaxClient[objNum].responseText != null ) && ( eBanAjaxClient[objNum].responseText != '' ) )
							{
								eBanReturnData[objNum] = eBanAjaxClient[objNum].responseText;
							}
							eBanObj[objNum].state = 1;
						}
						else if( eBanAjaxClient[objNum].readyState == 4 )
						{
							eBanObj[objNum].state = -1;
						}
						else if( eBanAjaxClient[objNum].readyState > 0 )
						{
							if( eBanAjaxClient[objNum].status >= 400 )
							{
								eBanObj[objNum].state = -2;
							}
						}
					}
					catch(e)
					{
						//alert( e );
					}
				};
				eBanAjaxClient[objNum].open( "GET", pageHost+"/banners/banner.php5?"+qStr, true );
				eBanAjaxClient[objNum].setRequestHeader( "Content-Type", "text/html; charset=UTF-8" );
				eBanAjaxClient[objNum].send( qStr );
			}
			catch(e)
			{
				//alert( e );
			}
		}
	}

	EbBannerObj.prototype.getData = EbBannerObj_getData;

	/**
	 * funckja wstawia w okreslonym miejscu na stronie wybrany banner
	 * @author piotrj
	 * @param string divId - id diva, ktory ma byc uaktualniony
	 * @param integer objNum - numer objektu, ktory ma byc uzyty
	 */
	function eBanUpdateDoc( divId, objNum )
	{
		if( ( ( eBanObj[objNum].state == 1 ) || ( eBanObj[objNum].state < 0 ) || ( eBanTimeoutHit[objNum] > 10 ) ) )
		{
			var eBanScript = '';
			if( eBanReturnData[objNum].indexOf( 'e2f5d94ea30e04d2e14f36d95a0ad776' ) > -1 )
			{
				var eBanData = eBanReturnData[objNum].split( 'e2f5d94ea30e04d2e14f36d95a0ad776' );
				eBanReturnData[objNum] = eBanData[0];
				eBanScript = eBanData[1];
			}
			if( eBanObj[objNum].placeToInsert != 6 )
			{
				var oDiv;
				/*if( oDiv = document.getElementById( divId ) )
				{
					oDiv.innerHTML = eBanReturnData[objNum];
				}*/
				/*if( eBanReturnData[objNum] == '' )
				{
					if( oDiv = document.getElementById( divId ) )
					{
						oDiv.style.display = 'none';
					}
				}
				else*/
				if( oDiv = document.getElementById( divId ) )
				{
					if( ( eBanReturnData[objNum] != '' ) || ( ( eBanScript != '' ) && ( eBanScript.indexOf( 'swfobject.embedSWF' ) > -1 ) ) )
					{
						if( eBanReturnData[objNum] != '' )
						{
							oDiv.innerHTML = eBanReturnData[objNum];
						}
						oDiv.style.display = 'block';
					}
				}
			}
			if( eBanScript != '' )
			{
				eval( eBanScript );
			}
		}
		else
		{
			eBanTimeoutHit[objNum]++;
			setTimeout( "eBanUpdateDoc( \'"+divId+"\', "+objNum+" )", 100 );
		}
	}

	/**
	 * funckja wstawia w okreslonym miejscu na stronie wybrany banner
	 * @author piotrj
	 * @param string divId - id diva, ktory ma byc uaktualniony
	 * @param integer objNum - numer objektu, ktory ma byc uzyty
	 */
	function eBanChangeBg( pathToImg )
	{
		document.body.style.backgroundImage = 'url( '+pathToImg+')';
	}

	/**
	 * funckja ukrywa diva z bannerem
	 * @author piotrj
	 * @param string divId - id diva, ktory ma byc ukryty
	 */
	function eBanCloseBanner( divId )
	{
		if( oDiv = document.getElementById( divId ) )
		{
			oDiv.style.display = 'none';
		}
	}

	/**
	 * funckja tworzy obiekt bannerowy, laczy sie ze skryptem bannerowym i wyswietla wynik
	 * @author piotrj
	 * @param integer type1 - czy dokument (0), czy modul (1), czy grupa (2)
	 * @param integer type2 - typ modulu (dla type1=1)
	 * @param integer type3 - id grupy (dla type1=2), lub dokumentu (dla type1=0)
	 * @param integer placeToInsert - liczbowe okreslenie miejsca, w ktore banner ma byc wstawiony
	 * @param integer lang - jezyk dla jakiego pobieramy banner
	 * @param string opts - w razie gdyby cos trzeba bylo dodac
	 */
	function eBanGetBanner( type1, type2, type3, placeToInsert, lang, opts )
	{
		if( typeof( opts ) == "undefined" )
		{
			opts = '';
		}
		eBanObjCount++;
		var objCount = eBanObjCount;
		eBanTimeoutHit[objCount] = 0;
		eBanReturnData[objCount] = '';
		var divId = 'divBan'+type1+'_'+type2+'_'+type3+'_'+placeToInsert+'_'+objCount;
		if( placeToInsert != 6 )
		{
			if( placeToInsert == 5 )
			{
				//document.write( '<div id="'+divId+'" style="position: absolute; display: block;"></div>' );
				document.write( '<div id="'+divId+'" style="position: absolute; display: none;"><div id="'+divId+'_flash"></div></div>' );
			}
			else
			{
				//document.write( '<div id="'+divId+'" style="display: inline;"></div>' );
				document.write( '<div id="'+divId+'" style="display: none;"><div id="'+divId+'_flash"></div></div>' );
			}
			opts = ''+divId+'';
		}
		eBanObj[objCount] = new EbBannerObj( type1, type2, type3, placeToInsert, lang, opts );
		eBanObj[objCount].getData( objCount );
		eBanUpdateDoc( divId, objCount );
	}

	/**
	 * odpala skrypt php aktualizujacy ilosc views i clicks na danym bannerze
	 * @author piotrj
	 * @param integer banId - id bannera, dla ktorego dane uaktualniamy
	 * @param integer eventType - czy wyswietlenie (0), czy klikniecie (1)
	 * @param integer insertType - czy wstawic nowy rekord, czy uaktualnic stary z danego dnia
	 * @param string opts - w razie gdyby cos trzeba bylo dodac
	 */
	function eBanUpdateBanner( banId, eventType, insertType, opts, urlToRedirect )
	{
		if( typeof( opts ) == "undefined" )
		{
			opts = '';
		}
		//alert( 'in' );
		qStr = "banId="+banId+"&eventType="+eventType+"&insertType="+insertType+"&opts="+opts+"";
		var eBanAjaxClientU = eBanCreateXMLHttpRequest();
		if( eBanAjaxClientU != null )
		{
			try
			{
				eBanAjaxClientU.open( "GET", pageHost+"/banners/bannerUpdater.php5?"+qStr, true );
				eBanAjaxClientU.setRequestHeader( "Content-Type", "text/html; charset=UTF-8" );
				eBanAjaxClientU.send( qStr );
			}
			catch(e)
			{
				//alert( e );
			}
		}
		if( urlToRedirect != '' )
		{
			window.open( urlToRedirect, "BannerWindow", "channelmode=yes, toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes, titlebar=yes, width=800, height=600" );
		}
	}

/**-------------------------------------------------------------------*/
