//------------------------------------------------------------------------------------------

var EndlessPage = {
    ep_getWindowSize : function(w) {
	var width, height;
        w = w ? w : window;
	var win = {
	    width  : (w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth)),
	    height : (w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight))
	};
        return win;
    },
    ep_loadMoreItems : function(opt) {
	// compute amount of page below the current scroll position
	var remaining = ($(opt.wrapper).viewportOffset()[1] + $(opt.wrapper).getHeight()) - document.ep_getWindowSize().height;

	// compute height of bottom element
	var last = $$('.' + opt.line).last().getHeight();

	if (remaining < last*2 && !$('ep_complete')) {
	    if (Ajax.activeRequestCount == 0) {
	        var url = opt.url;
		// Making this possible to config through opt.element_prefix
	        var last = $$('.' + opt.line).last().className.match(/el_[0-9]+/)[0];
                new Ajax.Request(url, {
                        method: 'get',
                        parameters: 'o=' + last + '&' + opt.param,
                        onLoading: function(){
                            $('loading_endlesspage').show();
                        },
                        onComplete: function(xhr){
                            $('loading_endlesspage').hide();
                            $('loading_endlesspage').insert({before : xhr.responseText})
			},
                        onFailure: function(xhr){
                            $('loading_endlesspage').hide();
			}
                    });
            }
	} else {
	    $('loading_endlesspage').hide();
	}
    },
    ep_init : function(opt) {
	//alert('init:' + opt.wrapper);
	// hide the pagination links
	if ($(opt.pagination)) {
	    $(opt.pagination).hide();
	    $(opt.pagination).insert({before : '<div id="loading_endlesspage" style="display: none;"><img src="/tools/ajax/img/loading_animation_liferay.gif" alt="loading..." /></div>'});
	}

	// find to events that could fire loading items at the bottom
	Event.observe(window, 'scroll', function(e) {
		document.ep_loadMoreItems(opt);
	    });

	Event.observe(window, 'resize', function(e) {
		document.ep_loadMoreItems(opt);
	    });
	
    }

};

// Extend document object with new functions.
Object.extend(document, EndlessPage);



/**
 * You can identify a swipe gesture as follows:
 * 1. Begin gesture if you receive a touchstart event containing one target touch.
 * 2. Abort gesture if, at any time, you receive an event with >1 touches.
 * 3. Continue gesture if you receive a touchmove event mostly in the x-direction.
 * 4. Abort gesture if you receive a touchmove event mostly the y-direction.
 * 5. End gesture if you receive a touchend event.
 * 
 * @author Dave Dunkin
 * @copyright public domain
 */
function addSwipeListener(el, listener) {
    var startX;
    var dx;
    var direction;
 
    function cancelTouch()
    {
	el.removeEventListener('touchmove', onTouchMove);
	el.removeEventListener('touchend', onTouchEnd);
	startX = null;
	startY = null;
	direction = null;
    }
 
    function onTouchMove(e)
    {
	if (e.touches.length > 1)
	    {
		cancelTouch();
	    }
	else
	    {
		dx = e.touches[0].pageX - startX;
		var dy = e.touches[0].pageY - startY;
		if (direction == null)
		    {
			direction = dx;
			e.preventDefault();
		    }
		else if ((direction < 0 && dx > 0) || (direction > 0 && dx < 0) || Math.abs(dy) > 100)
		    {
			cancelTouch();
		    }
	    }
    }

    function onTouchEnd(e)
    {
	cancelTouch();
	if (Math.abs(dx) > 50)
	    {
		listener({ target: el, direction: dx > 0 ? 'right' : 'left' });
	    }
    }
 
    function onTouchStart(e)
    {
	if (e.touches.length == 1)
	    {
		startX = e.touches[0].pageX;
		startY = e.touches[0].pageY;
		el.addEventListener('touchmove', onTouchMove, false);
		el.addEventListener('touchend', onTouchEnd, false);
	    }
    }
 
    el.addEventListener('touchstart', onTouchStart, false);
}



//  //------------------------------------------------------------------------------------------
//  // Init functions we think you need after dom is loaded
//  document.observe("dom:loaded", function(){
//  	if ($('ep_artlist')) {
//  	    document.ep_init({
//  		    wrapper    : 'ep_artlist',
//  		    line       : 'ep_line',
//  		    pagination : 'pagination',
//  		    url        : '/tools/wip_v4/template/layout_3/wip4/inc/middle/article_list_1_endless.epl'
//  		});
//  	}
//  
//      });

