$(document).ready(function() {
	// Fiche aide en popup
	if($('.lienDetailEvenement').length){
        $('.lienDetailEvenement').click(function(el){
			var offset = $(this).offset();
  			el.stopPropagation();
            if($('#detailEvenement').length){$.closeWindow("detailEvenement");}
            if($('#planEvenement').length){$.closeWindow("planEvenement");}
            var popupTitle = jQuery.trim($(this).text()).substring(0, 45).split(" ").slice(0, -1).join(" ") + "...";
            var evenementId = $(this).attr('id');
            $.newWindow({id:"detailEvenement", width: 450, height: 200,posx:80,posy:offset.top,title:popupTitle, type:"normal"});
            $.updateWindowContentWithAjax("detailEvenement", "/mes-services/getDetailEvenement?id="+evenementId, true);
        });
	}
});

cg27modalWindow = function() 
{
	var win;
	var tpanel;
	var libelle;
	var commune;
	var rue;
	return {
		openModalWindow: function openModalWindow(applyTo, p_libelle, p_commune,p_rue, p_departement) {
			if(!win){
				tpanel = new Ext.TabPanel({
                    applyTo: applyTo,
                    autoTabs:true,
                    activeTab:0,
                    deferredRender:false,
                    border:false,
                    items:[{
	                    id: "ajaxtab",
                        title: 'Ajax Tab',
                        autoScroll:true
                    }]
                });
	            win = new Ext.Window({
	                applyTo:'hello-win',
	                layout:'fit',
	                width:470,
	                height:520,
	                closeAction:'hide',
	                plain: false,
	                shim: false,
	                modal:false,
	                items: tpanel,
	                buttons: [{
	                    text: 'Fermer',
	                    handler: function(){
	                        win.hide();
	                    }
	                }]
	            });
	        } 
        	var updt = tpanel.getActiveTab().getUpdater();
        	libelle = p_libelle;
        	commune = p_commune;
        	if (p_departement != "" && p_departement != "null"){
        		commune += ", " + p_departement;
        	}
        	rue = p_rue;
        	updt.update({
                url: "/mes-services/geolocalise",
                callback: cg27modalWindow.showGoogleMap
            });
	        win.center();
	        win.show(this);
		},
		showGoogleMap: function showGoogleMap() {
			var mapCenter = new google.maps.LatLng(49.09, 1.07);
		  	var myOptions = {
		    zoom: 9,
		    center: mapCenter,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  	};
		  	var map = new google.maps.Map(document.getElementById("map"), myOptions);
		  	var geocoder = new google.maps.Geocoder();
    		var desc="<h3 style='color:#FF4D00;margin-top: 5px;'>" + libelle + "</h3>";
    		if (rue != null && rue != '')
    		{
    			desc += rue + "<br/>";
    		}
    		desc += commune;
    		
    		var _location = rue + " " + commune;
    		var marker = new google.maps.Marker();
		  	marker.setMap(map);
		  	var infowindow = new google.maps.InfoWindow({ content: "<div class='infodescription'>" + desc + "</div>"});
    		geocoder.geocode({'address': _location}, function(results, status) {
    	    	if (status == google.maps.GeocoderStatus.OK) {
    	    		marker.setPosition(results[0].geometry.location);
    	    		infowindow.open(map,marker);
    	    		map.setCenter(marker.getPosition());
    	    	} 
    	    	else {
    	    		if(status == google.maps.GeocoderStatus.ZERO_RESULTS){
    	    			location = commune;
    	    			geocoder.geocode({'address': _location}, function(results, status) {
    		    			if (status == google.maps.GeocoderStatus.OK) {
    		    			  	marker.setPosition(results[0].geometry.location);
    		    			  	infowindow.open(map,marker);
    		    			} 
    	    			});
    	    		}
    	    	}
    		});

		}
	}	
}();
// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
var regExpBeginning = /^\s+/;
var regExpEnd       = /\s+$/;
	
function trim(p_string) 
{
return p_string.replace(regExpBeginning, "").replace(regExpEnd, "");
}
	
function addslashes(ch) { 
	ch = ch.replace(/\\/g,"\\\\");
	ch = ch.replace(/\'/g,"\\'"); 
	ch = ch.replace(/\"/g,"\\\""); 
	return ch 
}

function deleteObject(myForm, actionValue) 
{ 
   var answer=confirm("Souhaitez-vous effectuer cette suppression ?");
   if (answer) {
      myForm.action.value = actionValue;
      myForm.submit();
   } 
}

function getAjaxRequest()
{
  if (typeof XMLHttpRequest != 'undefined')
  {
    return new XMLHttpRequest(); 
  } 
  try 
  { 
    return new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e) 
  {
    try 
    {
      return new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {}
  } 
  return null;
}	

function getTextContent(Node){
    if((Node || (Node = this)).nodeType === 3)
        return Node.nodeValue;
    var childNodes = Node.childNodes,
        data = [],
        i = 0;
    while(childNodes[i])
        data[i] = getTextContent(childNodes[i++]);
    return data.join("");
};

function getChildNodeValue(obj,name)
{
	var result = "";
	try
	{
		result = obj.getElementsByTagName(name).item(0).firstChild.nodeValue;
	}
	catch(e)
	{
		//noeud non présent
	}
	finally
	{
		return result;
	}	
}

/*
* Retourne l'indice selectionné dans une liste box 
*/
function getIndexListboxSelectedValue(zeObjet)
{
  for (i=0;i<zeObjet.length;i++)
  {
    if (zeObjet[i].selected==true)
    {       
      return i;
    }
  } 
  return -1;
}

/*
* Retourne la valeur associée à une option selectionné dans une listbox
*/
function getListboxSelectedValue(zeObjet)
{
 var indice=getIndexListboxSelectedValue(zeObjet);
 return zeObjet[indice].value;
}

/** multibox **/

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}
