
window.addEvent('domready', function(){
    var $slidemenu = $$('ul#menu2 a');
    
    var $imgInsert = new Element('img', {
        id: 'image-insert'
    });
    
    var $sizeImg;
    var $fxImgInsert;
    
    var $izqFlecha = new Element('a', {
        id: 'left'
    });
    
    var $derFlecha = new Element('a', {
        id: 'right'
    });
    
    var $cuadroPicture = $('pictures');
    
    // Creamos objeto de request por AJAX para cargar el film de cada proyecto.
    var $ajaxRequest = new Request.HTML({
        onFailure: function(xhr){
            if (xhr.status == 404) {
                $cuadroPicture.set('html');
            }
        },
        onSuccess: function($tree, $nodes, $html, $js){
            $cuadroPicture.empty(); // <- Vaciamos el contenido de lo que hay dentro del nodo ul con atributo id="pictures"
            // Insertamos los nodos necesarios para explorar el film
            $imgInsert.inject($cuadroPicture);
            $izqFlecha.inject($cuadroPicture);
            $derFlecha.inject($cuadroPicture);
            $widthImg = $imgInsert.getSize().x;
            
            // Menu de navcegacion del Film
            
            // Condiciones iniciales
            $cursor = 450;
            $imgInsert.setStyle('left', 0);
            
            $fxImgInsert = new Fx.Morph($imgInsert);
			calcConditions();
            // --------------------------------------------
        }
    });
    
    $izqFlecha.addEvents({
        'mouseenter': function($ev){
            if ($izqFlecha.enabled) $izqFlecha.addClass('left-over')
        },

        'mouseleave': function($ev){
            if ($izqFlecha.enabled) $izqFlecha.removeClass('left-over')
        },
        'click': function($ev){
            if ($izqFlecha.enabled) {
                $cursor -= 450;
                $fxImgInsert.start({
                    'left': $cursor * (-1) + 450
                });
                calcConditions();
            }
        }
    });

    $derFlecha.addEvents({
        'mouseenter': function($ev){
            if ($derFlecha.enabled) 
                $derFlecha.addClass('right-over')
        },
        
        'mouseleave': function($ev){
            if ($derFlecha.enabled) 
                $derFlecha.removeClass('right-over')
        },
        'click': function($ev){
            if ($derFlecha.enabled) {
                $cursor += 450;
                $fxImgInsert.start({
                    'left': $cursor * (-1) + 450
                });
                calcConditions();
            }
        }
    });
    
    // Calculamos condiciones de navegacion del film		
    var calcConditions = function(){
        // Condiciones para la flecha derecha
        if ($widthImg > $cursor) 
            $derFlecha.enabled = true;
        else {
	        $derFlecha.enabled = false;
            $derFlecha.removeClass('right-over');
        }
        
        // Condiciones para la flecha izquierda
        if ($cursor > 450) 
            $izqFlecha.enabled = true;
        else {
			$izqFlecha.enabled = false;
            $izqFlecha.removeClass('left-over');
        }
        
        
    }
    
    // Menu de proyectos
    $slidemenu.each(function($option, $iO){
        $option.addEvent('click', function($ev){
            $ev.stop();
            
            //Paramos el barackslideshow	
            slideshow.stop();
            
            // Definimos algunas propiedades del objeto AJAX
            $ajaxRequest.options.url = this.get('href');
            $imgInsert.set('src', this.get('href'));
            
            $ajaxRequest.send();
        });
    })
    
    
    
    
});
