
function ag_cyclo_showevent(div, text){document.getElementById(div).innerHTML =  '<center>'+text+'</center>';}
function ag_cyclo_hideevent(div){document.getElementById(div).innerHTML =  '<center></center>';}
var ag_cyclo_frames_affichees = 0;

function ag_cyclo_addslashes (str) {
    // Escapes single quote, double quotes and backslash characters in a string with backslashes  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/addslashes
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman
    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday' 
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

function ag_cyclo_trim (myString){return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')} 



ag_cyclo_tableau = function(mois,annee)
{
   var url = ag_cyclo_home +'/widget_ajax/ajax_calendrier.php';
   var parametres = 'mois=' + mois + '&annee=' + annee;
    jQuery('#ag_cyclo_extrait').html("<center><b>...</b></center>");
	(function($){$.getJSON(url,{mois:mois,annee:annee},function(json){ag_cyclo_remplirCalendrier(json);});})(jQuery);
}


function ag_cyclo_remplirCalendrier(data) {
	   var mois;
       //On place les liens suivants,précédents et le mois en cours
	   jQuery("#ag_cyclo-suiv").unbind("click");
       jQuery("#ag_cyclo-suiv").click(function(){eval(data.lien_suivant)});
	   jQuery("#ag_cyclo-prec").unbind("click");
       jQuery("#ag_cyclo-prec").click(function(){eval(data.lien_precedent)});
       mois=data.mois_en_cours;
       jQuery('#ag_cyclo_mois_en_cours').html(data.mois_en_cours);
	   jQuery('#ag_cyclo_extrait').html("<center></center>");
		document.getElementById('ag_cyclo_mois_en_chiffre').value = data.mois_en_cours_chiffre;

	   //Maintenant, on affiche tous les jours du calendrier
	   var today = data.aujourdhui;
       var compteur=1;
       var id='';
	   var content;
	   var reg=new RegExp("[|]+", "g");
	   var tableau;
	   var innerhtml="";
	   var classname;
	   var mouseover="";
	   var mouseout="";
	   var label;
	   var jour = 0;
	   var type;

       while(compteur<43){
		  mouseover="";
		  mouseout="";
		  innerhtml="";
          id='ag_cyclo_'+compteur.toString();
		  obj_id = document.getElementById(id);
		  content = data.calendrier[(compteur-1)].fill;
		  if (content.length ==0 ) {classname = 'ag_cyclo-days-cell ag_cyclo-other-month ag_cyclo-unselectable';}
		  else
		  {
			jour++;
			tableau =content.split(reg);
			if (tableau.length>1)
			{
				type = ag_cyclo_trim(tableau[0]);
				innerhtml = tableau[2];
				label = tableau[1];				
				switch(type)
				{
					case 'sortie-matin': classname = 'ag_cyclo-days-cell ag_cyclo_matin'; break;
					case 'sortie-apresmidi': classname = 'ag_cyclo-days-cell ag_cyclo_apres-midi'; break;
					default: classname = 'ag_cyclo-days-cell ag_cyclo_journee';				
				}
				mouseover= "ag_cyclo_showevent(\"ag_cyclo_extrait\",\" " + ag_cyclo_addslashes(label) + "\")";
				mouseout= "ag_cyclo_hideevent('ag_cyclo_extrait')";
			}
			else
			{
				innerhtml = tableau[0];
				classname = 'ag_cyclo-days-cell ag_cyclo-unselectable ag_cyclo_date_libre';
			}
			if (jour==today){classname = classname + ' ag_cyclo-today';}
		  }
//		  document.getElementById(id).innerHTML=innerhtml;
//		  document.getElementById(id).className=classname;
//		  document.getElementById(id).setAttribute("onmouseover", mouseover);
//		  document.getElementById(id).setAttribute("onmouseout", mouseout);
		  obj_id.innerHTML=innerhtml;
		  obj_id.className=classname;
		  obj_id.setAttribute("onmouseover", mouseover);
		  obj_id.setAttribute("onmouseout", mouseout);
          compteur++;
       }
}


function ag_cyclo_showSortie(rang_id){
	var url = ag_cyclo_home + '/widget_ajax/ajax_sortie.php';
	document.getElementById('ag_cyclo_extrait').innerHTML="<center><b>...</b></center>"
	var parametres = 'rang_id=' + rang_id ;
//	(function($){$.get(url,{rang_id:rang_id},function(responseText){ag_cyclo_fillsortie(responseText);});})(jQuery);
//	(function($){$.ajax(url,{rang_id:rang_id},function(responseText){ag_cyclo_fillsortie(responseText);});})(jQuery);

	(function($){$.ajax({
						type: "GET",
						url: url,
						cache: false,
						data: parametres,
						success: function(responseText, status){ ag_cyclo_fillsortie(responseText);},
						error: function(xhr, status){ag_cyclo_handle_sortie_error(xhr.responseText);}
				});} ) (jQuery);

}

function ag_cyclo_handle_sortie_error(responseText){
// handle database error access set by wordpress
    var commentaires=responseText;
	document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>"
    document.getElementById('ag_cyclo_parcours').innerHTML=commentaires;
	document.getElementById("ag_cyclo_parcours").style.display="block";
	document.getElementById('ag_cyclo_print').style.display="none";
	location.hash = "ag_cyclo_anchor";
	if (ag_cyclo_frames_affichees != 0	)
	{
		document.getElementById("liste_parcours").innerHTML =  '<center></center>';
		document.getElementById("parcours").innerHTML ='<center></center>';
		frames['carte_parcours'].location.href = ag_cyclo_home + '/pages/blank.html';
	}
}

function ag_cyclo_fillsortie(responseText){
    var commentaires=responseText;
	document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>"
    document.getElementById('ag_cyclo_parcours').innerHTML=commentaires;
	ag_show_circuit();
	location.hash = "ag_cyclo_anchor";
	if (ag_cyclo_frames_affichees != 0	)
	{
		document.getElementById("liste_parcours").innerHTML =  '<center></center>';
		document.getElementById("parcours").innerHTML ='<center></center>';
		frames['carte_parcours'].location.href = ag_cyclo_home + '/pages/blank.html';
	}
}


function ag_cyclo_showEvent(rang_id){
	var url = ag_cyclo_home + '/widget_ajax/ajax_event.php';
	document.getElementById('ag_cyclo_extrait').innerHTML="<center><b>...</b></center>";
	var parametres = 'rang_id=' + rang_id ;
	(function($){$.get(url,{rang_id:rang_id},function(responseText){ag_cyclo_fillEvent(responseText);});})(jQuery);
}


function ag_cyclo_fillEvent(responseText){
    var commentaires=responseText;
	document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>"
    document.getElementById('ag_cyclo_parcours').innerHTML=commentaires;
	ag_show_circuit();
	location.hash = "ag_cyclo_anchor";
}


function ag_cyclo_set_display_circuit()
{
var pos1 = findPos(document.getElementById("agenda_cyclo_div"));
var pos2 = findPos(document.getElementById("sidebar"));
var height = pos1[1]- pos2[1] -5;
var message = "<table width='670'><tbody>";
message = message + "<tr><td > <iframe name = 'bidon'  width='100%' height='"+ height +"' frameborder='0' marginwidth='0px'></iframe> </td></tr>";
message = message + "<tr ><td ><div id='liste_parcours' style=\"width:660px; height:60px; overflow:auto; border:solid 1px black;\"></div></td></tr>";
message =message + "<tr><td ><div id='ag_cyclo_article'  style=\"width:100%; display:none;\" ></div></td></tr>";
message =message + "<tr><td ><iframe id='carte_parcours' name ='carte_parcours'  width='100%' height='440' frameborder='0' marginwidth='0px'></iframe></td></tr>";
message = message + "<tr ><td ><div id ='parcours' style=\"width:660px; height:60px; overflow:auto; border:solid 1px black;\" ></div></td></tr>";
message = message +  "</tbody></table>";
return message;
}

function ag_cyclo_affiche_frames()
{
	if (ag_cyclo_frames_affichees == 0)
	{
		 if ( ! document.getElementById("cyclocontentleft"))
		 {
			var theNewDiv = document.createElement('div');
			theNewDiv.setAttribute('id',"cyclocontentleft");
			document.getElementById('content').insertBefore(theNewDiv,document.getElementById('sidebar') );
			document.getElementById("cyclocontentleft").innerHTML =  ag_cyclo_set_display_circuit();
		 }
		 document.getElementById('contentleft').style.display="none";
		 document.getElementById("cyclocontentleft").style.display="block";
         ag_cyclo_frames_affichees = 1;
		 location.hash = "ag_cyclo_anchor";
	}
}

function ag_cyclo_cache_frames()
{
	if (ag_cyclo_frames_affichees == 1)
	{
		if ( document.getElementById("cyclocontentleft")){document.getElementById("cyclocontentleft").style.display="none";}
		document.getElementById("contentleft").style.display="block";
        ag_cyclo_frames_affichees = 0;
	}
}

function ag_cyclo_affiche_variante(event_id,heure){
   var url = ag_cyclo_home + '/widget_ajax/ajax_variante.php';
   var parametres = 'rang_id=' + event_id + '&heure=' + heure;
   ag_cyclo_affiche_frames();
	(function($){$.getJSON(url,{rang_id:event_id,heure:heure},function(json){ag_cyclo_detail_variante(json);});})(jQuery);
}



function ag_cyclo_detail_variante(data){
var ref=data.ref_openrunner;
var style;
var url = ag_cyclo_home + '/widget_ajax/get_circuit.php';
if (ref != 0){
//	frames['carte_parcours'].location.href='http://www.openrunner.com/displayRoute.php?id='+ref+'&w=630&h=370&k=5&m=0';
	frames['carte_parcours'].location.href= url+'?id='+ref+'&w=630&h=370&k=5&m=0';
	}
else frames['carte_parcours'].location.href=ag_cyclo_home +'/Images/capturer.jpg';
style=document.getElementById('liste_parcours').style.display;
if (style=="none")
{
	document.getElementById('ag_cyclo_article').style.display="none";
	document.getElementById('liste_parcours').style.display="block";
	document.getElementById('carte_parcours').style.display="block";
	document.getElementById('parcours').style.display="block";
}
document.getElementById("liste_parcours").innerHTML =  data.description;
document.getElementById("parcours").innerHTML = data.depart ;
document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>";
}

function ag_show_print() {
	document.getElementById("ag_cyclo_parcours").style.display="none";
	document.getElementById('ag_cyclo_print').style.display="block";
	document.getElementById('ag_cyclo_btn').style.display="none";
	document.getElementById('ag_cyclo_btn_circuit').style.display="block";
}

function ag_show_circuit() {
	document.getElementById("ag_cyclo_parcours").style.display="block";
	document.getElementById('ag_cyclo_print').style.display="none";
	document.getElementById('ag_cyclo_btn').style.display="block";
	document.getElementById('ag_cyclo_btn_circuit').style.display="none";
}

function ag_cyclo_detail_print(reponse){
		 document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>"
         document.getElementById('ag_cyclo_print').innerHTML=reponse;
		 document.getElementById("ag_cyclo_parcours").style.display="none";
		 document.getElementById('ag_cyclo_print').style.display="block";
}

function ag_update_print() {
   var url = ag_cyclo_home + '/widget_ajax/ajax_print.php';
	var mois_en_cours = document.getElementById('ag_cyclo_mois_en_chiffre').value;
	(function($){$.get(url,{mois_en_cours:mois_en_cours },function(response){ag_cyclo_update_detail_print(response);});})(jQuery);
}

function ag_cyclo_update_detail_print(reponse){
		 document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>";
         document.getElementById('ag_cyclo_print').innerHTML=reponse;
}

function ag_cyclo_gotomonth(mois,annee){
	ag_cyclo_tableau(mois,annee);
	document.getElementById('ag_cyclo_mois_en_chiffre').value = annee+"-"+mois;
	ag_update_print();
}

function ag_cyclo_affiche_article(no_article)
{
	var url = ag_cyclo_home + '/widget_ajax/ajax_article.php';
	document.getElementById('ag_cyclo_extrait').innerHTML="<center><b>...</b></center>";
	(function($){$.get(url,{article_id:no_article},function(responseText){ag_cyclo_showPost(responseText);});})(jQuery);
}

function ag_cyclo_showPost(reponse){
		var style;
		 ag_cyclo_affiche_frames();
		 document.getElementById('ag_cyclo_extrait').innerHTML="<center></center>";
         document.getElementById('ag_cyclo_article').innerHTML=reponse;
		 if (typeof shutterOnload == 'function') shutterOnload();
		 style=document.getElementById('ag_cyclo_article').style.display;
		 if (style=="none")
		 {
			 document.getElementById('ag_cyclo_article').style.display="block";
			 document.getElementById('liste_parcours').style.display="none";
			 document.getElementById('carte_parcours').style.display="none";
			 document.getElementById('parcours').style.display="none";
		 }
}

