/* DASHBOARD */

function dashboardFormatItem(row) {
	return row[1] + "|" + row[0]; // + " (id: " + row[1] + ")";
}

function dashboardSelectItem(li) {
    $(".dashboardKeywordSearch").val(li.extra[0] + "|" + li.selectValue);
}

$(document).ready(function() { 

	if($(".dashboardKeywordSearch").length > 0)
	{
	$(".dashboardKeywordSearch").autocomplete(
		"/Keywords.aspx",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
//			onItemSelect:dashboardSelectItem,
//			formatItem:dashboardFormatItem,
			autoFill:true
		}
	);
	}
	
    $('.date').attachDatepicker({ dateFormat: 'dd/mm/yy' });  
});

/* DATA TYPE */


function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	alert("The value you selected was: " + sValue);
}

function selectItem(li) {
    refreshTerm(li.extra[0]);
}

function refreshTerm(id) {  
    $("#focusedTerm").queue(function () {
        $(this).fadeOut("fast"); 
        $(this).dequeue();
        });
        
    $("#focusedTerm").queue(function () {
        $(this).load("/FocusedTerm.aspx?id=" + id);
        $(this).dequeue();
        });
        
    $("#focusedTerm").queue(function () {
        $(this).fadeIn("slow"); 
        $(this).dequeue();
        }); 
        
    return false;
}

function addItem(id, name) {   
    var newText = $(".hiddenValues").val();
    
    if(newText.length > 0)
        newText = newText + ",";
        
    newText = newText + id + "|" + name;

    $(".hiddenValues").val(newText);
    
    // Append to table
    $("#keywordsTable").append("<tr id=\"keyword" + id + "\"><td>" + id + "</td><td>" + name + "</td><td><input type=\"button\" onclick=\"removeItem('" + id + "','" + name +"');\" value=\"Remove\" /></td></tr>");
    
    
    // Clear Term Panel
    $("#focusedTerm").queue(function () {
        $(this).fadeOut("fast"); 
        $(this).dequeue();
        });
        
    $("#focusedTerm").queue(function () {
        $(this).html("Term added successfully!");
        $(this).dequeue();
        });
        
    $("#focusedTerm").queue(function () {
        $(this).fadeIn("slow"); 
        $(this).dequeue();
        });  
	return false;
}

function removeItem(id,name) {  
    // Remove from table
    $("#keywordsTable tr#keyword" + id).remove();
    
    // Remove from hidden value
    var newText = $(".hiddenValues").val();
    newText = newText.replace(id + '|' + name,'');
    newText = newText.replace(',,',',');
    $(".hiddenValues").val(newText);
    
    return false;
}

function formatItem(row) {
	return row[0]; // + " (id: " + row[1] + ")";
}

/*
function lookupAjax(){
	var oSuggest = $(".keywordSearch")[0].autocompleter;

	oSuggest.findValue();

	return false;
}
*/

$(document).ready(function() {
	if($(".keywordSearch").length > 0)
{
	$(".keywordSearch").autocomplete(
		"/Keywords.aspx",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	);
}
    $(".hiddenValues").hide();
});
