/**
 * @author Filip Michalowski / Fi Stockholm
 */
Namespace.create("ng.modules.videoGalleryModule");

ng.modules.videoGalleryModule.VideoGalleryModule = function () {

	//private variables:
	var moduleContainer;
	var moduleData;
	var SORT_DATE_DESC = "date_desc";
	var SORT_DATE_ASC = "date_asc";
	var SORT_ALPH_DESC = "alph_desc";
	var SORT_ALPH_ASC = "alph_asc";
	var currentSort;
	var sortDateBt;
	var sortAlphBt;

	//private methods:
	
	var draw = function() {		
		initializeSorting();			
	};
	
	var initializeSorting = function() {
		$("#videoGalleryModule p.sort").css({ visibility:"visible" });
		
		currentSort = SORT_DATE_DESC;
		
		var sortDateBt = moduleContainer.find(".sort a.date");
		var sortAlphBt = moduleContainer.find(".sort a.alph");
		
		sortDateBt.click( function() {
			if (currentSort === SORT_DATE_DESC) {
				currentSort = SORT_DATE_ASC;
			} else {
				currentSort = SORT_DATE_DESC;
			}
			doSorting();
			return false;
		});
		
		sortAlphBt.click( function() {
			if (currentSort === SORT_ALPH_DESC) {
				currentSort = SORT_ALPH_ASC;
			} else {
				currentSort = SORT_ALPH_DESC;
			}
			doSorting();
			return false;
		});		
		
	};
	
	var doSorting = function() {
		
		var sortDateBt = moduleContainer.find(".sort a.date").removeClass("sortDown sortUp");
		var sortAlphBt = moduleContainer.find(".sort a.alph").removeClass("sortDown sortUp");
		var sortFunction;
		
		// update styles
		switch(currentSort) {
			case SORT_DATE_DESC:
			sortDateBt.addClass("sortDown");
			sortFunction = sortAlphDescFunction;
			break;
			
			case SORT_DATE_ASC:
			sortDateBt.addClass("sortUp");
			sortFunction = sortAlphAscFunction;
			break;
			
			case SORT_ALPH_DESC:
			sortAlphBt.addClass("sortDown");
			sortFunction = sortAlphDescFunction;
			break;
			
			case SORT_ALPH_ASC:
			sortAlphBt.addClass("sortUp");
			sortFunction = sortAlphAscFunction;
			break;
		}
		
		// Grab all the existing items
		var sortedList = new Array();
		$("div.mediaListHolder li", moduleContainer).each(function(i, item) {
			$(this).removeClass("right");
			sortedList.push($(this));
		})
		
		sortedList.sort(sortFunction);
		
		var list = moduleContainer.find("div.mediaListHolder ul");
		list.empty();
		
		for (var i in sortedList) {
			var item = sortedList[i];			
			list.append(item);
		}
				
	};
	
	var sortAlphDescFunction = function(a,b) {
		
		var textA = $(a).find("span.caption").text();
		var textB = $(b).find("span.caption").text();
		
		return textA < textB
		
	}
	
	var sortAlphAscFunction = function(a,b) {
		
		var textA = $(a).find("span.caption").text();
		var textB = $(b).find("span.caption").text();
		
		return textA > textB
		
	}

	//the returned object here will become ng.modules.videoGalleryModule.VideoGalleryModule:
	return  {
		
		// public
		
		init: function (container, data) {
			moduleContainer = container;
			moduleData = data;
			
			draw();
		}

	};
}(); // the parens here cause the anonymous function to execute and return


/**
 * Markup templates for the VideoGalleryModule
 */
ng.modules.videoGalleryModule.VideoGalleryModuleTemplates = 
{
		
};