function getDate(strDate){
	//La date doit être au format mm-dd-yyyy ou autres séparateurs
    if (LOCALISATION.langue.format_date_js == 'dd-mm-yy'){
        day = strDate.substring(0,2);
        month = strDate.substring(3,5);
    } else { // mm-dd-yyyy
        month = strDate.substring(0,2);
        day = strDate.substring(3,5);
    }

	year = strDate.substring(6,10);
	d = new Date(year, month-1, day);
	return d;
}

function compare(date_1, date_2){
	diff = date_1.getTime()-date_2.getTime();
	return (diff==0?diff:diff/Math.abs(diff));
}

function couleur(obj) {
     obj.style.backgroundColor = "#FFFFFF";
}

function form_submit() {
	var check_color = "#FF0000";
	var valid_color = "#FFFFFF";
	var msg = "";

	if (document.formulaire.date_debut.value != "") {
		var re=RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$");
		if (!re.test(document.formulaire.date_debut.value)) {
			document.formulaire.date_debut.style.backgroundColor = check_color;
			msg += translate("The arrival date must be a MM-DD-YYYY format (example: 03-25-2008)")+"\n";
		} else document.formulaire.date_debut.style.backgroundColor = valid_color;
	}
	
	if (document.formulaire.date_debut.value == "" && document.formulaire.date_fin.value != "") {
		document.formulaire.date_debut.style.backgroundColor = check_color;
		msg += translate("Please fill in the arrival date")+"\n";
	}

	if (document.formulaire.date_fin.value != "") {
		var re=RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$");
		if (!re.test(document.formulaire.date_fin.value)) {
			document.formulaire.date_fin.style.backgroundColor = check_color;
			msg += translate("The departure date must be a MM-DD-YYYY format (example: 03-25-2008)")+"\n";
		} else document.formulaire.date_fin.style.backgroundColor = valid_color;
	}
	
	if (document.formulaire.date_debut.value != "" && document.formulaire.date_fin.value == "") {
		document.formulaire.date_fin.style.backgroundColor = check_color;
		msg += translate("Please fill in the departure date")+"\n";
	}

	if (msg == "") { // on compare les deux dates
		date1 = getDate(document.formulaire.date_debut.value);
		date2 = getDate(document.formulaire.date_fin.value);
		if ( date1 > date2 ) {
			document.formulaire.date_debut.style.backgroundColor = check_color;
			document.formulaire.date_fin.style.backgroundColor = check_color;
			if ( date1 > date2 ) msg += translate("The opening date must be prior to the closing date of stay")+"\n";
		}
	}

	if (msg == ""){
		// on rafraichie la carte
        run_map('/utils/get-map-data');
	}
	else {
		alert(msg);
	}
}

function carousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
		$('.jcarousel-control a').removeClass('active');
		$(this).addClass('active');
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });
    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });
    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};
function carousel_animationCallback(carousel, list) {
    var index = ($(list).attr('jcarouselindex')%3);
    if (index == 0) index = 3;
	$('.jcarousel-control a').removeClass('active');
	$(".jcarousel-control a:contains("+index+")").addClass('active');      
}

function apartment_map_click(id, url){
    window.location.href = url;
    return false;
}

jQuery(document).ready(function($){
    var arrondissements;
    var appartements;
	
    first_run_map('/utils/get-map-data');

	unjour = 86400000;
	date_from = new Date();
	date_to = new Date();
	date_from.setTime(date_from.getTime() + unjour);
	date_to.setTime(date_to.getTime() + unjour*12*30);

	$('#date_debut').datepick($.extend(configuration_datepick, {
		firstDay: LOCALISATION.langue.date_firstday,
		dateFormat: LOCALISATION.langue.format_date_js,
		minDate: new Date(date_from),
		maxDate: new Date(date_to),
		currentText: '',
		onSelect: function(){
			temp_date_min = new Date();
			temp_date_max = new Date();
			if ($('#date_debut').datepick("getDate") != null){ // une date est choisie
				temp_date_min.setTime($('#date_debut').datepick("getDate").getTime() + unjour*(php_nuitee_min-1));	// 3 jours minimum
				temp_date_max.setTime($('#date_debut').datepick("getDate").getTime() + unjour*3*30); // 3 mois de resa maxi
			} else { //la date a été effacée
				temp_date_min.setTime(temp_date_min.getTime() + unjour);
			}
			document.formulaire.date_debut.style.backgroundColor = '#ffffff';
			$('#date_fin').datepick("disable");
			$('#date_fin').datepick("change", {
				minDate: temp_date_min,
				maxDate: temp_date_max
			});
			document.formulaire.date_fin.value = "";
			$('#date_fin').datepick("enable");
		}
	})).attr("readonly", "readonly");
	
	$('#date_fin').datepick($.extend(configuration_datepick, {
		firstDay: LOCALISATION.langue.date_firstday,
		dateFormat: LOCALISATION.langue.format_date_js,
		minDate: new Date(date_from.setTime(date_to.getTime() + unjour)),
		maxDate: new Date(date_to),
		currentText: '',		
		onSelect: function(){ document.formulaire.date_fin.style.backgroundColor = '#ffffff'; }
	})).attr("readonly", "readonly");
	
	$('#date_debut_img').click(function () { $('#date_debut').datepick("show"); });
	$('#date_fin_img').click(function () { $('#date_fin').datepick("show"); });

    jQuery('#carousel').jcarousel({
        scroll: 1,
		visible: 1,
		initCallback: carousel_initCallback,
        itemFirstInCallback: carousel_animationCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
		wrap: 'circular'
    });
    
    load_slideshow();
	
	if ($.browser.msie && $.browser.version.substr(0,1)<=9) $("#city-apt-box").css("backgroundColor", "#3c3330");
    
});
