//
// Home ACtu
//
var sliderNews = new Slider(
{
	"animation":
	{
		"infinite": true,
		"delay": "2000"
	},
	"standelone": 0,
	"startIndice": 0,	
	"init": function(params)
	{
		this.indice = this.startIndice;
		this.standelone = this.startIndice;
		this.initialized = true;
		
		// Lorsque la page est ré-initialiser : 
		// ne pas repartir là obn l'animation s'était arrétée
		this.gotoSlide({
			target: this.getStartSlide(),
			type: "start"
		});
	},

	"onsuccess": function()
	{
		if (typeof this.standelone == "undefined")
		{
			return false;		
		}
		
		var 
			_this = this,
			_slides = this.getSlides() || [],
			_maxSlides = _slides.length,			
			_delay = "6000"
		;
		
		this.standelone++;
		setTimeout(function()
		{
			if (typeof _this.standelone != "undefined")
			{
				// Déroulement de slide en slide
				_this.gotoSlide({
					"target": 
					{
						"data-action": "next", 
						"data-slider": "page-news"
					},
					"type": "standalone"
				});
			}

			// Arret de l'animation
			if (_this.standelone >= _maxSlides)
			{
				delete _this.standelone;
			}
			
		}, _delay);
	}
});

// 
// Page produit : Gallery 
//
var _slider360 = {};
var productView = new Slider(
{		
	"animation": 
	{
		"infinite": true,
		"delay": 500
	},

	// FadeIn/FadeOut des produits
	"animate": function(params)
	{
		if (typeof params != "object" || !params)
		{
			return false;
		}

		var
			_this = this,
			_newSlide = params.newSlide,
			_oldSlide = params.oldSlide,
			_delay = params.delay,
			
			_showNewSlide = function()
			{
				if (driver.hasClass(_newSlide, "active"))
				{
					_activeSlide();
					return false;
				}
				driver.animateCSS(_newSlide, {"opacity" : "1"}, _delay, _activeSlide);
			},
			_activeSlide = function()
			{
				_this.activeSlide(_newSlide, _oldSlide)
			}
		;
		if (_oldSlide)
		{
			driver.changeCSS(_newSlide, {"opacity" : "0"});
			driver.animateCSS(_oldSlide, {"opacity" : "0"}, _delay, _showNewSlide);
		}
		else
		{
			_showNewSlide();
		}
		
	},

	// Initialisation du composant 360
	"onsuccess": function(elm)
	{
		// FIXME : pas d'images de 360 pour l'instant donc
		// on n'active pas la fonctionnalité
		/*
		var _current360 = _slider360[elm.id];
		if (typeof _current360 == "undefined" || !_current360)
		{
			_slider360[elm.id] = new view360();
			_slider360[elm.id].init(elm);
		}
		*/
	}
});

//
//Scroll Categories  
//
var otherCategoriesScrollElement = new scrollElement(
{
	"defaultPosition": "px -380px",
	"buttonWidth": 20
});



//
//Animation Categories  
//
var
	_setBackground = function(elm, action)
	{
		switch(action)
		{
			default:
				driver.changeCSS(elm.parentNode.parentNode, {"backgroundImage": "none"});
				break;

			case "mouseout":
				// Ajout Image de fond en Noir et Blanc
				var _styleBackground = "url('%url%')";
				driver.changeCSS(elm.parentNode.parentNode, {"backgroundImage": _styleBackground.replace("%url%", elm.getAttribute("data-hover"))});
				break;
		}
	},
	_getCSS = function(elm, action)
	{
		var 
			_size = (elm.getAttribute("data-size").toString() || "").split(","),
			_action = action || false
		;
		
		switch(_action)
		{
			default:
				return {
					"width": _size[1].split("|")[0] + "px",
					"height": _size[1].split("|")[1] + "px",
					"marginTop": "80px",
					"marginLeft": "20px"
				};
				break;
			
			case "mouseover":
				return {
					"width": _size[2].split("|")[0] + "px",
					"height": _size[2].split("|")[1] + "px",
					"marginTop": "50px",
					"marginLeft": "20px"
				};
				break;
			
			case "mouseout":
				return {
					"width": _size[0].split("|")[0] + "px",
					"height": _size[0].split("|")[1] + "px",
					"marginTop": "130px",
					"marginLeft": "50px"
				};
				break;
		}
	}
;

var categoryAccordion = new Accordion( 
{
	"handleEvent": "mouseover",
	"className":{"item": "category-picture"},
	"addToHistory": function()
	{
		var _animations = this.current["animations"];	
		for (var i=0, len=_animations.length; i<len; i++)
		{
			var _animation = _animations[i]
			if (_animation.length >= 2)
			{
				driver.deleteAnimate(_animation[0], _animation[1]);
			};
		}
		this.history = null;
		this.busy = 0;
		
		return this.history;
	},
	/*
		3 Possibilités :						
		1. mouseover : _isCurrentElement && value
		2. mouseout : _isCurrentElement && !value
		3. default : !_activeItem
	*/
	"animate" : function(elm, value, handleSuccess)
	{
		var
			_this = this,
			_isCurrentElement = this.current["element"] == elm,
			_activeItem = this.current["active"] || false,
			_delay = "800"
		;
		if (_activeItem)
		{
			if (_isCurrentElement)
			{
				var 
					_params = _getCSS(elm, "mouseover"),
					_animation = driver.animateCSS(elm, {"opacity": "1"}, "100", function()
					{
						_setBackground(elm);
						var _animation = driver.animateCSS(elm, _params, "650", handleSuccess);
						_this.current["animations"].push([elm, _animation]);
					})
				;
			}
			else
			{
				var
					_params = _getCSS(elm, "mouseout"),
					_animation = driver.animateCSS(elm, _params, "500", function()
					{
						_setBackground(elm, "mouseout");
						
						var _animation = driver.animateCSS(elm, {"opacity": "0"}, "250", handleSuccess);
						_this.current["animations"].push([elm, _animation]);
					})
				;
			}
		}
		else
		{
			var 
				_params = _getCSS(elm),
				_animation = driver.animateCSS(elm, {"opacity": "1"}, "150", function()
				{
					_setBackground(elm);
					_animation = driver.animateCSS(elm, _params, "500", handleSuccess);
					_this.current["animations"].push([elm, _animation]);
				})
			;
		}
		this.current["animations"].push([elm, _animation]);
		return false;
	},
	"init": function()
	{
		var
			_elements = this.items || [],
			_tooltips = {}
		;
		for(var i=0, len=_elements.length; i<len; i++)
		{
			var 
				_elm = _elements[i],
				_linkElement = _elm.parentNode,
				_listElement = _linkElement.parentNode
			;

			// Init des propriétés au chargement
			var 
				_width = _elm.offsetWidth,
				_height = _elm.offsetHeight,
				_rapport0 = 68/100,
				_rapport1 = 1.2
			;					
			_elm.setAttribute("data-size", [_width*_rapport0 + "|" + _height*_rapport0, _width + "|" + _height, _width*_rapport1 + "|" + _height*_rapport1]);
			driver.changeCSS(_elm, _getCSS(_elm));
			
			// Création du tooltip
			var plop = new Tooltip();
			plop.init(_listElement);			
		}
	}
});

//Ne pas valider le formulaire
//Si aucune valeur n'est saisie
//ou si la valeur correspond à la valeur par défaut
var _getSearchValue = function()
{
	var _field = this.elm["inputSearch"];
	if (_field)
	{
		if (!_field.value || _field.placeholder == _field.value)
		{
			return false;
		}
	}
	return true;
};

/*
 * Résultat de recherche
 * Champ global
 */
var _searchForm = new AjaxForm(
{
	"onBeforeSubmit": function() 
	{
		var _searchValue = _getSearchValue.call(this);
		return _searchValue;
	},
	"success": function(data)
	{
		// Pas de lightbox lorsqu'il n'y a pas de résultat
		// FIX : mettre un message d'erreur lorsque l'on ne trouve pas de résultat
		var _dataContent = data.responseText || false;
		if (!_dataContent)
		{
			return false;
		}
		
		// Création de la lightbox
		fakeWindowLinks.open(
		{
			"id": this.elm.getAttribute("data-window") || null,
			"data": _dataContent
		});
	}
});

/*
 * Résultat de recherche
 * Formulaire interne
 */
var _resultForm = new AjaxForm(
{
	"onBeforeSubmit": function()
	{
		var _searchValue = _getSearchValue.call(this);
		if (!_searchValue)
		{
			return false;
		}
		
		// masquer le contenu lorsque l'on recharge le résultat
		var _content = this.resultContent || driver.getElement(document.getElementById("searchResults"), ".box-content");
		if (_content)
		{
			this.resultContent = _content;
			driver.addClass(this.resultContent, "loading");
		}
		return true;
	},
	"success": function(response)
	{
		var _container = driver.getElement(document.getElementById("searchResults"), ".box-container");
		if (!_container)
		{
			return false;
		}
		// Update du contenu
		driver.putHTML(_container, response.responseText);
		
		// Exécution de scripts
		_handleScriptAfterOnload();
		
		// Affichage du contenu
		driver.removeClass(this.resultContent, "loading");
	}
});
