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

var JQ = jQuery.noConflict();  
function submitTcForm(tcForm) {
	if (JQ(tcForm).attr('keepidx') != true) {
		JQ('#departEntryIdx').attr('value', '');
		JQ('#arriveeEntryIdx').attr('value', '');
	}
	return true;

}

JQ(document).ready(function () {

	if (JQ("#tc-covoitureurs-map").size() > 0) {

		LaRoueVerte.GMap.loadCustomExtensions();
		LaRoueVerte.contextPath = JQ("#map-configuration #context-path").get(0).innerHTML;
		
		var mapLoaded = false;
		
		function loadMap() {
			var mapController = new LaRoueVerte.GMap.Helpers.TCAndCovMapController();
			mapLoaded = true;
			
			// overing behavior for covoiturage pathes
			JQ("#cov-details tbody tr").hover(function() {
				var geoContainer = JQ(this).children("td.pseudo").get(0);
				mapController.highlightCov(geoContainer);
			}, function() {
				mapController.unhighlightCov()
			}).click(function(event) {
				var geoContainer = JQ(this).children("td.pseudo").get(0);
				var jqPin = JQ(this).children(".pin");
				// if the source of the event is the cell's span for arrive / depart, zooming
				// on the point on the map
				var jqTarget = JQ(event.target);
				if (jqTarget.hasClass("depart-city")) {
					mapController.zoomCov(geoContainer, "from");
					jqPin.addClass("cov-pin");
					return;
				}
				if (jqTarget.hasClass("arrivee-city")) {
					mapController.zoomCov(geoContainer, "to");
					jqPin.addClass("cov-pin");
					return;
				}
				
				jqPin.toggleClass("cov-pin");
				// pin behavior
				if (jqPin.hasClass("cov-pin")) {
					mapController.pinCov(geoContainer);
				} else {
					mapController.unpinCov(geoContainer);			
				}
			});
		}
		
		if (JQ("#tc-covoitureurs-map").size() > 0 && JQ(".tccov-results").hasClass("map-mode")) {
			loadMap();
		}
	}
	
		
	function toggleText(input){
		if (this.value == this.alt) {
			this.value = "";
		} else if (this.value == "") {
			this.value = this.alt;
		}
	} 
	
	// button to switch map/non-map modes (e.g. add/remove a css class)
	JQ("#map-trigger").click(function() {
		JQ(".tccov-results").toggleClass("map-mode");
		if (JQ(".tccov-results").hasClass("map-mode") && !mapLoaded) {
			loadMap();
		}
	});
	
	// initializes auto clear behavior on fields with
	// "auto-clear" class
	JQ(".auto-clear input")
		.attr("alt", function() {return this.value})
		.blur(toggleText)
		.focus(toggleText);
	
	// deambiguisation links
	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');
		
		
		if ("depart" == JQ(this).attr("entrytype")) {
			JQ("#inputDepartAdresse").attr('value', JQ(option).attr("entryname"));
			JQ("#inputDepartVille").attr('value', JQ(option).attr("entrycity"));
		} else {
			JQ("#inputArriveeAdresse").attr('value', JQ(option).attr("entryname"));
			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");
});
