// QTTC scripts

// Ticker class based on Antonio Lupetti's code
// http://woork.blogspot.com/2008/07/news-ticker-with-horizontal-scroller.html
var Ticker = new Class({
    Implements: [Options],
    options: {
        transdur: 1000, // duration of transition 
        newsdur:  8000  // duration of each news item
    },
    initialize: function(el, options){
        this.setOptions(options);
        this.el = $(el);
        this.items = this.el.getElements('li');
        var w = 0;
        var h = 0;
        h = this.el.getSize().y;
        this.items.each(function(li, index) { w += li.getSize().x; });
        this.el.setStyles({
            position: 'absolute',
            'top':    0,
            'left':   0,
            'width':  w,
            'height': h
        });
        this.fx = new Fx.Morph(this.el, {
	    duration:   this.options.transdur,
	    onComplete: function() {
		var i = (this.current == 0) ? this.items.length : this.current;
		this.items[i-1].inject(this.el);
		this.el.setStyles({
                    'left': 0
		});
            }.bind(this)
	});
        this.current = 0;
        this.next.periodical(this.options.newsdur, this);
    },
    next: function() {
        var shiftx = this.items[this.current].getSize().x;
        this.fx.start({
            'left': -shiftx
        });
        this.current++;
        if (this.current >= this.items.length) {
	    this.current = 0;
	}
    }
});

var Site = {
    selectMenu: function (menuid) {
	window.addEvent('load', function () {
	    $(menuid).setStyle('background-position', 'left bottom');
	});
    },
    loadTTNews: function (el, media, mode) {
	args = $H({'media': media, 'mode': mode}).toQueryString();
	var myRequest = new Request({
	    method: 'get',
	    url: 'cgi-bin/ttnewsget.rb',
	    onSuccess: function () {
 		$(el).set('html', this.response.text);
		if (mode != "list") {
		    new Ticker('ttnews', {transdur: 1000, newsdur: 8000});
		}
	    }
	}).send(args);
    },
    selectBranch: function (branch) {
	selected = $(branch);
	$$('.branch').each(function (b) {
	    if (b != selected) {
		b.setStyle('display', 'none');
		$(b.id+'Button').setStyle('background-color', 'transparent');
	    } else {
		b.setStyle('display', 'block');
		$(b.id+'Button').setStyle('background-color', '#c00');
	    }
	});
    },
    setupTrnResults: function() {
	window.addEvent('domready', function(){
	    var acc = new Fx.Accordion($$('.trnyear'), $$('.trnyearresults'), {
		display: 0,
		alwaysHide: true,
		onActive: function(toggler){
		    // onComplete does not receive toggler but always
		    // receive the first toggled element, so save the
		    // toggler here.
		    this.target = toggler;
		}
// 		,
// 		onComplete: function(){
// 		    if (this.target) {
// 			('#'+this.target.getProperty('id')).toURI().go();
// 		    }
// 		}
	    });
	});
    },
    adrs: function(srda) {
	adrs = srda.split('').reverse().join('');
	link = '<a href="mailto:' + adrs + '">' + adrs + '</a>';
	return link;
    }
};

