var CategoriasProductosSlide = new Class({
	 options: {
		offsets: { x:0, y:0 },
		positionVertical: 'top',
		positionHorizontal: 'right',
		speed: 400
	 },
	addEvent: function(evento,funcion)
	{
		this.boton.addEvent(evento,funcion);
	},
	fireEvent: function (evento,parametro)
	{
		this.boton.fireEvent(evento,parametro);
	},
	initialize: function(menu,newoptions) {

		/*ENLACES agregarACategoriasProductos*/
		// IE 6 - XML prolog problem
		if(window.ie6 && document.compatMode=="BackCompat"){
			this.options.animateCaption = false;
		}

		this.anchors = [];
		$$('a').each(function(el){
			if (el.rel && el.rel.test(/^enlaceCategoria/i)){
				el.addEvent('click',ejecutarBusquedaCarrito.bind(el));				
				this.anchors.push(el);
			}
		}, this);

		
		/*SLIDE*/
		this.options = $merge(this.options, newoptions);//this.setOptions(options);
		this.menu = $(menu);
		this.boton = $(this.menu.id+"_Boton");
		this.contenido = $(this.menu.id+"_Contenido");
		this.divItemsCategoriasProductos = $(this.menu.id+"_Scrollable");
		
		if(this.contenido && this.boton)
		{	
			this.slider = new Fx.Slide(this.menu.id+"_Contenido",{mode:'horizontal'});			
			
			var slideOut = function(e){		  
				  e = new Event(e).stop();													  
				  this.slider.toggle();				  
				  if(typeof(carritoDeCompras) != 'undefined')
					  carritoDeCompras.slider.slideOut();
				  e.stop();
			}.bind(this);
			
			var slideScroll = new Scroller(this.menu.id+"_Scrollable", {area: 70, velocity: .1});
			this.contenido.addEvent('mouseover', slideScroll.start.bind(slideScroll));
			
			
			/*NO ANIMACION INICIAL**/
			this.slider.hide();
			/*NO ANIMACION INICIAL**/			
			this.contenido.setStyle('display','block');
						
			this.boton.addEvent('click',slideOut);
			this.contenido.addEvent('mouseleave',slideOut);
		}
		
		this.tween = new Fx.Styles(this.menu,{ duration: this.options.speed, wait: false });
		/*this.move = this.options.mode == 'vertical' ? 'y' : 'x';*/
		/*this.property = this.move == 'y' ? 'positionVertical' : 'positionHorizontal';*/

		/* ensure a few things */
		var css = { position: 'absolute', display:'block' };

		css[this.options.positionHorizontal] = 0;
		css[this.options.positionVertical] = 0;

		this.menu.setStyles(css);

		/* start listening */
		this.startListeners();
	},
	startListeners: function() {
		var action = function() {
			
			moveX = this.options.positionHorizontal == 'right' ? this.options.offsets['x'] - window.getSize().scroll['x'] : window.getSize().scroll['x'] + this.options.offsets['x'];
			moveY = window.getSize().scroll['y'] + this.options.offsets['y'];
			this.setPosition(moveX, moveY);

		}.bind(this);
		window.addEvent('scroll',action);
		var animacionInicial = function() 
		{		
			var css = {};
			moveX = this.options.positionHorizontal == 'right' ? this.options.offsets['x'] - window.getSize().scroll['x'] : window.getSize().scroll['x'] + this.options.offsets['x'];
			moveY = window.getSize().scroll['y'] + this.options.offsets['y'];
			css[this.options.positionHorizontal] = moveX;
			css[this.options.positionVertical] = moveY;
			
			var slideout = function (){
				this.slider.slideOut();
			}.bind(this);//delay(1500,this);
			this.tween.start(css).chain(function(){slideout.delay(1500,this);}.bind(this));//.chain(function(){alert('a');});
		}.bind(this);
		window.addEvent('load',animacionInicial);
	},
	setPosition: function(moveX,moveY) {		
		var css = {};
		css[this.options.positionHorizontal] = moveX;
		css[this.options.positionVertical] = moveY;
		this.tween.start(css);
		return this;
	}
});

/* usage */
var categoriasDeProductos = {};
var loadCategorias = function(){	
	if($('slideCategoriasProductos'))
	{
		categoriasDeProductos= new CategoriasProductosSlide('slideCategoriasProductos',{
		  positionHorizontal: 'left',
			offsets: {
				x: 65,
				y: 50
			}
		});
	}
}
window.addEvent('domready',function() {
	if($('slideCategoriasProductos'))
	{
		loadCategorias();
	}
	else
	{
			window.addEvent('domready',loadCategorias);
	}
});