// JavaScript Document


addButtonFuncs();

function setSlideShow()
{
	// to initialise the slideshow
	addButtonFuncs();
	// now hide all the images
	showAPic(0);
}

function showAPic(whichOne)
{
	// first hide old pic
	if(currImg != whichOne)
	{
		// not being asked to show the current picture -- so need to hide old pic 
		$('#pic'+currImg).fadeOut("slow", function()
		{
			showCaption(whichOne);
			$('#pic'+whichOne).fadeIn("slow", function()
			{
				currImg = whichOne;
			});
		});
	}else{
		// for some reason, no image to fade out - need to check
		showCaption(whichOne);
		$('#pic'+whichOne).fadeIn("slow", function()
		{
			currImg = whichOne;
		});
	}
	inTrans = false;
}


function showCaption(whichPic)
{
	// move caption way down
	$("#highlights .caption").css('bottom',-100);
	// show caption command
	$('#highlights .caption').html('<p>' + captions[whichPic] + '</p>');
	// move caption if required so just off screen
	ht = $("#highlights .caption").outerHeight()-1;
	ht = '-' + ht;
	$("#highlights .caption").animate({'bottom':ht},100);
}

function initPicCapt()
{
	/* move off screen */
	ht = $("#highlights .caption").outerHeight()-1;
	ht = '-' + ht;
	$("#highlights .caption").animate({'bottom':ht});
	$("#highlights").mouseenter(function(){
		$("#highlights .caption").clearQueue();		   
		/* get height of caption */
		ht = $("#highlights .caption").outerHeight()-1;
		$("#highlights .caption").animate({'bottom':0});
	});
	$("#highlights").mouseleave(function(){
		/* get height of caption */
		ht = $("#highlights .caption").outerHeight()-1;
		ht = '-' + ht;
		$("#highlights .caption").animate({'bottom':ht});
	});
}




function addButtonFuncs()
{
	$("#right").click(function(){
		if(inTrans == false)
		{
			inTrans = true;
			// move on
			if(currImg + 1 >= captions.length)
			{
				showPic = 0;
			}else{
				showPic = currImg + 1;
			}
			showAPic(showPic);
		}
	});

	$("#left").click(function(){
		if(inTrans == false)
		{
			inTrans = true;
			if(currImg - 1 < 0)
			{
				showPic = captions.length -1;
			}else{
				showPic = currImg - 1;
			}
			showAPic(showPic);
		}
	});
}







function getHighlights(captions)
{
	$.post("/assets/code/highlights.php",{   
       offset: 1,   
       limit: 10,   
       action: "postmsg",   
       time: timestamp   
     }, function(xml) {   
   updateHighlights(xml);   
 });
}
function updateHighlights(xml)
{
	if($("status",xml).text() == "2") return; // 2 means query failed to return any highlights
	var count = 0;
	$("agItem",xml).each(function(id) {   
		agItem = $("agItem",xml).get(id);
		// now add the item
		$("#highlights_title").html("<span id=\"text" + count + "\"><b>"+$("title",agItem).text()+   
             "</b><br />" + $("credit",agItem).text()+ "</span>"); 
		$("#pics").prepend("<img id=\"pic" + count + "\" src=\"http://www.agfax.net/" + $("pic",agItem).text() + "\" width = '400' height = '300' />");
		count++;
	})
	// now need to add the actions to the pictures
	for(i=0; i<=5;i++)
	{
		$("#pic" + i).hover(function(){showCaption(this.id);},function(){hideCaption(this.id);})
	}
}

function showDetails(chosenItem)
{
	whichPic = chosenItem.id.replace("pic", "");
	$("#xpos").append("<br />clicked on  " + whichPic);
	for(i=0; i<=2;i++)
	{
		if(i == whichPic)
		{
			$("#text"+i).show();
		}else{
			$("#text"+i).hide();
		}
	}
}
