// LME : JavaScript

function go_search(base_url)
{
    var text = jQuery.trim($('#search-field').val());
    if (text.length) location.href = base_url + '/search/' + escape(text);
    return false;
}

function toggle_search()
{
    $('#search-link').toggle();
    $('#search-field').toggle().focus().select();
    return false;
}

function toggle_id_if(id, value)
{
    value ? $(id).show() : $(id).hide();
    return false;
}

function toggle_class(id, klass)
{
    $(id).toggleClass(klass);
    return false;
}

function toggle_id(id)
{
    $(id).toggle();
    return false;
}

function toggle_css(rule)
{
    $(rule).toggle();
    return false;
}

function show_css(rule)
{
    $(rule).show();
    return false;
}

function hide_css(rule)
{
    $(rule).hide();
    return false;
}

function toggle_nav_indicator()
{
    $('#notice').html('');
    return toggle_id('#nav_indicator');
}

function load_xhr(id, url)
{
    $.ajax({url:url,
            dataType: 'html',
            beforeSend: function(xhr) { toggle_nav_indicator(); },
            success: function(data, textStatus) {
                toggle_nav_indicator();
                $(id).html(data);
            }
    });
    return false;
}

var preview = null;
function init_preview() { preview = new Preview($('table.visuel a.preview')); }

var Preview = function(elements) { this.init(elements); }
$.extend(Preview.prototype, {
    delay: 850, // delay before preview appears
    opacity: 1.0,
    appearDuration: 300,
    hideDuration: 150,
    width: 500, 
    height: 400,
    cancel: false,

    init: function(elements) {
        this.preview = $('#picture').center();
        this.initialized = false;
        $(document).bind('click', {_this:this}, this.hideEvent);
        for (var i = 0; i < elements.length; i++) {
            var element = $(elements[i]);
            element.bind('mouseover', {_this:this, alt:element.attr('alt')}, this.showEvent);
            element.bind('mouseout', {_this:this}, this.cancelEvent);
            element.children().attr('title', '');
        }
    },
    showEvent: function(evt) {
        var _this = evt.data._this;
        if (_this.initialized)
            if (_this.preview.attr('src') == evt.data.alt) return false;
            _this.preview.trigger('mouseout');
        _this.timer = window.setTimeout(function() { _this.appear(evt.data.alt); }, _this.delay);
        return false;
    },
    hideEvent: function(evt) {
        var _this = evt.data._this;
        if (_this.initialized) {
            _this.preview.unbind('mouseout');
            _this.preview.fadeOut(_this.hideDuration);
            _this.preview.attr('src', $('#nav_indicator img').attr('src'));
            _this.preview.center();
        }
        window.clearTimeout(_this.timer);
        _this.initialized = _this.cancel = false;
    },
    cancelEvent: function(evt) {
        var _this = evt.data._this;
        if (!_this.initialized) _this.cancel = true;
    },
    appear: function(alt) {
        this.preview.bind('mouseout', {_this:this}, this.hideEvent);
        if (this.cancel) {
            this.cancel = false;
            this.preview.trigger('mouseout');
        } else {
            this.initialized = true;
            this.preview.attr('src', alt);
            var preview = this.preview;
            this.preview.load(function() { preview.center(); });
            this.preview.fadeIn(this.appearDuration);
        }
    }
});

function show_preview(url) {
  var preview = $('#picture').show().attr('src', url);
}

function hide_preview() {
  var preview = $('#picture').hide().attr('src', $('#nav_indicator img').attr('src'));
}

function move_preview(evt) {
  $('#picture').css({'left':evt.pageX+10+'px', 'top':evt.pageY+10+'px'});
}

jQuery.fn.center = function() {
    this.css('position','absolute');
    this.css('left', ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + 'px');
    this.css('top', ( $(window).height() - this.height() ) / 2 + $(window).scrollTop() + 'px');
    return this;
};
