﻿var tbArtistId;
var start = 0;
var finish = 10;
var totalCount = 0;
var totalPages = 1;
var hfArtistIdControlId;

function getArtists(artistId, hfArtistId)
{	
	tbArtistId = artistId;
	hfArtistIdControlId = hfArtistId;
	var artistNames = document.getElementById(tbArtistId);
	start = 0;
	finish = 10;
	if (artistNames.value == "")
	{
		var dvResult = document.getElementById('dvResult');
		dvResult.style.display = "none";
	}
	else
	{
		var divElement = document.getElementById('dvResult');
		divElement.style.display = "none";
		element = document.getElementById(tbArtistId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbArtistId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		WebService.GetArtistNamesWithRowCount(artistNames.value, start, finish, onCompleteArtist, onErrorArtist, onTimeOutArtist);
	}
}


function nextStepArtist()
{
	if (finish <= totalCount)
	{
		element = document.getElementById(tbArtistId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbArtistId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		var divElement = document.getElementById('dvResult');
		divElement.style.display = "none";
		start += 10;
		finish += 10;
		var artistNames = document.getElementById(tbArtistId);
		WebService.GetArtistNamesWithRowCount(artistNames.value, start, finish, onCompleteArtist, onErrorArtist, onTimeOutArtist);
	}
}

function previousStepArtist()
{
	if (start >= 10)
	{
		element = document.getElementById(tbArtistId);
		var wheelElement = document.getElementById('wheelDiv');
		coordinates = getAnchorPosition(tbArtistId);
		wheelElement.style.top=coordinates.y+element.offsetHeight+"px";
		wheelElement.style.left=coordinates.x+"px";
		wheelElement.style.position="absolute";
		wheelElement.style.display = "block";
		var divElement = document.getElementById('dvResult');
		divElement.style.display = "none";
		start -= 10;
		finish -= 10;
		var artistNames = document.getElementById(tbArtistId);
		WebService.GetArtistNamesWithRowCount(artistNames.value, start, finish, onCompleteArtist, onErrorArtist, onTimeOutArtist);
	}
	
}

function PreviousPageClick()
{

}

function CalculateTotalPages()
{
	x = parseInt(totalCount/10); 
	pageCount = totalCount / 10;
	if (pageCount - x == 0)
		totalPages = x;
	else
		totalPages = x + 1;
}

function onCompleteArtist(arg)
{	
	var element = document.getElementById(tbArtistId);
	element.disabled = false;
	
	if (arg != null)
	 {
		if (arg.length != 0)
		{
			var divElement = document.getElementById('dvResult');
			divElement.innerHTML = "";
			ulElement = document.createElement("ul");
			totalCount = arg[arg.length-1];
			CalculateTotalPages();
			if (arg.length > 1)
				{
					var divList = document.createElement('div');
					var ulElement = document.createElement("ul");
					for(i=0;i<arg.length-1;i++)
					{
						last = arg[i].split("#")[0];
						first = arg[i].split("#")[1];
						artistId = arg[i].split("#")[2];
					
						var newText = document.createTextNode(last+' '+first);
						var newNode = document.createElement("li");
						var newLink=document.createElement('a');
						newLink.setAttribute('href', 'javascript:setResultArtist("'+last.replace("'","#")+' '+first.replace("'","#")+'",' + artistId + ')');
						newLink.appendChild(newText);
						newNode.appendChild(newLink);
						ulElement.appendChild(newNode);
					}
					
				divList.appendChild(ulElement);
			
				var newBr = document.createElement("br");
				newBr.setAttribute('clear', 'all');			
				
				var divNavigation = document.createElement('div');
				divNavigation.style.width = "190px";

				var previousLink=document.createElement('a');
				previousLink.setAttribute('href', 'javascript:previousStepArtist()');
				previousLink.setAttribute("style","float:left");
				var previousImage = document.createElement('img');
				previousImage.setAttribute('name','imgPreviousPage');
				previousImage.style.styleFloat = "left";
				var imageSrc = '../../../assets/images/template/arrow_left.gif';
				if (baseHTTPPath != null)
					imageSrc = baseHTTPPath + '/assets/images/template/arrow_left.gif';
				previousImage.setAttribute('src',imageSrc);
				previousLink.appendChild(previousImage);
				divNavigation.appendChild(previousLink);
				
				var	textNode = document.createElement('span');
				textNode.innerHTML = ' '+(finish/10) + ' of ' +  totalPages + ' Pages';
				textNode.style.styleFloat = "left";
				textNode.setAttribute("style","float:left");
				textNode.style.styleFloat = "left";
				divNavigation.appendChild(textNode);

				var nextLink=document.createElement('a');
				nextLink.setAttribute('href', 'javascript:nextStepArtist()');
				var nextImage = document.createElement('img');
				nextImage.setAttribute('name','imgPreviousPage');
				var imageSrc = '../../../assets/images/template/arrow_right.gif';
				if (baseHTTPPath != null)
					imageSrc = baseHTTPPath + '/assets/images/template/arrow_right.gif';
				nextImage.setAttribute('src', imageSrc);
				nextLink.appendChild(nextImage);
				divNavigation.appendChild(nextLink);
				
				divElement.appendChild(divList);
				divElement.appendChild(newBr);
				divElement.appendChild(divNavigation);

				if (element.value == "")
				{
					divElement.style.display = "none";
				}
				else
				{
					
					divElement.style.display = "block";
				}
				
				coordinates = getAnchorPosition(tbArtistId);
				divElement.style.top=coordinates.y+element.offsetHeight+"px";
				divElement.style.left=coordinates.x+"px";
				divElement.style.position="absolute";
				element.focus();
			}
		}
	}
	var wheelElement = document.getElementById('wheelDiv');
	wheelElement.style.display = "none";
}
	
function onTimeOutArtist(arg)
{	
}
				
function onErrorArtist(arg)
{	
}

function setResultArtist(selectedValue, artistId)
{
	document.getElementById(tbArtistId).value = selectedValue.replace("#","'");
	document.getElementById(hfArtistIdControlId).value = artistId;
	document.getElementById('dvResult').style.display = "none";
	document.getElementById(document.getElementById("hdnSelectedValueElementId").value).value = artistId;
}

function onFocusArtistOutControl()
{	
	document.getElementById('dvResult').style.display = 'none';
}