/*
 *  Telkku.com DropMenu object  
 */

function DropMenu(id, hideTimeout, showTimeout) {    
    this.timer = null;
    this.id = id;
    this.hideTimeout = hideTimeout;
    this.showTimeout = showTimeout;
}

DropMenu.prototype.show = function() {      
    var self = this;
    var actualShow = function() {        
        // actually show the menu (hide the footer ads)        
        Application.hideFooterAds();       
        Application.showElement(self.id);
    }
    
    if (arguments.length == 0) {
        // set the timeout and clear old if any
        if (this.timer != null) {
            window.clearTimeout(self.timer);                    
        }
        this.timer = window.setTimeout(actualShow, this.showTimeout);
    }
    else {
        // show without any delay
        actualShow();
    }
}

DropMenu.prototype.hide = function() {      
    var self = this;
    var actualHide = function() {        
        // actually hide the menu (show the footer ads)
        Application.showFooterAds();       
        Application.hideElement(self.id);
    }
    
    if (arguments.length == 0) {
        // set the timeout and clear old if any
        if (this.timer != null) {
            window.clearTimeout(self.timer);                    
        }
        this.timer = window.setTimeout(actualHide, this.hideTimeout);
    }
}

// create the drop menus
var channelPagesDropMenu = new DropMenu("channelPagesDropMenuContainer", 400, 500);
var calendarDropMenu = new DropMenu("calendarDropMenuContainer", 400, 500);
var settingsDropMenu = new DropMenu("settingsDropMenuContainer", 0, 0);

// specially for settings drop menu
function toggleSettingsDropMenu(showMenu) {
    var link = document.getElementById("omatelkkuLink");
    if (showMenu) {
        settingsDropMenu.show();        
        if (link) {
            link.onclick = function() {
                return toggleSettingsDropMenu(false);
            }
        }
    }
    else {
        settingsDropMenu.hide();
        if (link) {
            link.onclick = function() {
                return toggleSettingsDropMenu(true);
            }
        }            
    }
    return false;
}

function initCheckboxes(containingElementId) {        
    var containingElement = document.getElementById(containingElementId);
    if (containingElement) {
        var inputElements = containingElement.getElementsByTagName('input');
        for (var i = 0; i < inputElements.length; i++) {
            if ((inputElements[i].type == 'checkbox') || (inputElements[i].type == 'radio')) {                                        
                inputElements[i].onclick = function() {                  
                    var myLabel = document.getElementById(this.id + 'Label');                              
                    if (this.checked) {
                        myLabel.className = 'selected';                                                        
                    }
                    else {
                        myLabel.className = '';                            
                    }                        
                    if (this.type == 'radio') {
                        var inputElements = document.getElementById(containingElementId).getElementsByTagName('input');
                        for (var j = 0; j < inputElements.length; j++) {
                            if (!inputElements[j].checked) {
                                if ((inputElements[j].name == this.name) && (inputElements[j].id != this.id)) {                                    
                                    var label = document.getElementById(inputElements[j].id + "Label");
                                    label.className = '';
                                }
                            }
                        }
                    }
                };                    
                inputElements[i].onclick();
            }
        }
    }        
}
