var xmlRequest = null;
var hostname = null;

var currentPage = 0;
var numberOfTweets = 0;
var timer = null;

function twitterAJAX( target, update, notw, hostName )
{
	numberOfTweets = notw;
	hostname = hostName;
	var currentPage = document.getElementById("twitter_pagination").getAttribute("cp");
	
	var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
	
	if ( ! document.getElementById ( target ) )
		return;
	
	if ( window.ActiveXObject )
	{
		for ( var i = 0; i < activexmodes.length; i++ )
		{
			try
			{
				xmlRequest = new ActiveXObject ( activexmodes[i] );
			}
			catch ( e ) {}
		}
	}
	else if ( window.XMLHttpRequest )
	{
		xmlRequest = new XMLHttpRequest();
	}
	else
		return; // BAIL OUT
	
	// START DATA LOOP	
	//timer = setTimeout ( 'getData()', 20000 );
}

function twitter_pagination_left()
{
	if ( currentPage > 0 )	
		currentPage--;			
	else
		currentPage = 0;		
	
	document.getElementById("twitter_pagination").setAttribute('cp', currentPage);
	
	document.getElementById('twitter_nav_left').disabled="disabled";
	document.getElementById('twitter_nav_right').disabled="disabled";
	getData();
}

function twitter_pagination_right()
{	
	currentPage++;
	
	document.getElementById("twitter_pagination").setAttribute('cp', currentPage);
	document.getElementById('twitter_nav_left').disabled="disabled";
	document.getElementById('twitter_nav_right').disabled="disabled";
	getData();
}

function getData()
{	
	xmlRequest.open ( "GET", "http://" + hostname + "/wp-content/themes/giegold/twitter.php?page="+currentPage, true );
	xmlRequest.onreadystatechange = function ()
	{
		if ( xmlRequest.readyState == 4 )
		{
			if ( xmlRequest.status == 200 )
			{				
				data = eval( '(' + xmlRequest.responseText + ')' );
				
				if ( data.length > 0 )
				{
					if ( data[data.length-1].text )
					{
						list = document.getElementById ( "twitter" );
						
						list.innerHTML = "";
						
						for ( i = 0; i < data.length; i++ )
						{
							listNode = document.createElement("LI");
							
							tweetNode = document.createElement("DIV");
							tweetNode.setAttribute("class", "tweet");
							tweetNode.innerHTML = data[i].text;
							listNode.appendChild ( tweetNode );
																	
							dateNode = document.createElement("DIV");
							dateNode.setAttribute("class", "date");
							dateNode.innerHTML = data[i].created_at;
							listNode.appendChild ( dateNode );
												
							list.appendChild(listNode);
						}
						document.getElementById('twitter_nav_left').disabled="";
						document.getElementById('twitter_nav_right').disabled="";
					}
				}
				else
				{
					document.getElementById('twitter_nav_left').disabled="";
					currentPage--;
				}
			}
		}
	};
	xmlRequest.send ( null );
}

Date.prototype.formatDate = function(format)

{

    var date = this;

    if (!format)

      format="MM/dd/yyyy";               

 

    var month = date.getMonth() + 1;

    var year = date.getFullYear();    

 

    format = format.replace("MM",month.toString().padL(2,"0"));        

 

    if (format.indexOf("yyyy") > -1)

        format = format.replace("yyyy",year.toString());

    else if (format.indexOf("yy") > -1)

        format = format.replace("yy",year.toString().substr(2,2));

 

    format = format.replace("dd",date.getDate().toString().padL(2,"0"));

 

    var hours = date.getHours();       

    if (format.indexOf("t") > -1)

    {

       if (hours > 11)

        format = format.replace("t","pm")

       else

        format = format.replace("t","am")

    }

    if (format.indexOf("HH") > -1)

        format = format.replace("HH",hours.toString().padL(2,"0"));

    if (format.indexOf("hh") > -1) {

        if (hours > 12) hours - 12;

        if (hours == 0) hours = 12;

        format = format.replace("hh",hours.toString().padL(2,"0"));        

    }

    if (format.indexOf("mm") > -1)

       format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));

    if (format.indexOf("ss") > -1)

       format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));

    return format;

}

String.repeat = function(chr,count)

{    

    var str = ""; 

    for(var x=0;x<count;x++) {str += chr}; 

    return str;

}

String.prototype.padL = function(width,pad)

{

    if (!width ||width<1)

        return this;   

 

    if (!pad) pad=" ";        

    var length = width - this.length

    if (length < 1) return this.substr(0,width);

 

    return (String.repeat(pad,length) + this).substr(0,width);    

}    

String.prototype.padR = function(width,pad)

{

    if (!width || width<1)

        return this;        

 

    if (!pad) pad=" ";

    var length = width - this.length

    if (length < 1) this.substr(0,width);

 

    return (this + String.repeat(pad,length)).substr(0,width);

}

String.format = function(frmt,args)

{   

    for(var x=0; x<arguments.length; x++)

    {

        frmt = frmt.replace("{" + x + "}",arguments[x+1]);

    }

    return frmt;
}