﻿var idxMax = 0;
var idxCurrent = -1
var idxPrevious = -1;
var paused = 0;
var xPos;
var xVel;
var t1;
var t2;

function initPage()
{
	paused = 0;
	var initialLoad = 0;
	
	if(idxCurrent == -1 && idxPrevious == -1)
	{
		idxCurrent = 0;
		idxPrevious = idxMax-1	
		document.getElementById('pnlAd' + idxCurrent.toString()).style.left = "1px";
		document.getElementById('pnlAd' + idxCurrent.toString()).style.zIndex = 0;
		//document.getElementById('adCount').innerHTML = (idxCurrent + 1).toString() + ' / ' + idxMax.toString();
		initialLoad = 1;
		t2=null;
	}
	else
	{
		document.getElementById('pnlAd' + idxPrevious.toString()).style.left = "-576px";
		document.getElementById('pnlAd' + idxPrevious.toString()).style.zIndex = 50;
		document.getElementById('pnlAd' + idxCurrent.toString()).style.zIndex = 0;
	}
	
	
//	if(idxPrevious > -1)
//	{
//		document.getElementById('pnlAd' + idxPrevious.toString()).style.left = "-576px";
//		document.getElementById('pnlAd' + idxPrevious.toString()).style.zIndex = 50;
//	}
//	
//	if(idxCurrent > -1)
//	{
//		document.getElementById('pnlAd' + idxCurrent.toString()).style.zIndex = 0;
//	}
	
	idxPrevious = idxCurrent;
	
	idxCurrent++;
	
	if(idxCurrent > idxMax-1)
		idxCurrent = 0;
	xPos = -576;
	xVel = 32;
	
	if(initialLoad == 1)
		t2 = setTimeout("flipPage()", 7500);
	else
		t2 = setTimeout("flipPage()", 30);
}


function flipPage()
{
	document.getElementById('pnlAd' + idxCurrent.toString()).style.left = (xPos + "px");
	xPos += xVel;
	if(xVel > 1 && xPos > -64)
		xVel = xVel/2;
	if(xVel < 1) xVel = 1;
	if(xPos < 1)
		t2 = setTimeout("flipPage()", 30);
	else
	{
		t2 = null;
		if(paused == 0)
			t1 = setTimeout("initPage()", 7500);
	}
}

function pauseSlider()
{
	paused=1;
	t1 = clearTimeout(t1);
}

function resumeSlider()
{
	if(paused == 0)
		return;
	initPage();
}

function ffSlider()
{
	if(t2 != null)
		return;

	pauseSlider();	
	
	if(idxPrevious > -1)
	{
		document.getElementById('pnlAd' + idxPrevious.toString()).style.left = "-576px";
		document.getElementById('pnlAd' + idxPrevious.toString()).style.zIndex = 50;
	}
	
	if(idxCurrent > -1)
	{
		document.getElementById('pnlAd' + idxCurrent.toString()).style.zIndex = 0;
	}
	
	idxPrevious = idxCurrent;
	
	idxCurrent++;
	
	if(idxCurrent > idxMax-1)
		idxCurrent = 0;
	xPos = -576;
	xVel = 32;
	t2 = setTimeout("flipPage()", 30);
}

function rewSlider()
{
	if(t2 != null)
		return;

	pauseSlider();	
	
	if(idxPrevious > -1)
	{
		document.getElementById('pnlAd' + idxPrevious.toString()).style.left = "-576px";
		document.getElementById('pnlAd' + idxPrevious.toString()).style.zIndex = 50;
	}
	
	if(idxCurrent > -1)
	{
		document.getElementById('pnlAd' + idxCurrent.toString()).style.zIndex = 0;
	}
	
	idxPrevious = idxCurrent;
	
	idxCurrent--;
	
	if(idxCurrent < 0)
		idxCurrent = idxMax - 1;
	xPos = -576;
	xVel = 32;
	t2 = setTimeout("flipPage()", 30);
}

function addLoadEvent(func) 
{ 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') 
	  { 
	    window.onload = func; 
	  } 
	  else 
	  { 
	    window.onload = function() 
	    { 
	      if (oldonload)
	      { 
	        oldonload(); 
	     } 
      func(); 
    } 
  } 
} 

