
/*
function mycarousel_initCallback(carousel) {
    $('#mycarousel-next').bind('click',function(){
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click',function(){
        carousel.prev();
        return false;
    });
}
*/

$(document).ready(function() {    
	$('#mycarousel-prev').mouseover(function(){
		$(this).css({'background-image':'url(/images/Frontoffice/nav/left-cron-on.png)'})
	}).mouseleave(function(){
		$(this).css({'background-image':'url(/images/Frontoffice/nav/left-cron.png)'})
	});
	
	$('#mycarousel-next').mouseover(function(){
		$(this).css({'background-image':'url(/images/Frontoffice/nav/right-cron-on.png)'})
	}).mouseleave(function(){
		$(this).css({'background-image':'url(/images/Frontoffice/nav/right-cron.png)'})
	});
	
	/*	
	$('#jcarousel').jcarousel({
        visible: 7,
		scroll: 1,
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
    });
    */
	
	// setTimeout('startCarousel();', 1000);
	
	startCarousel();
});	

function startCarousel () {
	var carouselContainer = $('.carousel:eq(0)');
	if(carouselContainer.is('div')) {
		var carousel = new OM.carousel(carouselContainer, {});
	}	
}

var OM = OM || {};

OM.carousel = function(container, options)
{
	this.options = {visible: 7, easing: 'easeOutExpo', duration: 525};
	$.extend(this.options, options);
	
	this.container = $(container);
	this.wrapper = $('.carousel_wrapper', this.container);
	this.scroller = $('.carousel_container', this.wrapper);
	this.items = $('li', this.scroller);
	this.btnLeft = $('.left', this.container);
	this.btnRight = $('.right', this.container);
	this.current = 0, this.processing = false;
	return this.init();
};

$.extend(OM.carousel.prototype, {
	
	init: function()
	{
		var opt = this.options;
		
		opt.itemsWidth = parseInt(this.wrapper.width() / opt.visible, 10);
		var width = opt.itemsWidth * this.items.length;
		if(width)
		{
			this.scroller.width(width);
			$('ul', this.scroller).width(width);
			opt.lastItem = this.items.length - opt.visible;
		}
		
		var that = this;
		this.btnLeft.bind('click', function(e){
			e.preventDefault();
			if(!that.processing && that.current > 0)
			{
				that.movePrev();
			}
			return false;
		});
		
		this.btnRight.bind('click', function(e){
			e.preventDefault();
			if(!that.processing && that.current < opt.lastItem)
			{
				that.moveNext();
			}
			return false;
		});
		
		$(this).bind('moveNextEnded', function(e){
			that.current++;
			that.processing = false;
			that.nextCallback();
		});
		
		$(this).bind('movePrevEnded', function(e){
			that.current--;
			that.processing = false;
			that.prevCallback();
		});
		
		return this.bindItems().move();
	},
	
	moveNext: function()
	{
		this.processing = true;
		var opt = this.options, left = (this.current+1) * (opt.itemsWidth*-1), that = this;
		this.scroller.animate({left: left}, {duration: opt.duration, easing: opt.easing, complete: function(){
			$(that).trigger('moveNextEnded');
		}});
		
		return this;
	},
	
	movePrev: function()
	{
		this.processing = true;
		var opt = this.options, left = (this.current-1) * (opt.itemsWidth*-1), that = this;
		this.scroller.animate({left: left}, {duration: opt.duration, easing: opt.easing, complete: function(){
			$(that).trigger('movePrevEnded');
		}});
		
		return this;
	},
	
	move: function()
	{
		if(!this.current)
		{
			var opt = this.options, limit = this.items.length, forced = false;
			for(var i = limit; i >= opt.visible ; i--)
			{
				var current = $(this.items[i]);
				if(current.hasClass('on'))
				{
					this.current = i - opt.visible;
					forced = true;
					break;
				}
			}
		}
		
		if(!this.current && !forced) return this.prevCallback();
		else
		{
			return this.moveNext();
		}
	}, 
	
	nextCallback: function()
	{
		var visibility = 'visible', opt = this.options;
		if(this.current == opt.lastItem)
		{
			visibility = 'hidden';
		}
		this.btnRight.css({visibility: visibility});
		this.btnLeft.css({visibility: 'visible'});
			
		return this;
	},
	
	prevCallback: function()
	{
		var visibility = 'visible', opt = this.options;
		if(!this.current)
		{
			visibility = 'hidden';
		}
		this.btnLeft.css({visibility: visibility});
		this.btnRight.css({visibility: 'visible'});
		
		return this;
	},
	
	bindItems: function()
	{
		var items = this.items, limit = this.items.length;
		
		for(var i = 0; i < limit; i++)
		{	
			var current = items[i];
			$(current).bind('click', function(e){
				e.preventDefault();
				var target = $('a:first', this);
				if(target.is('a'))
				{
					var url = target.attr('href');
					if(url.length)
					{
						window.location.href = url;
					}
				}
				return false;
			});
		}
		
		return this;
	}
});
