(function($) {
  $.fn.aPhotoPad = function(settings) {
  	var config = {'url': '', 'extra': ''}; 
    if (settings) $.extend(config, settings);
	// Load before we get data
	showLoader();
	// Get the data
	$.getJSON(
	  config.url + '?callback=?', 
	  function(data) {
		siteData = data;
		// Site title
		if ($('h1').html.length == 0) {
			$('h1').html(data.site_title);
		}
		// Make the text smaller if the title is long
		if (data.site_title.length > 20) {
			$('h1').css({'font-size':'14px','letter-spacing':'-0.5px'});
		}
		// Pages 
		if(data.pages.length == 0) {
			// Hide info button if there are no pages
			$('#info').remove();
		} else {
			$.each(data.pages, function(count, item) {
				if(item.parent_id == 0 && item.page_title.length > 0 && item.page_title.toLowerCase() != 'facebook' && item.include_html != '0') {
					if (item.alt_html == "1") {
						var newItem = $('<li><a href="' + count + '">' + item.alt_title + '</a></li>');    
					} else {
						var newItem = $('<li><a href="' + count + '">' + item.page_title + '</a></li>');    
					}					
					$('#page_menu_list').append(newItem);
					newItem.data('page', item);
				}			
			});
		}
		// Galleries and categories
		$.each(data.categories, function(count, item) {
			if(item.category_name.length > 0 && item.category_name != "HIDDEN" && item.category_name != "_") {
				var newItem = $('<li id="category_' + item.category_id + '"><a class="category_link" href="' + count + '">' + item.category_name + '</a></li>');    
	    		$('#portfolio_menu_list').append(newItem);
				newItem.data('category', item);
			}
		});
		// Videos
		if(data.videos.length == 0) {
			// Hide button if there are no videos
			$('#videos').remove();
		} else {
			$.each(data.videos, function(count, item) {
				if (item.gallery_hidden == '0') {
					var newItem = $('<li><a href="' + count + '">' + item.gallery_name + '</a></li>');    
			    	$('#video_menu_list').append(newItem);
					newItem.data('video', item);
				}
			});
		}
		// Fade in nav buttons
		$('#nav_list').fadeIn();
		// Start slideshow or just show first image
		if (data.extra === null) {
			// There are no extra files so let's just show the first image from the first gallery
			$('<img />')
				.appendTo($('#main'))
				.css('display','none')
				.attr({src: config.extra + '/../gallery/large/' + data.galleries[0].images[0].image_file})
				.load(function() {
					var w = $(window).width();
					var h = $(window).height() - 75;
					$(this).css({
						'position':'absolute',
						'max-width':w,
						'max-height':h,
					});
					// Now get width and set left
					var l = (w - $(this).width()) / 2;
					$(this).css('left', l);
					$(this).fadeIn();
					removeLoader();
				})
		} else {
			startSlideshow(data.extra, config.extra);
		} 
		
		
	  }
	);
	
	return this;
  };
 
})(jQuery);
