$(function() {

	var img_offset = 0;
	var offsets_per_page = 0;
	var prev_img_offset = 0;
	var pagination_offset = 0;
	var jtime_offset = 0;
	var previous_offset = 0;
	var old_time = 0;

	$('#related-thumbnails-link').click(function(){
		$('#related-records-container').fadeOut("slow", function () {
			
			if($('#related-thumbnails').hasClass('related-tab-off')) {
				$("#related-thumbnails").removeClass("related-tab-off");
				$("#related-thumbnails").addClass("related-tab-on");
			}
			
			if($('#related-items').hasClass('related-tab-on')) {
				$("#related-items").removeClass("related-tab-on");
				$("#related-items").addClass("related-tab-off");
			}
			
			if($('#record-thumbnails-overall-container').hasClass('display_none')) {
				$("#record-thumbnails-overall-container").removeClass("display_none");
			} else {
				$('#record-thumbnails-overall-container').fadeIn("slow");	
			}
			
		});
	});

	$('#related-thumbnails-items').click(function(){
		$('#record-thumbnails-overall-container').fadeOut("slow", function () {
			
			if($('#related-thumbnails').hasClass('related-tab-on')) {
				$("#related-thumbnails").removeClass("related-tab-on");
				$("#related-thumbnails").addClass("related-tab-off");
			}
			
			if($('#related-items').hasClass('related-tab-off')) {
				$("#related-items").removeClass("related-tab-off");
				$("#related-items").addClass("related-tab-on");
			}

			$('#related-records-container').fadeIn("slow");
			
		});
	});

});

function open_thumbnails (no_of_stills, suffix, media_urn, stills_per_page, direction, copyright, record_id) {
	
	pagination_offset = parseInt ( document.getElementById('offset-thumbnails').value );
	jtime_offset = parseInt ( document.getElementById('offset-time').value );
	offsets_per_page = stills_per_page*jtime_offset;
	
	if(direction == "p"){
		pagination_offset = previous_offset;
	}
	
	$('#record-thumbnails-container').fadeOut("fast", function () {
																
		$('#record-thumbnails-container').empty();

		$.ajax({
			type: "GET",
			url: "/xml/record_thumbnails.php",
			dataType: "xml",
			data: "no_of_stills="+no_of_stills+"&stills_offset="+pagination_offset+"&suffix="+encodeURIComponent(suffix)+"&media_urn="+media_urn+"&time="+jtime_offset+"&copyright="+copyright+"&record_id="+record_id+"",
			success: function(xml) {
				$(xml).find('record').each(function(){
					var img = $(this).find('img').text();
					var img_display = $(this).find('time').text();
					img_offset = $(this).find('img_offset').text();
					
					if(copyright != 'true'){
						$('<div class="thumbnail-item"></div>').html('<p><a href="" onclick="MM_openBrWindow(\'/thumbnail.php?img='+img_offset+'&media_urn='+media_urn+'&stills='+no_of_stills+'&time_offset='+jtime_offset+'&record_id='+record_id+'\',\'thumbnail'+img_offset+'\', \'scrollbars=yes,resizable=yes,width=800,height=625\'); return false"><img src="'+img+'" height="96" width="128" alt="'+img_display+' thumbnail" /></a></p><p><a href="" onclick="MM_openBrWindow(\'/thumbnail.php?img='+img_offset+'&media_urn='+media_urn+'&stills='+no_of_stills+'&time_offset='+jtime_offset+'&record_id='+record_id+'\',\'thumbnail'+img_offset+'\', \'scrollbars=yes,resizable=yes,width=800,height=625\'); return false">'+img_display+'</a></p>').appendTo('#record-thumbnails-container');
					} else {
						$('<div class="thumbnail-item"></div>').html('<p><img src="'+img+'" height="96" width="128" alt="'+img_display+' thumbnail" /></p><p>'+img_display+'</p>').appendTo('#record-thumbnails-container');
					}
					
				});
				img_offset = parseInt(img_offset);
				var original_img_offset = parseInt(img_offset);
				
				img_offset = img_offset+jtime_offset;
				previous_offset = (pagination_offset-jtime_offset)-offsets_per_page;
				if(previous_offset < 0) previous_offset = 0;
				if(img_offset >= no_of_stills){ 
					img_offset = pagination_offset;
					$("#more-thumbnails").addClass("display_none");
				} else {
					if($('#more-thumbnails').hasClass('display_none')) {
						$("#more-thumbnails").removeClass("display_none");
					}
				}
				document.getElementById('offset-thumbnails').value = img_offset;
				
			}
		});
		
		
	});
	
	$('#record-thumbnails-container').fadeIn("fast");
	
}

function update_time_offset (new_time) {
	old_time = document.getElementById('offset-time').value;
	if($('#time-offset-'+old_time+'').hasClass('time-offset-on')) {
		$('#time-offset-'+old_time+'').removeClass("time-offset-on");
		$('#time-offset-'+old_time+'').addClass("time-offset-off");
	}
	if($('#time-offset-'+new_time+'').hasClass('time-offset-off')) {
		$('#time-offset-'+new_time+'').removeClass("time-offset-off");
		$('#time-offset-'+new_time+'').addClass("time-offset-on");
	}
	document.getElementById('offset-time').value = new_time;
	document.getElementById('offset-thumbnails').value = 0;
}