var slider = new Array();
bigelem = new Array();
bigelem["width"] = 175;
bigelem["height"] = 175;
bigelem["height2"] = bigelem["height"] + 25;
smallelem = new Array();
smallelem["width"] = 133;
smallelem["height"] = 133;
smallelem["height2"] = smallelem["height"] + 67;
font = new Array();
font["big"] = 16;
font["small"] = 12;
var timer = -1;

function equilibrate() {
	$('#slider ul li').each(function(i) {
		d = Math.abs(actual - i);
		//alert(d);
		//alert('li'+i+' - '+d);
		var a = $(this);
		var img = a.children('a').children('img');
		var ttl = a.children('.imtitle');
		switch(d) {
			case 0:
				img.animate({width: bigelem["width"], height: bigelem["height"]});
				a.animate({width: bigelem["width"], height: bigelem["height2"]}, 100, function () { ttl.css({'font-size': font["big"]+'px'}); });
				//showtext(a.attr('id').substr(5));
				//a.css({"padding-top": '0px'});
			break;
			case 1:
				//a.css({"margin-top": '40px'});
				ttl.css({'font-size': font["small"]+'px'});
				if (a.css('display') == 'none') {
				//if (slider[i] == 1) {
				//	slider[i] = 0;
					a.css({display: 'inline', width: '0px', height: '0px'});
					//a.css({opacity: '0'});
					img.css({width: '0px', height: '0px'});
					ttl.css({opacity: '0'});
				}
				
				img.animate({width: smallelem["width"], height: smallelem["height"]},function () { ttl.fadeTo(500, 1); });
				a.animate({width: smallelem["width"], height: smallelem["height2"]}, 100);
				//a.animate({},"fast");
			break;
			default:
				//if (slider[i] != 1) {
					//slider[i] = 1;
					a.hide('fast');
				//}
			break;
		}
	});
	//directlink();
	rotate();
}

function actual_change(diff) {
	
	//alert(actual + ':' + diff);
	//if(actual == 1) {actual = (max-1);}
	//if(actual == (max-1)) {actual = 1;}
	
	if (actual + diff > 0 && actual + diff < max) {
		actual = actual + diff;
		
		//alert(actual);
		
		//if(actual == 1) {actual = (max-1);}
		return true;
	}
	//return false;
};

/*
function directlink() {
	$('#directlink').attr('href', location.href.replace(location.search, '') + '?show=' + actual);
}
*/

$(document).ready(function(){
	//var requete = location.search.substring(1);
	//var tab_paires = requete.split("&");
	//alert(tab_paires);
	//var tab_elts = new Array();
	//for (var i = 0; i < tab_paires.length; i++) {
	//	temp = tab_paires[i].split("=");
	//	tab_elts[temp[0]] = unescape(temp[1]);
	//}
	$('#slider .cargo-body').css('background','none');
	$('#slider ul').css({overflow: 'hidden', display: 'block'});
	//$('#slider ul').prepend('<li><a><img src="src/img/vide.png" alt="" /></a></li>');
	$items = $('#slider ul li');
	
	max = $('#slider ul li').size();
	
	$items.filter(':first').before($items.slice(-max).clone());
	$items.filter(':last').after($items.slice(0, max).clone());
	
	max = $('#slider ul li').size();

	//i = (tab_elts["show"]) ? parseInt(tab_elts["show"]) : 0;
	
	//actual = (!isNaN(i) && i > 0 && i < max) ? i : 1;
	actual = Math.floor(max/2);
	//alert(actual);
	//alert(location.search.substring(1));
	
	//$('#slider ul li .text').appendTo("#texts");
	
	$('#slider li:first').before($('#slider li:last'));
	
	$('#slider ul li').each( function(i) {
		d = Math.abs(actual - i);
		//alert(d);
		var a = $(this);
		//a.addClass('a-'+i);
		var img = a.children('a').children('img');
		var ttl = a.children('.imtitle');
		switch(d) {
			case 0:
				ttl.css({'font-size': font["big"]+'px'});
				img.css({width: bigelem["width"]+'px', height: bigelem["height"]+'px'});
				a.css({height: bigelem["height2"]+'px'});
				//showtext(a.attr('id').substr(5));
			break;
			case 1:
				img.css({width: smallelem["width"]+'px', height: smallelem["height"]+'px'});
				a.css({width: smallelem["width"]+'px', height: smallelem["height2"]+'px'});
			break;
			default:
				a.css({display: 'none', width: '0px', height: '0px'});
			break;
		}
	});
	
	
	
	$('#butleft').click( function () {
		//actual = 1;
		//if (actual_change(-1)) {
			$('#slider li:first').before($('#slider li:last'));
			equilibrate();
			
		//}
		//timer_stop();
		return false;
	});
	
	$('#butright').click( function () {
		//actual = max-2;							
		//if (actual_change(+1)) {
			$('#slider li:last').after($('#slider li:first')); 
			equilibrate();
			
		//}
		//timer_stop();
		return false;
	});
	
	//next();
	/*
	$('#playpause').click( function () {
		var a = $('#playpause').children('img').attr('src');
		var a = a.split("/");
		if (a[a.length - 1] == 'pause.png') {
			timer_stop();
		}
		else {
			next();
			timer = window.setInterval("next()", 2200);
			$('#playpause').children('img').attr('src', 'images/pause.png');
		}

		return false;
	});
	$('#slider ul li a').click( function () {
		timer_stop();
	});
	*/
/*
	$('#slider').mousewheel(function(objEvent, intDelta){
		if (intDelta > 0) {
			if (actual_change(-1))
				equilibrate();
		}
		else if (intDelta < 0) {
			if (actual_change(+1))
				equilibrate();
		}
		return false;
	});
*/
	//timer = window.setInterval("next()", 3000);
});

function timer_stop() {
	var a = $('#playpause').children('img').attr('src');
	var a = a.split("/");
	if (a[a.length - 1] == 'pause.png') {
		$('#playpause').children('img').attr('src', 'images/playred.png'); /* ou play.png */
		window.clearInterval(timer);
	}
}

function next() {
	if (actual_change(+1))
		equilibrate();
}

function rotate() {
	//alert(max + ' ' + actual + ' ' + (max - actual));
/*
	if (actual < (max / 2)) {
		var a = $('#slider ul li').eq(max-1).remove();
		$('#slider ul').prepend(a);
		actual = actual + 1;
	}
*/
/*
		if (diff < 0) {
			var a = $('#slider ul li:last').remove();
			$('#slider ul').prepend(a);
		}
		else {
			var a = $('#slider ul li:first').remove();
			$('#slider ul').append(a);
		}
*/
}
/*
function showtext(id) {
	$('#texts .text').each( function (i) {
		if ($(this).css('display') != 'none')
			//$(this).fadeOut('fast');
			$(this).hide();
	});
	$('#texts #text'+id).slideDown('slow');
}
*/
