jQuery(document).ready(function() {
    function carouselInit(carousel, state)
    {        
        //
        // CUSTOM BITS BY ROB
        //
        
        // Add a button for every item in the carousel           
        for(i=0;i<carousel.options.size;i++)
        {
            $("#carousel_timeline").append("<a id=\"button_" + (i + 1) + "\" href=\"#\">" + (i + 1) + "<\/a>");
        };
        
        // Bind a function to each of those buttons
        $('#carousel_timeline a').bind('click', function() {
            carousel.startAuto(0);
            carousel.scroll(jQuery.jcarousel.intval($(this).html()));
            return false;
        });
        
        // Center the timeline
        var timeline_width = 700;
        var timeline_button_width = 8;
        var timeline_button_margin = 20;
        var timeline_buttons_width = ((timeline_button_width + timeline_button_margin) * carousel.options.size) - timeline_button_margin;
        $("#carousel_timeline").css("padding-left",((timeline_width/2) - (timeline_buttons_width/2)) + "px");
        
    };
    
    function carouselTimelineUpdate(carousel, state, idx)
    {
        $("#carousel_timeline a").removeClass("selected");
        $("#carousel_timeline a#button_" + idx).addClass("selected");
    };
    
    var startIndex = $('.portfolio_thumbnail_selected').attr("id").substring(8, $('.portfolio_thumbnail_selected').attr("id").lastIndexOf("_"));
    //alert(startIndex);
               
    jQuery('#mycarousel').jcarousel({
        auto: 0,
        wrap: 'both',
        scroll: 1,
        start: jQuery.jcarousel.intval(startIndex),
        animation: 1000,
        easing: "easeInOutBack",
        initCallback: carouselInit,
        itemVisibleInCallback: {
            onAfterAnimation: carouselTimelineUpdate
        }
    });
    
    $('.portfolio_thumbnail a').bind('click', function(event) {
        //event.preventDefault();
        
        //$("object").h/ide();
        //alert("loading new project");
    });
    
    function absTop(elementId){	
        var obj = document.getElementById(elementId);
	    var offset = 0;
	    while(obj.offsetParent) {
		    offset += obj.offsetTop;
		    obj = obj.offsetParent;
	    }
	    return(offset);
    }
    
    window.scrollTo(0, absTop("center_col_wide") - 20);
    
});