
/**
 * Dynamic header
 */
document.observe('dom:loaded', function(){

    
    var container = $$('div.dynamicheader')[0];
    if (container){
        
        container.left = 0;
        
        // Pause animation on mouseover, prevents a bug where the slider gets stuck halfway
        container.paused = false;
        container.observe('mouseover', function(){ container.paused = true; });
        container.observe('mouseout', function(){ container.paused = false; });
        
        container.select('ul li').each(function(li, key){
            
            li.observe('click', function(event){
                event.stop();
                if (container.locked) return false;
                container.locked = true;
                move_dynamic_header(container, li, key);
            }.bindAsEventListener());
        });
        
        setInterval(function(){
            if (container.paused) return false;
            
            container.select('li').each(function(li, key){
                if (li.hasClassName('active')){
                    var next_li = li.next();
                    
                    if (next_li){
                        move_dynamic_header(container, next_li, key+1); 
                    } else {
                        next_li = li.adjacent('li').first();
                        if(next_li){    
                            move_dynamic_header(container, next_li, 0); 
                        }
                        
                        
                    }
                    throw $break;
                }
            });
        }, 5000);
    }
});

/**
 * 
 */
function move_dynamic_header(container, li, key)
{
    var items = container.select('div.item');
    var item_width = items[0].getWidth();
    var inner = container.down('div.inner');
    
    var distance = key * item_width;
    var x = container.left - distance;
    
    // Store the current position
    container.left = (key * item_width);
    
    new Effect.Move(inner, {
        x: x,
        y: 0,
        mode: 'relative',
        duration: 0.5,
        afterFinish: function(){
            li.adjacent('li.active').invoke('removeClassName', 'active');
            li.addClassName('active');
            container.locked = false;
        }
    });
}

/**
 * Quotes ticker
 */
document.observe('dom:loaded', function(){

    var container = $$('div.quotes div.quote-ticker .inner')[0];
    
    if (container){
        new Carousel(container);
    }

    /*
    var container = $$('div.quotes div.quote-ticker')[0];
    if (container){
        
        var element = container.down('div.inner');
        var slides = container.select('.item');
        var control = false;
        var jumper = false;
        
        if (element){
            new Carousel(element, slides, $$(control, jumper), {
                circular: true,
                auto: true,
                frequency: 20,
                wheel: false
            });
        }
        
        
        container.observe('mouseover', function(){ container.paused = true; });
        container.observe('mouseout', function(){ container.paused = false; });
        
        var items = container.select('div.item');
        var inner = container.down('div.inner');
        var width = inner.getWidth();
        
        var current_item = items[0];
        var next_item = false;
        var current_width = current_item.getWidth();
        
        inner.setStyle({left: 0});
        inner.absolutize();
        
        setInterval(function(){
            if (!container.paused){
                var left = parseInt(inner.getStyle('left'));
                inner.setStyle({left: left-1 + 'px'});
                
                // When the left gets to the end of the current quote it resets left to zero 
                // and places the now hidden quote at the end causing an infinite cycle of the quotes.
                if (left <= -current_width){
                    inner.setStyle({left: 0});
                    next_item = current_item.next();
                    inner.insert({bottom: current_item});
                    current_item = next_item;
                    current_width = current_item.getWidth();
                }
            }
        }, 20)
        
        
    }
    */
});

/**
 * Property carousel
 */
document.observe('dom:loaded', function(){
    var container = $$('div.property div.carousel .inner')[0];
    if (container){
    
        if (container){
            new Carousel(container, {
                directionControls: false
            });
        }
    }
});

