/* xfade.js

CHANGELOG:
	07/16/07 - sctyler
			- added random image script
			- added comments
			- added method to set display time for each image
			- changed the stylesheet location to a global variable so we don't have to search for it.
			- changed stylesheet insertion to a seperate function, for modularity
			- added a fade time variable to control how long a fade lasts
			- added number of steps in fade
			- added populate_images() b/c we need to reuse it for random functionality
*/

//if we can add an eventListener, do so now. If not, attach the event
window.addEventListener ? window.addEventListener("load",so_init,false) : window.attachEvent("onload",so_init);

var d=document, 		// the document we are working with
	imgs = new Array(), // the array of images in div[ id='imageContainer' ]
	current=0, 			// the current index of imgs[]
	next=1,				// the index of the next image in imimgs[]
	img_idxs=new Array(),
	fadeTime=1,			// the time it takes an image to fade, in seconds
	fadeSteps=100,		// the number of steps in fade. The higher the value, the smoother the fade will look.
						// NOTE: (This may look choppy on slower browsers, but if we have opacity capabilities, chances are this won't matter.)
	displayTime=5,		// the display time of the images, in seconds
	doRandom = false,	// Set whether we want random images or not.
	cssLocation = "/ant/inc_unit/xfade.css", //the location of the CSS file, or false for none
	tagID="imageContainer"; //the ID  of the tag that contains the images
	tagName="div" //the kind of tags we want to change

//Create a new link to the CSS files and add it to current document.
function create_css_link(){
	css = d.createElement("link");			//create the link element
	css.setAttribute("href",cssLocation);	//add the href attr
	css.setAttribute("rel","stylesheet");	//add the rel attr
	css.setAttribute("type","text/css");	//add the type attr
	d.getElementsByTagName("head")[0].appendChild(css);	 //add the link to the document's HEAD tag
}


//set the opacity of the object.
function setOpacity(obj, value) {
	if(obj.xOpacity>0.99) {obj.xOpacity =0.99;return;}	//if we are at maximum opacity, do nothing
	obj.style.opacity = obj.xOpacity;					//else, set the opacity to the object's xOpacity value
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

//populate the img_idxs array with the indexes of the imgs array
function populate_img_idxs(){img_idxs=new Array();for (i=0; i<imgs.length; i++)img_idxs[i]=i;}

//get next index
function getNextIdx(){
	if (!doRandom){		//if random option is false, next is next index
		next=(next%(imgs.length-1))+1;
	}else{
		var img_idxs_idx=Math.round(Math.random()*img_idxs.length);
		next=img_idxs[ img_idxs_idx ];
		if (!imgs[next])getNextIdx();
		img_idxs.splice(img_idxs_idx,img_idxs_idx);										//delete the index in current
		if (img_idxs.length<=1)populate_img_idxs();
	}
}

//initilaize all values.
function so_init() {
	if(!d.getElementById || !d.createElement)return;	 //if we can't work with elements, return and do nothing. FAIL!
	if(cssLocation)create_css_link(); 					 //if we have a stylesheet defined, add it to the document

	if(!(d.getElementById(tagID) && d.getElementById(tagID).getElementsByTagName(tagName))){return;} //end it all if element does not exist
	imgs = d.getElementById(tagID).getElementsByTagName(tagName);  //populate imgs[] with the img elements under div[ id='imageContainer' ]
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;						//set all the images to zero opacity
	imgs[0].style.display = "block";										//set first image to black display
	imgs[0].xOpacity = .99;													//show first image (set opacity to 99%)

	populate_img_idxs();			//populate img_idxs so we can randomize

	setTimeout(so_xfade,displayTime*2);		//start the show!
}

//handle the fading and slideshow properties.
function so_xfade() {
	cOpacity = imgs[current].xOpacity; 			//opacity of the currently shown image
	nOpacity = imgs[next].xOpacity;				//get the opacity of the next image

	//step each image's opacity by (1/fadeSteps)
	cOpacity-=1/fadeSteps;
	nOpacity+=1/fadeSteps;

	imgs[next].style.display = "block";			//set the display style of the curretn image to block
	imgs[current].xOpacity = cOpacity;			//set the current image's xOpacity property to [current_value-.05]
	imgs[next].xOpacity = nOpacity;				//set the next image's xOpacity property to [current_value-.05]

	setOpacity(imgs[current]); 					//apply the xOpacity values
	setOpacity(imgs[next]);

	//if the current image's opacity is zero or less, display image for displayTime seconds, else, fade again by (1/fadeSteps)
	if(cOpacity<=0) {
		imgs[current].style.display = "none"; current = next; getNextIdx(); setTimeout(so_xfade,(displayTime*1000));
	} else {
		setTimeout(so_xfade,fadeTime*(1000/fadeSteps));
	}

}
function twitterCallback2(twitters) {
	  var statusHTML = [];
	  var numItemsInRow=5;
	  for (var i=0; i<twitters.length; i++){
	    var username = twitters[i].user.screen_name;
	    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
	      return '<a href="'+url+'">'+url+'</a>';
	    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
	      return  reply.charAt(0)+'<a href="http://www.twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	    });
	    statusHTML.push( 
	    		(((i%numItemsInRow)==0)?'<div class="twitter_row">':'')+
	    		'<div class="twitter_item"><span>'+status+'</span>'+
	    		'<a style="font-size:85%" href="http://twitter.com/'+
	    			username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+
	    		'</a></div>'+
	    		(((i%(numItemsInRow-1))==numItemsInRow-1)?'</div>':'')
	    		);
	  }
	  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	}

	function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);

	  if (delta < 60) {
	    return 'less than a minute ago';
	  } else if(delta < 120) {
	    return 'about a minute ago';
	  } else if(delta < (60*60)) {
	    return (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (120*60)) {
	    return 'about an hour ago';
	  } else if(delta < (24*60*60)) {
	    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    return '1 day ago';
	  } else {
	    return (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	}