var MusicCatalog_CurrentColumn         = 'song';
var MusicCatalog_CurrentDirection      = true;
var MusicCatalog_ServiceUrl            = '/application/services/guitarhero/musicCatalogSearch.php';
var MusicCatalog_MaxClientSortRows     = 100;
var MusicCatalog_CurrentGame           = 0;
var MusicCatalog_CurrentPlatform       = 0;
var MusicCatalog_DLC                   = 0;
var MusicCatalog_UseJSON               = 0;

var MusicCatalog_q                     = TimerQueue;
MusicCatalog_q.speed                   = 20; // set speed
var MusicCatalog_id                    = 0;

$(document).ready(function(){
	$("#music-catalog-reset").click(function(result) {
		
		MusicCatalog_CurrentColumn         = 'song';
		MusicCatalog_CurrentDirection      = true;
		
		MCServerRequest();
	});
	
	
	// new result set
	$("#music-catalog-form").submit(function() 
	{
		MCSearch();
		return false;
	});
	
	// sort the current result set
	$("#titles_area li").each(function(column){
		
		if ($(this).hasClass('sortArtist')) {
			$(this).click(MCSortArtist);
		}
		
		if ($(this).hasClass('sortSong')) {
			$(this).click(MCSortSong);
		}
		
		if ($(this).hasClass('sortPopular')) {
			$(this).click(MCSortPopular);
		}
		
		if ($(this).hasClass('sortDLC')) {
			$(this).click(MCSortDLC);
		}
		
		if ($(this).hasClass('sortAdded')) {
			$(this).click(MCSortAdded);
		}
		
		if ($(this).hasClass('sortDifficultyGuitar')) {
			$(this).click(MCSortDifficultyGuitar);
		}
		
		if ($(this).hasClass('sortDifficultyBass')) {
			$(this).click(MCSortDifficultyBass);
		}
		
		if ($(this).hasClass('sortDifficultyDrums')) {
			$(this).click(MCSortDifficultyDrums);
		}
		
		if ($(this).hasClass('sortDifficultyVocals')) {
			$(this).click(MCSortDifficultyVocals);
		}
	})
	
	// make init server request
	MCSearch();
	
});

function MCSearch()
{
	var frm        = "music-catalog-form";
	if(MCVerify(frm))
	{
		MCServerRequest(MCFormParams());
	}
}

function MCSetPlatform(value)
{
	MusicCatalog_CurrentPlatform = value
}


function MCSetGame(value)
{
	MusicCatalog_CurrentGame = value;
}

function MCSetDLC()
{
	MusicCatalog_DLC = MusicCatalog_DLC == 0 ? 1 : 0;
}

function MCRefreshCatalog(values)
{
	$("#music-catalog-rows").empty();
	MusicCatalog_q.clear();
	
	for(var i in values)
	{
		MusicCatalog_q.add(MCDisplayRow, values[i]);
	}
	
	MCUpdateRowCount();	
}

function MCDisplayRow(val)
{
	var cssClass = (MusicCatalog_id % 2) ? "even" : "odd";

	var ulRow = "<ul class=" + cssClass + ">" + val + "</ul>";
	
	$("#music-catalog-rows").append(ulRow);
	MusicCatalog_id++;
}

function MCUpdateRowCount()
{
	$("#number_items").text(MusicCatalog_q.size() + " items"); 
}



function MCServerRequest(params)
{
	$("#loader").show();
	MusicCatalog_id = 0;
	
	$.post(MusicCatalog_ServiceUrl, params, function(result) 
	{
		if(result)
		{
			MCRefreshCatalog(result)			
		}
		$("#loader").hide();
	}, "json");
}

function MCFormParams()
{
	var frm         = "music-catalog-form";
	var search      = $("#MusicCatalogInputSearch").val();//$("#" + frm + " .inputSearch").val();
	var dlc         = $("#view_only_btn:checked").length;  //MusicCatalog_DLC;//$("#" + frm + " .inputDLC").is(":checked") ? 1 : 0;
	var game        = $("#all_games_drop_down_btn").val(); //MusicCatalog_CurrentGame;//$("#" + frm + " .inputGame").val();
	var platform    = $("#all_platforms_drop_down_btn").val(); //MusicCatalog_CurrentPlatform;//$("#" + frm + " .inputPlatform").val();
	
	return {search:search, dlc:dlc, game:game, platform:platform, json:MusicCatalog_UseJSON};
}

/* SORTING */
function MCSongRows(){
	return $("#music-catalog-rows ul");
	//return $("#music-catalog-rows").children();
}

function MCColumns(){
	// the numeric values are legacy.  Change them if need be to string values.
	return {artist:'songArtist', song:'songLabel', guitar:2, bass:3, drums:4, vocals:5, popular:'songPopular', dlc:7, added:'songAdded'};
}

function MCSortSong()
{
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'song'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'song';
	MCSortColumn();
	
}

function MCSortArtist()
{
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'artist'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'artist';
	
	MCSortColumn();
	
}

function MCSortPopular()
{
	// sort most popular on top first (false).
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'popular'  ? !MusicCatalog_CurrentDirection : false;
	MusicCatalog_CurrentColumn    = 'popular';
	
	MCSortColumn();
}

function MCSortDLC(){
	
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'dlc'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'dlc';
	
	MCSortColumn();
}

function MCSortAdded()
{
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'added'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'added';
	
	MCSortColumn();
}

function MCSortDifficultyGuitar(){
	
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'guitar'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'guitar';
	
	MCSortColumn();
}

function MCSortDifficultyBass(){
	
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'bass'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'bass';
	
	MCSortColumn();
}

function MCSortDifficultyDrums(){
	
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'drums'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'drums';
	
	MCSortColumn();
}

function MCSortDifficultyVocals(){
	
	MusicCatalog_CurrentDirection = MusicCatalog_CurrentColumn == 'vocals'  ? !MusicCatalog_CurrentDirection : true;
	MusicCatalog_CurrentColumn    = 'vocals';
	
	MCSortColumn();
}


function MCSortColumn()
{
	// MusicCatalog_CurrentColumn = see MCColumns above for the keys
	if(MCSongRows().length <= MusicCatalog_MaxClientSortRows)
	{
		MCSortRows(MCColumns()[MusicCatalog_CurrentColumn]);
	}
	else
	{
		MCSortServer()
	}
}

function MCSortServer()
{
	params                   = MCFormParams()
	params['order']          = MusicCatalog_CurrentColumn;
	params['orderDirection'] = MusicCatalog_CurrentDirection;
	MCServerRequest(params);
}

function MCSortRows(key)
{
	rows = MCSongRows();
	
	rows.sort(function(a, b) 
	{
		if(key == MCColumns().dlc ||
		   key == MCColumns().guitar ||
		   key == MCColumns().bass ||
		   key == MCColumns().drums ||
		   key == MCColumns().vocals
		   )
		{
			var keyA = parseInt($(a).children('td').eq(key).text());
			var keyB = parseInt($(b).children('td').eq(key).text());
		}
	
		var keyA; 
		var keyB;
		
		$(a).children('li').each(function(){
			if($(this).hasClass(key))
			{
				if(key == MCColumns().song ||
				   key == MCColumns().artist)
				{
					keyA =  $(this).text();
				}
				else if(key == MCColumns().popular)
				{
					keyA = parseInt($(this).children().eq(0).text());
				}
				else if(key == MCColumns().added)
				{
					// month/day/year(2 digit)
					var value = $(this).text().split('/');
					var month = parseInt(value[0]) - 1;
					var day   = parseInt(value[1]);
					var year  = parseInt(value[2]) + 2000; //no song was added before the year 2000. All of the display dates are 2 digits not 4
					var date  = new Date()
					date.setFullYear(year, month, day)
					
					keyA = date.getTime();
				}
				
			}
		});
		
		$(b).children('li').each(function(){
			if($(this).hasClass(key))
			{
				if(key == MCColumns().song ||
				   key == MCColumns().artist)
				{
					keyB =  $(this).text();
				}
				else if(key == MCColumns().popular)
				{
					keyB = parseInt($(this).children().eq(0).text());
				}
				else if(key == MCColumns().added)
				{
					// month/day/year(2 digit)
					var value = $(this).text().split('/');
					var month = parseInt(value[0]) - 1;
					var day   = parseInt(value[1]);
					var year  = parseInt(value[2]) + 2000; //no song was added before the year 2000. All of the display dates are 2 digits not 4
					var date  = new Date()
					date.setFullYear(year, month, day)
					
					keyB = date.getTime();
				}
			}
		});
		
		if (keyA < keyB) return MusicCatalog_CurrentDirection == true ? -1 : 1;
		if (keyA > keyB) return MusicCatalog_CurrentDirection == true ?  1 : -1;
		
		return 0;
	});
	
	MCApplyRows(rows);
}

function MCApplyRows(rows)
{
	var alternatingRow = 'odd';
	$.each(rows, function(index, row) {
		$(this).attr('class', alternatingRow);
		alternatingRow = alternatingRow == 'odd' ? 'even' : 'odd';
		$("#music-catalog-rows").append(row);
	});
}

/* FORM */
function MCVerify(frm)
{
	/*
	var inputFields = ["Search"];
	var inputFieldsLen = inputFields.length;
	var isVerified = true;
	var i = 0;
	for( ; i < inputFieldsLen; i++)
	{		
		isVerified &= MCVerifyInputField(frm, inputFields[i]);
	}
	
	return isVerified;
	*/
	//return !($("#MusicCatalogInputSearch").val() == "");
	return true
}

function MCVerifyInputField(frm, field, isNumber)
{
	var isError = false;
	if ($("#" + frm + " .input"+ field).val() == "") {
		$("#" + frm + " .input" + field).css({ backgroundColor: "#ffc8c8" });
		isError = true;
	}
	else 
	{
		$("#" + frm + " .input" + field).css({ backgroundColor: "none" });
	}

	return !isError; 
}