// JavaScript Document


var RESULTCOLS = 1;
var RESULT_HEIGHT = 124;
var RESULT_WIDTH = 760;
var RESULT_V_MARGIN = 5;


function LiveMusic(objects, noSorting) {
	
	if (!noSorting) {

		$(".liveMusicContainer #nothingToShow").fadeOut(1);

		this.results = [];
	
		for (var i=0; i<objects.size(); i++) {
			this.results.push(new SearchResult($(objects.get(i)), i));	
		}
		
		// populate the type filter menu
		var options = {};
	
		for (var i=0; i<this.results.length; i++) {	// all of the sub type names
			for (var subTypeId in this.results[i].data.subTypes) {
				var subTypeName = this.results[i].data.subTypes[subTypeId];
			
				options[subTypeId] = subTypeName;				// make a hash for deduping purposes
			}
		}
		
		var optionsArray = [];				// make an array from the deduped hash for sorting purposes
		for (var subTypeId in options) {
			optionsArray.push({id:subTypeId, name:options[subTypeId]});
		}
	
		optionsArray.sort(alphaSorter);
		
		for (var i=0; i< optionsArray.length; i++) {
			$("#resultsFilter #subTypeId").append("<option value='" + optionsArray[i].id + "'>" + optionsArray[i].name + "</option>");
		}
		
			
		this.filter = {};
	
		this.updateFilter();
	
		
		var me = this;
		// bind the events
		$("#resultsFilter #styleId").change(function()	{$("#resultsFilter #subTypeId").attr("selectedIndex",0); me.updateFilter()});
		$("#resultsFilter #subTypeId").change(function() {$("#resultsFilter #styleId").attr("selectedIndex",0); me.updateFilter()});

	
		this.setContainerHeight();
	}
	
}

function alphaSorter(a,b) {
	return a.name == b.name ? 0 : a.name < b.name ? -1 : 1;
}


LiveMusic.prototype.updateFilter = function () {
	
	this.filter.styleId = $("#styleId option:selected").val() || "";
	this.filter.typeId = $("#subTypeId option:selected").val() || "";
	// sort this.results here

	//console.log("updated: style:" + this.filter.styleId + " type:" + this.filter.typeId);
	// set the indexes of each result
	var index=0;
	for (var i=0; i<this.results.length; i++) {
		this.results[i].setVisible(this.filter);

		if (this.results[i].visible) {
			this.results[i].index = index;
			this.results[i].setLocation();
			
			this.results[i].preMove();
			index++;
		}
		
	}
	for (var i=0; i<this.results.length; i++) {
		this.results[i].move();
	}

	this.setContainerHeight();
	
	this.showNoResultsMsg();
}


LiveMusic.prototype.showNoResultsMsg = function() {
	if (this.visibleCount() > 0) {
		$(".liveMusicContainer #nothingToShow").fadeOut(1500);
	} else {
		$(".liveMusicContainer #nothingToShow").fadeIn(1500);
	}
}


LiveMusic.prototype.setContainerHeight = function() {
	var containerHeight = this.rowCount() * (RESULT_HEIGHT + RESULT_V_MARGIN) + "px";
	$(".liveMusicContainer").css("height", containerHeight);
}

LiveMusic.prototype.rowCount = function() {
	return Math.ceil(this.visibleCount()/RESULTCOLS);
}

LiveMusic.prototype.visibleCount = function() {
	var count=0;
	for (var i=0; i<this.results.length; i++) {
		if (this.results[i].visible) {
			count++;
		}
	}
	return (count);
}

// ===================================================================================
// Search result object definition
// ===================================================================================


function SearchResult(object, index) {
	// pass in a jquery object
	this.WIDTH = RESULT_WIDTH + RESULT_V_MARGIN;
	this.HEIGHT = RESULT_HEIGHT + 5;
	this.obj = object;
	this.index = index;
	this.visible = true;
	
	//console.log(this.obj);
	
	
	// sorting data should be:
	//	{
	//		"subTypes":	{
	//						"ID":"NAME",
	//						etc
	//					},
	//		"styleIds":["ID", "ID", etc]
	//	}

	this.data = JSON.parse(object.find(".sortingData").val());
	
	// now make sure all the fields exist so we can use them later
	if (!this.data.subTypes) {
		this.data.subTypes = {};
	}
	if (!this.data.styleIds) {
		this.data.styleIds = [];
	}

	this.setLocation();
	
	//this.obj.css("left", (this.left * this.WIDTH) + "px");
	//this.obj.css("left", "10px")
	this.obj.css("top", (this.top * this.HEIGHT) + "px");
	this.obj.css("width", this.WIDTH + "px");
	this.obj.css("position", "absolute");
	//this.obj.css("height", "100px");
}


SearchResult.prototype.setVisible = function(filter) {
	this.visible = true;
	
	if ( ($.toNum(filter.styleId) > 0) && ($.inArray(filter.styleId, this.data.styleIds) == -1) ) {
		this.visible=false;
	}

	if ( ($.toNum(filter.typeId) > 0) && (!(this.data.subTypes[filter.typeId])) ) {
		this.visible=false;
	}
	

}

SearchResult.prototype.showHide = function() {

	if (this.visible) {
		//this.obj.fadeIn(1500);
		this.obj.show();
	} else {
		this.obj.hide();
		//this.obj.fadeOut(1500);	
	}
}


SearchResult.prototype.setLocation = function() {
	//this.left = (this.index % RESULTCOLS);
	this.top = Math.floor(this.index / RESULTCOLS);

	//console.log(this.index + " :  " + this.left + " : " + this.WIDTH);
}

SearchResult.prototype.preMove = function() {
	// first move the object to the right place if it's already invisible
	if (this.visible && (this.obj.css("opacity") == 0)) {
		this.obj.css(
			{
				display: 'block',
				//opacity: 0,
				//left: this.WIDTH + "px",
				top: this.top * this.HEIGHT + "px"
			}
		);
		
	}
	
}

SearchResult.prototype.move = function() {

	// fade in / out and move to new positions
	var me = this;
	this.obj.stop();	// stop previous animations if they haven't finished yet...

	this.obj.animate(
			{
				top: (this.top * this.HEIGHT) + "px"
				//opacity: (this.visible ? 1 : 0)
			},
			{
				duration: 500,
				complete: me.removeInvisible
			}
	
	);
	this.showHide();

}

SearchResult.prototype.removeInvisible = function () {
	
	if ($(this).css("opacity") == 0) {
		$(this).css("display", "none");	
	} else {
		//$(this).css("opacity", 0.5);
	}
}



