// (c) 2008 mediaunit.de
/**
 * Layout and interaction of news entries on <code>fhecor.com</code>.
 *
 * @author tc
 * @since 2008-04-30
 */
/*
 * Simulate namespaces.
 */
if (typeof es == "undefined") 
    es = new Object();
if (typeof es.fhecor == "undefined") 
    es.fhecor = new Object();

/**
 * CSS layout and interactivity of news entries.
 *
 * @param element	ID of or reference to the container element.
 */
es.fhecor.News = Class.create({

    root: null,
    news: new Array(),
    details: new Array(),
    rightCol: new Element('div', {'class': 'rightCol'}),
	language: "en",
    
    initialize: function(element){
    
        this.root = $(element);
		this.news = [];
		this.details = [];
		this.rightCol = new Element('div', {'class': 'rightCol'});
		this.language = this.root.readAttribute("lang") || "en";
        this.splitLayout();
        this.prepareDetails();
        this.activate();
        this.compact();
        this.select(0);
    },
    
    splitLayout: function(){
		var label = es.fhecor.Locale[this.language].DETAIL;
		this.root.update(new Element('div').addClassName('leftCol').update(this.root.innerHTML));
        this.rightCol.insert(new Element('h2').insert(label));
        this.root.insert(this.rightCol);
    },
    
    prepareDetails: function(){
    
        var detail;
        var detailList = new Element('ul', {
            id: "details"
        });
        
        this.root.select('li').each(function(entry){
        
            detail = new Element('li');
            entry.childElements().each(function(content){
            
                switch (content.tagName.toLowerCase()) {
                    case 'h4':
                        return;case 'h3':
                        detail.insert(content.cloneNode(true));
                        break;
                    case 'img':
                        detail.insert({
                            top: content
                        });
                        break;
                    default:
                        detail.insert(content);
                }
                
            });
            detailList.insert(detail.hide());
            
            this.details.push(detail);
            this.news.push(entry);
            
        }, this);
        
        this.rightCol.insert(detailList);
    },
    
    activate: function(){
    
        var link;
        this.news.each(function(entry){
            link = entry.select('h4 span').first();
            link.observe('click', this.clickHandler.bindAsEventListener(this));
            link.observe('mouseover', this.mouseoverHandler.bindAsEventListener(this));
            link.observe('mouseout', this.mouseoutHandler.bindAsEventListener(this));
        }, this);
        
    },
    
    clickHandler: function(event){
        var index = this.news.indexOf(event.findElement('li'));
        this.select(index);
        event.stop();
    },
    
    mouseoverHandler: function(event){
        event.element().setStyle({
            color: 'white',
            background: '#0036cd',
            cursor: 'pointer'
        });
        event.stop();
    },
    
    mouseoutHandler: function(event){
        event.element().setStyle({
            color: '#0036cd',
            background: 'none',
            cursor: 'auto'
        });
        event.stop();
    },
    
    select: function(index){
    
        if (typeof this.selection == 'number') {
            this.news[this.selection].removeClassName('selected');
            this.details[this.selection].hide();
        }
        
        this.news[index].addClassName('selected');
        this.details[index].show();
        
        this.selection = index;
    },
    
    compact: function(){
    
        if (this.news.length <= 3) 
            return;
        
		var label = es.fhecor.Locale[this.language].MORE_NEWS;
        var link = new Element('a', {
            href: '#'
        }).update(label).wrap('h3', {
            'class': 'resource'
        });
        this.news[2].insert({
            after: link
        });
        link.observe('click', this.toggleMoreHandler.bindAsEventListener(this));
        this.toggleMore();
    },
    
    toggleMoreHandler: function(event){
    
        this.toggleMore();
		event.element().blur();
        event.stop();
    },
    
    toggleMore: function(){
        this.news.slice(3).invoke('toggle');
    }
});

Object.extend(es.fhecor.News, {
	
	instance: null,
	
	update: function() {
		var news = $$('#content.news');
		if (news.length > 0 && news.first().down('.leftCol') == undefined)
    		es.fhecor.News.instance = new es.fhecor.News(news.first());
	}
});

/*
 * Apply when document is ready.
 */
Event.observe(document, 'dom:loaded', function(){
	es.fhecor.News.update();
});
