// open window
function OW(page,target,larg,haut,scraul,agrandir,foc){
	//var posXpop = (screen.availWidth-larg)/2;
	//var posYpop = (screen.availHeight-haut)/2;
	var posXpop = 5;
	var posYpop = 5;
	param = "width="+larg+",height="+haut+",left="+posXpop+",top="+posYpop+",scrollbars="+scraul+",resizable="+agrandir;
	popup = window.open(page,target,param);
	if(foc) popup.focus();
}
// fin open window
//ouverture popups coureurs
function openCoureur(page){
	OW(page,"coureur",588,600,1,0,true);
}
//fin ouverture popups coureurs





var makeRequest = function(url, urlAlternative, type, container, load, timestamp, callback) {
	var suffixeUrl;
	if (timestamp) {
		var timeDate = new Date();
		var timeHours=timeDate.getHours();
		var timeMins=timeDate.getMinutes();
		var timeSecs=timeDate.getSeconds();
		timeHours=timeHours<10?"0"+timeHours:timeHours;
		timeMins=timeMins<10?"0"+timeMins:timeMins;
		timeSecs=timeSecs<10?"0"+timeSecs:timeSecs;
		time="?timestamp="+timeHours+timeMins+timeSecs;
	} else {
		time="";
	}
	url+=time;
	urlAlternative+=time;
	log(2,"makeRequest "+type+" > start");
	if (container!=null) {
		// display loading
		var saveHeight=0;
		if (load) {
			if ($("#"+container).html()) {
				saveHeight=$("#"+container).height();
			}
			log(4,"Height: "+saveHeight);
			$("#"+container).html('<div class="loading"><div class="loadingImg"></div></div>');
			if (saveHeight>0) {
				$("#"+container).height(saveHeight);
			}
			log(4,"Height: "+$("#"+container).height());
		}
		// display container
		$("#"+container).fadeIn("slow", function() {
			log(1,container+" makeRequest "+type+" > "+url, "info");
			$.ajax({
				url: url,
				async: true,
				dataType: type,
				error: function () {
					log(1,container+" makeRequest "+type+" > error :"+url, "error");
					log(2,container+" makeRequest "+type+" alternative :"+urlAlternative, "info");
					$.ajax({
						url: urlAlternative,
						async: false,
						error: function () {
							log(1,container+" makeRequest "+type+" alternative > error :"+urlAlternative, "error");
							log(4,"Height: "+$("#"+container).height());
							return false;
						},
						success: function(html) {
							log(3,container+" makeRequest "+type+" alternative > success");
							if (html) {
								$("#"+container).html(html);
								$("#"+container).css("height","100%");
							}
							if (callback) {
								callback.call();
							}
							log(4,"Height: "+$("#"+container).height());
							log(2,container+" makeRequest "+type+" > stop");
							return true;
						}
					});
				},
				success: function(html) {
		   		log(3,container+" makeRequest > success");
					if (html) {
						$("#"+container).html(html);
			   		//$("#"+container).removeAttr("height");
			   		$("#"+container).css("height","100%");
					}
					if (callback) callback.call();
					log(4,"offsetHeight: "+$("#"+container).height());
					log(2,container+" makeRequest > stop");
					return true;
		 		}
			});
		});
	} else {
		log(1,"MakeRequest "+type+" > "+url, "info");
		$.ajax({
			url: url,
			async: true,
			dataType : type,
			error: function () {
				log(1,"MakeRequest "+type+" > error :"+url, "error");
				if (urlAlternative) {
					log(2," MakeRequest "+type+" alternative :"+urlAlternative, "info");
					$.ajax({
						url: urlAlternative,
						async: true,
						error: function () {
							log(1,"MakeRequest "+type+" alternative > error :"+urlAlternative, "error");
							return false;
						},
						success: function(html) {
							log(3," MakeRequest "+type+" alternative > success");
							if (callback) {
								callback.call();
							}
							log(2,"MakeRequest "+type+" alternative > stop");
							return true;
						}
					});
				}
			},
			success: function(data) {
		  	log(3,"MakeRequest "+type+" > success");
				if (callback) callback(data);
				log(2,"MakeRequest "+type+" > stop");
				return true;
		 	}
		});
	}
};
var selectOnglet = function(focusObject, targetName, refresh) {
	var arrayFocus=$(focusObject).attr("href").split("#");
	var blocParentDiv = $(focusObject).parent("span").parent("li").parent("ul").parent("div");
	var blocRootDiv = $(blocParentDiv).parent("div");
	log(4,"focus > "+focusObject.href);
	log(4,"parent > "+$(blocParentDiv).attr("id"));
	log(4,"root > "+$(blocRootDiv).attr("id"));
	if (refresh) {
		var commande = $(blocRootDiv).attr("id")+'Display("on","'+arrayFocus[1]+'")';
		eval(commande);
	} else {
		$("#"+targetName+" > div[id]").hide();
		$("#"+arrayFocus[1]).show();
		$(blocParentDiv).find("li").removeClass();
		$(blocParentDiv).find("li").addClass("off");
		$(focusObject).parent("span").parent("li").removeClass();
		$(focusObject).parent("span").parent("li").addClass("on");
	}
}
var changeSelectStage = function(select, page) {
	url_redir="/"+lang+"/"+course+"/"+annee+"/"+$("#"+select).val()+prefix_url+"/"+page;
	document.location=url_redir;
}
