
var photosPerPage;
var minRange = 0;
var maxRange;

var entityID = '';
var photosAvailable = 0;
var typeID = '';

var errorRetry = 0;

function initGallery(entity,type,pAvailable,photosPerGalleryPage)
{
	entityID 		= entity;
	typeID			= type;
	photosAvailable = pAvailable;
	photosPerPage 	= photosPerGalleryPage;
	
	maxRange = photosPerPage;
	
	photoChange(0);
}
function photoClick()
{	
	$('#checkall').click(function () {
		$(this).parents('form:eq(0)').find(':checkbox').attr('checked', this.checked);
		$(this).parents('form:eq(0)').find('.image-select').text("Selected");
		$(this).parents('form:eq(0)').find('.image-select').addClass("image-selected");
		$(this).parents('form:eq(0)').find('.image-select').removeClass("image-select");
		
		// Check all on second click needs built still
	});
	$('.image-select').click(function() {
		$('#galleryTools').show('fast');
  		$(this).removeClass("image-select");
  		$(this).addClass("image-selected");
  		$(this).text('Selected');
	});
	$('.image-selected').click(function() {
  		$(this).removeClass("image-selected");
  		$(this).addClass("image-select");
  		$(this).text('Select');
  		// If last checkbox hide #galleryTools
	});	
}
function setGallerySize(select)
{
	photosPerPage = select.value;
	
	photoChange(0);
		
	for(i=0; i < select.length; i++)
	{
		if(select.options[i].value == photosPerPage)
		{
			select.selectedIndex = i;
			break;
		}
	}
	
	return true;
}
// For the uploader and delete feature (pass a negative value for count)
function addPhotos(count)
{
	photosAvailable += count;
	
	photoChange(0);
}
//Called from link on vehicleProfile
//Starts the AJAX request.

function photoChange(dir,loadFirst) 
{
	var ic = document.getElementById('profileGallery');
	
	if(dir != 0)
		ic.innerHTML = "<img src=\"/images/icons/ajax-loader.gif\" style=\"margin:100px 150px\" alt=\"Loading Vehicle Gallery\" />";
	
		if(photosAvailable > (minRange+photosPerPage) && dir == 0)
		{
			document.getElementById('NextLink').style.display = "inline";
			
			if(minRange > 0)
				document.getElementById('PrevLink').style.display = "inline";
				
			else
				document.getElementById('PrevLink').style.display = "none";
				
		}
		else if(maxRange < photosAvailable && dir == 1)
		{
			minRange += photosPerPage;
			maxRange += photosPerPage;
			
			document.getElementById('PrevLink').style.display = "inline";
			
			if(maxRange >= photosAvailable)
			{	
				document.getElementById('NextLink').style.display = "none";
			}
		}
		else if(dir == -1)
		{
			if(minRange >= photosPerPage)
			{
				minRange -= photosPerPage;
				maxRange -= photosPerPage;
				
				if(minRange == 0)
				{
					document.getElementById('NextLink').style.display = "inline";
					document.getElementById('PrevLink').style.display = "none";
				}
				else
				{
					document.getElementById('NextLink').style.display = "inline";
					document.getElementById('PrevLink').style.display = "inline";
				}
			}
			else
				document.getElementById('PrevLink').style.display = "none";
		}
		else
		{
			minRange = 0;
			maxRange = photosPerPage;
			
			document.getElementById('NextLink').style.display = "none";
			document.getElementById('PrevLink').style.display = "none";
		}
		
		$.ajax({
 		   url: '/ajax/gallery.php?min='+minRange+'&max='+photosPerPage+'&entityID='+entityID+'&typeID='+typeID,
 		   type: 'GET',
		   timeout: 5000,
		   error: function(){
		   
		   },
		   success: function(str){
		       handlePhotoChange(str);
		       photoClick(); 
		       
		       if(loadFirst)
		       {
		       		if(dir > 0)
		       			$("a.vehiclePictures:first").click();
		       			
		       		else if(dir < 0)
		       			$("a.vehiclePictures:last").click();
		       }
		   }
		});
}
//Called when the AJAX response is returned.
function handlePhotoChange(str) 
{
		var ic = document.getElementById('profileGallery');
		
		ic.innerHTML = str;
		
		prepGallery();
}
function prepGallery()
{
	$("a.vehiclePictures").fancybox();
	
}
