// TODO: Restore Q V X Z as possible selections when there is content that is available
g_asLetters = ['A', 'B', 'C', 'D', 'E', 'F',
		    'G', 'H', 'I', 'J', 'K', 'L',
		    'M', 'N', 'O', 'P', 'R',
		    'S', 'T', 'U', 'V', 'W',
		    'Y', ];

$(document).ready(function(){
	$('.letter').click(function () {
		//pickFooterLetter($(this).html());
    pickItemsForFooterLetter($(this).html());
	});

	pickRandomFooterLetter();
});

function pickRandomFooterLetter () {
	
	l_nIndex = Math.floor(g_asLetters.length * Math.random());
	l_sLetter = g_asLetters[l_nIndex];
	
	//pickFooterLetter(g_asLetters[l_nIndex]);
	pickItemsForFooterLetter(l_sLetter);
}
function pickFooterLetter (p_sLetter, p_nPage) {
	// if (InterviewAjax.loading) {
	// 	return false;
	// }

	$('.letter').removeClass("selected");						
	$('#fletter_'+ p_sLetter).addClass("selected");
	
	l_nPage = p_nPage ? p_nPage : 0;

	l_axArgs = [];
	l_axArgs.data = "a=footerletter&letter="+ p_sLetter +"&page="+ l_nPage;

	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArgs, onFooterLetterPicked);
}


function nextLetterPage () {
	advanceLetterPage("next");
}
function previousLetterPage () {
	advanceLetterPage("previous");
}
function advanceLetterPage (p_sDirection) {
	l_sCurrentLetter = $("#footer_cletter").html();
	l_nCurrentPage = parseInt($("#footer_cpage").html());
	l_nNextPage = p_sDirection == "next" ? l_nCurrentPage + 1 : l_nCurrentPage - 1;

	pickFooterLetter(l_sCurrentLetter, l_nNextPage);
}


function onFooterLetterPicked (p_sResult) {
	$("#footer_list").html(p_sResult);
	
	$("#footer_content").hide();
	$("#footer_list, #browse_pagination").fadeIn(300);

	l_nCurrentPage = parseInt($("#footer_cpage").html()); 
	l_nTotalPages = parseInt($("#footer_tpage").html());

	if (l_nTotalPages > 0) {
		l_sPagesText = l_nTotalPages != 1 ? "PAGES" : "PAGE";
		$("#browse_pagination").html("SHOWING "+ (l_nCurrentPage + 1) +" OF "+ l_nTotalPages +" "+ l_sPagesText);
	} else {
		$("#browse_pagination").html("NO RESULTS FOUND");
	}

	// show left arrow only if needed
	if (l_nCurrentPage == 0 || l_nTotalPages == 0) {
		$("#left_arrow").css("visibility", "hidden");
	} else {
		$("#left_arrow").css("visibility", "visible");
		$('#left_arrow').click(function () {
			previousLetterPage();
		});
	}
	
	// show right arrow only if needed
	if (l_nCurrentPage == (l_nTotalPages - 1) || l_nTotalPages == 0) {
		$("#right_arrow").css("visibility", "hidden");
	} else {
		$("#right_arrow").css("visibility", "visible");
		$("#right_arrow").click(function () {
			nextLetterPage();
		});
	}

	// finally - add click functionality to the tags
	$(".footer_tag").click(function () {
		pickFooterTag($(this).html());
	});
}


function pickFooterTag (p_sTag) {
	l_axArgs = [];
	l_axArgs.data = "a=footertagclick&tag="+ p_sTag +"&page=0";

	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArgs, onPickFooterTagCallback);
}


function onPickFooterTagCallback (p_sMessage) {
	$("#footer_content").html(p_sMessage);

	$("#footer_list").hide();
	$("#footer_content, #browse_pagination").fadeIn(700);	
}



function nextItemsForLetterPage () {
	advanceItemsForLetterPage("next");
}
function previousItemsForLetterPage () {
	advanceItemsForLetterPage("previous");
}
function advanceItemsForLetterPage (p_sDirection) {
	l_sCurrentLetter = $("#fc_tag").html();
	l_nCurrentPage = parseInt($("#fc_page").html());
	l_nNextPage = p_sDirection == "next" ? l_nCurrentPage + 1 : l_nCurrentPage - 1;

	pickItemsForFooterLetter(l_sCurrentLetter, l_nNextPage);
}



function pickItemsForFooterLetter (p_sLetter, p_nPage) {
	// if (InterviewAjax.loading) {
	// 	return false;
	// }
	
	$('.letter').removeClass("selected");						
	$('#fletter_'+ p_sLetter).addClass("selected");
	
	l_nPage = p_nPage ? p_nPage : 0;

	l_axArgs = [];
	l_axArgs.data = "a=footerletteritems&letter="+ p_sLetter +"&page="+ l_nPage;

	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArgs, pickItemsForFooterLetterCallback);
}


function pickItemsForFooterLetterCallback (p_sResult) {
//	$("#footer_list").html(p_sResult);
	
//	$("#footer_content").hide();
//	$("#footer_list, #browse_pagination").fadeIn(300);
	$("#footer_content").html(p_sResult);
	
	$("#footer_list").hide();
	$("#footer_content, #browse_pagination").fadeIn(300);

	l_nCurrentPage = parseInt($("#fc_page").html()); 
	l_nTotalPages = parseInt($("#fc_total").html());
	if (isNaN(l_nTotalPages)) {
		l_nTotalPages = 0;
	}

	if (l_nTotalPages > 0) {
		l_sPagesText = l_nTotalPages != 1 ? "PAGES" : "PAGE";
		$("#browse_pagination").html("SHOWING "+ (l_nCurrentPage + 1) +" OF "+ l_nTotalPages +" "+ l_sPagesText);
	} else {
		$("#browse_pagination").html("NO RESULTS FOUND");
	}

	// show left arrow only if needed
	if (l_nCurrentPage == 0 || l_nTotalPages == 0) {
		$("#fc_left_arrow").css("visibility", "hidden");
	} else {
		$("#fc_left_arrow").css("visibility", "visible");
		$('#fc_left_arrow').click(function () {
			previousItemsForLetterPage();
		});
	}
	
	// show right arrow only if needed
	if (l_nCurrentPage == (l_nTotalPages - 1) || l_nTotalPages == 0) {
		$("#fc_right_arrow").css("visibility", "hidden");
	} else {
		$("#fc_right_arrow").css("visibility", "visible");
		$("#fc_right_arrow").click(function () {
			nextItemsForLetterPage();
		});
	}
}
