/*************************************************************************************************\
|* @author Unknown                                                                               *|
|* @version 1.0                                                                                  *|
\*************************************************************************************************/

/*************************************************************************************************\
|* @module RemoteArticlesPagination                                                              *|
|*                                                                                               *|
|* @description This class contains everything needed for the WIW-article pagination via ajax    *|
|*                                                                                               *|
|* @version 1.0                                                                                  *|
\*************************************************************************************************/
if (!CLUBNICK.Modules) {
	CLUBNICK.Modules = {};
}

CLUBNICK.Modules.RemoteArticlesPagination = (new function () {
	var self;
	var $elements;
    var $elementForLoadingIndicator;
    var $note;
    var loadingIndicatorClassName = 'ajax_load';
	
	/*********************************************************************************************\
	|* @method init                                                                              *|
	|*                                                                                           *|
	|* @description Initializes the RemoteArticlesPagination and starts binding the              *|
	|* navigation-links.                                                                         *|
	\*********************************************************************************************/
	this.init = function () {
		self = this;
		
		$note = $('note');
		
		if (!$note) {
		    return;
		}
		
		refreshModule();
	};

	/*********************************************************************************************\
	|* @method refreshModule                                                                     *|
	|*                                                                                           *|
	|* @description Gets some elements and save it in global module variables and bind.          *|
	|* adds the observer.                                                                        *|
	|*                                                                                           *|
	|* @see this.init                                                                            *|
	|* @see loadNewPage                                                                          *|
	\*********************************************************************************************/
	function refreshModule () {
	    $elementForLoadingIndicator = $note.select('.blue').first();
		
		if (!$elementForLoadingIndicator) {
		    return;
		}
		
        $elements = $$('#page_pagination a, .pagination a');
	    if ($elements.empty()) {
	        return;
	    }
	
		addObserver();
	}

	/*********************************************************************************************\
	|* @method addObserver                                                                       *|
	|*                                                                                           *|
	|* @description Adds the observer to the pagination links and binds loadNewPage method.      *|
	\*********************************************************************************************/
    function addObserver () {
        $elements.invoke('observe', 'click', loadNewPage);
    }

	/*********************************************************************************************\
	|* @method loadNewPage                                                                       *|
	|*                                                                                           *|
	|* @description Computes the url to request, by the given event.                             *|
	|*                                                                                           *|
	|* @param {Event} event is NOT brought automaticly by the browser. You MUST care.            *|
	\*********************************************************************************************/
    function computePageUrlByEvent (event) {
        return event.element().readAttribute('href');
    }

	/*********************************************************************************************\
	|* @method loadNewPage                                                                       *|
	|*                                                                                           *|
	|* @description Called by the observed elements, if a pagination link is pressed.            *|
	|* Starts an ajax request to resolve new content, and displays the loading indicator.        *|
	|*                                                                                           *|
	|* @param {Event} event is brought AUTOMATICLY by the browser. You must not care.            *|
	\*********************************************************************************************/
    function loadNewPage (event) {
        event.stop();
        var url = computePageUrlByEvent(event);

        new Ajax.Request(url, {
            method: 'get',
            onCreate: function () {
    		    showLoadingIndicator();
    	    },
    	    
    	    // use onSuccess to make sure the request was successfull
            onSuccess: function (transport) {
    		    hideLoadingIndicator();
    		    $note.update(transport.responseText);
				refreshModule();
            },
            onFailure: function () {
                // load the url with http
                window.location = url;
            }
        });
    }

	/*********************************************************************************************\
	|* @method showLoadingIndicator                                                              *|
	|*                                                                                           *|
	|* @description Shows the loading indicator.                                                 *|
	\*********************************************************************************************/
    function showLoadingIndicator () {
	    $elementForLoadingIndicator.addClassName(loadingIndicatorClassName);
    }

	/*********************************************************************************************\
	|* @method hideLoadingIndicator                                                              *|
	|*                                                                                           *|
	|* @description Hides the loading indicator.                                                 *|
	\*********************************************************************************************/
    
    function hideLoadingIndicator () {
	    $elementForLoadingIndicator.removeClassName(loadingIndicatorClassName);
    }
});