
var aDiaporamasConfig = new Array();

function set_js_active() {
    $('body').addClass("js_active");
}

jQuery(document).ready(function(){
    
     
	$('.section_head').click(function() {
		$(this).next().slideToggle();
		$(this).toggleClass('open');
		return false;
	}).next().hide();
	
	if($("#main_nav")){
        select_list($("#main_nav .niveau_1"));
    }
	
    
    /**  Ouverture en lightbox */
	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$("a[rel]").colorbox({
		speed: 'fast',
		effect: '',
		width: '800px',
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");
			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}
	});
	
	
	for(i = 0 ; i < aDiaporamasConfig.length ; i++)
		{
		sID = aDiaporamasConfig[i][0];
		oOptions = aDiaporamasConfig[i][1];
		oPlaylist = aDiaporamasConfig[i][2];
		
		if ($('#diaporama_' + sID).length > 0) 
			new Player( $('#diaporama_' + sID), oPlaylist, oOptions, sID);
		}
		
	/*if ($('#diaporama_0').length > 0) {
        new Player( $('#diaporama_0'), playlist_0, options_0, '0');
    }
	if ($('#diaporama_1').length > 0) {
        new Player( $('#diaporama_1'), playlist_1, options_1, '1' );
    }*/
	
	/*
            $("a[rel='slideshow_1']").colorbox();
			$("a[rel='slideshow_0']").colorbox();*/
	$(".popin").colorbox({scalePhotos : true, maxWidth : 800});

});


/** 
listes deroulantes
*/
function select_list(btn){
    
    btn.attr("tabindex", "0");
	btn.each(function(index, el) {
        $(this).attr("tabindex", "0");
        $(this).mouseover(function() {
            $(this).addClass("open");
        })
         $(this).mouseout(function() {
            $(this).removeClass("open");
        })
         $(this).focus(function() {
            $(this).addClass("open");
        })
         var last_a = $("a",this);
         last_a = last_a[last_a.length-1];
         $(last_a).blur(function() {
        $(el).removeClass("open");
        })
	});
}

/**
*	FONCTION DE CRYPTAGE DE MAIL
**/

var uncrypt_emails = new Array();

$(document).ready(function() {
	uncrypt_links();
	Kaliop_Register_Helper.init();
});


function uncrypt_links(){
	
	var links = $('a.uncrypt');
	
	links.each(function(i,item){	
		var key = $(this).attr("href");
		var cryptedArray = uncrypt_emails[key];
		
		// Décryptage
		var ch = 'm' + String.fromCharCode(97) + 'ilt' + String.fromCharCode(111) + ':';
		
		for(i = cryptedArray.length - 1; i >= 0; i--){
			var tmp = cryptedArray[i].split(':');
			
			ch += String.fromCharCode(tmp[0]);
			
			if(tmp[1] != ''){
				ch += tmp[1];
			}
		}
		
		$(this).attr('hr' + 'ef', ch);
	});
	
}


var Kaliop_Register_Helper =
{
    init : function()
    {
	  $('.numclient')
		.next(0)
		.children('input')
		.bind('keyup', Kaliop_Register_Helper.onNumClient_Change)
		.trigger('keyup')
	  ;
    },
    
    onNumClient_Change : function(e)
    {
	  if($.trim($(this).val())==''){
		$('.kaliop_register_helper_hideme_numclient').hide();
	  }
	  else{
		$('.kaliop_register_helper_hideme_numclient').show();
	  }
	  
    }
}

var GMap = function () 
	{
    return {
        IS_LOADING : false,
        READY_CALLBACKS : [],

        // Stockage des callbacks jusqu'au chargement de la librairies GMap puis execution
        ready : function (callback) {
            if (!this.IS_LOADING && typeof(google) != 'undefined') this.IS_LOADING = true;

            if (this.IS_LOADING) {
                if (this.READY_CALLBACKS.length > 0) {
                    var _callback = false;
                    while (_callback = this.READY_CALLBACKS.shift()) {
                        _callback();
                    }
                }
                if (typeof (callback) == 'function') {
                    callback();
                }
            } else {
                if (typeof (callback) == 'function') {
                    this.READY_CALLBACKS.push(callback);
                }
            }
        },

        create_map : function (id, options) {
            options = options || {};
            options.mapTypeId = options.mapTypeId || google.maps.MapTypeId.ROADMAP;
			
			if(id.indexOf("eng-GB",0) != -1)
				 options.zoom = options.zoom || 2;
			else
				options.zoom = options.zoom || 5;
			
			options.center = new google.maps.LatLng(47.861327,3.352734);
			
            return new google.maps.Map(document.getElementById(id), options);
        },
       
        show_node_location : function (id, ezgmaplocation) {
            var options = {};
            options.zoom = ezgmaplocation.zoom || false;

            var map = this.create_map(id, options);
            var marker = GMap.Json.toMarker(ezgmaplocation);
            marker.setMap(map);
      
        },

        Json : {
            toLatLng : function (json) {
                return new google.maps.LatLng(json.latitude, json.longitude);
            },
            toMarker : function (json) {
                var cfg = {};
                if (json.latLng) cfg['position'] = GMap.Json.toLatLng(json.latLng);
                return new google.maps.Marker(cfg);
            }
        },

        Collection : {
            map : false,
            markers : [],

            init : function (id, markers) {
                this.map = GMap.create_map(id);
                if (markers) this.add_markers(markers);
            },
            
            add_markers : function (markers) {
                var id, latLng, bounds, marker, infowindow = false;

                bounds = new google.maps.LatLngBounds();
                infowindow = new google.maps.InfoWindow({maxWidth : 300});

                for(id in markers) {
                    marker = GMap.Json.toMarker(markers[id]);
                    bounds.extend(marker.getPosition());

                    if (markers[id].infoWindow) {
                        google.maps.event.addListener(marker, 'click', function (map, marker, infoWindow, json) {
                                return function() {
                                    infoWindow.close();
                                    if (json.id) {
                                        infoWindow.setContent(jQuery('#' + json.id).html());
                                    } else {
                                        infoWindow.setContent(json.content);
                                    }
                                    infoWindow.open(map, marker);
                                }
                            }(this.map, marker, infowindow, markers[id].infoWindow)
                        );
                    }

                    marker.setMap(this.map);
                }
				
				this.map.fitBounds(bounds);
				setTimeout(function () { if (GMap.Collection.map.getZoom() > 8) GMap.Collection.map.setZoom(8) }, 250);
            },
            
            load_gpx : function(filepath) {
                jQuery.ajax({
                    url : filepath,
                    dataType : 'xml',
                    success : GMap.Collection.add_gpx
                });
                
            },
            
            add_gpx : function (xml) {
                var parser = new GPXParser(xml, GMap.Collection.map);
                parser.setTrackColour("#ff0000");		// Set the track line colour
                parser.setTrackWidth(5);			// Set the track line width
                parser.setMinTrackPointDelta(0.001);		// Set the minimum distance between track points
				//parser.CenterAndZoom(xml, G_SATELLITE_MAP);	// Center and Zoom the map over all the points.
                parser.addTrackpointsToMap();			// Add the trackpoints
                parser.addWaypointsToMap();
            }
        }
    }
}();


function GMapReady (callback) 
	{
    GMap.ready(callback);
	}
	
	
	
	
	
/*
 * Gestion des popups
 */
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(identifier){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#" + identifier).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(identifier){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#" + identifier).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(identifier)
	{
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + identifier).height();
	var popupWidth = $("#" + identifier).width();
	//centering
	$("#" + identifier).css({
							"position": "absolute",
							"top": windowHeight/2-popupHeight/2,
							"left": windowWidth/2-popupWidth/2
						});
						
	//only need force for IE6
	$("#backgroundPopup").css({
								"height": windowHeight
								});
	
	}
	
