if (typeof (com_hp_gc) === 'undefined')
{ var com_hp_gc = {}; }

com_hp_gc.addListener = function(elm, evType, fn, useCapture) {
    var ret = null;
    if (elm.addEventListener)
    { ret = elm.addEventListener(evType, fn, useCapture); }
    else {
        if (elm.attachEvent)
        { ret = elm.attachEvent('on' + evType, fn); }
        else
        { elm['on' + evType] = fn; }
    }
    return ret;
};

com_hp_gc.Overlay = {
    opaqueDiv: null,
    callerID: null,
    currentModal: null,
    cleanUp: null,
    showDropDowns: function() {
        var allDropDowns, cnt;
        allDropDowns = document.getElementsByTagName('select');
        for (cnt = 0; cnt < allDropDowns.length; cnt++) {
            allDropDowns[cnt].disabled = false;
        }
    },
    getWindowDim: function() {//taken from lightbox
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;
        if (self.innerHeight) {	// all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if (xScroll < windowWidth) {
            pageWidth = windowWidth;
        } else {
            pageWidth = xScroll;
        }
        arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
        return arrayPageSize;
    },
    hideDropDowns: function() {
        // hide dropdowns
        var allDropDowns, cnt;
        allDropDowns = document.getElementsByTagName('select');
        for (cnt = 0; cnt < allDropDowns.length; cnt++) {
            allDropDowns[cnt].disabled = true;
        }
    },
    hideModal: function(contentId) {
        var modalDiv, temp;
        this.opaqueDiv = document.getElementById('opaqueLayer');
        modalDiv = document.getElementById(contentId);

        this.opaqueDiv.style.visibility = 'hidden';
        this.opaqueDiv.style.display = 'none';
        this.opaqueDiv.innerHTML = '';
        modalDiv.style.display = 'none';
        this.opaqueDiv = null;
        if (this.currentModal) { this.currentModal = null; }

        this.showDropDowns();
        temp = document.getElementById(this.callerID);
        if (temp)
        { temp.focus(); }
        if (typeof (this.cleanUp) === 'function') {
            this.cleanUp();
        }
    },
    previousOnResize: null,
    showModal: function(overlayStyle, contentId, callerId, closeID) {
        var opaqueDiv, modalDiv, overlayColor, overlayBorder, temp;
        this.callerID = callerId;
        this.hideDropDowns();
        this.opaqueDiv = document.getElementById('opaqueLayer');
        modalDiv = document.getElementById(contentId);

        if (this.currentModal) { this.currentModal.style.display = 'none'; }
        this.currentModal = modalDiv;

        overlayColor = '';
        overlayBorder = '';
        if (overlayStyle === 'black') { overlayColor = "#000000"; overlayBorder = "#000000"; }
        else if (overlayStyle === 'white') { overlayColor = "#FFFFFF"; overlayBorder = "#999999"; }

        this.opaqueDiv.style.backgroundColor = overlayColor;
        modalDiv.style.border = '3px solid ' + overlayBorder;

        this.opaqueDiv.style.visibility = 'visible';
        modalDiv.style.display = 'inline';
        this.opaqueDiv.style.height = this.getWindowDim()[0] + 'px';
        this.opaqueDiv.style.width = '110%';
        temp = document.getElementById("copyright");
        if (temp)
        { this.opaqueDiv.style.height = temp.offsetTop + 20 + 'px'; }
        document.getElementById(closeID).focus();
    }
}

var EventCalendar = {
    ec_days: null,
    ec_dates: null,
    nextElement: null,
    prevElement: null,
    ec_currentAgendaDay: 0,
    frequencyId: null,
    frequencyDiv: null,
    opaqueDiv: null,
    frequencies: {},
    cleanUp: function() {
        var temp;
        if (this.opaqueDiv) {
            temp = document.getElementById(this.opaqueDiv.id);
            if (temp) {
                temp.parentNode.removeChild(temp);
                this.opaqueDiv = null;
            }
        }
    },
    showButtons: function() {
        var temp, height;
        if (this.prevElement) {
            if (this.ec_currentAgendaDay <= 0)
            { this.prevElement.style.visibility = 'hidden'; }
            else
            { this.prevElement.style.visibility = 'visible'; }
        }
        if (this.nextElement) {
            if (this.ec_currentAgendaDay >= this.frequencies[this.frequencyId].ec_dates.length - 1)
            { this.nextElement.style.visibility = 'hidden'; }
            else
            { this.nextElement.style.visibility = 'visible'; }
        }
    },
    prev: function() {
        var temp, width;
        if (this.ec_currentAgendaDay <= 0) {
            return;
        }
        this.ec_currentAgendaDay = this.ec_currentAgendaDay - 1;
        temp = document.getElementById('ec_agendaCurrentDate');
        if (temp) {
            temp.innerHTML = this.frequencies[this.frequencyId].ec_dates[this.ec_currentAgendaDay];
        }
        temp = document.getElementById('ec_agendaPlaceHolder');
        if (temp) {
            temp.innerHTML = this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay];
        }
        this.showButtons();
    },
    next: function() {
        var temp, width;
        if (this.ec_currentAgendaDay >= this.frequencies[this.frequencyId].ec_days.length - 1) {
            return;
        }
        this.ec_currentAgendaDay++;
        temp = document.getElementById('ec_agendaCurrentDate');
        if (temp) {
            temp.innerHTML = this.frequencies[this.frequencyId].ec_dates[this.ec_currentAgendaDay];
        }
        temp = document.getElementById('ec_agendaPlaceHolder');
        if (temp) {
            temp.innerHTML = this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay];
        }
        this.showButtons();
    },
    showAgenda: function(frequencyId, tableId) {
        var i, temp, widthWindow, widthCustom, widthHeader, widthTableMax, width, widthPopup, agendaPopup;
        if (tableId) {
            this.frequencyId = frequencyId;
            this.ec_currentAgendaDay = 1;
            this.frequencyDiv = document.getElementById(tableId);
            this.frequencyDiv.innerHTML = this.frequencies[this.frequencyId].html;
        }
        else {
            if (typeof (this.frequencies[frequencyId]) !== 'object')
            { return; }
            this.ec_currentAgendaDay = 1;
            this.frequencyId = frequencyId;
            if (this.frequencyDiv)
            { document.body.removeChild(this.frequencyDiv); }
            this.frequencyDiv = document.createElement('div');
            this.frequencyDiv.innerHTML = this.frequencies[this.frequencyId].html;
            document.body.appendChild(this.frequencyDiv);
            agendaPopup = document.getElementById('ec_agendaPopup');
            widthWindow = com_hp_gc.Overlay.getWindowDim()[0];
            if (EventCalendar.frequencies[frequencyId].popupWidth != -1) {
                width = EventCalendar.frequencies[frequencyId].popupWidth;
            }
            else {
                widthTableMax = this.frequencies[this.frequencyId].maxWidth + 3;
                temp = document.createElement('div');
                temp.style.position = 'absolute';
                temp.style.visibility = 'hidden';
                temp.width = 'auto';
                temp.innerHTML = document.getElementById('ec_location').innerHTML;
                temp.style.fontSize = '1.2em';
                temp.style.fontFamily = 'Arial,Verdana,Helvetica,Sans-serif';
                document.body.appendChild(temp);
                widthHeader = temp.clientWidth + 158;
                agendaPopup.style.minWidth = widthHeader + 'px';
                document.body.removeChild(temp);
                width = Math.max(widthHeader, widthTableMax);
                if (width > widthWindow) {
                    width = widthWindow - widthWindow * 0.1;
                }
            }

            if (width >= widthTableMax) { // no scrolling -> set popup size
                temp = document.getElementById('ec_agendaPlaceHolder');
                temp.className = 'ec_agendaPlaceHolderSmall';
            }
            else {
                temp = document.getElementById('ec_agendaPlaceHolder');
                temp.className = 'ec_agendaPlaceHolderBig';
            }

            if (EventCalendar.frequencies[frequencyId].popupHeight != -1)
            { agendaPopup.style.height = EventCalendar.frequencies[frequencyId].popupHeight; }
            agendaPopup.style.width = width + 'px';
            agendaPopup.style.left = (widthWindow - width) / 2 + 'px';
        }
        this.nextElement = document.getElementById('ec_agendaDayNext');
        this.prevElement = document.getElementById('ec_agendaDayPrev');
        this.prev();
        if (!tableId) {
            temp = document.getElementById('opaqueLayer');
            temp.parentNode.removeChild(temp);
            document.body.appendChild(temp);
            com_hp_gc.Overlay.cleanUp = this.cleanUp;
            com_hp_gc.Overlay.showModal('black', 'ec_agendaPopup', 'ec_showAgenda', 'ec_agendaDismiss');
        }
    },
    select: function(session) {
        var imgEl, imgUrl;
        imgEl = document.getElementById('sessionImg_' + session);
        if (imgEl) {
            imgUrl = imgEl.src;
            imgEl.src = imgUrl.replace(/select_icon/, 'selected_icon');
            this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay] = this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay].replace('preselect_icon.gif" id="sessionImg_' + session + '"', 'preselected_icon.gif" id="sessionImg_' + session + '"');
        }
    },
    unSelect: function(session) {
        var imgEl, imgUrl;
        imgEl = document.getElementById('sessionImg_' + session);
        if (imgEl) {
            imgUrl = imgEl.src;
            imgEl.src = imgUrl.replace(/selected_icon/, 'select_icon');
            this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay] = this.frequencies[this.frequencyId].ec_days[this.ec_currentAgendaDay].replace('preselected_icon.gif" id="sessionImg_' + session + '"', 'preselect_icon.gif" id="sessionImg_' + session + '"');
        }
    }
};
