(function($) {

	function VideosList(config) {
		
		this.config = config;
		
		this.init = function() {
			this.limit = this.numVideos;
		    this.config.page = 1;
			this.config.sum = 0;
			this.config.numPages = 0;
			this.config.buttonsInitalized = false;
			this.getSum();
			this.loadVideos();
			this.initControls();
		}
		
		this.smallInit = function() {
		    this.config.page = 1;
			this.getSum();
		}
		
		
		
		this.initButtons = function() {
			var vL = this;
			
			if(!this.config.buttonsInitalized) {
				if(this.config.nextButtons){
					$(this.config.nextButtons).click(function() { vL.showNext(); })
				}	
				
				if(this.config.previousButtons){
					$(this.config.previousButtons).click(function() { vL.showPrevious(); })
				}
				
				this.config.buttonsInitalized = true;	
			}
			
			this.buttonsVisible();
		}
		
		this.initControls = function() {
			var vL = this;
			
			switch ( this.config.order) {
				case "RAND()": var sortClass = "sortRandom"; break;
				case "videos.views DESC": var sortClass = "sortViews"; break;
				case "videos.votes DESC": var sortClass = "sortVotes"; break;
				case "video.comments DESC": var sortClass = "sortComments"; break;
			}
			
			// HIGHLIGHT ACTIVE
		    $('#'+this.config.controlsContainer+' *').removeClass('active');
		    $('#'+this.config.controlsContainer+' .'+sortClass).addClass('active');
		    
			if(this.config.controlsContainer) {
				$('#'+this.config.controlsContainer+' .sortRandom').click(function() { vL.changeSort("RAND()"); });
				$('#'+this.config.controlsContainer+' .sortViews').click(function() { vL.changeSort("videos.views DESC"); });
				$('#'+this.config.controlsContainer+' .sortVotes').click(function() { vL.changeSort("videos.votes DESC"); });
				$('#'+this.config.controlsContainer+' .sortComments').click(function() { vL.changeSort("videos.comments DESC"); });
			}
			
		
		}
		this.changeSort = function(sort) {
			
			$("#"+this.config.videosListContainer).slideUp('fast');
			
			this.config.order = sort;
			this.config.page = 1;
			
			switch ( this.config.order) {
				case "RAND()": var sortClass = "sortRandom"; break;
				case "videos.views DESC": var sortClass = "sortViews"; break;
				case "videos.votes DESC": var sortClass = "sortVotes"; break;
				case "videos.comments DESC": var sortClass = "sortComments"; break;
			}
			
			// HIGHLIGHT ACTIVE
		    $('#'+this.config.controlsContainer+' *').removeClass('active');
		    $('#'+this.config.controlsContainer+' .'+sortClass).addClass('active');
		    
		    this.loadVideos();
		    this.buttonsVisible();
		}
		
		this.buttonsVisible = function() {
			// DISPLAY VS. HIDE
			if(this.config.nextButtons) {
				if(this.config.page < this.config.numPages) {
					$(this.config.nextButtons).addClass("active");
				}
				else {
					$(this.config.nextButtons).removeClass("active");
				}
			}
			
			if(this.config.previousButtons) {
				if(this.config.page > 1) {
					$(this.config.previousButtons).addClass("active");
				}
				else {
					$(this.config.previousButtons).removeClass("active");
				}
			}
		}
		
		this.showNext = function() {
			if(this.config.page < this.config.numPages) {
				this.config.page++;
				var limit = (this.config.page - 1) * this.config.numVideos + ", " + this.config.numVideos;
				this.buttonsVisible();
	
				var vL = this;
	
				if($("#"+vL.config.videosListContainer + " .page_" + vL.config.page).length > 0) {
					$("#"+vL.config.videosListContainer + ' .page').slideUp("slow");
					$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).slideDown("slow");
				}
				else {
					$.post("/video-list-ajax", { order: this.config.order, where: this.config.where , limit: limit, special: this.config.special },
					   function(data){
						    $("#"+vL.config.videosListContainer + ' .page').slideUp("slow");
							$("#"+vL.config.videosListContainer).append("<div class='page page_" + vL.config.page + "' style='display: none;'></div>")
							$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).html(data);
							$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).slideDown("slow");
					    }
					);
				}
			}
		}
		
		this.showPrevious = function() {
			if(this.config.page > 1) {
				this.config.page--;
				var limit = (this.config.page - 1) * this.config.numVideos + ", " + this.config.numVideos;
				this.buttonsVisible();
				
				var vL = this;
				
				if($("#"+vL.config.videosListContainer + " .page_" + vL.config.page).length > 0) {
					$("#"+vL.config.videosListContainer + ' .page').slideUp("slow");
					$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).slideDown("slow");
				}
				else {
					alert
					$.post("/video-list-ajax", { order: this.config.order, where: this.config.where , limit: limit, special: this.config.special },
					   function(data){
						    $("#"+vL.config.videosListContainer + ' .page').slideUp("slow");
							$("#"+vL.config.videosListContainer).append("<div class='page page_" + vL.config.page + "' style='display: none;'></div>")
							$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).html(data);
							$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).slideDown("slow");
					    }
					);
				}
			}
		}
		
		this.container = function() {
			return $('#'+config.videosListContainer);
		}
		
		this.getSum = function(){
			var vL = this;
			
			$.post("/video-list-ajax", { mode: "getSum", where: this.config.where, special: this.config.special },
			   function(data){
					vL.config.sum = parseInt(data);
					vL.config.numPages = Math.ceil(vL.config.sum / vL.config.numVideos);
					vL.initButtons();
			    }
			);	
		}	
	
		this.loadVideos = function(slideUp){
			var containerId = this.config.videosListContainer;
			var vL = this;
			
			if(slideUp)
				$("#"+vL.config.videosListContainer).slideUp('fast');
			
			$.post("/video-list-ajax", { order: this.config.order, where: this.config.where , limit: this.config.numVideos, special: this.config.special },
			   function(data){
					$("#"+vL.config.videosListContainer).html('');
					$("#"+vL.config.videosListContainer).append("<div class='page page_" + vL.config.page + "'></div>")
					$("#"+vL.config.videosListContainer + " .page_" + vL.config.page).html(data);
					$("#"+vL.config.videosListContainer).slideDown('fast');
			    }
			);	
		}
	}	

	$.videosList = {
		defaults: {
			where: "",
			numVideos: 5,
			limit: null,
			order: "videos.votes DESC",
			page: 1,
			sum: 0,
			numPages: 0,
			previousButtons: null,
			nextButtons: null,
			controlsContainer: null,
			special: null
		}
	};
	

	$.fn.extend({
		videosList:function(config) {
			var config = $.extend({}, $.videosList.defaults, config);
			config.videosListContainer = this.attr("id");
			this.object = new VideosList(config);
			this.object.init();
			
			
			return this.object;
		}
	});

	
})(jQuery);


