var _curPos = -1;
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

function main()
{
	try {
		//round the corners
		Nifty("div#menu_div");
		Nifty("#banner", "big");
	} catch (e) {
		//oh well; fails on IE
	}

	//grab the initial list of thumbnails
	getNewerThumbnails();

	//populate main content with the latest news
	getLatestNews();
}

function main_entry()
{
	try {
		//round the corners
		Nifty("#banner", "big");
	} catch (e) {
		//oh well; fails on IE
	}

	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getEntry&link=" + PAGE;
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:entryCallback, onError:onErr});
}

function main_article()
{
	try {
		//round the corners
		Nifty("#banner", "big");
	} catch (e) {
		//oh well; fails on IE
	}

	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getArticle&articleId=" + ARTICLE_ID;
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:articleCallback, onError:onErr});
}

//grab newer thumbnails
function getNewerThumbnails()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getNewerThumbnails&thumbnailId=" + _curPos;
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:thumbnailCallback, onError:onErr});
}

//grab older thumbnails
function getOlderThumbnails()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getOlderThumbnails&thumbnailId=" + _curPos;
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:thumbnailCallback, onError:onErr});
}

//grab the list of miniatures
function getMiniatures()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getAllMiniatures";
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:displayThumbsInMainCallback, onError:onErr});
}

//grab the list of terrain
function getTerrrain()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getAllTerrain";
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:displayThumbsInMainCallback, onError:onErr});
}

//grab the latest news
function getLatestNews()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getLatestNews";
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:displayNewsCallback, onError:onErr});
}

//grab the latest news
function getAllNews()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getAllNews";
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:displayNewsCallback, onError:onErr});
}

//grab the article summaries
function getArticleSummaries()
{
	var responseUrl = CONTEXT_PATH + "/DataProvider.action";
	var params = "getArticleSummaries";
	new Ajax.Request(responseUrl, {method:'post', postBody:params, onSuccess:articleSummaryCallback, onError:onErr});
}

function thumbnailCallback(xhr)
{
	$("messages").innerHTML = "";
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  {
  	_curPos = obj.curPos;

  	//hide or show the up arrow, depending on whether or not we're at the top
  	if (obj.atTop)
  		$('thumb_up').hide();
  	else
  		$('thumb_up').show();

  	//hide or show the down arrow, depending on whether or not we're at the bottom
  	if (obj.atBottom)
  		$('thumb_down').hide();
  	else
  		$('thumb_down').show();

  	drawThumbnails($('left_nav'), obj.data);
	}
}

function displayThumbsInMainCallback(xhr)
{
	$("messages").innerHTML = "";
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  	drawThumbnails($('main_content'), obj.data);
}

//refactor: draw thumbnails
function drawThumbnails(parentEl, data)
{
	var i, count = data.length;

	//clear out anything that might have already been in there
	parentEl.update('');

	//loop the incoming data and build up the thumbnail html
	for (i=0; i<count; i++)
	{
		var thumbnail = data[i];
		var d = new Date(thumbnail.paintDate.time);

		var divEl = new Element('div', { 'class': 'thumbnail'});
		parentEl.appendChild(divEl);
		var aEl = new Element('a', {'href':thumbnail.link + '.html'});
		divEl.appendChild(aEl);
		var imgEl = new Element('img', {'src':thumbnail.image, 'alt':thumbnail.shortDesc, 'title':thumbnail.shortDesc});
		aEl.appendChild(imgEl);
		var br1El = new Element('br', {});
		divEl.appendChild(br1El);
		divEl.appendChild(document.createTextNode(thumbnail.title));
		var br2El = new Element('br', {});
		divEl.appendChild(br2El);
		divEl.appendChild(document.createTextNode(m_names[d.getMonth()] + ". " + d.getFullYear()));
	}
}

function displayNewsCallback(xhr)
{
	$("messages").innerHTML = "";
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  {
  	var parentEl = $('main_content');
  	var i, count = obj.data.length;

		//clear out anything that might have already been in there
		parentEl.update('');

		//loop the incoming data and build up the news html
		for (i=0; i<count; i++)
		{
			var news = obj.data[i];
			var d = new Date(news.date.time);

			var newsEl = new Element('div', { 'class': 'news'});
			parentEl.appendChild(newsEl);

			//date
			var dateSpanEl = new Element('span', {'class':'date'});
			dateSpanEl.appendChild(document.createTextNode(m_names[d.getMonth()] + ". " + d.getDate() + ", " + d.getFullYear()));
			newsEl.appendChild(dateSpanEl);
			newsEl.appendChild(new Element('br', {}));
			newsEl.appendChild(new Element('br', {}));

			//title
			if (news.title)
			{
				var titleSpanEl = new Element('span', {'class':'title'});
				titleSpanEl.appendChild(document.createTextNode(news.title));
				newsEl.appendChild(titleSpanEl);
				newsEl.appendChild(new Element('br', {}));
				newsEl.appendChild(new Element('br', {}));
			}

			//content
			var contentEl = new Element('div', {}).update(news.content);
			newsEl.appendChild(contentEl);

			//if this is not the last item, put in some space
			if (i+1 < count)
			{
				newsEl.appendChild(new Element('br', {}));
				newsEl.appendChild(new Element('br', {}));
				newsEl.appendChild(new Element('br', {}));
				newsEl.appendChild(new Element('br', {}));
			}
		}
	}
}

function articleSummaryCallback(xhr)
{
	$("messages").innerHTML = "";
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  {
  	var parentEl = $('main_content');
  	var i, count = obj.data.length;

		//clear out anything that might have already been in there
		parentEl.update('');

		var ulEl = new Element('ul', {'class':'articles'});
		parentEl.appendChild(ulEl);

		//loop the incoming data and build up the news html
		for (i=0; i<count; i++)
		{
			var news = obj.data[i];

			var liEl = new Element('li', {});
			ulEl.appendChild(liEl);

			var aEl = new Element('a', {'href':'article/'+news.id + '.html'}).update(news.summary);
			liEl.appendChild(aEl);
		}
	}
}

function entryCallback(xhr)
{
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  {
  	var i, count, j, jcount;
  	var d = new Date(obj.paintDate.time);
  	var title = obj.title;
  	var paintDate = m_names[d.getMonth()] + ". " + d.getFullYear();

		//print out main info
  	document.title = title;
  	$('entry_title').update(title);
  	$('entry_date').update(paintDate);
  	$('entry_mfg').update(obj.manufacturer);
  	$('desc').update(obj.description);

  	//print out any main images
  	var parentEl = $('desc');
  	if (obj.images && obj.images.length > 0)
  	{
  		parentEl.appendChild(new Element('br'));
  		parentEl.appendChild(new Element('br'));
  		count = obj.images.length;
  		for (i=0; i<count; i++)
  		{
  			addImage(parentEl, obj.images[i]);
  		}
  	}

		//print out each subsection
		if (obj.subsections && obj.subsections.length > 0)
		{
			count = obj.subsections.length;
			for (i=0; i<count; i++)
			{
	  		parentEl.appendChild(new Element('br'));
	  		parentEl.appendChild(new Element('br'));
				parentEl.appendChild(new Element('hr', {'class':'subs'}));
				var divEl = new Element('div', {}).update(obj.subsections[i].description);
				parentEl.appendChild(divEl);

				//print out any subsection images
				if (obj.subsections[i].images && obj.subsections[i].images.length > 0)
				{
		  		parentEl.appendChild(new Element('br'));
		  		parentEl.appendChild(new Element('br'));
					jcount = obj.subsections[i].images.length;
					for (j=0; j<jcount; j++)
					{
						addImage(parentEl, obj.subsections[i].images[j]);
					}
					parentEl.appendChild(new Element('div', {'style':'clear:left'}));
				}
			}
		}
  }
}

function addImage(parentEl, img)
{
	var el;

	//if image has a thumbnail, display the thumbnail with a link to the actual image
	if (img.thumbnail)
	{
		el = new Element('a', {'href':img.image,'target':'_blank'});

		var imgEl;
		if (img.description)
			imgEl = new Element('img', {'src':img.thumbnail, 'alt':img.description});
		else
			imgEl = new Element('img', {'src':img.thumbnail});

		el.appendChild(imgEl);
	}
	else // no thumbnail; just display the image
	{
		if (img.description)
			el = new Element('img', {'src':img.image, 'alt':img.description});
		else
			el = new Element('img', {'src':img.image});
	}

	var divEl = new Element('div', {'class':'imgx'});
	divEl.appendChild(el);
	parentEl.appendChild(divEl);
}

function articleCallback(xhr)
{
	var obj;
	try {
  	obj = eval("(" + xhr.responseText + ")");
  } catch (ex) {
  	onErr(ex || ex.message);
  	return;
  }
  if (obj.ERR)
    onErr(obj.ERR);
  else
  {
		//print out main info
		title = obj.summary;
  	document.title = title;
  	$('meta_info').update(title);
  	$('desc').update(obj.content);
  }
}

function onErr(xhr)
{
  alert("Error:\n" + xhr)
}

