//==================================================================================
//  This JavaScript class contains the code necessary for the sidebar events list
//  slider/paginator on Jeep Experience 
//==================================================================================

var List = {
    lwCurrentTop : 0,
	//lwItemsPP : 8,
	lwItemsPP : 2,
    visibleLocCt : 0,
    currentPage : 1,
	//currentPage : 5,
    scrollDuration : 1200,
	locListId : "sidebar_events",
	pageMsgIds : ["events_list_bottom"],
    pagerLBtm : "events_list_left_bottom",
    pagerRBtm : "events_list_right_bottom",
	blockScrollSize: 620,
	
    locWinScroll : function(dir) {
		//stop window scrolling which screws up the events list
		//global.stopWindowScroll();
		
		if(global.cur_Item){
			global.cur_Item.fireEvent('click');
		}
		
		/*console.log("global.cur_Item: " + global.cur_Item);
		console.log("events.item_index: " + events.item_index);
		console.log("List.currentPage: " + List.currentPage);*/
		
        var scroller = new Fx.Scroll($(List.locListId), { wait: false, duration: List.scrollDuration, transition: Fx.Transitions.Quad.easeInOut });
		//var scroller = new Fx.Scroll($(List.locListId), { wait: false, duration: List.scrollDuration, transition: Fx.Transitions.Quad.easeInOut, onComplete: global.startWindowScroll() });
		
        if(dir == "down") {
			List.visibleLocCt = events.item_index; //pull number of events from sidebar_events.js
        	var totalPgs = Math.ceil(List.visibleLocCt/List.lwItemsPP);
			
			if(List.currentPage >= totalPgs) {
				List.lwCurrentTop += (this.blockScrollSize - 60); //account for gap at bottom of list
			} else {
				List.lwCurrentTop += this.blockScrollSize;
			}
			
            scroller.scrollTo(0,List.lwCurrentTop);
            List.currentPage = List.currentPage + 1;
        } else {
			List.lwCurrentTop -= this.blockScrollSize;
            
            if(List.lwCurrentTop < 0) 
            {
                List.lwCurrentTop = 0;
            }
	        
	        scroller.scrollTo(0,List.lwCurrentTop);
	        List.currentPage = List.currentPage - 1;
        }
        
        scroller = null;
        
        List.setPagingInfo();
    },
    
    setPagingInfo : function() {
        
		List.visibleLocCt = events.item_index; //pull number of events from sidebar_events.js (which loads them into accordion)
		
        var totalPgs = Math.ceil(List.visibleLocCt/List.lwItemsPP);
        
        if(List.currentPage < 1) { List.currentPage = 1; }
        if(List.currentPage > totalPgs) { List.currentPage = totalPgs; }
		
        var msg = "Page " + List.currentPage + " of " + totalPgs;
        window.status = msg;
        
        for(var i=0; i<List.pageMsgIds.length; i++){ 
            var msgTag = document.getElementById(List.pageMsgIds[i]);
            
            msgTag.innerHTML = "";
            msgTag.innerHTML = msg;
        }
		
		var ltBtm = $(List.pagerLBtm);
        var rtBtm = $(List.pagerRBtm);
        		
		ltBtm.style.visibility = List.currentPage <= 1 ? "hidden" : "visible";
        rtBtm.style.visibility = List.currentPage == totalPgs ? "hidden" : "visible";
		
    }
};