(function($){ 
	$.fn.tabloid=function(options){ // nom de la fonction et passage de parametres
		var defaults = { 
		    nbpics:68,
		    nbvignettes:96,
		    premier:1
		}
		var options = $.extend(defaults, options); // recupere les valeurs - options ecrasant default)
		return this.each(function(){ // renvoie l'ensemble des instances demandees - permet la chainabilite jQuery
			
			//remet a zero les cases, au clic sur la rubrique "What I Like"
			$('#accordeon>ul>#univers>a')
			.click(function(e){
				$('#accordeon ul #univers .contents .vignettes').each(function() {
						$(this).css( {background: 'url(css/img/vignettes/v-default.png) no-repeat black'} );
				});					
			});
			
			
			//place les vignettes, au clic sur la rubrique "What I Like"
			$('#accordeon>ul>#univers>a')
			.click(function(e){
				_demarrage(options.premier);
				$.ajax({
					url: 'http://www.apadeloup.com/js/tabloid-light.json',
					dataType: 'json',
					data: '',
					success: function(data) {
						//alert(JSON.stringify(data, null, null));
						positions = _positions(options.nbvignettes);  //cree le tableau des positions aleatoires
						var tabloids=data.univers;
						var i = 0;
						$('#accordeon ul #univers .contents .vignettes').each(function() {
							var pos = positions[i];
							if(pos < options.nbpics && i < options.nbvignettes) {
								$(this).css( {background: 'url('+tabloids[pos].url+') no-repeat black'} );
								$(this).attr("id", pos);
							}
							i++;
						});					
					},
					error: function(xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                        alert(thrownError);
                    }, 
				});
			});
							
			//recupere les donnees associees a la vignette cliquee
			$('#accordeon ul #univers .contents .vignettes')
			.mouseover(function(e) { $(this).css({cursor:'pointer'}); } )
			.click(function(e){ 
				var i = $(this).attr("id");
				$.ajax({
					url: 'http://www.apadeloup.com/js/tabloid-full.json',
					dataType: 'json',
					data: '',
					success: function(data) {
						//alert(JSON.stringify(data, null, null));
						var tabloids=data.univers;
						var artiste = '<div id="artiste">'+tabloids[i].personne;
						if((tabloids[i].metier) != "") { artiste = artiste + ' - '+tabloids[i].metier; }
						artiste = artiste +'</div>';
						artiste = $(artiste);
						
						var oeuvre = '<div id="oeuvre">'+tabloids[i].oeuvre;
						if((tabloids[i].type) != "") { oeuvre = oeuvre +' ('+tabloids[i].type+')'; }
						oeuvre = oeuvre +'</div>';
						oeuvre = $(oeuvre);
						
						$('#accordeon ul #univers .contents #artiste').replaceWith(artiste);
						$('#accordeon ul #univers .contents #oeuvre').replaceWith(oeuvre);						
						$('#accordeon ul #univers .contents #selection').css( {background: 'url('+tabloids[i].url+') no-repeat black'} );
						Cufon.refresh();
					},
					error: function(xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                        alert(thrownError);
                    }, 
				});
			});
			
			return false;
		});
		
		
		//recupere les donnees de la vignette par defaut
		function _demarrage(premier) {
			$.ajax({
					url: 'http://www.apadeloup.com/js/tabloid-full.json',
					dataType: 'json',
					data: '',
					success: function(data) {
						//alert(JSON.stringify(data, null, null));
						var tabloids=data.univers;
						var artiste = '<div id="artiste">'+tabloids[premier].personne;
						if((tabloids[premier].metier) != "") { artiste = artiste + ' - '+tabloids[premier].metier; }
						artiste = artiste +'</div>';
						artiste = $(artiste);

						var oeuvre = '<div id="oeuvre">'+tabloids[premier].oeuvre;
						if((tabloids[premier].type) != "") { oeuvre = oeuvre +' ('+tabloids[premier].type+')'; }
						oeuvre = oeuvre +'</div>';
						oeuvre = $(oeuvre);
						
						$('#accordeon ul #univers .contents #artiste').replaceWith(artiste);
						$('#accordeon ul #univers .contents #oeuvre').replaceWith(oeuvre);
						$('#accordeon ul #univers .contents #selection').css( {background: 'url('+tabloids[premier].url+') no-repeat black'} );
						Cufon.refresh();
					},
					error: function(xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                        alert(thrownError);
                    }, 
			});
			return false;
		}
		
		//fonction aleatoire
		function _randOrd(){
				return (Math.round(Math.random())-0.5); 
			}
	
		//cree le tableau des positions aleatoires	
		function _positions(max) {
			nb = 0;
			positions = new Array();
			while(nb < max) {
				positions[nb] = nb;
				nb++;
			}			
			positions.sort( _randOrd );
			return positions;
			//alert('Random : ' + positions + '<br />');	
		}
	
	};
})(jQuery);
