/*--------------------------------------------------------------------------
 *  YOJA-WEB.COM : module cycle
 *  Copyright (c) Jamal Chaqouri, Yohann Fontaine, of yoja team (http://www.yoja-web.com)
 *  module cycle is a yoja-web.com work.
 *  For details, see the yoja web site: http://www.yoja-web.com
/*--------------------------------------------------------------------------*/

YOJA.module.cycle = {
	
	constants : {
		CYCLE_CLASS : ".yojaCycle"
	},
	
	parameters : {
		activeDefault : true
	},
	
   getParameters : function(cycle){
		var parameters = null;
		if(cycle == null){
		   parameters = YOJA.module.cycle.parameters["yojaCycle"] || []; 
		} else {
		 parameters = YOJA.module.cycle.parameters[cycle.attr("id")] || [];   
		}
	   parameters.fx =  parameters.fx || 'fade';
	   parameters.speed =  parameters.speed || 1000;
	   parameters.pause =   parameters.pause || 1;
	   parameters.delay =  parameters.delay ||  -2000 ;
	   return  parameters;
   },
   
   loadCycle: function(){
	     var listCategories = $j("div.category_item:has(h3 a)");
		 var params = YOJA.module.cycle.getParameters();
     	 listCategories.throttle(function(i){					  
				 var hrefToload = $j("h3 a", $j(this)).attr("href");
				 var conteneur = $j("div.category_item_content div.center", $j(this));
				 conteneur.hide();          
				 conteneur.load(hrefToload+" .center a:has(img)", function(){       
						$j("img", conteneur).each(function(){
					        $j(this).attr("src", $j(this).attr("src").replace(/large/g, "medium")); 
						});
						
						if($j("a", conteneur).length>1){
							$j("a img", conteneur).css({height:85, width:85});
							conteneur.cycle(params);
						}
						conteneur.css({height:105, width:87, margin:'auto'});
						conteneur.removeClass("center").addClass("category_yoja_cycle");
						conteneur.show();
					}); 
				 
    	 },{per: 3, pause: 35});
	   
   },
	
	init:function(){
		if(YOJA.module.cycle.parameters.activeDefault){
			YOJA.module.cycle.loadCycle();
		}
	
		$j(YOJA.module.cycle.constants.CYCLE_CLASS).each(function(){
	   		var conteneur = $j(this);
			conteneur.cycle(YOJA.module.cycle.getParameters(conteneur));    
		});
		
	}

}
/*******************************************************************************
 * Charger dynamiquement les dépendances puis exécution du module
 ******************************************************************************/
YUI(
		{
			loadOptional :(typeof jQuery == 'undefined'),
			modules : {
				jquery : {
					name :"jquery",
					type :"js",
					fullpath :"http://yoja-web.com/javascript/build/plugins/jquery/jquery.js"
				},
				yoja : {
					name :"yoja",
					type :"js",
					fullpath :"http://yoja-web.com/javascript/build/plugins/yoja/yoja.core.js",
					optional :[ "jquery" ]
				},				
				cycle : {
					name :"cycle",
					type :"js",
					fullpath :"http://yoja-web.com/javascript/build/plugins/cycle/jquery.cycle.all.js",
					optional : [ "jquery" ]
				},
				throttle : {
					name :"throttle",
					type :"js",
					fullpath :"http://yoja-web.com/javascript/build/plugins/jThrottle/jquery.jthrottle.js",
					optional : [ "jquery" ]
				}				
			}
		}).use('yoja', 'cycle', 'throttle', function(Y) {
			YOJA.module.cycle.init();

});