// JavaScript Document
var defilStop = false;
var idNews,idPrev,idNext;
var browser;
var carousel;

var keepPosi = false;

$(document).ready(function(){
	$("#news").jcarousel({
		animation: 1000,
		auto: 0,
		easing: 'easeOutBack',
		initCallback: mycarousel_initCallback,
		itemLastOutCallback:  mycarousel_itemLastOutCallback,
		itemVisibleInCallback: {
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
        },
		 itemVisibleOutCallback: {
            onAfterAnimation:  mycarousel_itemVisibleOutCallbackAfterAnimation
        },
		scroll: 3,
		wrap:'last',
		visible: 3
	});
	
	$(".miniNews").click(function(){
		var $miniNews = $(this);
		var $toolTip = $('#toolTip');
		
		idNews = $miniNews.attr('id');
		// On va chercher la position de l'element cliqué dans la page
		var offset = $miniNews.offset();
		var top = offset.top;
		var left = offset.left;
		
		$("#content").html("");
		if(keepPosi == false){
			$toolTip
				.css({
				'top':top - $toolTip.height(),
				'left':left - ($(this).width()/4),
				'opacity':0
				})
				.show()
				.animate({
					opacity: 1,
					'top': top - $toolTip.height() - 20
				}, 500,"easeOutBounce", function() {
					getNewsContent();
				});
		}
		else getNewsContent();
	})
	
	$('.jcarousel-next, .jcarousel-prev').click(function(){
		$('#toolTip').fadeOut();
	})
	
	$("#close").click(function(){
		$('#toolTip').fadeOut();
	})
	
	$('.newsPrevNext').live('click',function(){
		var offset = $("#"+$(this).attr('idNews')).offset();	
		if(parseInt(offset.left)>950){
			var carousel =$("#news").data('jcarousel');
			carousel.next();
			keepPosi = true;
			
		}
		else if(parseInt(offset.left)<170){
			var carousel = $("#news").data('jcarousel');
			carousel.prev();
			keepPosi = true;
		}
 
		$("#"+$(this).attr('idNews')).click();
	})
		
})

function mycarousel_initCallback(carousel) {
	var container = carousel.container.parent();
	$(container).click(function(){
		carousel.stopAuto();	
		defilStop = true;
	})
	
	$(container).mouseenter(function() {
		carousel.stopAuto();
		
	}).mouseleave(function() {
		if(!defilStop)
			carousel.startAuto();
	});
}

function mycarousel_itemLastOutCallback(carousel, item, idx, state) {
	// alert(state);
    //display('Item #' + idx + ' is no longer the last item');
};

function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    // alert('Item #' + idx + ' is now visible');
	
};

function mycarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
    // console.log('Item #' + idx + ' is no longer visible');
	
};



function getNewsContent(){	
	if($('#load').is(':hidden'))
		$('#load').show();
	
	var tabPrevNext  = getPrevNext(idNews);
	var dataSent = 'i='+idNews+"&p="+tabPrevNext["prevId"]+"&n="+tabPrevNext["nextId"];
	
	$.ajax({
		type:'GET',
		url:'news/ajax_news.php',
		data:dataSent,
		cache:false,
		success:function(msg){
			keepPosi = false;
			$('#load').hide();
			$("#content").html(msg);
			$("#close").fadeIn();					
		},
		error:function(){
			alert('Wait wait wait ?.... What ?!');
		}
	})	
}

function getPrevNext(idNews){

	var prev = $("#"+idNews).prev('.miniNews');
	var prevId = prev.attr('id');	

	var next = $("#"+idNews).next('.miniNews');
	var nextId = next.attr('id');

	var tabPrevNext = new Array();	
	tabPrevNext["prevId"] = prevId;
	tabPrevNext["nextId"] = nextId;
	
	return tabPrevNext;
}
