(function($){
	
	/**
	 * Background Slides plugin for jQuery
	 * 
	 * @version		1.0
	 * @category	jQuery Plugins
	 * @author		Juri Saltbacka <juri.saltbacka@gmail.com>
	 * @website		http://www.ebola.fi/
	 * @copyright	Copyright (c) 2010, Juri Saltbacka
	 */
	$.fn.backgroundSlides = function(images, options)
	{
		var images = images,
			options = $.extend($.fn.backgroundSlides.defaults, options),
			el = this,
			currentImage = 0,
			loading, slides, lastImage;
		
		
		function createLoading()
		{
			var divTag = $('<div>'),
				spanTag = $('<span>');
			divTag.addClass('bgslides-loading').hide();
			spanTag.html('Loading...');
			el.append(divTag.append(spanTag));
			return divTag;
		}
		
		function createSlides()
		{
			var divTag = $('<div>');
			divTag.addClass('bgslides-slides');
			el.append(divTag);
			return divTag;
		}
		
		function loadImage(imageSrc, callback)
		{
			log('Loading image...', imageSrc);
			
			// Show loader
			if(options.showLoading) loading.fadeIn();
			
			var img = new Image();
			return $(img)
				.load(function(){
					slides.append(this);
					update();
					$(this).show().animate(
						{
							opacity: 1,
						},
						(lastImage) ? options.fadetime : 500,
						function(){
							// Do callback
							if (typeof callback == "function") callback();
						}
					);
					
					// Hide loading
					if(options.showLoading) loading.fadeOut();
				})
				.error(function(e){
					log('Error loading image', imageSrc, e)
				})
				.hide()
				.css('opacity', 0)
				.attr('src', imageSrc);
		}
		
		function slideSequence()
		{
			var image = loadImage(images[currentImage], function(){
				if(lastImage) lastImage.remove();
				lastImage = image;
				currentImage = (currentImage + 1 == images.length) ? 0 : currentImage + 1;
				setTimeout(slideSequence, options.interval);
			});
		}
		
		function init()
		{
			loading = createLoading();
			slides = createSlides();
			if(options.random) currentImage = Math.round(Math.random() * (images.length - 1));
			slideSequence();
			log('Initialized');
		}
		
		function update()
		{
			// Define image ratio
			var ratio = options.startHeight/options.startWidth;
			// Gather browser and current image size
			var imagewidth = el.width(),
				imageheight = el.height(),
				browserwidth = $(window).width(),
				browserheight = $(window).height(),
				offset, w, h;
			
			// Resize image to proper ratio
			if ((browserheight/browserwidth) > ratio) {
				w = Math.round(browserheight / ratio);
				h = browserheight;
				slides.children()
					.height(h)
					.width(w)
					.css('left', (browserwidth - w)/2)
					.css('top', (browserheight - h)/2);
			} else {
				w = browserwidth;
				h = Math.round(browserwidth * ratio);
				slides.children()
					.width(w)
					.height(h)
					.css('left', (browserwidth - w)/2)
					.css('top', (browserheight - h)/2);
			}
		}
		
		function resized()
		{
			update();
		}
		
		$(window).bind("load", init);
		$(window).bind("resize", resized)
	}
	
	$.fn.backgroundSlides.defaults = {
		startWidth: 1024,
		startHeight: 768,
		showLoading: true,
		interval: 10000,
		fadetime: 1000,
		random: false
	};

})(window.jQuery);



window.log = function(){
  log.history = log.history || []; 
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);


