
var topicIDList = new Array();
var ajaxCallInProgress = 0, ajaxRequest;


$(document).ready( function() {

	/**
	* Resources List : Search Auto-complete 
	**/
	$("#resource_search_term").autocomplete({
			source: "/resources/search/",
			minLength: 3,
			select: function( event, ui ) {
				$("#resource_search_term").val(ui.item.value);
				$("#resource_search_item").val(ui.item.id);
				$("#resource_search_title").val(ui.item.value);
				$("#resource_search_term").get(0).disabled = true;
				$('<a href="#" class="ico ico_x"></a>').insertAfter($("#resource_search_submit").get(0)).click( function() {
					$("#resource_search_term").val('');
					$("#resource_search_item").val('');
					$("#resource_search_title").val('');
					$("#resource_search_term").get(0).disabled = false;
					$(this).remove();
					return false;
				});
			}
		});


	/**
	* Resources List : Preset filters
	**/
	$("#filtertopic a").each(function() {
		var data, id;
		if($(this).hasClass('on')) {
			data = $(this).attr('id').split('_');
			id = data[1];
			topicIDList.push(id);
		}
	});


	/**
	* Resources List : Topic select/deselect
	**/
	$("#filtertopic a").click( function() {

		//
		// parse data
		var data = $(this).attr('id').split('_');
		var id = data[1];
		var title = $(this).attr('title');

		//
		// filter list
		handleSearchFilters(title, id);

		return false;
	});


});



function handleSearchFilters(title, id){
	
	var selector = "a#filterTopic_" + id;
	var pos = $.inArray(id, topicIDList);
	
	if( pos == -1){
		topicIDList.push(id);
		$(selector).attr("class", "on");
	}
	else {
		topicIDList.splice(pos,1);
		$(selector).attr("class", "off");
	}
	
	if(ajaxCallInProgress == 1)	{
		//abort the old call
		ajaxRequest.abort();
	}

	var topicStr = "";
	var count = 1;
	
	for(var itr in topicIDList){
		if(count == topicIDList.length){
			topicStr += topicIDList[itr];
		}
		else {
			topicStr += topicIDList[itr] + ',';
		}
		count += 1;
	}

	//	
	// force a reload if all are unchecked
	if(topicStr.length == 0) {
		location.reload();
		return;
	} else {
		$("ol.pagination").remove();
	}

	//make new call
	ajaxCall('topicStr=' + topicStr, "/resources/filter/", "resourceList");
}
	
function ajaxCall(dataString, ajaxurl,htmlElement, successMessage) {
	// Fade in the progress bar
	/*
	$('#formProgress').hide();
	$('#formProgress').html('<img src="../www/img/ajax-loader2.gif" />');
	$('#formProgress').fadeIn();
	*/
	
	$.ajaxSetup ({
		cache: false
	});

	ajaxCallInProgress = 1;
	
	ajaxRequest = $.ajax({
		type: "GET",
		url: ajaxurl,
		data: dataString,
		success: function(result) {
			// Update the progress bar
			//$('#formProgress').fadeOut();
			ajaxCallInProgress = 0;

			if(htmlElement.length > 0){
				document.getElementById(htmlElement).innerHTML = result;
			}
			
			if(successMessage.length){
				$.dTpopup({
					'content' : '<h3>' +  successMessage + '</h3>'
				});
			}
		},
		error: function(ob,errStr) {
			//$('#formProgress').html('');
			ajaxCallInProgress = 0;
			alert('There was an error getting the data from server. Please try later.');
		}
	});
}


