String.prototype.trim = function() {
    var str = this;
    
    for(var i=0; i<this.length; i++)
        if(this[i] == ' ')
            str = str.replace(' ', '');

    return str;
}


var Rule13 = {
    current_image: null,
    current_rel: null,
    current_holder: [],
    images: [],
    loaded: false,
    init: function() {
        var emails = document.getElementsByClassName('email');
        for(var i=0; i < emails.length; i++) {
            var email = emails[i].innerHTML;
            email = email.trim();
            email = email.replace('[replace_by_at]', '@');
            email = email.replace('[replace_by_dot]', '.');
            email = email.replace('[replace_by_com]', 'com');
            emails[i].innerHTML = email;
            emails[i].href = 'mailto:'+email;
        }
        
        var errors = document.getElementsByClassName('error');
        for(var i=0; i < errors.length; i++) {
            errors[i].onkeydown = function (){
                this.removeClassName('error');
            };
        }
        
        var links = document.getElementsByClassName('image_link');
        for(var i=0; i < links.length; i++) {
            this.images[i] = new Image();
            this.images[i].src = links[i].href;
 
            links[i].onclick = function() {
                return Rule13.image(this, i);
            };
        }
        
        this.loaded = true;
    },
    image: function(link, number) {
        if(!this.loaded) return false;
        if(!this.current_holder[link.rel]) this.current_holder[link.rel] = 1;
        
        this.current_rel = link.rel;
        this.disable();

        if(link.href != this.current_image && !this.fading) {
            this.fading = true;
            this.current_image = link.href;
            link.className = 'image_link active';

            if(this.current_holder[link.rel] == 1) {
                $(link.rel+'_2').src = link.href;
                new Effect.Appear(link.rel+'_2', { duration: 0.5 });
                new Effect.Fade(link.rel+'_1', { duration: 0.5, afterFinish: function() { Rule13.fading = false; } });
            
                this.current_holder[link.rel] = 2;
            }
            else {
                $(link.rel+'_1').src = link.href;
                new Effect.Appear(link.rel+'_1', { duration: 0.5 });
                new Effect.Fade(link.rel+'_2', { duration: 0.5, afterFinish: function() { Rule13.fading = false; } });
                
                this.current_holder[link.rel] = 1;
            }
            
            return false;
        }
        return false;
    },
    
    disable: function() {
        var links = document.getElementsByClassName('image_link');
        for(var i=0; i<links.length; i++) {
            if(links[i].rel == this.current_rel) {
                links[i].className = 'image_link';
                links[i].onclick = function() {
                    return Rule13.image(this);
                };
            }
        }
    }
};

window.onload = function() {
    Rule13.init();
}