/**
 * @class ScrollComponent
 * 
 */
var ScrollComponent = Class.create();

ScrollComponent.prototype = {

    /**
     * Instantiates a ScrollComponent object
     * @constructor 
     * @member ScrollComponent
     */
	initialize: function(source, scrollComponentId, itemsPanelId, nextPageButtonId, previousPageButtonId) {
		this.currentPage = 1;
		this.numPages = 1;
		this.highestPageLoaded = 1;
		this.lastPageReached = false;
		
		// default values for scroll component on the home page
		this.numItemsPerPage = 4;

		// TODO: give the user some indication that a page is loading.
		this.pageLoading = false;  // is there a page currently loading
		this.nextOnFinish = false;  // is the user waiting for the next page to load
		
		this.source = source;
		this.scrollComponentId = scrollComponentId;
		this.itemsPanelId = itemsPanelId;
		this.nextPageButtonId = nextPageButtonId;
		this.previousPageButtonId = previousPageButtonId;

		this.setItemsPanelWidth($(this.itemsPanelId).clientWidth);

        this.indicator = new Indicator('indicator' + scrollComponentId);

		YAHOO.util.Event.addListener(this.nextPageButtonId, 'click', this.nextPage.bind(this));
		YAHOO.util.Event.addListener(this.previousPageButtonId, 'click', this.previousPage.bind(this));
	},
	
	setNumItemsPerPage: function(numItemsPerPage) {
		this.numItemsPerPage = numItemsPerPage;
	},
	
	setItemsPanelWidth: function(itemsPanelWidth) {
		this.itemsPanelWidth = itemsPanelWidth;
	},

	setItemWidth: function(itemWidth) {
		this.itemWidth = itemWidth;
	},
	
	setItemHeight: function(itemHeight) {
		this.itemHeight = itemHeight;
	},

	loadPage: function(pageNumber) {
		if(pageNumber > this.highestPageLoaded && !this.lastPageReached) {
			this.indicator.show();
			url = localPath + this.source + "?";
			url += "pageNumber=" + pageNumber + "&";
			url += "resultsLimit=" + this.numItemsPerPage + "&";
			url += "itemWidth=" + this.itemWidth + "&";
			url += "itemHeight=" + this.itemHeight + "&";
			url += "classId=" + this.scrollComponentId + "&";
			
			if(this.source == "/export/ActivityThumbnails.jspx") {
				if(this.username != null && this.username != "") {
					url += "usernameSelect=" + this.username + "&";
				}
				if(this.keywords != null && this.keywords != "") {
					url += "keywordsSelect=" + this.keywords + "&";
				}
				if(this.privateActivities) {
					url += "privateActivities=" + this.privateActivities + "&";
				}
				if(this.callerActivities) {
					url += "callerActivities=" + this.callerActivities + "&";
				}
				if(this.featuredActivities) {
					url += "featuredActivities=" + this.featuredActivities + "&";
				}
			} else if(this.source == "/export/ActivityPhotos.jspx") {
				if(this.tags) {
					url += "tags=" + this.tags + "&";
				}
				url += "minLng=" + this.minLng + "&";
				url += "minLat=" + this.minLat + "&";
				url += "maxLng=" + this.maxLng + "&";
				url += "maxLat=" + this.maxLat + "&";
				url += "accuracy=" + this.accuracy + "&";
			} else {
				alert("Invalid source - " + this.source);
			}

			this.pageLoading = true;

			new Ajax.Request(url, {
				onSuccess: function(resp) {
					// 204 = no new information to return
					// meaning the page that we requested didn't have any activities
					if(resp.status == 204 || resp.responseText == null) {
					    this.indicator.hide();
						this.lastPageReached = true;
						// no more pages to load
						// disable the next button
					}
					else {
					this.indicator.hide();
						this.numPages = this.highestPageLoaded = pageNumber;
						
						var newWidth = $(this.itemsPanelId).clientWidth + this.itemsPanelWidth;

						$(this.itemsPanelId).style.width =  newWidth + "px";
						
						$(this.itemsPanelId).innerHTML += resp.responseText;
						
						if(this.nextOnFinish) {
							this.nextOnFinish = false;
							this.nextPage();
						}
					}
					this.pageLoading = false;
				}.bind(this),
				onFailure: function(resp) {
					this.lastPageReached = true;
					this.indicator.hide();
					// currently ie reaches this point when we return a 204 so I'm taking out the alert until we find a solution
					// alert("Error retrieving activities for Scroller");
				}.bind(this)
			});
		}

	},
	
	onLastPage: function() {
		return (this.currentPage == this.numPages);
	},
	
	nextPage: function() {
		if(this.onLastPage() && this.pageLoading) {
			this.nextOnFinish = true;
			return;
		}
	
		if(this.currentPage < this.numPages) {

			this.loadPage(this.currentPage + 2);
			
			var startX = YAHOO.util.Dom.getX(this.itemsPanelId);
			var startY = YAHOO.util.Dom.getY(this.itemsPanelId);
	
			var moveLeftAnim = new YAHOO.util.Motion(this.itemsPanelId, { points: {to: [startX - this.itemsPanelWidth,startY] } }, 0.3 );
	
			moveLeftAnim.animate();

			this.currentPage += 1;
			
			// enable the previous button
		}
		else {
			// either do nothing, or wrap around to the first page
		}
	},

	previousPage: function() {
		if(this.currentPage > 1) {
			
			var startX = YAHOO.util.Dom.getX(this.itemsPanelId);
			var startY = YAHOO.util.Dom.getY(this.itemsPanelId);
		
			var moveRightAnim = new YAHOO.util.Motion(this.itemsPanelId, { points: {to: [startX + this.itemsPanelWidth,startY] } }, 0.3 );
				
			moveRightAnim.animate();

			this.currentPage -= 1;
			
			if(this.currentPage == 1) {
				// disable the previous button
			}
			
			// enable the next button 
		}
		else {
			// either do nothing, or wrap around to the last page
		}
	}
};