// Poltergeist - wyskakująca reklama na warstwie ponad stroną, wersja 0.40, korzysta z MooTools i SWFObject
var closeButtonUrl="./img/close.gif";
var startRendering;
var frames=0;
var frame=0;
var positions = new Array();
var flash=false;

var templatePath = '';

function getPageSize() 
{
	var abc='fff';
    var pageSize = document.getSize();
    var windowSize = window.getSize();

    return new Array(pageSize.x, pageSize.y, windowSize.x, windowSize.y);
}

function getPageScroll(){
    var scrollSize = window.getScroll();
    return new Array(scrollSize.x, scrollSize.y);
}

// Usuwa obiekt Poltergeist z ekranu
function killPoltergeist()
{
	var poltergeist = document.getElementById('poltergeist');
    poltergeist.parentNode.removeChild(poltergeist);
	window.onscroll = function () { return true; }
}

// Powoduje animowany ruch Poltergeista
function slidePoltergeist(duration, fps, direction, percX, percY)
{
	var posY;
	var arrayPageSize=getPageSize();
	var arrayScrollSize=getPageScroll();

    var width = flashWidth;
    var height = flashHeight;

	destX=Math.round((arrayPageSize[2]*(percX/100))-(width/2));
	destY=Math.round((arrayPageSize[3]*(percY/100))-(height/2));

	if (direction)
	{
		switch (direction)
		{
			case "top":
			{
				step = Math.floor(Math.abs((destY+height))/frames);
				posY=step*frame+arrayScrollSize[1]-height;
				posX=destX;
				break;
			}
			case "bottom":
			{
				step = Math.floor(Math.abs((destX+height))/frames);
				posY=arrayPageSize[3]+arrayScrollSize[1]-(step*frame);
				posX=destX;
				break;
			}
			case "left":
			{
				step = Math.floor(Math.abs((destX+width))/frames);
				posX=step*frame-width;
				posY=destY+arrayScrollSize[1];
				break;
			}
			case "right":
			{
				step = Math.floor(Math.abs((destX+width))/frames);
				posX=arrayPageSize[2]-(step*frame);
				posY=destY+arrayScrollSize[1];
				break;
			}
			case "none":
			{
				posX=destX;
				posY=destY+arrayScrollSize[1];
				movePoltergeist(posX, posY);
			    // Obiekt Poltergeist b�dzie przesuwa� si� wraz ze stron�
			    //window.onscroll = function () { scrollPoltergeist(percX, percY);}
				// Wyłączenie animacji
				clearInterval(startRendering);
				break;
			}
		}

		movePoltergeist(posX, posY);

		// Przy pierwszej klatce animacji nale�y ods�oni� Poltergeista
		if (frame>0) {
    		document.getElementById('poltergeist').style.display = 'block';
        }

		frame++;
		// Zakończenie animacji
		if (frame>=frames)
		{
			// Obiekt Poltergeist będzie przesuwać się wraz ze stron�
			//window.onscroll = function () { scrollPoltergeist(percX, percY);}

			// Obiekt Poltergeist ustawiany jest na finałowym miejscu - sam tam najprawdopodobniej
			// nie dojedzie, ze względu na błedy zaokrągleń przy obliczaniu kroku
			movePoltergeist(destX, destY+arrayScrollSize[1]);

			// Wyłączenie animacji
			clearInterval(startRendering);
		}
	}
	else
	{
		// Obiekt Poltergeist będzie przesuwać się wraz ze stroną
		// window.onscroll = function () { scrollPoltergeist(percX, percY);}
		// Wyłączenie animacji
		clearInterval(startRendering);
	}
}

function createPoltergeist(advertImgUrl, closeOrginX, closeOrginY, closeWidth, closeHeight, duration, fps, direction, percX, percY, multipleRuns,  linkOrginX, linkOrginY, linkWidth, linkHeight, linkURL, newWindow)
{
	if (multipleRuns==true || !alreadyRunned())
	{
		// Zabezpieczenie na wypadek nie podania podstawowych danych
		if (!closeOrginX) closeOrginX=0;
		if (!closeOrginY) closeOrginY=0;
		if (!closeWidth) closeWidth=25;
		if (!closeHeight) closeHeight=25;
		if (!duration) duration=1000;
		if (!fps) fps=25;
		if (!direction) direction="top";
		if (!percX) percX=50;
		if (!percY) percY=50;

		// Tworzy nast�puj�cy kawa�ek kodu html
		//	<div id='poltergeist'>
		//		<img id='poltergeistCloseButton' src='/img/dummy.gif' onclick='killPoltergeist();'/>
		//      <a href='' id='poltergeistLinkArea' target='_blank'><img src='/img/dummy.gif' width="" height=""/></a>
		//		<img id='poltergeistAdvert' src=' -link do advertu- '/>
		//	</div>
		poltergeistBase = document.createElement('div');
		poltergeistBase.setAttribute('id','poltergeist');
		poltergeistBase.style.display = 'none';

		if (linkOrginX && linkOrginY && linkWidth && linkHeight && linkURL)
		{
			linkArea = document.createElement("a");
			linkArea.href=linkURL;
			if (newWindow==true) linkArea.target='_blank';
			linkArea.setAttribute('id','poltergeistLinkArea');
			linkArea.innerHTML='<img src="./img/dummy.gif" width="'+linkWidth+'" height="'+linkHeight+'"/>';
			linkArea.style.top=linkOrginY+"px";
			linkArea.style.left=linkOrginX+"px";
			linkArea.style.width=linkWidth+"px";
			linkArea.style.height=linkHeight+"px";
		}

        if (flash == true) {
            flashElement = document.createElement ('div');
            flashElement.setAttribute('id','poltergeistAdvert');
        } else {
            imageElement = document.createElement('img');
            imageElement.setAttribute('id','poltergeistAdvert');
            imageElement.src = advertImgUrl;
        }

		closeButton = new Image();
		closeButton.src = closeButtonUrl;
		closeButton.setAttribute('id','poltergeistCloseButton');
		closeButton.style.top="0px";
		closeButton.style.left=(flashWidth-closeWidth)+"px";
		closeButton.style.width=closeWidth+"px";
		closeButton.style.height=closeHeight+"px";
		closeButton.style.position='absolute';
		closeButton.style.cursor='pointer';
		closeButton.title='Zamknij';

		poltergeistBase.appendChild(closeButton);
		if (linkOrginX && linkOrginY && linkWidth && linkHeight && linkURL) poltergeistBase.appendChild(linkArea);
        if (flash == true) {
            poltergeistBase.appendChild(flashElement);
        } else {
            poltergeistBase.appendChild(imageElement);
        }
		document.getElementsByTagName("body")[0].appendChild(poltergeistBase);
		document.getElementById('poltergeistCloseButton').onclick = function () {killPoltergeist();}

        if (flash == true) {
			var flashvars = {};
			var attributes = {};
			var params = { wmode: "transparent" };
			swfobject.embedSWF(advertImgUrl, "poltergeistAdvert", flashWidth, flashHeight, "8.0.0", "./flash/expressInstall.swf", flashvars, params, attributes);
            document.getElementById('poltergeistAdvert').width = flashWidth;
            document.getElementById('poltergeistAdvert').height = flashHeight;
        }

		// Oblicza długość animacji i uruchamia ją
		frames = Math.round(fps*duration/1000);	// Calkowita liczba renderowanych klatek
		interval = duration / frames; 			// Czas co jaki jest renderowana nowa ramka
		startRendering=setInterval("slidePoltergeist("+duration+", "+fps+", \""+direction+"\", "+percX+", "+percY+")", interval);
	}
}

// Powoduje przesunięcie obiektu Poltergeist w nowe miejsce
function movePoltergeist(x, y)
{
	document.getElementById('poltergeist').style.top=y+"px";
	document.getElementById('poltergeist').style.left=x+"px";
}

// Powoduje przesunięcie obiektu Poltergeist wraz ze scrollowaniem strony
function scrollPoltergeist(percX, percY)
{
	var arrayPageSize=getPageSize();
	var arrayScrollSize=getPageScroll();
	var width = 501; //=document.getElementById('poltergeistAdvert').width;
	var height = 250;
	//document.getElementById('poltergeistAdvert').height;

	destX=Math.round((arrayPageSize[2]*(percX/100))-(width/2));
	destY=arrayScrollSize[1]+Math.round((arrayPageSize[3]*(percY/100))-(height/2));

	movePoltergeist(destX, destY);
}

function alreadyRunned()
{
    var myCookie = Cookie.read("poltergeist");
   
	if (myCookie == null)
	{
		Cookie.write("poltergeist", "yes");
		return false;
	}
	return true;
}
