var xmlResp;
var piecesCache;
var demeure;
var mode;
var titleCat;

function loadXml(xml_file) {
	if (!titleCat) {
		titleCat = $('#titleCat');
		titleCat.data('initName', $('#titleCat').html());
	}
	$.ajax({
		type: "GET",
		url: "xml/"+xml_file,
		dataType: 'xml',
		success: function(xml_response) {
			xmlResp = xml_response;
			showAllPieces();
			$('#showAll').click(function(e) {
				e.preventDefault();
				showAllPieces();
				return false;
			});
			$('#carousel a').live('click', function(e) {
				e.preventDefault();
				if (mode == 'all') {
					showPiece($(this).attr('rel'));
				} else
					$('#content_carousel_img')
						.attr('src', $(this).attr('href'))
						.attr('alt', $('#content_carousel_titre').text())
						.attr('title', $('#content_carousel_titre').text());
				return false;
			});
		}
	});
}

function showAllPieces() {
	if (xmlResp) {
		if (!piecesCache) {
			piecesCache = '<ul>';
			$('piece', xmlResp).each(function(i) {
				var me = $(this);
				piecesCache+= '<li><a href="#" rel="'+i+'">';
				piecesCache+= '<img src="../images/'+demeure+'/'+me.attr('small')+'" height="78" border="0" alt="'+me.attr('nom')+'" title="'+me.attr('nom')+'" />';
				piecesCache+= '</a></li>';
			});
			piecesCache+= '</ul>';
		}
		
		$('#showAll').hide();
		titleCat.html(titleCat.data('initName'));
		
		$('#carousel').empty();
		$('#carousel').get(0).innerHTML = piecesCache;
		
		remakeCarousel();
		mode = 'all';

		var myPieceCur = $('piece', xmlResp).eq(0);
		var titre = myPieceCur.attr('nom');

		$('#content_carousel_titre').text(titre);
		removeFlexScroll();
		$('#content_carousel_ajax').html(myPieceCur.find('text').text());
		if ($('#content_carousel_txt').height() > 280) {
			$('#content_carousel_txt').height(280);
			CSBfleXcroll('content_carousel_txt');
		}

		$('#content_carousel_img')
			.attr('src', '../images/'+demeure+'/'+$(myPieceCur).find('images img').eq(0).attr('big'))
			.attr('alt', $('#content_carousel_titre').text())
			.attr('title', $('#content_carousel_titre').text());
	}
}

function removeFlexScroll() {
	$('#content_carousel_txt')
		.attr('style', '')
		.empty()
		.html('<div id="content_carousel_ajax"></div>')
		.get(0).scrollUpdate = undefined;
}

function showPiece(index) {
	var myPiece = $('piece', xmlResp).eq(index);
	if (myPiece) {
		var titre = myPiece.attr('nom');
		titleCat.html(titre);

		$('#content_carousel_titre').text(titre);
		removeFlexScroll();
		$('#content_carousel_ajax').html(myPiece.find('text').text());
		if ($('#content_carousel_txt').height() > 280) {
			$('#content_carousel_txt').height(280);
			CSBfleXcroll('content_carousel_txt');
		}

		var carouselCont = '<ul>';
		$('piece[nom="'+titre+'"] images img', xmlResp).each(function(i) {
			var me = $(this);
			carouselCont+= '<li><a href="../images/'+demeure+'/'+me.attr('big')+'">';
			carouselCont+= '<img src="../images/'+demeure+'/'+me.attr('small')+'" height="78" border="0" alt="'+titre+'" title="'+titre+'" />';
			carouselCont+= '</a></li>';
		});
		carouselCont+= '</ul>';
		mode = 'piece';

		$('#showAll').attr('style', '');
		
		$('#carousel').empty();
		$('#carousel').get(0).innerHTML = carouselCont;
		$('#carousel')
			.find('a')
				.eq(0).click();
		remakeCarousel();
	}
}

var curLi = 0;

function remakeCarousel() {
	if ($('#carousel').width() == $('#carousel ul').width()) {
		$('#carousel').carousel($('#carousel_previous').unbind('click').show(), $('#carousel_next').unbind('click').show());
	} else {
		$('#carousel_previous').hide();
		$('#carousel_next').hide();
	}
}

function refreshScroll(divId) {
	var myDiv = document.getElementById(divId);
	if(myDiv.scrollUpdate) {
		myDiv.scrollUpdate();
	}
}