﻿var EventList =
{
    // member init
    isSliding: false,   // flags whether sliding is currently in progress
    eventCount: 0,
    delta: 0,
    listContainer: null,
    ajaxLoaderUrl: null,

    // ------------------------------------------------------------
    /// Initialize class
    // ------------------------------------------------------------
    initialize: function(evCount, idOfContainer, ajaxLoaderUrl) {
        // member init
        this.eventCount = evCount;
        this.listContainer = $(idOfContainer);
        this.ajaxLoaderUrl = ajaxLoaderUrl;
    },

    // ------------------------------------------------------------
    /// Sets the list's current active item
    // ------------------------------------------------------------
    SetActiveItem: function(idOfItem) {
        // get the item to be set active
        var activeItem = $(idOfItem);
        var items = null;
        // get all items
        if (window.sidebar) {// for FF
            items = this.listContainer.getElementsByClassName('event_item_active');

        }
        else { // for all other browsers
            items = Element.getElementsByClassName(this.listContainer, 'event_item_active');
        }
        // reset all classNames to non active
        for (var i = 0; i < items.length; i++) {
            items[i].addClassName('event_item');
            items[i].onmouseover = "this.className = 'event_item_active'";
            items[i].onmouseout = "this.className = 'event_item'";
            items[i].removeClassName('event_item_active');
        }
        // set active class
        activeItem.removeClassName('event_item');
        activeItem.addClassName('event_item_active');
        activeItem.onmouseover = "";
        activeItem.onmouseout = "";
    },

    // ------------------------------------------------------------
    /// Slides the element with specified id leftways. 
    // ------------------------------------------------------------
    SlideLeft: function(controlId) {
        // check whether already sliding, avoid another call if it is
        if (this.isSliding == false) {
            if (this.eventCount > 3 && (this.eventCount - 3) > this.delta) {
                // set sliding flag
                this.isSliding = true;
                // set new delta value
                this.delta += 1;
                // use scriptaculous to slider container
                new Effect.Move($(controlId), { x: -277, y: 0, mode: 'relative', duration: 0.7, afterFinish: this._onAfterEndSliding.bind(this) });
            }
        }
    },

    // ------------------------------------------------------------
    /// Slides the element with specified id rightways. 
    // ------------------------------------------------------------
    SlideRight: function(controlId) {
        // check whether already sliding, avoid another call if it is
        if (this.isSliding == false) {
            if (this.eventCount > 3 && this.delta > 0) {
                // set new delta value
                this.delta -= 1;
                // set sliding flag
                this.isSliding = true;
                // use scriptaculous to slider container
                new Effect.Move($(controlId), { x: 277, y: 0, mode: 'relative', duration: 0.7, afterFinish: this._onAfterEndSliding.bind(this) });
            }
        }
    },

    // ------------------------------------------------------------
    /// When sliding is finished, this method is called to reset the
    /// sliding flag
    // ------------------------------------------------------------
    _onAfterEndSliding: function() {
        // reset flag
        this.isSliding = false;
    },

    // ------------------------------------------------------------
    /// Shows ajaxloadericon when a new event is loaded
    // ------------------------------------------------------------
    showAjaxIcon: function() {
        $('ajaxicon_eventdetail').show();
    },

    // ------------------------------------------------------------
    /// Hides ajaxloadericon when a new event is loaded
    // ------------------------------------------------------------
    hideAjaxIcon: function() {
        $('ajaxicon_eventdetail').hide();
    },

    // ------------------------------------------------------------
    /// Loads eventdetail for given event using SmartAjax
    // ------------------------------------------------------------
    loadEventDetail: function(eventCaption) {
        // show loader icon
        this.showAjaxIcon();
        // convert umlaut
        eventCaption = Misc.ConvertUmlaut(eventCaption);
        // create request
        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "EventService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "LoadEventDetail");
        ajaxRequest.appendRequest("eventCaption", eventCaption);
        ajaxRequest.onResponse = this._onGetEventDetailResponse.bind(this);
        ajaxRequest.sendRequest();

        // update "locationbar"
        $('locationbar').update(eventCaption);
    },

    // ------------------------------------------------------------
    /// Handles reponse for eventdetail ajax-requests
    // ------------------------------------------------------------
    _onGetEventDetailResponse: function(ajaxObj) {
        // check for error
        if (ajaxObj.error == null) {
            // no error, update eventdetail
            $('eventdetail_wrapper').update(ajaxObj.response);
        }
        else {
            // alert error
            alert(ajaxObj.error);
        }
        // hide icon
        this.hideAjaxIcon();
    },

    // ------------------------------------------------------------
    /// Handles reponse for eventlist ajax-request
    // ------------------------------------------------------------
    _onGetEventListResponse: function(ajaxObj) {
        // check for error
        if (ajaxObj.error == null) {
            // no error, update eventdetail
            $('eventlist_wrapper').update(ajaxObj.response);
        }
        else {
            // alert error
            alert(ajaxObj.error);
        }
        // hide icon
        this.hideAjaxIcon();
    },

    // ------------------------------------------------------------
    /// Handles AfterFilterChanged event of the FilterDropDown
    // ------------------------------------------------------------
    _onFilterChanged: function(filterDropDown) {

         if (window["Eventlist_CoverFlow"].busy) {
            setTimeout(this._onFilterChanged.bind(this, filterDropDown), 100);
            return;
        }

        // get selected value from filter box
        var selectedValue = filterDropDown.getSelectedItemValue();
        // show loader icon
        this.showAjaxIcon();
        // create request
        var ajaxRequest = new SmartAjax();
        ajaxRequest.serviceId = "EventService";
        ajaxRequest.url = this.ajaxLoaderUrl;
        ajaxRequest.appendRequest("command", "LoadEventListByFilter");
        ajaxRequest.appendRequest("year", selectedValue);
        ajaxRequest.onResponse = this._onGetEventListResponse.bind(this);
        ajaxRequest.sendRequest();
    },

    // ------------------------------------------------------------
    /// Performs async postback (check whether still needed with site rearangements)
    // ------------------------------------------------------------
    doPostBackAsync: function(eventName, eventArgs) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
            prm._asyncPostBackControlIDs.push(eventName);
        }

        if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
            prm._asyncPostBackControlClientIDs.push(eventName);
        }
        __doPostBack(eventName, eventArgs);
    }
};
