var currentMenu = 0;

$(document).ready( init );

window.onload = function() { $(".lavaLamp li:first").mouseover()   };

function init() {

    $("div.menu ul").lavaLamp({ fx: "backout", speed: 700 })
    //$(".lavaLamp li").click( scroll );
    //$.serialScroll({target: '#sections', items: 'td:has(div.menuContent)', axis:'xy', duration:500, force:true, lazy: true});    
    getImageLetters();
    //getTweet();
    //new rssdisplayer("blog", "http://www.brentnichols.com/wordpress/?feed=rss2", 1, "date, description")
}

function scroll( navMenu ) {
 
    var currentIndex = $("#menuContents td:has(div.menuContent)").index( $("#sections td[seq=" + $(this).attr("seq") + "]") );

    var nextTableCell = $("#menuContents td[seq=" + $(this).attr("seq") + "]").html();
    $("#menuContents td:has(div.menuContent)").eq(currentIndex).attr("seq","").html("<div class='menuContent'>&nbsp;</div>");
    $("#menuContents td:has(div.menuContent)").eq(currentMenu).after( "<td valign='top' seq='" + $(this).attr("seq") + "'>" + nextTableCell + "</td>");
    currentMenu++;

    switch( $(this).attr("seq") )
    {
        case "2":
            if( $.trim( $("#news").html() ) == "" ) {
                new rssdisplayer("news", "http://www.google.com/reader/public/atom/user%2F03618550387486354249%2Fstate%2Fcom.google%2Fbroadcast", 5, "date, description")
            }
            scrollNews();
        case "3":
            if( $.trim( $("#menuContents td[seq=3] div.menuContent").html() ) == "" )
                startPhotos();
            break;
        case "4":
            $("#letterList").accordion( {header: 'h4', active: false, collapsible: true, animated: 'bounceslide'} );
        case "5":
            $.serialScroll({target: '#poolWinners', items: 'div.poolYear', duration: 300, prev: '#poolLeft', next: '#poolRight'});
            if( $("#oscarCountdown").html() == "" )
                $("#oscarCountdown").countdown({until: new Date(2011, 2 - 1, 27, 18, 0, 0)});
            break;
    }
        
    $("#sections").trigger( 'goto', [currentMenu] );
   
}

function getTweet() {

    $.getScript("http://twitter.com/statuses/user_timeline/brentnichols.json?callback=showTweet&count=1");
}


function scrollNews() {
    $.serialScroll({target: '#news', items: 'div.newsBlurb', duration: 300, prev: '#newsLeft', next: '#newsRight'});
}


function showTweet( tweetJSON ) {
  var twitters = tweetJSON;
  var statusHTML = "";
  var username = "";
  var created = "";
  var picture = "";
  for (var i=0; i < twitters.length; i++){
    username = twitters[i].user.screen_name;
    picture = twitters[i].user.profile_image_url;
    statusHTML = twitters[i].text;
    if( statusHTML.substring(0,1) == "-" )
        statusHTML = $.trim( statusHTML.substring(1) );
    statusHTML = statusHTML;
    created = "<a href='http://twitter.com/brentnichols' target='_blank'>" + relative_time( twitters[i].created_at ) + "</a>";
  }
  if( statusHTML != "" )
	  $("#tweet").html( statusHTML + "<br/>" + created );
//  $("#tweetPicture").attr("src", picture );

}


//Twitter's function to relate the time since the last tweet
function relative_time(C)
{
    var B=C.split(" ");C=B[1]+" "+B[2]+", "+B[5]+" "+B[3]
    var A=Date.parse(C)
    var D=(arguments.length>1)?arguments[1]:new Date()
    var E=parseInt((D.getTime()-A)/1000)
    E=E+(D.getTimezoneOffset()*60)
    if(E<60){return"less than a minute ago"}
    else{
        if(E<120){return"about a minute ago"}
        else{
            if(E<(60*60)){return(parseInt(E/60)).toString()+" minutes ago"}
            else{
                if(E<(120*60)){return"about an hour ago"}
                else{
                    if(E<(24*60*60)){return"about "+(parseInt(E/3600)).toString()+" hours ago"}
                    else{
                        if(E<(48*60*60)){return"1 day ago"}
                        else{return(parseInt(E/86400)).toString()+" days ago"}
                    }
                }
            }
        }
    }
}


function rssdisplayer(divid, url, feedlimit, showoptions){
    this.showoptions=showoptions || "" //get string of options to show ("date" and/or "description")
    var feedpointer=new google.feeds.Feed(url) //create new instance of Google Ajax Feed API
    feedpointer.setNumEntries(feedlimit) //set number of items to display
    //document.write('<div id="'+divid+'">Loading feed...</div>')
    this.feedcontainer=document.getElementById(divid)
    var displayer=this
    feedpointer.load(function(r){displayer.formatoutput(r)}) //call Feed.load() to retrieve and output RSS feed
}


rssdisplayer.prototype.formatdate=function(datestr){
    var itemdate=new Date(datestr)
    return "<span style='color:gray; font-size: 90%'>"+itemdate.toLocaleString()+"</span>"
}


rssdisplayer.prototype.formatoutput=function(result){
    if (!result.error){ //if RSS feed successfully fetched
    var thefeeds=result.feed.entries //get all feed entries as a JSON array
    var rssoutput="<table cellspacing='0' cellpadding='0' border='0'><tr>"
    for (var i=0; i<thefeeds.length; i++){ //loop through entries
    var itemtitle="<h3><a href=\"" + thefeeds[i].link + "\" target='_blank'>" + thefeeds[i].title + "</a></h3>"
    var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : ""
    var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
    rssoutput+="<td valign='top'><div class='newsBlurb'>" + itemtitle + " " + itemdate + itemdescription + "</div></td>"
    }
    rssoutput+="</tr></table>"
    this.feedcontainer.innerHTML=rssoutput
    scrollNews();
    }
    else //else, output error
    alert("Error fetching feeds: "+result.error.message)
}

//USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions")
//new rssdisplayer("adiv", "http://www.cssdrive.com/index.php/news/rss_2.0/", 5, "date, description")
