(function($) {
 	var defaults = {
		// superBGimage defaults
		id: 'superbgimage',   // id for the containter
		z_index: 0, 		  // z-index for the container
		inlineMode: 0,		  // 0-resize to browser size, 1-do not resize to browser-size
		showimage: 1, 		  // number of first image to display
		vertical_center: 1,   // 0-align top, 1-center vertical
		transition: 1, 		  // 0-none, 1-fade, 2-slide down, 3-slide left, 4-slide top, 5-slide right, 
		                      // 6-blind horizontal, 7-blind vertical, 90-slide right/left, 9-slide top/down
		transitionout: 1, 	  // 0-no transition for previous image, 1-transition for previous image
		randomtransition: 0,  // 0-none, 1-use random transition (0-7)
		showtitle: 0, 		  // 0-none, 1-show title
		slideshow: 1, 		  // 0-none, 1-autostart slideshow
		slide_interval: 8000, // interval for the slideshow
		randomimage: 1, 	  // 0-none, 1-random image
		speed: 2000, 		  // animation speed
		preload:  1, 		  // 0-none, 1-preload images
		reload: true,
		// AOB defaults
		json_url: '/pics',
		img_path: '/assets/images/slides/',
		thumbs_container_id: 'thumbs',
		controls_id: 'bg_controls',
		loader_id: 'loader'
	};
	var opts    = {};
	var playing = false;
	var loading = false;
	var debug   = false;

	var methods = {
		init: function(options) {
			// superBGimage is not jQuery 1.4-friendly, so we cannot use
			// the data() facility and instead need to be naughty and stor
			opts = $.extend({}, defaults, options);

			$.fn.superbgimage.options = {
				id:               opts.id,
				z_index:          opts.z_index,
				inlineMode:       opts.inlineMode,
				showimage:        opts.showImage,
				vertical_center:  opts.vertical_center,
				transition:       opts.transtion,
				transitionout:    opts.transitionout,
				randomtransition: opts.randomtransition,
				showtitle:        opts.showtitle,
				slideshow:        opts.slideshow,
				slide_interval:   opts.slide_interval,
				randomimage:      opts.randomimage,
				speed:            opts.speed,
				preload:          opts.preload,
				reload:           opts.reload
			};

			var thumbs_container_id = opts.thumbs_container_id;

			load_images(opts.json_url, opts.img_path, thumbs_container_id);

			initialize_controls(opts.controls_id, opts.thumbs_id);

			return this;
		},
		playing: function() { playing; },
		start: function() {
			if (loading) return false;
			if (debug) console.log("caller is " + arguments.callee.caller.toString());
			var thumbs_id = opts.thumbs_container_id;
			if ( !playing ) {
				var controls_id = opts.controls_id;
				$('#'+thumbs_id).startSlideShow();		
				$('#'+controls_id+' a.startSL').hide();							
				$('#'+controls_id+' a.stopSL').show();
				playing = true;
			}

			return $('#'+thumbs_id).nextSlide();
		},
		stop: function() {
			if (loading) return false;
			if (debug) console.log('playing: '+playing);
			if (playing) {
				$('#'+opts.thumbs_id).stopSlideShow();		
				var controls_id = opts.controls_id;
				$('#'+controls_id+' a.startSL').show();							
				$('#'+controls_id+' a.stopSL').hide();
				playing = false;
			}
		},
		hide_controls: function() { $('#'+opts.controls_id).slideUp(500) },
		show_controls: function() { $('#'+opts.controls_id).slideDown(500) }
	};

	$.fn.aob_background = function(method) {
		if (debug) console.log("caller is " + arguments.callee.caller.toString());
		if ( methods[method] ) {
			if (debug) console.log('going to call "'+method+'".aob_background...');
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ))
		} else if ( typeof method === 'object' || !method ) {
			if (debug) console.log('going to init aob_background...');
			return methods.init.apply( this, arguments )
		} else {
			$.error('Method '+ method + ' does not exist on jQuery.aob_background')
		}
	};

	function load_images(json_url,img_path, thumbs_container_id){
		if (debug) console.log('load_images (json_url: '+json_url+')');
		loading = true;
		jQuery.getJSON(json_url,function(data,textStatus,jqXHR){
			loading = false;
			var thumbs_container = jQuery('#'+thumbs_container_id);
			jQuery.each(data, function(index,elem){
				var anchor = document.createElement('a');
				anchor.setAttribute('href', img_path+elem['image']);
				anchor.setAttribute('title', '');
				var img = document.createElement('img');
				img.setAttribute('src', img_path+elem['thumb']);
				img.setAttribute('alt', index+1);
				var span = document.createElement('span');
				var span_content = document.createTextNode(index+1);
				span.appendChild(span_content);
				anchor.appendChild(img);
				anchor.appendChild(span);
				thumbs_container.append(anchor);
			});
			if (debug) console.log('going to start bg');

			$('#'+thumbs_container_id).superbgimage().hide();
			playing = true;
		});
	}

	function initialize_controls(controls_id, thumbs_id) {
		$('#'+controls_id+' a.startSL').hide();
		
		//	start/stop
		$('#'+controls_id+' a.stopSL').click(function()  { $.fn.aob_background('stop') });
		$('#'+controls_id+' a.startSL').click(function() { $.fn.aob_background('start') });

		// prev/next
		$('#'+controls_id+' a.prev').click(function() { return $('#'+thumbs_id).prevSlide() });
		$('#'+controls_id+' a.next').click(function() { return $('#'+thumbs_id).nextSlide() });
	}

})(jQuery);

