(function($) {
    var _selectedIndex = -1;
    var _count;

    $.fn.irdscarousel = function() {
        _count = $(this).children(".irdscarousel-image").length;
        
        var index = 0;
        
        $(this).children(".irdscarousel-image").each(function() {
            $(this).attr("id", "item-" + index);
            $(this).attr("index", index);
            $(this).click(function() {
                var newIndex = $(this).attr("index");
                if (newIndex == _selectedIndex)
			        //display(newIndex);
				//else
				    window.location = $(this).attr("ref");
            });

            index++;
        });
        
        $("#irdscarousel-previous").click(function() {
            var newIndex = _selectedIndex - 1;
            if (newIndex < 0)
                display(_count - 1);
            else
                display(newIndex);
        });
        
        $("#irdscarousel-next").click(function() {
            var newIndex = _selectedIndex + 1;
            if (newIndex > (_count - 1))
                display(0);
            else
                display(newIndex);
        });
        
        layout(Math.floor(Math.random()*_count));
        
        return this;
    };
    
    function layout(index)
    {
		_selectedIndex = index;
        
        $("#item-" + ((index == 0) ? _count - 1 : index - 1)).css({ top : 180, left : 152 }).animate({ top : 95, left : 0, width : 304, height : 171 });
        $("#item-" + index).css({ top : 180, left : 470, zIndex : 3 }).animate({ top : 0, left : 170, width : 640, height : 360 });
        $("#item-" + ((index == _count - 1) ? 0 : index + 1)).css({ top : 180, left : 808 }).animate({ top : 95, left : 676, width : 304, height : 171 });
        
        showControls();
    }
    
    function display(index)
    {
        var first = ((_selectedIndex == 0) ? _count - 1 : parseInt(_selectedIndex) - 1);
        var third = ((_selectedIndex == _count - 1) ? 0 : parseInt(_selectedIndex) + 1);
        
        $("#irdscarousel-previous, #irdscarousel-next").hide();
        
        if (index == first)
        {
            $("#item-" + first).css({ zIndex : 3 }).animate({ top : 0, left : 170, width : 640, height : 360 });
            $("#item-" + _selectedIndex).css({ zIndex : 1 }).animate({ top : 95, left : 676, width : 304, height : 171 });
            $("#item-" + ((index == 0) ? _count - 1 : index - 1)).css({ top : 180, left : 152, zIndex : 1 }).animate({ top : 95, left : 0, width : 304, height : 171 });
            $("#item-" + third).css({ zIndex : 0 }).animate({ top : 180, left : 808, width : 0, height : 0 });
        }
        else
        {
            $("#item-" + third).css({ zIndex : 3 }).animate({ top : 0, left : 170, width : 640, height : 360 });
            $("#item-" + _selectedIndex).css({ zIndex : 1 }).animate({ top : 95, left : 0, width : 304, height : 171 });
            $("#item-" + ((index == _count - 1) ? 0 : parseInt(index) + 1)).css({ top : 180, left : 808, zIndex : 1 }).animate({ top : 95, left : 676, width : 304, height : 171 });
            $("#item-" + first).css({ zIndex : 0 }).animate({ top : 180, left : 152, width : 0, height : 0 });
        }
        
        showControls();
		_selectedIndex = index;        
    }
    
    function showControls()
    {
        var time = 500;
        
        if($.browser.msie)
            time = 1000;
        
        setTimeout(function() {
            if($.browser.msie)
                $("#irdscarousel-previous, #irdscarousel-next").show();
            else
                $("#irdscarousel-previous, #irdscarousel-next").fadeIn();
        }, time);
    }
})(jQuery);