(function($) {
    var _selectedIndex = -1;
    var _count;

    $.fn.casecarousel = function() {
        _count = $(this).children(".case-study").length;
        
        var index = 0;
        
        $(this).children(".case-study").each(function() {
            $(this).attr("id", "item-" + index);
            $(this).attr("index", index);
            $(this).click(function() {
                var newIndex = $(this).attr("index");
                if (newIndex != _selectedIndex)
					display(newIndex);
            });
            index++;
        });
        
        index = 0;
        
        $("#case-study-image").children().each(function() {
            $(this).attr("id", "image-" + index);
            index++;
        });
        
        index = 0;
        
        $("#case-study-detail").children().each(function() {
            $(this).attr("id", "detail-" + index);
            index++;
        });
        
        $("#case-study-previous").click(function() {
            var newIndex = ((_selectedIndex == 0) ? _count - 1 : parseInt(_selectedIndex) - 1);
			display(newIndex);
        });
        
        $("#case-study-next").click(function() {
            var newIndex = ((_selectedIndex == _count - 1) ? 0 : parseInt(_selectedIndex) + 1);
			display(newIndex);
        });
        
        var start = (($.jqURL.get("c") != null) ? parseInt($.jqURL.get("c")) : Math.floor(Math.random()*_count));
        
        layout(start);
        
        return this;
    };
    
    function layout(index)
    {
		_selectedIndex = index;
        
        $("#case-study-previous, #case-study-next").show();
        $("#item-" + ((index == 0) ? _count - 1 : index - 1)).css({ top : 0, left : 10 }).show();
        $("#item-" + index).css({ top : 0, left : 179 }).show();
        $("#item-" + ((index == _count - 1) ? 0 : index + 1)).css({ top : 0, left : 348 }).show();
        $("#image-" + index).show();
		detail(index);
    }
    
    function display(index)
    {
        var first = ((_selectedIndex == 0) ? _count - 1 : parseInt(_selectedIndex) - 1);
        var third = ((_selectedIndex == _count - 1) ? 0 : parseInt(_selectedIndex) + 1);
        
        if (index == first)
        {
            $("#item-" + first).animate({ top : 0, left : 179 });
            $("#item-" + _selectedIndex).animate({ top : 0, left : 348 });
            $("#item-" + ((index == 0) ? _count - 1 : index - 1)).css({ top : 0, left : -169 }).animate({ top : 0, left : 10, width : 149, height : 36 });
            $("#item-" + third).animate({ top : 0, left : 517 });
            $("#image-" + first).css({ top : 0, left : -826 }).animate({ top : 0, left : 0, width : 826, height : 349 });
            $("#image-" + _selectedIndex).animate({ top : 0, left : 826 });
        }
        else
        {
            $("#item-" + third).animate({ top : 0, left : 179 });
            $("#item-" + _selectedIndex).animate({ top : 0, left : 10 });
            $("#item-" + ((index == _count - 1) ? 0 : parseInt(index) + 1)).css({ top : 0, left : 517 }).animate({ top : 0, left : 348, width : 149, height : 36 });
            $("#item-" + first).animate({ top : 0, left : -169 });
            $("#image-" + third).css({ top : 0, left : 826 }).animate({ top : 0, left : 0, width : 826, height : 349 });
            $("#image-" + _selectedIndex).animate({ top : 0, left : -826 });
        }
        
		_selectedIndex = index;
		detail(index);
    }
    
    function detail(index)
    {
        $("#case-study-title").html($("#item-" + index + " img").attr("alt"));
        //$("#case-study-image").attr("src", "img/" + $("#item-" + index + " img").attr("rel"));
        //$("#case-study-image").attr("alt", $("#item-" + index + " img").attr("alt"));
        $("#case-study-detail").children().hide();
        $("#detail-" + index).show();
    }
})(jQuery);