function loadItemHandler(carousel, start, last, available)
{
    if (available) {
        // Trigger loaded
        carousel.loaded();
        return;
    }

    var cr = carousel;

    jQuery.get("dynamic_ajax_carousel.txt", function(data) {
        appendItemCallback(cr, start, last, data);
    });
};

function appendItemCallback(carousel, start, last, data)
{
    var items = data.split("|");

    for (i = start; i <= last; i++) {
        if (items[i-1] == undefined) {
            break;
        }

        var item = carousel.add(i, getItemHTML(items[i-1]));

        item.each(function() {
            // Urgh...ThickBox should provide a function for that
            jQuery("a.thickbox", this).bind("click", function() {
                var t = this.title || this.name || null;
                var g = this.rel || false;
                TB_show(t,this.href,g);
                this.blur();
                return false;
            });
        });
    }

    // Trigger loaded
    carousel.loaded();
};

function getItemHTML(data)
{
    var split = data.split(";");
    var url   = jQuery.trim(split[0]);
    var title = jQuery.trim(split[1]);
		var url_l = jQuery.trim(split[2]);
    var url_m = url.replace(/_s.jpg/g, '_m.jpg');
    return '<a href="' + url_l + '" title="' + title + '" target="_blank"><img src="' + url + '" width="' + 90 + '" height="' + 125 + '" alt="' + title + '" /></a>';
};

// Next-Button handling...
var nextOver = function() {
    jQuery(this).attr("src", "images/next-over.gif");
};

var nextOut = function() {
    jQuery(this).attr("src", "images/next.gif");
};

var nextDown = function() {
    jQuery(this).attr("src", "images/next-down.gif");
};

function nextButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "images/next.gif")
                      .bind("mouseover", nextOver)
                      .bind("mouseout", nextOut)
                      .bind("mousedown", nextDown);
    } else {
        jQuery(button).attr("src", "images/next-disabled.gif")
                      .unbind("unmouseover", nextOver)
                      .unbind("unmouseout", nextOut)
                      .unbind("unmousedown", nextDown);
    }
}

// Prev-Button handling
var prevOver = function() {
    jQuery(this).attr("src", "images/prev-over.gif");
};

var prevOut = function() {
    jQuery(this).attr("src", "images/prev.gif");
};

var prevDown = function() {
    jQuery(this).attr("src", "images/prev-down.gif");
};

function prevButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "images/prev.gif")
                      .bind("mouseover", prevOver)
                      .bind("mouseout", prevOut)
                      .bind("mousedown", prevDown);
    } else {
        jQuery(button).attr("src", "images/prev-disabled.gif")
                      .unbind("unmouseover", prevOver)
                      .unbind("unmouseout", prevOut)
                      .unbind("unmousedown", prevDown);
    }
}

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};  

//###################################
// On Load Function...
//###################################
jQuery(document).ready(function() {
    /**
     * We show a simple loading indicator
     * using the jQuery ajax events
     */
    jQuery().ajaxStart(function() {
        jQuery(".loading").show();
    });

    jQuery().ajaxStop(function() {
        jQuery(".loading").hide();
    });

    jQuery("#mycarousel").jcarousel({
        itemVisible: 1,
        itemScroll: 1,
        autoScroll: 4,
        wrap: true,
        loadItemHandler: loadItemHandler,
        nextButtonStateHandler: nextButtonStateHandler,
        prevButtonStateHandler: prevButtonStateHandler
    });
		
    $("#nav-one li").hover(
      function(){ $("ul", this).slideDown("normal"); },
      function() { }
    );
    if (document.all) {
      $("#nav-one li").hoverClass ("sfHover");
    }

	//open any anchor with a rel="" in a new window
	$("a[@rel]").click(function() {
		newWindow = window.open(this, "newWin", "toolbar=yes,location=yes,scrollbars=yes,resizable=yes,width=815,height=650");
		newWindow.focus(); //puts window on top of all others
		return false; //stop link from working normally
	});
	
	//put focus on PowerSearch text field
	$("#search_field").focus();
	
	//round the bottom edges of the eBooks carousel
	$("#mycarousel").corner("10px");

});
