$(document).ready( function () {
    CAROUSEL_TIME = 5000;
    var dhc = $("#dhCarousel");
    
    //IE8 has a problem with this, apparently
    //$("#dhCarousel img:first").clone().appendTo("#dhCarousel img:last");

    $("#dhCarousel").click(function () {
        href = $("#dhCarousel img:eq("+ $("#dhCarousel").data("imgCursor") +")").attr("href");
        if (href != undefined && href != "") {
            document.location = href;
        }
    });
    
    dhc.css("overflow","hidden");
    var width = dhc.width(); var height = dhc.height();
    dhc.data("imgWidth",width);
    dhc.data("snapBackAfter",false);
    
    
    var ih = dhc.html();
    var imageCount = $("img",dhc).length;

    dhc.data("imgCount",imageCount);
    dhc.data("imgCursor",0);
    var innerwidth = String((imageCount + 1) * width);
    dhc.html("<div id='dhCarouselHandle' style='width: "+innerwidth+"px; margin-left: 0px;'>" + ih + "<div style='clear:both;'></div></div>");
    $("img",dhc).css("float","left");
    $("img",dhc).show();
    
   var city = $("#dhCarousel img:eq(0)").attr("city");
   var price = $("#dhCarousel img:eq(0)").attr("price");
   $("#infobox").html(city + " - " + price).fadeIn(500);

    
    if (imageCount > 1) {
        setTimeout("dhCarouselInterval()",CAROUSEL_TIME);
    }
});

function dhCarouselInterval() {
        var cursor = $("#dhCarousel").data("imgCursor");
        var count = $("#dhCarousel").data("imgCount");
        var width = $("#dhCarousel").data("imgWidth");
        
        
        //implementation - specific
        
        $("#infobox").fadeOut(500);
        
        cursor++;
        if (cursor + 1 == count) $("#dhCarousel").data("snapBackAfter",true);
        if (cursor == count) cursor = 1;
        $("#dhCarousel").data("imgCursor",cursor);
        var newLeft = cursor * width * -1;
        $("#dhCarouselHandle").animate({"marginLeft":newLeft},1000,"easeInOutCirc", function () {
           setTimeout("dhCarouselInterval()",CAROUSEL_TIME);    
           var c = $("#dhCarousel").data("imgCursor");
           var city = $("#dhCarousel img:eq("+ c +")").attr("city");
           var price = $("#dhCarousel img:eq("+ c +")").attr("price");
           $("#infobox").html(city + " - " + price).fadeIn(500);

           if ($("#dhCarousel").data("snapBackAfter")) {
               $("#dhCarouselHandle").css("marginLeft",0);
               $("#dhCarousel").data("snapBackAfter",false);
           }
        });        
}

