$(document).ready(function() {

	/* General - START */
	$('.error, .success').click(function () {
		$(this).fadeOut('slow');
	});
	/* General - STOP */

	/* News - START */
	$(function() {
		$( "#publicationStart" ).datepicker({
			dateFormat: 'yy-mm-dd 00:00:00',
			minDate: new Date(2011, 1-1, 1),
			maxDate: new Date(2030, 12-1, 31)
		});
		$( "#publicationStop" ).datepicker({
			dateFormat: 'yy-mm-dd 23:59:59',
			minDate: new Date(2011, 1-1, 1),
			maxDate: new Date(2030, 12-1, 31)
		});
		// Convert textarea to elastic textarea
		$('.elastic').elastic();
	});
	$('#publicationStopNever').click(function () {
		if ($(this).attr('checked'))
		{
			$('#publicationStop').val('2037-01-01 00:00:00');
			$('#publicationStop').hide();
		}
		else
		{
			var currentTime = new Date();
			var month = currentTime.getMonth() + 2;
			if (month.toString().length < 2)
				month = '0' + month;
			$('#publicationStop').val(currentTime.getFullYear() + '-' + month + '-' + currentTime.getDate() + ' 23:59:59');
			$('#publicationStop').show();
		}
	});
	/* News - STOP */
	
	/* Catalog - START */
	$('#catalogTypeReal').click(function () {
		$('#catalogTypeExternalDetails').hide();
		$('#externalToolUrl').hide();
		$('#catalogFileCheck').show();
		$('#internalToolUrl').show();
	});
	$('#catalogTypeExternal').click(function () {
		$('#catalogTypeExternalDetails').show();
		$('#externalToolUrl').show();
		$('#catalogFileCheck').hide();
		$('#internalToolUrl').hide();
	});

	$('#catalogUrl').keyup(function () {
		$('#catalogUrlDisplay').text($('#catalogUrl').val());
	});

	$('#catalogName').keyup(function () {
		if ($("#catalogTypeReal").attr("checked"))
			$('#catalogUrl').val($('#catalogName').val().replace(/[^a-zA-Z0-9-.\/]+/g, "_"));
	});
	/* Catalog - END */

	/* Welcome - START */
	$("#homeLeftProduct1").hover(function() {
			$("#homeLeftProduct2Banner").stop(true, true).hide();
			$("#homeLeftProduct3Banner").stop(true, true).hide();
			$("#homeLeftProduct1Banner").show('slide', {direction: 'right'}, 1000);
		}, function() {
			$("#homeLeftProduct1Banner").hide();
			$('#presentationFlash').show();
	});
	$("#homeLeftProduct2").hover(function() {
			$("#homeLeftProduct1Banner").stop(true, true).hide();
			$("#homeLeftProduct3Banner").stop(true, true).hide();
			$("#homeLeftProduct2Banner").show('slide', {direction: 'right'}, 1000);
		}, function() {
			$("#homeLeftProduct2Banner").hide();
			$('#presentationFlash').show();
	});
	$("#homeLeftProduct3").hover(function() {
			//$('#presentationFlash').hide();
			$("#homeLeftProduct1Banner").stop(true, true).hide();
			$("#homeLeftProduct2Banner").stop(true, true).hide();
			$("#homeLeftProduct3Banner").show('slide', {direction: 'right'}, 1000);
		}, function() {
			$("#homeLeftProduct3Banner").hide();
			$('#presentationFlash').show();
	});
	/* Welcome - STOP */

	/* Contact - START */
	$('#contactForm input[type=text], #contactForm textarea').click(function () {
		$(this).val('');
	});
	/* Contact - STOP */
});

/* Check files for catalogs */
function checkFiles(catalogId)
{
	$('#catalogCheckFileMessage').show();
	$('#catalogCheckFileList').html('');
	$.getJSON('/adminCatalog/getFiles/' + catalogId, false, function(files){
		$('#catalogCheckFileList').append('<thead><tr><td>Fichier</td><td>Status</td></tr></thead><tbody>');
	    $.each(files.data, function(i, item) {
	    	var className = 'no';
	    	if (item.exist == 'Ok')
	    		className = 'yes';
	    	$('#catalogCheckFileList').append('<tr id="file_' + i + '" class="' + className +'"><td>' + item.name + '</td><td>' + item.exist + '</td></tr>');
	    });
	    $('#catalogCheckFileList').append('</tbody>');
    	$('#catalogCheckFileMessage').hide();
    });
}
