/**
 * Show the spinner
 * @return
 */
function ShowSpinner()
{
	var scrollX = 0;
	var scrollY = 0;

	if (window.innerHeight)
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}

	var pageSize = getPageSize();
	var AjaxSpinner = document.getElementById('AjaxSpinner');

	AjaxSpinner.style.left = (scrollX + Math.round(pageSize[0] / 2) - 8) + "px";
	AjaxSpinner.style.top  = (scrollY + Math.round(pageSize[3] / 2) - 8) + "px";
	AjaxSpinner.style.display = "block";
}

/**
 * Hide the spinner
 * @return
 */
function HideSpinner()
{
	var AjaxSpinner = document.getElementById('AjaxSpinner');
	AjaxSpinner.style.display = "none";
}

/**
 * getPageSize() from lightbox.
 * Returns array with page width, height and window width, height
 * Core code from - quirksmode.org
 * Edit for Firefox by pHaez
 * @return array An array containing the page's width and height, the window's width and height
 */
function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize;
}

/**
 * This function is used to show an  auto completer for Unique
 */
function showUniqueAutoCompleter(guid)
{
	var autoCompleter = $('autocomplete_' + guid);
	var pSelectName = $('pSelectName_' + guid);
	if (autoCompleter)
		autoCompleter.style.display = '';
	if (pSelectName)
		pSelectName.style.display = 'none';
}

/**
 * This function is used to hide an auto completer
 */
function hideUniqueAutoCompleter(guid, dontChangeName, buttonNameAfterSelectionAddSlashes)
{
	var autoCompleter = $('autocomplete_' + guid);
	var pSelectName = $('pSelectName_' + guid);
	var spanSelectName = $('spanSelectName_' + guid);
	if (autoCompleter)
		autoCompleter.style.display = 'none';
	if (pSelectName)
		pSelectName.style.display = '';
	if(spanSelectName && !dontChangeName)
		spanSelectName.innerHTML = buttonNameAfterSelectionAddSlashes;
}


/**
 * This function is called by ajax and add a new line
 * in the list of element
 */
function addUniqueElementInListWithResultLine(inputTextValue, liContainer)
{
	//create li prototype object
	var theLi = $(liContainer);

	//check the li element
	if (theLi)
	{
		var guid = $(inputTextValue).id.substr('autocomplete_'.length);
		var callBackJavascriptWithSlashes = $(inputTextValue).getAttribute('callBackJavascriptWithSlashes');
		var buttonNameAfterSelectionAddSlashes = $(inputTextValue).getAttribute('buttonNameAfterSelectionAddSlashes');
		var inputName = $(inputTextValue).name.substr('autocomplete_'.length);

		//retrieve informations
		var content = theLi.innerHTML.stripTags().unescapeHTML();
		addUniqueElementInList(content, theLi.readAttribute('id'), guid, callBackJavascriptWithSlashes, inputName, buttonNameAfterSelectionAddSlashes);
	}
}

/**
 * This function is called by ajax and add a new line
 * in the list of element
 */
function addElementInListWithResultLine(inputTextValue, liContainer)
{
	//create li prototype object
	var theLi = $(liContainer);

	//check the li element
	if (theLi)
	{
		var guid = $(inputTextValue).id.substr('autocomplete_'.length);
		var callBackJavascriptWithSlashes = $(inputTextValue).getAttribute('callBackJavascriptWithSlashes');
		var easyquizzServerURL = $(inputTextValue).getAttribute('easyquizzServerURL');
		var inputName = $(inputTextValue).name.substr('autocomplete_'.length);

		// Check if we have to add all items
		if (theLi.readAttribute('id') == 'allElements')
		{
			eval('var allIds = $H(' + theLi.down('DIV.allIds').innerHTML + ');');
			allIds.each(function (row)
									{
											var content = row[1].stripTags().unescapeHTML();
											addElementInList(content, row[0], guid, callBackJavascriptWithSlashes, inputName, true, easyquizzServerURL)
									});
			return;
		}

			// Check if we have to add a new item
			if (theLi.readAttribute('id').substring(0, 4) == 'new[')
			{
				addElementInList(theLi.readAttribute('initialValue'), theLi.readAttribute('id'), guid, callBackJavascriptWithSlashes, inputName, true, easyquizzServerURL)
				return;
			}

		//retrieve informations and call method
		var content = theLi.innerHTML.stripTags().unescapeHTML();
			addElementInList(content, theLi.readAttribute('id'), guid, callBackJavascriptWithSlashes, inputName, true, easyquizzServerURL);

	}
}

/**
 * this function modify the element in the link
 */
function addUniqueElementInList(nameOfElement, idOfElement, guid, callBackFunctionName, inputName, buttonNameAfterSelectionAddSlashes)
{
	//update the span
	var span = $('spanName_' + guid);
	if (span)
		span.innerHTML = nameOfElement;

	//update the id
	var inputId = $('theInputHiddenElementId_'+guid);
	if (inputId)
		inputId.value = idOfElement;

	//finally hide the auto completer
	hideUniqueAutoCompleter(guid, false, buttonNameAfterSelectionAddSlashes);

	//call the function with the content of the li
	if (callBackFunctionName && (callBackFunctionName != ''))
		eval(callBackFunctionName+'('+idOfElement+');');

	inputId.fire('autoCompleter:change', {inputName: inputName, inputId: inputId});
}

/**
 * this function add a element in the list
 *
 */
function addElementInList(nameOfElement, idOfElement, guid, callBackFunctionName,
													elementName, noCallbackCall, easyquizzServerURL)
{
	var listOfElementInList = $('listContainer_' + guid);
	if (listOfElementInList)
	{
		var count = 0;

		if (listOfElementInList.childNodes)
			count = listOfElementInList.childNodes.length;

		var inputHiddenElement = document.createElement("input");
		inputHiddenElement.setAttribute('name', elementName + '[' + idOfElement + ']');
		inputHiddenElement.setAttribute('value', idOfElement);
		inputHiddenElement.setAttribute('type', 'hidden');

		var imageElement = document.createElement("img");
		imageElement = $(imageElement);
		imageElement.src = easyquizzServerURL + 'images/close.gif';
		imageElement.id = 'imageClose_' + guid + '_' + idOfElement;

		if (callBackFunctionName && (callBackFunctionName != ''))
			imageElement.observe('click', function() {removeElement(imageElement.id);
																								eval(callBackFunctionName+'('+idOfElement+');');});
		else
			imageElement.observe('click', function() {removeElement(imageElement.id);});

		imageElement.style.cursor = 'pointer';
		imageElement.style.cssFloat = 'right';
		imageElement.style.styleFloat = 'right';
		imageElement.style.padding = '1px 3px 0 0';

		var textElement = document.createTextNode(nameOfElement);

		var liElement = document.createElement("li");
		liElement.style.border = '1px solid black';
		liElement.style.margin = '3px 0 0 0';
		liElement.style.padding = '2px 0 4px 3px';
		liElement.style.display = 'block';

		liElement.appendChild(inputHiddenElement);
		liElement.appendChild(imageElement);
		liElement.appendChild(textElement);

		listOfElementInList.appendChild(liElement);

		//call the function with the content of the li
		if (callBackFunctionName && (callBackFunctionName != '') && !noCallbackCall)
			eval(callBackFunctionName+'('+idOfElement+');');

		//remove value from user input
		var inputUser = $('autocomplete_' + guid);
		if (inputUser)
			inputUser.value = '';
	}
}

/**
 * This function is used to remove a liElement
 */
function removeElement(idImageElement)
{
	var liElement = $(idImageElement).up('LI');

	if (liElement)
		liElement.remove();
}
