var  prevblock = 0;
var  currentblock = 1;
var  nextblock = 2;

var topurl;
var boturl;

var t = 1;
var totalscroll;
var yy = 1;
var inittopval =0;


var PreImage2=new Image();
PreImage2.src="/images/buttonhi.jpg";

var PreImage3=new Image();

PreImage3.src="/images/bbuttonhi.jpg";

function request(el,direction,durl,tstartdate)
{ 
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) 
              {
              
				               var SplitResult = xhr.responseText.split("|");
     				            document.getElementById(el).innerHTML=SplitResult[0];
									boturl = SplitResult[1];
									topurl  = SplitResult[2];
									totalscroll=0;
									yy=1;
									inittopval = document.getElementById('innerwotsit').style.top.replace("px","");
									inittopval = inittopval.replace("pt","");

									document.getElementById('status').innerHTML="&nbsp;";
									doscroll(direction);

              }
              else 
              {
                 document.ajax.dyn="Error code " + xhr.status;
                 }
         }
    }; 

   xhr.open("GET", "upcoming_fairlist.php?fromdate="+durl+"&amp;tstartdate="+tstartdate,  true); 
   xhr.send(null); 
} 



function doscroll(direction)
{

 yy = yy *2;
  totalscroll +=yy;

  amount=300;
if (totalscroll < amount)
{
 	 topval = document.getElementById('innerwotsit').style.top.replace("px","");
 	 topval = topval.replace("pt","");
 	 
 	 
 	 if (direction==1)
 	 {
  		document.getElementById('innerwotsit').style.top =  (parseInt(topval) - yy) + "px";
	 }
	 else
	 {
			document.getElementById('innerwotsit').style.top =  (parseInt(topval) + yy) + "px";
			
	 }
  
   setTimeout('doscroll('+direction+');',75);
}
else
	{
			if (direction==1)
 			{	
				document.getElementById('innerwotsit').style.top =  (parseInt(inittopval) - amount) + "px";
			}
			else
			{
				document.getElementById('innerwotsit').style.top =  (parseInt(inittopval) + amount) +  "px";
			}
	
			if (direction==1)
			{
							
					if (document.getElementById('x'+nextblock))
					{
					divh=300;
						document.getElementById('innerwotsit').removeChild(document.getElementById('x'+nextblock));
						var topval = document.getElementById('innerwotsit').style.top.replace("px","");
						topval = topval.replace("pt","");
						document.getElementById('innerwotsit').style.top = (parseInt(topval) + divh) + "px";
						addElement(nextblock);
					}
}
else
{
	//remove lastblock
	if (document.getElementById('x'+prevblock))
	{

		divh=300;
		document.getElementById('innerwotsit').removeChild(document.getElementById('x'+prevblock));
		addElementBefore(prevblock,currentblock);
		var topval = document.getElementById('innerwotsit').style.top.replace("px","");
		topval = topval.replace("pt","");
		document.getElementById('innerwotsit').style.top = (parseInt(topval) - divh) + "px";
	}
}	

if (topurl!="-")
{
document.getElementById('toptop').innerHTML="<a  id=\"rolloverb\" href=\"javascript:moveit(-1,'"+topurl+"');\"><span>scroll up<\/span><\/a>";
}
document.getElementById('botbot').innerHTML="<a  id=\"rollover\" href=\"javascript:moveit(1,'"+boturl+"');\"><span>scroll down<\/span><\/a>";

}

}


function addElement(n) 
{

  var newdiv = document.createElement('div');
  
  var divIdName = 'x'+n;
  newdiv.setAttribute('id',divIdName);
  newdiv.setAttribute('style','height:300px;');
  document.getElementById('innerwotsit').appendChild(newdiv);
  document.getElementById(divIdName).style.height = 300; //ie
}

function addElementBefore(n,node) 
{
  var newdiv = document.createElement('div');
  var divIdName = 'x'+n;
  newdiv.setAttribute('id',divIdName);
  newdiv.setAttribute('style','height:300px;');
  document.getElementById('innerwotsit').insertBefore(newdiv,document.getElementById('x'+node));
  document.getElementById(divIdName).style.height = 300; //ie
}


function moveit(direction,durl,tstartdate)
{

currentblock += direction;
if (currentblock<0) currentblock=2;
currentblock %=3;

prevblock+=direction;
if (prevblock<0) prevblock=2;
prevblock %=3;

nextblock+=direction;
if (nextblock<0) nextblock=2;
nextblock %=3;

if (direction==-1)
{
document.getElementById('toptop').innerHTML="<a id=\"disb\" href=\"#\"><span>scroll up</span></a>";
}
else
	{
document.getElementById('botbot').innerHTML="<a id=\"dis\" href=\"#\"><span>scroll down</span></a>";
}
document.getElementById('status').innerHTML="<img src=\"/images/loading.gif\" alt=\"loading\"/>";
request('x'+currentblock,direction,durl,tstartdate); // element

}
