$(function(){
	//page load
	Initialize();
});
Initialize = function()
{
	LoadXMLGalleryLists();
	SetupRollovers();
	SetupPhotoArrows();
	LoadVideo();
	$(".media").media();	
	//load initial image
	//imgMarkup = "<li><img src='images/all/gallery/ItalianFestival/CIMG9849.jpg' /></li>";
	//$("#imageList").append(imgMarkup);
	$("#rightMedia,#videoList span,").hide();
	$("#vid1").show();
}

/*Setup rollovers on dynamically created elements */
SetupRollovers = function()
{
	$(".mediaCollection li").live('mouseover',function(){
		$(this).addClass("active");
	});
	$(".mediaCollection li").live('mouseout',function(){
		$(this).removeClass("active");
	});
}
SetupPhotoArrows = function()
{
	$("#prevImage").click(function(e){
		e.preventDefault();
		ChangePhoto("down");
	});
	$("#nextImage").click(function(e){
		e.preventDefault();
		ChangePhoto("up");
	});
}
/* Load XML depending on which language session is loaded */
LoadXMLGalleryLists = function()
{
	$.get("_xml-content/mediaPhoto.xml",{},function(xml){
		//loop through each content element
		$('album',xml).each(function(i){
			//check if french or english
			lang = getParameterByName("lang");
			//set album name
			if(lang == "EN")
				albumTitle = $(this).attr("titleEng");
			else
				albumTitle = $(this).attr("titleFre");
			//get thumbnail src and create the image markup
			imageSrc = $(this).find('thumbSource').text();
			imageMarkup = "<img class='thumbs' src='images/all/gallery/" + imageSrc + "' alt='" + albumTitle + "' />";
			//get date
			if(lang == "EN")
				albumDate = "<small>" + $(this).find('albumDate').attr('eng') + "</small>";
			else
				albumDate = "<small>" + $(this).find('albumDate').attr('fre') + "</small>";
			//add them all together and append them inside the photo list
			itemString = "<li id=gallery" + i + ">" + imageMarkup + albumTitle + "<br />" + albumDate + "</li>";
			$("#photoList").append(itemString);
					
			//add onclick to gallery li
			$("#gallery" + i).live("click",function(){
				//loop through li elements and remove selected class
				$("#photoList li").each(function(i){
					$(this).removeClass("selected");
				});
				//hide all videos
				$("#vid1,#vid2,#vid3,#vid4").hide();
				//show photo container
				$("#rightMedia").show();
				//add selected class to the selected element
				$(this).addClass("selected");
				//load photos into tha gallery
				LoadGalleryPhotos(i);
			});
		});
	});
}

/* Load the photos within the selected gallery */
LoadGalleryPhotos = function(galleryID)
{
	$.get("_xml-content/mediaPhoto.xml",{},function(xml){
		//loop through each content element
		$('album',xml).each(function(i){
			albumID = $(this).attr("id");
			//check if french or english
			lang = getParameterByName("lang");
			imgArray = [];
			if(albumID == galleryID)
			{
				//load all images into an array and display the first image in the placeholder
				$("#imageList").empty();
				imgCount = 0;
				//loop trough each image in this album
				$(this).find('image').each(function(i){
					imgSrc = "images/all/gallery/" + $(this).attr("src");
					imgMarkup = "<li class='hide' id='galImg" + i + "'><img src='" + imgSrc + "' /></li>";
					$("#imageList").append(imgMarkup);
					imgCount++;
				});
				//append first image onto the placeholder
				currentImage = $("#hiddenPhotoIndex").val("0");
				//store the size of the array in a hidden field
				$("#hiddenAlbumCount").val(imgCount);
				//append Album Count and the first image number
				$("#albumCountHolder").text(imgCount);
				$("#imgIndexHolder").text("1");
				
				//show first image
				$("#galImg0").show();
				//image counter text
				//$("#imageCounter").text();
				
				//set album name
				if(lang == "EN")
					albumTitle = $(this).attr("titleEng");
				else
					albumTitle = $(this).attr("titleFre");
				$("#galleryTitle").text(albumTitle);
			}
		});
	});	
}
//when the user clicks an arrow, switch to next or previous photo
ChangePhoto = function(direction)
{	
	currentImage = $("#hiddenPhotoIndex").val();
	imgInt = parseInt(currentImage);
	albumCount = $("#hiddenAlbumCount").val();
	albumInt = parseInt(albumCount);
	if(direction == "up")
	{		
		//hide the previous image
		$("#galImg" + (imgInt)).hide();
		//if the image is the last in the album go back to the start
		if(imgInt == (albumInt - 1))
		{
			$("#galImg0").show();
			$("#imgIndexHolder").text("1");
			$("#hiddenPhotoIndex").val("0");
		}
		else
		{
			$("#galImg" + (imgInt + 1)).show();
			$("#imgIndexHolder").text((imgInt + 2).toString());
			$("#hiddenPhotoIndex").val(imgInt + 1);
		}
		//console.log($("#hiddenPhotoIndex").val());
	}
	else
	{	
		//hide the previous image
		$("#galImg" + (imgInt)).hide();
		//of the image is the first in the album go back to the end
		if(imgInt == 0)
		{
			imgInt = (albumInt);	
		}
		$("#galImg" + (imgInt - 1)).show();
		if(imgInt == 0)
			$("#imgIndexHolder").text((albumCount + 2).toString());
		else
			$("#imgIndexHolder").text((imgInt).toString());
			
		$("#hiddenPhotoIndex").val(imgInt - 1);
		
		//console.log($("#hiddenPhotoIndex").val());
	}
}

/* This function gets the value of a querstring paramater */
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function LoadVideo()	{
	$("#video1").click(function(){$("#rightMedia,#videoList span,").hide();$("#vid1").show();});
	$("#video2").click(function(){$("#rightMedia,#videoList span,").hide();$("#vid2").show();});
}
