(function($){
	
	var tiles = function (element, options) {
		
		var settings = $.extend ($.fn.tiles.defaults, options || {});
		
		// PRIVATE: debug ()
		var debug = function (msg) {
			if (settings.debug && window.console && window.console.log) {
				window.console.log ('tiles: ' + msg);
			}
		};
		
		var ul = $(element).children ('ul');
		if (!ul.length) {
			debug ('List not found.');
			return;
		}
		
		$(ul).addClass ('tile-grid');
		$(ul).children ('li').each (function () {
			$(this).css ({width: settings.tileWidth + 'px', height: settings.tileHeight + 'px'});
			var div = $(this).children ('div');
			var img = $(this).children ('img');
			$(div).css ({zIndex:200, width: settings.tileWidth + 'px', height: settings.tileHeight + 'px', overflow:'hidden'});
			$(img).css ({zIndex:300});
			var hover = $(this).attr ('data-hover');
			if (hover !== '0' && hover !== 'false') {
				$(this).hover (
					function () {
						$(img).stop().animate ({opacity: 0}, settings.fxOverSpeed, function () {
							$(img).css ({zIndex:100});
						});
					}, 
					function () {
						//debug ('out');
						$(img).stop();
						$(img).css ({zIndex:300});
						$(img).animate ({opacity: 1}, settings.fxOutSpeed);
					}
				);
			}
		});
		
		debug ('Loaded tiles plugin.');
	};
	
	// PUBLIC tiles () - main plugin function
	$.fn.tiles = function (options) {
		return this.each (function () {
			var element = $(this);
			// store plugin object in this element's data
			$(this).data ('tiles', new tiles (this, options));
		});
	};
	
	/*
	Default (overridable) settings.
	*/
	$.fn.tiles.defaults = {
		debug: true,
		fxOverSpeed: 400,
		fxOutSpeed: 400,
		tileWidth: 200,
		tileHeight: 200
	};

})(jQuery);


