/*
 * Contrary to appearances, this kind of makes sense, just bear with me for a sec.
 *
 * This is the code we use to breathe life into the otherwise dull and static news thingy.
 * First, we set up whatever we have to set up,
 * Second, we declare newsScrollTo(), which does the necessary calculations and shifts divs,
 * accordingly, centering the view on the the box number n 
 * Third, a number of helper functions that call newsScrollTo() when we click on the 
 * interactive parts of the news thingy (that is, the clickable balls and the
 * arrows)
 * Fourth, juggle_boxes automatically juggles the boxes. 
 * 
 */

$(document).ready(function()
                  {

                  window.newsScrollerWhere = 0;
                  window.newsScrollerWidth = 350;
                  var containerz =
                  $.makeArray($("#newscontainer div.newscontent"));
                  window.newsButtonz = new Array(); for (i = 0;
                                                         i <
                                                         containerz.length;
                                                         i++) {

                  var ball_thingy = jQuery('<img/>', {
      			src: '/media/static/img/ball_shy_and_smaller.png', 
			style: 'margin:5px 5px 5px 5px; ', nScrollTo:i,}
                  ).click(function() {
                          newsScrollTo($(this).attr("nScrollTo"));
                          window.theDeadCantJuggle = 1; return false;}
                  ).appendTo("#selektah");
                  window.
                  newsButtonz[(window.newsButtonz.length ? window.
                               newsButtonz.length : window.newsButtonz.
                               length)] = ball_thingy;}

                  $(window.newsButtonz[0]).attr("src",
                                                '/media/static/img/ball_ballsy_but_smaller.png');
                  newsScrollTo = function(myTarget) {

                  containerz =
                  $.makeArray($("#newscontainer div.newscontent"));
                  if (myTarget < 0) myTarget = 0;
                  if (myTarget > containerz.length - 1) myTarget = 0;
                  // I feel slightly too lazy to think of funny a a = b % c expression right 
                  // now. 
                  $(window.newsButtonz).each(function() {
                                             $(this).attr("src",
                                                          '/media/static/img/ball_shy_and_smaller.png')}
                  );
                  $(window.newsButtonz[myTarget]).attr("src",
                                                       '/media/static/img/ball_ballsy_but_smaller.png');
                  $("#newsbarrack").attr("position", "relative");
                  $("#newsbarrack").animate( {
                                             "left": -myTarget * (16+$("#newscontainer div.newscontent").first().width()) - 399}
                  )

                  window.newsScrollerWhere = myTarget;}

                  function juggle_boxes() {
                  {
                  if (!window.theDeadCantJuggle)
                  newsScrollTo(++window.newsScrollerWhere)
                  // i = i%quickNDirtySlides.length;
                     // I seem to recall this was somehow important.
                  setTimeout(arguments.callee, 5000);}
                  }

                  setTimeout(juggle_boxes, 5000);

                        // Yes, sweetie. Hardcoded timeout. 
                        // Do you seriously think that we'll EVER have to change this? 

                  $("#clickleft").click(function() {

                                        newsScrollTo(++window.
                                                     newsScrollerWhere)
                                        window.theDeadCantJuggle = 1;}
                  ); $("#clickright").click(function() {

                                            newsScrollTo(--window.
                                                         newsScrollerWhere)
                                            window.theDeadCantJuggle = 1;}
                  );}

)

