
var Aniero = {};

Aniero.start = function() {

    $$('a.view-links').each(function(el) {
        el.setStyle('display', 'inline');

        var textarea = el.getParent().getNext();
        var m = textarea.value.match(/http:\/\//g);
        var n = m.length + 1;
        if (window.opera) n++;
        textarea.setAttribute('rows', n);
        textarea.setAttribute('wrap', 'off');

        textarea.setStyle('display', 'block');
        var fx = new Fx.Slide(textarea, { duration: 250 });
        fx.hide();

        el.addEvent('click', function(event) {
            new Event(event).stop();
            fx.toggle();
        });

        textarea.addEvent('click', function() {
            textarea.focus();
            textarea.select();
            
            if ( !!(window.attachEvent && !window.opera)
                && navigator.userAgent.match(/windows/i)
                && textarea.value.length) {
                copy_obj = textarea.createTextRange();
                copy_obj.execCommand('Copy');
            }
        });
    })

    $$("a.smoothbox").each(function(el) {
        el.onclick = TB_bind
    });
}


/*
 * Smoothbox v20070814 by Boris Popoff (http://gueschla.com)
 *
 * Based on Cody Lindley's Thickbox, MIT License
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// prevent javascript error before the content has loaded
var TB_WIDTH    = 0,
    TB_HEIGHT   = 0,
    TB_doneOnce = 0;

var TB_loading_img = '<img src="/static/images/loading.gif" />';

function TB_bind(event) {
     var event = new Event(event);

    // stop default behaviour
    event.preventDefault();

    // remove click border
    this.blur();

    // get rel attribute for image groups
    var group = this.rel || false;

    // display the box for the elements href
    TB_show(this.href, group);

    this.onclick = TB_bind;
    return false;
}

    // called when the user clicks on a smoothbox link
function TB_show(url, rel ) {

    // create iframe, overlay and box if non-existent

    if ( !$('TB_overlay') ) {
        new Element('iframe')
            .setProperty('id', 'TB_HideSelect')
            .injectInside(document.body);

        $('TB_HideSelect').setOpacity(0);

        new Element('div')
            .setProperty('id', 'TB_overlay')
            .injectInside(document.body);

        $('TB_overlay').setOpacity(0);

        TB_overlaySize();

        TB_create_loading();
        TB_load_position();

        new Fx.Style('TB_overlay', 'opacity', {
            duration: 400,
            transition: Fx.Transitions.sineInOut
        }).start(0, 0.6);
    }

    if ( !$('TB_load') ) {
        TB_create_loading();
        TB_load_position();
    }

    if ( !$('TB_window') ) {
        new Element('div')
            .setProperty( 'id', 'TB_window')
            .injectInside(document.body);

        $('TB_window').setOpacity(0);
    }

    $('TB_overlay').onclick = TB_remove;
    window.onscroll = function() {
        TB_load_position();
        TB_positionEffect();
    }

    // check if a query string is involved
    var baseURL = url.match(/(.+)?/)[1] || url;

    TB_WIDTH  = window.getWidth()  - 100 + 30;
    TB_HEIGHT = window.getHeight() - 140 + 40;

    var contentW = TB_WIDTH  - 30 + 29;
    var contentH = TB_HEIGHT - 45 + 17;

    var link = [
        '<a href="', url, '" title="', url, '">', url, '</a>'
    ].join('');

    $('TB_window').innerHTML = [
        '<div id="TB_title">',
            '<div id="TB_ajaxWindowTitle">', link, '</div>',
            '<div id="TB_closeAjaxWindow"><a href="#" id="TB_closeWindowButton" title="Close">close</a></div>',
        '</div>',
        '<iframe frameborder="0" hspace="0" src="', url, '" id="TB_iframeContent" ',
        'name="TB_iframeContent" style="width:', contentW, 'px;height:', contentH, 'px; " onload="TB_remove_loading()"></iframe>'
    ].join('');

    $("TB_closeWindowButton").onclick = TB_remove;

    TB_position();

    if ( frames['TB_iframeContent'] == undefined ) {
        // be nice to safari
        $(document).keyup( function(e) {
            var key = e.keyCode;
            if ( key == 27 ) { TB_remove() }
        });
        TB_showWindow();
    }

    window.onresize = function() {
        TB_position();
        TB_load_position();
        TB_overlaySize();
    }

    document.onkeyup = function(event) {
        var event = new Event(event);
        // close
        if ( event.code == 27 ) {
            TB_remove();
        }
    }

    TB_showWindow();
}

// helper functions below
function TB_showWindow() {

    if ( TB_doneOnce == 0 ) {
        TB_doneOnce = 1;
        var myFX = new Fx.Style('TB_window', 'opacity', {
            duration : 250,
            transition : Fx.Transitions.sineInOut,
            onComplete : function() {
//                if ( $('TB_load') ) $('TB_load').remove();
            }
        }).start(0, 1);
    }
    else {
        $('TB_window').setStyle('opacity', 1);
        if ( $('TB_load') ) {
//            $('TB_load').remove();
        }
    }
}

function TB_remove() {
    $("TB_overlay").onclick = null;
    document.onkeyup   = null;
    document.onkeydown = null;

    if ( $('TB_imageOff') )
        $('TB_imageOff').onclick = null;

    if ( $('TB_closeWindowButton') )
        $('TB_closeWindowButton').onclick = null;

    if ( $('TB_prev') )
        $('TB_prev').onclick = null;

    if ( $('TB_next') )
        $('TB_next').onclick = null;

    new Fx.Style('TB_window', 'opacity', {
        duration : 250,
        transition : Fx.Transitions.sineInOut,
        onComplete : function() {
            $('TB_window').remove();
        }
    }).start(1, 0);

    new Fx.Style('TB_overlay', 'opacity', {
        duration : 400,
        transition : Fx.Transitions.sineInOut,
        onComplete : function() {
            TB_remove_loading();
            $('TB_overlay').remove();
        }
    }).start(0.6, 0);

    window.onscroll = null;
    window.onresize = null;

    $('TB_HideSelect').remove();
    TB_init();
    TB_doneOnce = 0;
    return false;
}

function TB_position() {

    TB_WIDTH  = window.getWidth() - 100 + 30;
    TB_HEIGHT = window.getHeight() - 140 + 40;

    $('TB_iframeContent').setStyles({
        width: TB_WIDTH  - 30 + 29 + 'px',
        height: TB_HEIGHT - 45 + 17 + 'px'
    });

    $('TB_window').setStyles({
        width: TB_WIDTH + 'px',
        height: TB_HEIGHT + 'px',
        left: ( window.getScrollLeft() + ( window.getWidth()  - TB_WIDTH  ) / 2 ) + 'px',
        top : ( window.getScrollTop()  + ( window.getHeight() - TB_HEIGHT ) / 2 ) + 'px'
    });
}

function TB_positionEffect() {
    new Fx.Styles('TB_window', {
        duration: 75,
        transition : Fx.Transitions.sineInOut
    }).start({
        'left': ( window.getScrollLeft() + ( window.getWidth()  - TB_WIDTH  ) / 2 ) + 'px',
        'top' : ( window.getScrollTop()  + ( window.getHeight() - TB_HEIGHT ) / 2 ) + 'px'
    });
}

function TB_overlaySize() {
    // we have to set this to 0 px before so we can reduce the size / width of the overflow onresize
    var dim = {
        'height': '0px',
        'width' : '0px'
    };
    $('TB_overlay').setStyles(dim);
    $('TB_HideSelect').setStyles(dim);

    dim = {
        'height': window.getScrollHeight() + 'px',
        'width' : window.getScrollWidth()  + 'px'
    };

    $('TB_overlay').setStyles(dim);
    $('TB_HideSelect').setStyles(dim);
}

function TB_create_loading() {
    new Element('div')
        .setProperty('id', 'TB_load')
        .injectInside(document.body);

    $('TB_load').innerHTML = TB_loading_img;
}

function TB_remove_loading() {
    if ( $('TB_load') ) $('TB_load').remove();
}

function TB_load_position() {
    if ( $('TB_load') )
        $('TB_load').setStyles({
            left: ( window.getScrollLeft() + ( window.getWidth()  - 56 ) / 2 ) + 'px',
            top : window.getScrollTop()  + 15 + 'px',
            display : 'block'
        });
}

