function Carousel(car){
    var scroller        = car;
    var scrollStep      = 259;
    var scrollInterval  = 700;
    var scrollLength    = scroller.find(".carousel-item").length;
    var canScroll       = true;
    var scrollCurItem   = null;

    if ( scrollLength < 2 )
        return;

    function sizeToNumber(sz){
        return sz == "auto" ? 0 : +sz.replace("px", "");
    }

    function getFirst()
    {
        var minLeft = 10000000;
        var minNode = null;

        scroller
            .find(".carousel-item")
            .each(function(){
                var left = sizeToNumber($(this).css("left"));
                if ( left < minLeft ){
                    minLeft = left;
                    minNode = $(this);
                }
        });

        return minNode;
    }
    function getLast()
    {
        var maxLeft = -10000000;
        var maxNode = null;

        scroller
            .find(".carousel-item")
            .each(function(){
                var left = sizeToNumber($(this).css("left"));
                if ( left > maxLeft ){
                    maxLeft = left;
                    maxNode = $(this);
                }
        });

        return maxNode;
    }
    function getNext(item)
    {                                    
        var itemLeft = sizeToNumber(item.css("left"));
        var nextNode = null;

        scroller
            .find(".carousel-item")
            .each(function(){
                var left = sizeToNumber($(this).css("left"));
                if ( left == itemLeft + scrollStep ){
                    nextNode = $(this);
                }
        });

        return nextNode;
    }
    function getPrev(item)
    {
        var itemLeft = sizeToNumber(item.css("left"));
        var prevNode = null;

        scroller
            .find(".carousel-item")
            .each(function(){
                var left = sizeToNumber($(this).css("left"));
                if ( left == itemLeft - scrollStep ){
                    prevNode = $(this);
                }
        });

        return prevNode;
    }
    function scrollCarousel(direction)
    {
        var firstItem = getFirst();
        var lastItem  = getLast();
        var curItem   = scrollCurItem;

        if ( direction == 1 && scrollCurItem.css("left") ==  firstItem.css("left") ) {
            var leftSize = sizeToNumber( lastItem.css("left") );

            lastItem.css("left", (leftSize - scrollLength * scrollStep) + "px" );
            scrollCurItem = lastItem;
        }
        else if ( direction == -1 && scrollCurItem.css("left") ==  lastItem.css("left") ) {
            var leftSize = sizeToNumber( firstItem.css("left") );

            firstItem.css("left", (leftSize + scrollLength * scrollStep) + "px" );
            scrollCurItem = firstItem;
        }
        else {
            if ( direction == 1 ){
                scrollCurItem = getPrev( scrollCurItem );
            }
            if ( direction == -1 ){
                scrollCurItem = getNext( scrollCurItem );
            }
        }

        scroller
            .find(".carousel-item")
            .each(function(){
                var elem = $(this).get(0);

                if ( elem != curItem.get(0) && elem != scrollCurItem.get(0) )
                    $(this).hide();
                else
                    $(this).show();
            });
     
        scroller
            .find(".carousel-item")
            .animate({
                left : (direction == 1 ? "+=" : "-=") + scrollStep
                }, scrollInterval, function() { canScroll = true; } );
    }

    try
    {
        var pos = 0;
        
        scroller
            .find(".carousel-item")
            .each(function(){
                $(this).css("left", pos + "px");
                pos += scrollStep;
            });

        scrollCurItem = scroller.find(".carousel-item:first");
    }
    catch(e)
    {
        alert(e);
    }
    
    scroller
        .find(".arrow-left")
        .click(function(){
            if ( !canScroll )
                return false;

            try{
                canScroll = false;

                scrollCarousel(-1);
            }
            catch(e){
            }
        });
    
    scroller
        .find(".arrow-right")
        .click(function(){
            if ( !canScroll )
                return false;

            try{
                canScroll = false;

                scrollCarousel(+1);
            }
            catch(e){
                alert(e);
            }
        });
}



$(document).ready(function(){
    $(".choosing-status").each(function(){
        var carousel = new Carousel($(this)); 
    });
    $('#status-form label').mouseover(function(){
    	$(this).addClass('hover');
    	$(this).mouseout(function(){
    		$(this).removeClass('hover');
    	});
    });
    $('#status-form label').click(function(){
    	$('#status-form label').removeClass('active');
    	$(this).addClass('active');
    	$('input', this).get(0).click();
    });
});
function popup(url, width, height, param)
{
	if(!width){width=400;}
	if(!height){height=200;}
	
	top=Math.ceil($(window).height()/2-Math.ceil(height/2));
	left=Math.ceil($(window).width()/2-Math.ceil(width/2));
	
	if (url.indexOf('?')!=-1)
	{
		param='&'+param;
	}
	else
	{
		param='?'+param;
	}
	
	window.open(url+param, 'popup', 'top='+top+', left='+left+', width='+width+', height='+height);
}
