// JavaScript Document
Javascript = {
//parses xml document and places images into 'content' div
parseXML: function(file) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  } else {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.open("GET",file,false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML; 
	$(xmlDoc).find('image').each(function(){
		var fileName = $(this).attr('filename');
		var url = $(this).attr('url');
		var desc = $(this).attr('desc');
		$('#content').append('<table><tr><td style="height:68px"><a href="'+url+'" rel="colorbox" title="'+desc+'"><img src="'+fileName+'" style="border:none;"/></a></td></tr><tr><td><a href="'+url+'" rel="colorbox2" title="'+desc+'" >'+Javascript.shorten(desc,6)+'</a></td></tr></table>');
	});
},
// The function that is run when the document is ready.
startup: function(file){
	Javascript.parseXML(file);
	//how many items per page to show stored in hidden input field
	var show_per_page = $('#show_per_page').val(); 
	//getting the amount of elements inside content div
	var number_of_items = $('#content').children().size();
	//calculate the number of pages there will be
	var number_of_pages = Math.ceil(number_of_items/show_per_page);
	$('#num_pages').val(number_of_pages);
	//set the value of the hidden input fields
	$('#current_page').val(0);
	//$('#show_per_page').val(show_per_page);
	//setting the drop down selection value
	//$('#imgs_per_page').val(show_per_page);
	//Previous page link initialization
	var prev_navigation_html = '<a class="previous_link" href="javascript:Javascript.previous();"><img src="/gallery/prevPage.gif" style="border:none" /></a>';
	//Numbered page links initialization
	var navigation_html = '';
	var current_link = 0;
	while(number_of_pages > current_link){
		navigation_html += '<a class="page_link" href="javascript:Javascript.go_to_page(' + current_link +')" longdesc="_' + current_link +'">'+ (current_link + 1) +'</a>';
		current_link++;
	}
	//Next page link initialization
	var next_navigation_html = '<a class="next_link" href="javascript:Javascript.next();"><img src="/gallery/nextPage.gif" style="border:none" /></a>';
	//jQuery to set the navigation div's content
	$('#prev_page_navigation').html(prev_navigation_html);
	$('#next_page_navigation').html(next_navigation_html);
	$('#page_navigation').html(navigation_html);
	//add active_page class to the first page link
	$('#page_navigation .page_link:first-child').addClass('active_page');
	//hide all the elements inside content div
	$('#content').children().hide();
	//and show the first n (show_per_page) elements
	$('#content').children().slice(0, show_per_page).show();
	
	Javascript.go_to_page(0);
},
previous: function(){
	new_page = parseInt($('#current_page').val()) - 1;
	//if there is an item before the current active link run the function
	if($('.active_page').prev('.page_link').length==true){
		Javascript.go_to_page(new_page);
	}
},
next: function(){
	new_page = parseInt($('#current_page').val()) + 1;
	//if there is an item after the current active link run the function
	if($('.active_page').next('.page_link').length==true){
		Javascript.go_to_page(new_page);
	}
},
go_to_page: function(page_num){
	$('#prev_page_navigation').show();
	$('#next_page_navigation').show();
	//get the number of items shown per page
	var show_per_page = parseInt($('#show_per_page').val());
	//get the element number where to start the slice from
	start_from = page_num * show_per_page;
	//get the element number where to end the slice
	end_on = start_from + show_per_page;
	//hide all children elements of content div, get specific items and show them
	$('#content').children().hide().slice(start_from, end_on).show();
	/*get the page link that has longdesc attribute of the current page and add active_page class to it
	and remove that class from previously active page link*/
	$('.page_link[longdesc=_' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
	//update the current page input field
	$('#current_page').val(page_num);
	if(page_num == 0){$('#prev_page_navigation').hide();}
	if(page_num >= ($('#num_pages').val()-1)){$('#next_page_navigation').hide();}
},//ImgsPerPage and num_pages_navigation only called by drop down box to update everything.
ImgsPerPage: function(){
	var show_per_page = $('#imgs_per_page').val();
	$('#show_per_page').val(show_per_page);
	//calculate the number of pages we are going to have
	Javascript.num_pages_navigation(show_per_page);
	if($('#current_page').val() > ($('#num_pages').val()-1))
	{
		$('#current_page').val($('#num_pages').val()-1)
	}
	Javascript.go_to_page($('#current_page').val());
},
num_pages_navigation: function(show_per_page)
{
	//getting the amount of elements inside content div
	var number_of_items = $('#content').children().size();
	//calculate the number of pages we are going to have
	var number_of_pages = Math.ceil(number_of_items/show_per_page);
	$('#num_pages').val(number_of_pages);
	$('#page_navigation').empty();
	var navigation_html = '';
	var current_link = 0;
	while(number_of_pages > current_link){
		navigation_html += '<a class="page_link" href="javascript:Javascript.go_to_page(' + current_link +')" longdesc="_' + current_link +'">'+ (current_link + 1) +'</a>';
		current_link++;
	}
	$('#page_navigation').html(navigation_html);
},
shorten: function(string, num)
{
	var output = "";
	var originalIter = 0;
	var numCounter=0;
	while(originalIter<string.length) {
		output = output+ string.charAt(originalIter);
		if(numCounter==num) {
			output = output + '...';
			originalIter=string.length;
		}
		originalIter++;
		numCounter ++;
	}
	return output;
},
wordWrap: function(string, num)
{
	var output = "";
	var originalIter = 0;
	var numCounter=0;
	while(originalIter<string.length) {
		if(numCounter==num) {
			output = output + '<br>';
			numCounter = 0;
		}
		output = output+ string.charAt(originalIter);
		originalIter++;
		numCounter ++;
	}
	return output;
}
}
