function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(ms) {
var sec = Math.floor(ms/1000)
ms = ms % 1000

var min = Math.floor(sec/60)
sec = sec % 60

var hr = Math.floor(min/60)
min = min % 60

var day = Math.floor(hr/24)
hr = hr % 60

if ((day == 0) && (hr == 0)) {
 if (min == 1) {
  stamp = min + " minute";
 } else {
  stamp = min + " minutes";
 }
} else if (day == 0) {
 if (hr == 1) {
  stamp = hr + " hour";
 } else {
  stamp = hr + " hours";
 }
} else {
 if (day == 1) {
  stamp = day + " day";
 } else {
  stamp = day + " days";
 }
}
return stamp;
}

function getStamp(date) {
	//alert(date);
	var c = new Date();
	var d = new Date(date);
	return time(c - d) + ' ago';
}

var skybox_itemList;

function skybox_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, skybox_itemList.length);
    carousel.add(i, skybox_getItemHTML(skybox_itemList[idx - 1]));
};

function skybox_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function skybox_getItemHTML(item)
{
    return '<div class="sb_story"><span class="sb_section">' + item.section + '&raquo; </span>' + '<a href="' + item.link + '">' + item.headline + '<\/a>' + getStamp(item.date) + '</div>';
};

jQuery(document).ready(function() {
	
	
	jQuery.getJSON("http://blogs.miscellanynews.com/skybox/stories.php?callback=?",
        function(data){
          skyboxHTML = "";
          
          
          for(i = 0; i < data.length; i++) {
          	story = data[i];
			story.headline = unescape(story.headline);
			story.link = unescape(story.link);
			story.section = unescape(story.section);
			
			skyboxHTML += skybox_getItemHTML(story);
			
          }
        
          $('#sb_stories').html(skyboxHTML);
          
          $('#sb_stories').cycle({ 
 				fx:    'scrollHorz', 
				speed:  500,
				timeout: 5500,
				next: '#sb_next',
				prev: '#sb_prev'
			 });
        
    });


   
});
