var RedLink,
do_red_links,
red_links = {};

window.addEvent( 'domready' , function(){
    do_red_links();
} );

do_red_links = function(){
    var links = document.getElements( 'li > a' );
    links.each( function( link ){
        var link = new RedLink( link ),
        key = link.get_key();
        if ( red_links[key] == undefined ) {
            red_links[key] = [link];
        } else {
            red_links[key].include( link )
        }
    } );

    Object.each( red_links , function(rl , i){
        if ( rl.length == 2 ) {
            rl[0].corresponding = rl[1];
            rl[1].corresponding = rl[0];
        }
    } );
}

RedLink = new Class({
    link : undefined,
    _parent : undefined,
    link_wrapper : undefined,
    image_link_wrapper : undefined,
    image : undefined,
    corresponding : undefined,
    old_colour : undefined,

    initialize : function( link ){
        this.link = link;
        this.old_colour = this.link.getStyle('color');
        this._parent = this.link.getParent();
        this.link_wrapper = new Element( 'span' );
        this.configure();
    },

    get_key : function(){
        if ( this.link.get('rel') == null ) {
            return Math.floor(Math.random() * 10000);
        }
        return this.link.get('rel');
    },

    configure : function(){
        var parent_size = this._parent.getSize();

        this._parent.setStyle( 'position' , 'relative' );
        this.link_wrapper.setStyles({
            'position' : 'absolute',
            'top' : ( parent_size.y - 1 ) / 2,
            'left' : 0,
            'width' : '100%',
            'height' : 1,
            'display' : 'block',
            'opacity' : 0,
            'background' : '#c31f06',
            'margin' : 0,
            'padding' : 0,
            'cursor' : 'pointer',
            'z-index' : 9999,
            'overflow' : 'hidden'
        });
        this.link_wrapper.addEvent( 'click' , ( function(){
            window.location = this.link.get('href');
        }).bind(this) );
        this._parent.grab( this.link_wrapper );

        this.image = this._parent.getElement('a > img');
        if ( this.image !== null ) {
            this.configure_image();
        }
        if ( this.link.get('href') == window.location.pathname.toString() && !this.link.hasClass('dont-autoselect') ) {
            this._enter(false , false);
        } else {
            this._parent.addEvents({
                'mouseenter' : this._enter.bind(this),
                'mouseleave' : this._leave.bind(this)
            });
        }
    },
    _enter : function(event , recurse){
        if ( recurse === undefined ) {
            recurse = true;
        }
        if ( this.corresponding && recurse == true ) {
            this.corresponding._enter(event , false);
        }
        if ( this.image == undefined ) {
            this.link_wrapper.set('tween' , {duration : 1}).tween('opacity' , 1)
            this.link.set('tween' , {duration : 1}).tween('color' , '#98979A')
        } else {
            this.link_wrapper.set('tween' , {duration : 1}).tween('opacity' , 1)
            this.image_link_wrapper.set('tween' , {duration : 400}).tween('opacity' , 0.9);
        }
    },
    _leave : function(event , recurse){
        if ( recurse === undefined ) {
            recurse = true;
        }
        if ( this.corresponding && recurse == true ) {
            this.corresponding._leave(event , false);
        }
        if ( this.image == undefined ) {
            this.link_wrapper.set('tween' , {duration : 1}).tween('opacity' , 0)
            this.link.set('tween' , {duration : 1}).tween('color' , this.old_colour)
        } else {
            this.link_wrapper.set('tween' , {duration : 1}).tween('opacity' , 0)
            this.image_link_wrapper.set('tween' , {duration : 400}).tween('opacity' , 0)
        }
    },
    configure_image : function(){
        this.image_link_wrapper = new Element( 'span' );
        this._parent.grab( this.image_link_wrapper );

        this.image_link_wrapper.setStyles({
            'background' : '#0e0f19',
            'opacity' : 0,
            'width' : '100%',
            'position' : 'absolute',
            'top' : '0',
            'left' : '0',
            'z-index' : '9998',
            'display' : 'block',
            'cursor' : 'pointer'
        });

        this.image_link_wrapper.addEvent( 'click' , (function(){
            window.location = this.link.get('href');
        }).bind(this) );

        this.image.addEvent( 'load' , (function(){
            var size = this.image.getSize();
            this.link_wrapper.setStyles({
                'top' : ( size.y - 2 ) / 2,
                'width' : size.x - 40,
                'left' : 20
            });
            this.image_link_wrapper.setStyle('height' , size.y);
        }).bind(this) );
    }
});

