var carousel = new Object();

carousel.moveLeft = function() {
	
	if ($('#facebox').length <= 0) {
		carousel.newLiLeft = (carousel.marginLeft-carousel.liWidth);
		carousel.marginLeft = carousel.newLiLeft;
		carousel.negTotalWidth = carousel.totalWidth * -1;
		if (carousel.newLiLeft > carousel.negTotalWidth) {
			carousel.thumbNail();
			carousel.Container.animate({ 'marginLeft':carousel.newLiLeft },'slow');
		} else {
			carousel.marginLeft = 0;
			carousel.thumbNail();
			carousel.Container.animate({ 'marginLeft': 0 },'slow');
		}
	} 
}

$(document).bind('close.facebox', function() { $('#facebox').remove(); carousel.setInterval(); });
	

carousel.moveRight = function() {
	carousel.newLiLeft = (carousel.marginLeft+carousel.liWidth);
	carousel.marginLeft = carousel.newLiLeft;
	carousel.counter = carousel.counter-2;
	if (carousel.newLiLeft < 0) {
		carousel.thumbNail();
		carousel.Container.animate({ 'marginLeft':carousel.newLiLeft }, 'slow');
	} else {
		carousel.marginLeft = 0;
		carousel.thumbNail();
		carousel.Container.animate({ 'marginLeft': 0 },'slow');
	}
}

carousel.thumbNail = function() {  
    carousel.counter = (carousel.counter >= carousel.liCount)?carousel.counter=1:carousel.counter+1;
    id = 'carousel-' + carousel.counter;
    $('.carousel-thumbs').children('li').removeClass('highlight');
    $('.' + id).addClass('highlight');
}


carousel.pause = function() {
    if (carousel.paused) {
	  carousel.paused = false;
	  $('.pause').removeClass('play');
    } else {
	  carousel.paused = true;
	  carousel.stop();
	  $('.pause').addClass('play');
    }
}


carousel.slideToThumb = function(x) {
    xClass = $(x).attr('class');
    if (!($(x).hasClass('pause'))) {
	  carousel.counter 	= xClass.replace(/[^0-9]/g, '');
	  carousel.newLiLeft 	= (carousel.liWidth * carousel.counter)-carousel.liWidth;
	  carousel.newLiLeft 	= carousel.newLiLeft * -1;
	  carousel.marginLeft 	= carousel.newLiLeft;
	  carousel.counter--;
	  carousel.thumbNail();
	  carousel.Container.animate({ 'marginLeft':carousel.newLiLeft }, 'slow');
    }
}

carousel.getLeft = function() {
	Left = parseInt(carousel.firstLi.css('margin-left'));
	return Left;
}

carousel.getWidth = function() {
	Width = parseInt(carousel.LIs[0].scrollWidth);
	return Width;
}

carousel.buttons = function() {
    $('.carousel-next').click(function() {
	  carousel.moveLeft();
    })
    $('.carousel-previous').click(function() {
	  carousel.moveRight();
    })
    $('.pause').click(function() {
	  carousel.pause();
    })
}


carousel.setInterval = function() {
    carousel.rotation = window.setInterval("carousel.moveLeft()", carousel.interval);
}


carousel.stop = function() {
	window.clearInterval(carousel.rotation);
}


carousel.start = function(el,options) {
    carousel.options	= options;
    carousel.Container 	= $(el);
    carousel.counter	= 0;
    carousel.interval 	= (options['interval'])?options['interval']:13000;    
    carousel.liCount 	= carousel.Container.children('li').length;
    
    if (carousel.liCount > 1) {
	  carousel.LIs 		= carousel.Container.children('li');
	  carousel.firstLi 	= $(carousel.LIs[0]);
	  carousel.marginLeft 	= carousel.getLeft();
	  carousel.liWidth 	= carousel.getWidth();
	  carousel.totalWidth 	= carousel.liWidth * carousel.liCount;
	  
	  carousel.thumbNail();
	  carousel.buttons();
	  
	  $('.carousel').mouseover(function() {
		carousel.stop();
	  });
	  $('.carousel').mouseout(function() {
		if (!carousel.paused) {
		   carousel.setInterval();
		}
	  });
	  $('.carousel-thumbs').children('li').click(function() {
		carousel.slideToThumb(this);
	  });
	  
	  carousel.setInterval();
    }
}

carousel.stop = function() {
	window.clearInterval(carousel.rotation);
}

carousel.buttons = function() {
    $('.carousel-next').click(function() {
	  carousel.moveLeft();
    })
    $('.carousel-previous').click(function() {
	  carousel.moveRight();
    })
    $('.pause').click(function() {
	  carousel.pause();
    })
}
