// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function when(obj, fn){
    if (Object.isString(obj)) 
        obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj);
    if (Object.isArray(obj) && !obj.length) 
        return;
    if (obj) 
        fn(obj);
}

document.observe('dom:loaded', function(){

    when('success', function(notice){
        new Effect.Fade(notice, {
            delay: 3
        });
    });
    
    when('error', function(notice){
        new Effect.Fade(notice, {
            delay: 3
        });
    });
    
    when('prev', function(prev){
        prev.observe('click', function(event){
            var x = get_x('l')
            new Effect.Move($('featured_container'), {
                x: x,
                y: 0,
                duration: 1,
                mode: 'relative'
            })
            Event.stop(event);
            
        });
    });
    
    when('next', function(next){
        next.observe('click', function(event){
            var x = get_x('r')
            new Effect.Move($('featured_container'), {
                x: -x,
                y: 0,
                duration: 1,
                mode: 'relative'
            })
            Event.stop(event);
            
        });
    });
    
});

function get_x(d){
    hor_position = parseInt($('featured_container').style.left.gsub(/px/, ''))
    if (isNaN(hor_position)) {
        hor_position = 0;
    }
    
    if (d == 'l') {
        if (hor_position < 0) {
            var xx = 195;
        }
        else {
            var xx = 0;
        }
    }
    else {
        if (hor_position > (-(featured_images_size - 3) * 195)) {
            var xx = 195
        }
        else {
            var xx = 0
        }
    }
    return xx;
};