
// jQuery feature for preventing conflict with mootools (jQuery must then be loaded _after_ mootools)

var JQ = jQuery.noConflict();  
function submitTcForm(tcForm) {
	return true;
}

JQ(document).ready(function () {	
		
	function toggleText(input){
		if (this.value == this.alt) {
			this.value = "";
		} else if (this.value == "") {
			this.value = this.alt;
		}
	}
	function clearIdx(input) {
		if (this.id == 'inputDepartAdresse' || this.id == 'inputDepartVille') {
			JQ('#departEntryIdx').attr('value', '');
		} else if (this.id == 'inputArriveeAdresse' || this.id == 'inputArriveeVille') {
			JQ('#arriveeEntryIdx').attr('value', '');
		}
	} 
		
	// initializes auto clear behavior on fields with
	// "auto-clear" class
	JQ("input.auto-clear")
		.attr("alt", function() {return this.value})
		.blur(toggleText)
		.focus(toggleText);

	// resets index on address changed
	// for input field having "reset-index" class declared
	JQ("input.reset-index")
		.change(clearIdx);
	
	// deambiguisation links
	// MCR 9-9-2010: seems deprecated...
	JQ(".pointsList a").click(function() {
		// getting ref
		var ref = this.href.split("#")[1];
		JQ("#" + ref).attr("value", JQ(this).attr("entryidx"));
		JQ("#tcCov-form").attr('keepidx', 'true');
		if ("depart" == JQ(this).attr("entrytype")) {
			JQ("#inputDepartAdresse").attr('value', JQ(this).attr("entryname"));
			JQ("#inputDepartVille").attr('value', JQ(this).attr("entrycity"));
		} else {
			JQ("#inputArriveeAdresse").attr('value', JQ(this).attr("entryname"));
			JQ("#inputArriveeVille").attr('value', JQ(this).attr("entrycity"));
		}
		JQ("#tcCov-form").submit();
	});
	
	JQ(".pointsList select").change(function() {
		// getting ref
		var option = JQ(this).children(":selected");
		
		JQ(JQ(this).attr("ref")).attr("value", JQ(option).attr("entryidx"));
		JQ("#tcCov-form").attr('keepidx', 'true');
		
		var address = JQ(option).attr("entryname") + " " + JQ(option).attr("entrycity");
		if ("depart" == JQ(this).attr("entrytype")) {
			JQ("#inputDepartAdresse").attr('value', address);
			JQ("[name=departCoordX]").attr('value', JQ(option).attr('coordx'));
			JQ("[name=departCoordY]").attr('value', JQ(option).attr('coordy'));
			//JQ("#inputDepartVille").attr('value', JQ(option).attr("entrycity"));
		} else {
			JQ("#inputArriveeAdresse").attr('value', address);
			JQ("[name=arriveeCoordX]").attr('value', JQ(option).attr('coordx'));
			JQ("[name=arriveeCoordY]").attr('value', JQ(option).attr('coordy'));
			//JQ("#inputArriveeVille").attr('value', JQ(option).attr("entrycity"));
		}
		// check both are selected
		var submit = true;
		JQ(".pointsList select").children(":selected").each(function(obj, val){
			submit = submit & (JQ(val).attr("entryname") != undefined);
		});
		if (submit) {
			JQ("#tcCov-form").submit();
		}
	});
	
	// initializes date / time inputs
	var time = new Date();
	function get2DigitsNumber(date) {
		if (date < 10) {
			return "0" + date;
		}
		return date;
	}
	
	// utility for date computing
	Date.prototype.isSameDay = function(date) {
		return ((this.getFullYear() == date.getFullYear()) &&
			(this.getMonth() == date.getMonth()) &&
			(this.getDate() == date.getDate()));
	};
	
	JQ(".auto-date input").attr("value", function() {
		if (this.value == "") {
			return get2DigitsNumber(time.getDate()) + "/" + (time.getMonth() + 1) + "/" + time.getFullYear();
		}
		return this.value;
	}).each(function(index, element) {
		// setting up calendar
		Calendar.setup({
			inputField     :    element.id,     // id of the input field
			ifFormat       :    "%d/%m/%Y",      // format of the input field
			singleClick    :    true,
			button         :    "imgAller",
			weekNumbers : false,
			firstDay : 1,
			dateStatusFunc : function (date) {
				return date < new Date() && !date.isSameDay(new Date());
			}
		});
	});
	
	
	JQ(".auto-time input").attr("value", function() {
		if (this.value == "") {
			// value is time.get<Class>, where class is the input' class attribute (seconds, hours ...)
			if (JQ(this).hasClass("minutes")) {
				return get2DigitsNumber(time.getMinutes());
			}
			return get2DigitsNumber(time.getHours());
		}
		return this.value;
	});
	
	JQ(".page-lrv #menu-covoiturage #menu-lrv li.toplevel").hover(
		function () {
			JQ(this).children("ul").show();
		}, 
		function () {
			JQ(this).children("ul").hide();
		}
	);

	JQ(".page-lrv #menu-covoiturage #menu-lrv2 li.toplevel").hover( /*click(*/
	  function () { 
		JQ("#page-lrv #menu-covoiturage #menu-lrv2 li.toplevel ul:visible").hide(); //slideUp("fast");
	    if (! JQ(this).children("ul").is(":visible")) {
	    	JQ(this).removeClass("closed").addClass("opened").children("ul").show();
	    }
	  },
	  function () {
		  JQ(this).removeClass("opened").addClass("closed").children("ul").hide();
	  }
	);
	//JQ(".page-lrv #menu-covoiturage li.toplevel ul:eq(0)").show();
    	    
	// contournement aux styles hardcodés dans quelques jsp (resultatCarte.jsp) 
	JQ(".page-lrv #content .tabsContent .page-actions").css("margin-right", "0");
	JQ(".page-lrv #content .tabsContent .page-section #leftSideSection .covlistclass h3").css("font-size", "1.1em");
});

