/*
 * Author: Marcus Månsson
 * Date:  2009-02-25
 * Description: 
 * 		Javascript functions for updating the user interface. Relies on the predefined 
 *		Ajax-functions in the prototype framework.
 */

var ajaxapi = null;
var sessionCategory = null;
var startPage = null;

function AjaxAPI ()
{
	this.init = function()
	{
		ajaxapi = this;
		this.ajaxLogo = document.createElement('img');
		this.ajaxLogo.src = 'images/ajax-loader.gif';
		this.ajaxLogo.style.position = 'absolute';
		this.ajaxLogo.style.top = '0px';
		this.ajaxLogo.style.left = '0px';
		this.ajaxLogo.style.visibility = 'hidden';
		document.body.appendChild(this.ajaxLogo);

		this.bannerTimer = null;
		this.updateMenuList();

		if (startPage != null)
			this.updateStaticContent(startPage)
		else if (sessionCategory != null)
			this.updateProductView(sessionCategory);
		else
			this.updateStaticContent('start.php');

	}

	this.updateMenuList = function ()
	{
		new Ajax.Request('getCategories.php',
		{
			method: 'get',
			onSuccess: function (response)
			{
				if ($('categoryList'))
					$('categoryList').innerHTML = response.responseText;
			}
		});
	}

	this.updateProductView = function (category)
	{
		new Ajax.Request('getCategoryDisplay.php',
		{
			parameters: { category: category },
			method: 'get',
			onSuccess: function (response)
			{
				document.location.hash = '?category=' + category;

				if ($('categoryViewer'))
					$('categoryViewer').innerHTML = response.responseText;

				initLightbox();
			}
		});
	}

	this.updateStaticContent  = function (page)
	{
		document.location.hash = '?page=' + page;

		new Ajax.Request(page,
		{
			method: 'get',
			onSuccess: function (response)
			{
				var container = $('categoryViewer');
				if (container)
				{
					while (container.firstChild)
						container.removeChild(container.firstChild);

					var contentBox = document.createElement('div');
					contentBox.id = 'staticContent';
					contentBox.innerHTML = response.responseText;
					container.appendChild(contentBox);
				}

			}
		});
	}

	this.updateRotatingBanner = function ()
	{
		new Ajax.Request('getBanner.php',
		{
			onSuccess: function (response)
			{
				if ($('rotating-banner'))
				{
					$('rotating-banner').innerHTML = response.responseText;
					ajaxapi.bannerTimer = setTimeout('ajaxapi.updateRotatingBanner()', 5000);
				}
			}
		});
	}

	this.updateNewsLetter = function ()
	{
		new Ajax.Request('newsletter.php',
		{
			onSuccess: function (response)
			{
				var container = $('newsletter');
				if (container)
					container.innerHTML = response.responseText;
			}
		});
	}

	this.submitNewsLetter = function (formName)
	{
		var form = $(formName);
		if (form == null) return;

		new Ajax.Request('newsletter.php',
		{
			parameters: Form.serialize(form),
			onSuccess: function (response)
			{
				if ($('newsletter'))
					$('newsletter').innerHTML = response.responseText;
			}
		});
	}

	this.init();
}
