// carousel...
function carousel(vp, dir, width, n) {
    var left    = new String(vp.children('div.c').css('left'));
    var curpos  = parseInt(left.substr(0, left.length - 2));
    var nextpos = 0;
    var ind     = 0;
    var first   = false ;
    var last    = false ;
    
	if (dir == 'next') { 
	    nextpos = curpos - width;
	} else { 
	    nextpos = curpos + width;
    }
    
    ind = Math.floor(nextpos / width * -1);
    
    /* rotate carousel */
	if (ind == n) {  //we're at the last item trying to go to the next    
        li = vp.children('div.c').children('ul').children('li:eq(0)');
        ul = vp.children('div.c').children('ul') ;
        li.clone().appendTo(ul);
        ul.children('li:eq('+n+')').addClass('remove');
        last = true ;
    } else if(ind == -1) { //we're at the first item trying to go to the previous
         li = vp.children('div.c').children('ul').children('li:eq('+(n-1)+')');
         ul = vp.children('div.c').children('ul') ;
         li.clone().prependTo(ul);
         vp.children('div.c').css('left',-width);
         ind++ ;
         ul.children('li:eq(0)').addClass('remove');
         first = true ;
   }
   /* end rotate */
   
	vp.children('div.c').animate({
		left: -width * ind
	}, 'fast', function() {
	    /* more rotation */
		if (last) {  //we're at the last item trying to go to the next
		     vp.children('div.c').css('left',0); //after anim, reset position to the first
             $('.remove').remove(); //remove extra item we added above
             first = false ;
		} else if(first) { //we're at the first item trying to go to the previous
            vp.children('div.c').css('left',(-n+1)*width);
            $('.remove').remove();
            last = false ;
		}
		/* end rotation */
	});
}

$(document).ready(function() {
    //initialize carousels
    $('.carousel').each(function(intIndex) {
        var viewport  = $(this).children('div.v');
        var container = $(this).children('div.v').children('div.c');
        var width     = container.children('ul').children('li:eq(0)').width();
        var first     = container.children('ul').children('li:eq(0)');
        var n         = container.children('ul').children('li').size()+1;

        if($(this).parent('div').attr('id') == 'videot') {
            width += 2 ;
        }
	    if (n <= 1) {
	        $(this).children('a.prev, a.next').hide();
	    }
	    
	    container.css('width', (n+1) * width); //add 1 to accommodate first and last items on carousel flip
    });
    
	
	$('div.carousel a.prev, div.carousel a.next').click(function() {
	    
	    if($('.c:animated').size() > 0) return false ; // prevent additional clicks
	            
	    var vp  = $(this).parent('div').children('div.v');
	    var w   = vp.children('div.c').children('ul').children('li:eq(0)').width();
	    if($(this).parent().parent().attr('id') == 'videot') {
	        w += 2 ;
	    }
	    var n   = vp.children('div.c').children('ul').children('li').size();
	    var dir = $(this).attr('class');
		carousel(vp, dir, w, n);

		return false;
	});
});
// ...carousel