/*
 * JS pour les sites satellites
 * SGL - egzakt.com
 * 2010-02-08
 */

function sites_sat_ready() {
	Sites.init();
}


/* OBJET Sites
 * Sites satellites
 * @author SGL
 * @date 2010-02-08
 */
 
var Sites = {

	donnees_i18n: {
		fr: [
			{ titre: "Sites des associations d'avocats", lien: "/associations/index.html", img: "assos.jpg" },
			{ titre: "Sites des<br> 15 barreaux<br> de section", lien: "/sections/index.html", img: "sections.jpg" },
			{ titre: "Fonds d'assurance responsabilité professionnelle", lien: "/organismes/assurance-responsabilite/index.html", img: "farpbq.jpg" },
			{ titre: "Centre d'accès à l'information juridique (CAIJ)", lien: "/organismes/caij/index.html", img: "caij.jpg" },
			{ titre: "Corporation de services du Barreau ", lien: "/organismes/corporation-services/index.html", img: "corpo.jpg" },
			{ titre: "Information juridique dans un langage simple et accessible  ", lien: "/organismes/educaloi/index.html", img: "educaloi.jpg" },
			{ titre: "Le sténographe officiel&nbsp;: un métier d'avenir", lien: "/organismes/ecole-stenographie/index.html", img: "steno.jpg" },
			{ titre: "École du Barreau&nbsp;: pour les futurs avocats ", lien: "/organismes/ecole-barreau/index.html", img: "ecole.jpg" },
			{ titre: "Pour l’avancement des connaissances  ", lien: "/organismes/fondation/index.html", img: "fondation.jpg" },
			{ titre: "Programme d'aide aux membres du Barreau (PAMBA)", lien: "/organismes/pamba/index.html", img: "pamba.jpg" },
			{ titre: "Pro Bono Québec&nbsp;: avocats  pour le bien public   ", lien: "/organismes/pro-bono-quebec/index.html", img: "pro-bono.jpg" }
		],
		en: [
			{ titre: "Links to associations of lawyers", lien: "/associations/index.html", img: "assos.jpg" },
			{ titre: "Regional bars web sites", lien: "/sections/index.html", img: "sections.jpg" },
			{ titre: "Fonds d'assurance responsabilité professionnelle", lien: "/organismes/assurance-responsabilite/index.html", img: "farpbq.jpg" },
			{ titre: "Centre d'accès à l'information juridique (CAIJ)", lien: "/organismes/caij/index.html", img: "caij.jpg" },
			{ titre: "Corporation de services du Barreau ", lien: "/organismes/corporation-services/index.html", img: "corpo.jpg" },
			{ titre: "Legal information in plain language (French)", lien: "/organismes/educaloi/index.html", img: "educaloi.jpg" },
			{ titre: "Le sténographe officiel&nbsp;: un métier d'avenir", lien: "/organismes/ecole-stenographie/index.html", img: "steno.jpg" },
			{ titre: "École du Barreau&nbsp;: for future lawyers ", lien: "/organismes/ecole-barreau/index.html", img: "ecole.jpg" },
			{ titre: "For advancement of knowledge (French)", lien: "/organismes/fondation/index.html", img: "fondation.jpg" },
			{ titre: "Programme d'aide aux membres du Barreau (PAMBA)", lien: "/organismes/pamba/index.html", img: "pamba.jpg" },
			{ titre: "Pro Bono Québec&nbsp;: lawyers for the public good", lien: "/organismes/pro-bono-quebec/index.html", img: "pro-bono.jpg" }
		]
	},

	image_path: "/images/"+langue+"/sites-sat/",
	donnees: { },

	in_anim: false,		// Si l'animation est active (change_realisation_anim)
	in_action: false,	// En cours de transision	(in_action_realisation)
	change_time: 3000,	// Temps entre chaque frame (millisecondes) (change_frame_time)
	change_timer: null,	// Timer (change_realisation_timer))
	site_ini: 0,		// Index du site inital dans le tableau (0 = premier)
	
	nav_css: "#sites_sat",
	conteneur: '#sites_sat .site_conteneur',
	
	init: function() {

		this.donnees = langue == "en" ? this.donnees_i18n.en : this.donnees_i18n.fr;
		
		this.populate();

		$(this.nav_css+' .handle').click(function() {
			Sites.change(this,Sites.conteneur,true);
			return false;
		});

		$(this.nav_css+' .site').hover(
			function() { $(this).children('a.titre').addClass('titre_selected'); return Sites.stop(); },
			function() { $(this).children('a.titre').removeClass('titre_selected'); return Sites.start(); }
		);

		$(this.nav_css+' .handle').hover(
			function() { return Sites.stop(); },
			function() { return Sites.start(); }
		);

		this.debuter();
	},
	
	debuter: function () {
	
		this.in_anim = true;
	
		if (!this.change_timer)
			this.change_timer = setInterval(function() { Sites.interval_function(); },this.change_time);
	},

	interval_function: function () {
		this.change('.handle_right',Sites.conteneur,false);
	},

	change: function (handle,conteneur,force) {
	
		if (this.in_action)
			return false;

		if (!this.in_anim && !force)
			return false;
		
		this.in_action = true;
		
		$(handle).addClass('handle_selected');
		
		var offset = !force || $(handle).is(".handle_right") ? 1 : -1;

		// Deplacement vers la droite : next
		if (offset == 1) {

			$(this.conteneur+ ' .selected').prev().animate({ width:0 },1000, function() {
	
				$(conteneur+' .selected').removeClass('selected').next().addClass('selected');
				$(conteneur+' div.site:first').appendTo(conteneur).css('width','92px');

				var id_selected = parseInt($(conteneur+' .selected').attr('id').replace('site_sat_',''));
				var id_next = id_selected == Sites.donnees.length - 1 ? 0 : id_selected+1;

				var n = Sites.donnees[id_next];

				$(conteneur+' .selected').next().attr('id','site_sat_'+id_next).children('a.logo').attr('href',n.lien).css('backgroundImage','url('+Sites.image_path+n.img+')').next().html(n.titre).attr('href',n.lien);

				$(handle).removeClass('handle_selected');
				Sites.in_action = false;
			});
		
		// Deplacement vers la droite : prev
		} else {
		
			$(conteneur+' div.site:last').css('width','0px').prependTo(conteneur);

			$(this.conteneur+ ' div.site:first').animate({ width:'92px' },1000, function() {
				$(conteneur+' .selected').removeClass('selected').prev().addClass('selected');
				
				var id_selected = parseInt($(conteneur+' .selected').attr('id').replace('site_sat_',''));
				var id_prev = id_selected == 0 ? Sites.donnees.length - 1 : id_selected-1;
				
				var n = Sites.donnees[id_prev];
				
				$(conteneur+' .selected').prev().attr('id','site_sat_'+id_prev).children('a.logo').attr('href',n.lien).css('backgroundImage','url('+Sites.image_path+n.img+')').next().html(n.titre).attr('href',n.lien);
				
				$(handle).removeClass('handle_selected');
				Sites.in_action = false;
			});
		}

		return false;
	},
	
	erreur: function () {
		this.in_action = false;
		//alert("Une erreur s'est produite.");
	},
	
	start: function () {
		if (!this.change_timer) this.change_timer = setInterval(function() { Sites.interval_function(); },this.change_time);
		this.change_anim = true;
	},

	stop: function () {
		this.change_anim = false;
		clearInterval(this.change_timer);
		this.change_timer = null;
	},
	
	populate: function () {
	
		if (this.donnees.length < 2)
			return false;
		
		var site_index = this.site_ini;
		var site_prec = this.site_ini == 0 ? this.donnees.length - 1 : this.site_ini - 1;
		var site_suiv = this.site_ini == this.donnees.length - 1 ? 0 : this.site_ini + 1;
		
		var site = Array();
		
		site[0] = this.donnees[site_prec];	// Precedent
		site[1] = this.donnees[site_index];	// Selected
		site[2] = this.donnees[site_suiv];	// Suivant
		
		$.each(site,function(i, n) {

			if (i == 0) {id=site_prec;} else if (i == 1) {id=site_index;} else {id=site_suiv;}
			
			$('<div>').attr("id","site_sat_"+id).addClass('site').appendTo(Sites.conteneur);
			$('<a>').addClass('logo').attr('href',n.lien).attr('target','_parent').attr('title',n.titre).css('backgroundImage','url('+Sites.image_path+n.img+')').prependTo("#"+"site_sat_"+id);
			$('<a>').addClass('titre').attr('href',n.lien).attr('target','_parent').attr('title',n.titre).html(n.titre).appendTo("#"+"site_sat_"+id);
		});
		
		$(Sites.conteneur+' div:first').next().addClass('selected');
	}
};
