// Poltergeist - wyskakująca reklama na warstwie ponad stroną, wersja 0.35
var closeButtonUrl="img/dummy.gif";
var startRendering;
var frames=0;
var frame=0;
var positions = new Array();

function createFlash(movie, width, height, background, idName, dest, fVars)
{
	obj_setFlash=document.getElementById(dest);
	if (obj_setFlash)
	{
		pytajnik = "";
		if (fVars != "") pytajnik = "?";
		objTag = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='"+width+"' height='"+height+"' id='"+idName+"'>";
		objTag2 = "<embed src='"+movie+pytajnik+fVars+"' quality=high wmode='transparent' width='"+width+"' height='"+height+"' type='application/x-shockwave-flash' pluginspace='http://www.macromedia.com/go/getflashplayer'></embed>";
		paramTag = "<param name='movie' value='"+movie+pytajnik+fVars+"' /><param name='wmode' value='transparent'><param name='bgcolor' value='"+background+"' /><param name='quality' value='high' /><param name='menu' value='false' /></object>";
		obj_setFlash.innerHTML=objTag+objTag2+paramTag;
//		alert (obj_setFlash.innerHTML);
	}
}

// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

// Usuwa obiekt Poltergeist z ekranu
function killPoltergeist()
{
	document.getElementById('poltergeist').style.display="none";
	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=600; //document.getElementById('poltergeistAdvert').width;
	var height=396; //document.getElementById('poltergeistAdvert').height;

	// Końcowe położenie obiektu Poltergeist - jeśli rozmiar okna zostanie
	// zmieniony podczas trwania animacji Poltergeist dostosuje sie
	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.visibility="visible";

		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łędy 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())
	{
		advertImg = new Image();
		// Jeśli ścieżka do obrazka będzie błędna, Poltergeist nie zostanie wogóle stworzony
		advertImg.onload = function()
		{
			// 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');

			closeButton = new Image();
			closeButton.src = closeButtonUrl;
			closeButton.setAttribute('id','poltergeistCloseButton');
			closeButton.style.top=closeOrginY+"px";
			closeButton.style.left=closeOrginX+"px";
			closeButton.style.width=closeWidth+"px";
			closeButton.style.height=closeHeight+"px";

			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";
			}

			flashElement = document.createElement ('div');
			flashElement.setAttribute('id','poltergeistAdvert');
      

			imageElement = document.createElement('img');
			imageElement.setAttribute('id','poltergeistAdvert');
			imageElement.src = advertImgUrl;

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

			// 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);
			createFlash ('/splash/flash/top_layer.swf', '501', '317', '#000000', 'flashContentCreated02', 'poltergeistAdvert', '');
		}

		advertImg.src = advertImgUrl;
	}
}

// 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 przesuwanie 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);
}

// From http://www.w3schools.com/js/js_cookies.asp
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 alreadyRunned()
{
	if (getCookie("poltergeist")=="")
	{
		document.cookie="poltergeist=yes";
		return false;
	}
	return true;
}


function initPoltergeist () {
					window.onload = function(){
            createPoltergeist('/splash/img/dummy.gif', 350, 0, 1, 1, null, 25, "none", 52, 50, true, "pl");
					  loadPage();
					}
				}



