function callback() {
	// Empty the images
	$('#images *').remove();

	// Insert new images
	//$('#images').buildMlsGallery(json.MLSNumber, json.ImageCount);

	// Start the Cycle Plugin
	$('#images').cycle({
		fx: 'fade',
		pager: '#nav',
		prev: '#prev',
		next: '#next',
		pause: 1
	});

	// Pause the cycle if an anchor is clicked
	$('#nav a').click(function(e){
		$.toggleCycle('pause');
		e.preventDefault();
	});

	// Turn on toggling for the pause button
	$('#pause').click(function(e){
		$.toggleCycle();
		e.preventDefault();
	});
}

$(document).ready(function(){
	//console.log("ready");
	$('#images *').remove();
	if(window.source !== undefined && source == 2) {
		var letters = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
	}
	else {
		var letters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	}
	
	$.fn.buildMlsGallery = function(mls, total){
		for(i = 0; i < total; i++){
			$('<img>')
				.mlsImage(mls, source, letters[i], i)
				.appendTo($("#images"));
		}
	}


	// Builds an MLS Image
	$.fn.mlsImage = function(mlsNum, source, letter, i){
		if($(this).is('img')) {
			if(source == 1) {
				url = image[i];
			} else if(source == 2) {
				mlsNum = mlsNum+"";
				if (mlsNum < 1000000){
					folder = mlsNum.substring(0, 3);
				}
				else{
					folder = mlsNum.substring(0, 4);
				}
				if(i < 100) {
					newNum = mlsNum.substring(0, 4);
				}
				else {
					newNum = mlsNum.substring(0, 3);
				}
				if(typeof(letter) == 'number')
					letter = String.fromCharCode(letter);
				url = '/' + source + '-mls-images/' + folder + '/' + mlsNum + '/' + mlsNum + letter + '.jpg';
			}
			return $(this).attr('src', url);
		}
		else
			return $(this);
	}
	
	
	// Additional helper functions for the Cycle plugin
	$.toggleCycle = function(action){
		if(typeof(action) == 'string')
			action = action.toLowerCase();
		
		switch(action){
			case 'pause':
				pauseCycle();
			break;
			case 'resume':
				resumeCycle();
			break;
			default:
				if($('#pause').text() == 'Pause')
					pauseCycle();
				else
					resumeCycle();
			break;
		}

		function pauseCycle(){
			$('#images').cycle('pause');
			$('#pause').text('Resume');
		}

		function resumeCycle(){
			$('#images').cycle('resume');
			$('#pause').text('Pause');
		}
	}

	// Matches the height of the given element
	$.fn.matchHeight = function(elm){
		return $(this).css("height", $(elm).height() + 'px');
	}

	// Builds a property type image 
	$.fn.propertyImage = function(source, options){
		if($(this).is('img')){
			if(typeof(source) == 'object'){
				options = source;
				delete source;
			}
			else{
				settings.source = source;
			}

			settings = jQuery.extend({
				classes: ''
			}, options);
		
			srcUrl = 'http://www.myhomematches.com/img/source' + settings.source + '.jpg';
			return $(this)
				.attr('src', srcUrl)
				//.addClass(settings.classes);
		}
		else
			return $(this);
	}
	
	// Initialize
	if($('#images').length != 0) {
		$('#images').buildMlsGallery(mls_number, image_count);
		// Start the Cycle Plugin
		$('#images').cycle({
			fx: 'fade',
			pager: '#nav',
			prev: '#prev',
			next: '#next',
			pause: 1
		});

		// Pause the cycle if an anchor is clicked
		$('#nav a').click(function(e){
			$.toggleCycle('pause');
			e.preventDefault();
		});

		// Turn on toggling for the pause button
		$('#pause').click(function(e){
			$.toggleCycle();
			e.preventDefault();
		});
	}
});
