Event.observe(window, 'load', init);

function init(){
    bindMenu();
    startRotator();
    bindSearch();
    bindComposerBoxes();
}

function bindMenu(){
    $$('li.menu_item').invoke('observe', 'mouseover', menuEvent);
    $$('li.menu_item').invoke('observe', 'mouseout', menuEvent);
    function menuEvent(event){
        var ul = this.down('ul');
        if (ul == null) 
            return;
        if (event.type == 'mouseover') {
            ul.show();
        }
        else 
            if (event.type == 'mouseout') {
                ul.hide();
            }
    }
    $$('ul.dropdown li').each(function(li){
        li.observe('click', function(e){
            if (e.target == li) {
                self.location.href = li.down('a').href;
            }
        });
    });
}

function bindComposerBoxes(){
    $$('.composer_box').each(function(e){
        e.observe('click', function(){
            self.location.href = e.down('a').href;
        });
    });
}

function startRotator(){
    var texts = [];
    var imgs = $$('img[class="rotator"]');
    if (imgs.size() > 0) {
        imgs[0].show();
        texts = $$('b[class="rotator"]');
        if (texts.size() > 0) {
            texts[0].show();
        }
    }
    
    if (imgs.size() > 1) {
        var rotator = new PeriodicalExecuter(nextImage, 5);
        rotator.i = 0;
        rotator.imgs = imgs;
        rotator.label = texts;
    }
    
    function nextImage(){
        this.imgs[this.i].fade();
        this.label[this.i] != null && this.label[this.i].hide();
        this.i++;
        if (this.i == this.imgs.length) 
            this.i = 0;
        this.imgs[this.i].appear();
        this.label[this.i] != null && this.label[this.i].appear();
    }
}

function onDayClick(el){
	return; //links are moved in tpl file for multy language links
    var dayInfo = getDayInfo(el);
    var today = new Date();
    var day = new Date(dayInfo['year'], dayInfo['month'] - 1, dayInfo['daynum']);
    if (day > today || isToday(today, day)) {
        var url = '/program/' + dayInfo['year'] + '-' +
        dayInfo['month'].toPaddedString(2) +
        '-' +
        dayInfo['daynum'].toPaddedString(2);
        self.location.href = url;
    }
}

function isToday(today, day){
    return (today.getYear() == day.getYear() &&
    today.getMonth() == day.getMonth() &&
    today.getDate() ==
    day.getDate());
}

function bindSearch(){

    var inputSearch = $('inputSearch');
    
    new Ajax.Autocompleter("inputSearch", "searchResults", "/en/play/data/search_data/", {
        paramName: "dataId",
        minChars: 2,
        indicator: 'filterLoading',
        afterUpdateElement: goToSearchResult
    });
    
    function goToSearchResult(text, li){
        var a = li.select('a')[0];
        self.location.href = a.href;
        inputSearch.value = '';
    }
}

function debug(msg){
    var box = $('debug');
    if (box == null) 
        return;
    box.innerHTML += msg + '<br/>';
}
