$(document).ready(function() {
	randomBD();
	initChangePage();
	
	$("div#infoDialog").dialog({
		autoOpen: false,
		modal: true,
		bgiframe: true,
		width: 400,
		height: 300,
		buttons: {
			'Fermer' : function() {
				$(this).dialog('close');
			}
		}
	});
});

function randomBD() {
	var dialog = $("div#infoDialog");	
	
	$("div#randomBD").cycle({
		fx: 'shuffle'
	});
		
	$("img#lastBd").click(function() {
		dispAjaxLoader(dialog);
		dialog.dialog('option', 'title', $(this).attr('alt'));
		dialog.dialog('open');
		
		$.ajax({
			dataType: 'html',
			url: '/collec/' + $(this).attr('name'),
			success: function(content) {				
				dialog.html(content);
			}
		});
	});
	
	$("div#randomBD img").click(function() {
		dispAjaxLoader(dialog);
		dialog.dialog('option', 'title', $(this).attr('alt'));
		dialog.dialog('open');
		
		$.ajax({
			dataType: 'html',
			url: '/collec/' + $(this).attr('name'),
			success: function(content) {				
				dialog.html(content);
			}
		});
	});
}

var newsPage = 0;

function initChangePage() {
	var nbPages = $("input[type='hidden']#pages").val();
	
	if (newsPage + 1 >= nbPages) {
		$("div .clear a[name='next']").hide();
		$("span#pager").hide();
	}
	
	if (newsPage == 0) {
		$("div .clear a[name='prev']").hide();
		$("span#pager").hide();
	}
	
	$("div .clear a[name='prev']").click(function() {
		newsPage--;
		changePage();
		
		if (newsPage + 1 >= nbPages) {
			$("div .clear a[name='next']").hide();
			$("span#pager").hide();
		}
		
		else {
			$("div .clear a[name='next']").show();
			if (newsPage != 0)
				$("span#pager").show();
		}
		
		if (newsPage == 0) {
			$("div .clear a[name='prev']").hide();
			$("span#pager").hide();
		}
		
		else
			$("div .clear a[name='prev']").show();
	});
	
	$("div .clear a[name='next']").click(function() {
		newsPage++;
		changePage();
		
		if (newsPage + 1 >= nbPages) {
			$("div .clear a[name='next']").hide();
			$("span#pager").hide();
		}
		
		else {
			$("div .clear a[name='next']").show();
			if (newsPage != 0)
				$("span#pager").show();
		}
		
		if (newsPage == 0) {
			$("div .clear a[name='prev']").hide();
			$("span#pager").hide();
		}
		
		else
			$("div .clear a[name='prev']").show();
	});
}

function changePage() {
	dispAjaxLoader($("div#news"));
	
	$.ajax({
		async: false,
		dataType: 'html',
		url: '/news/page-' + newsPage,
		success: function(content) {
			$("div#news").html(content);
		}
	});
}
