/* twitter loader by Cathedral Inc., Thomas Mulloy, tm@cthedrl.com */

/* https://github.com/twmulloy/showcase */

(function($, undefined){
	$.widget('ui.showcase', {
	
		options: {
			data: {},
			grid: 12,
			duration: 10000,
			transition: 750
		},
		
		_keys : [],
		
		_buildDisplay: function(element){
			var parent = element;
			
			$.each(this.options.data, function(index){
				$('<li/>').attr({ 'class':'grid_' + $.ui.showcase.prototype.options.grid })
					.append(
						$('<a/>').attr({'href':this.link})
							.append($('<div/>')
								.attr({ 'class' : 'img' })
								.css({ 
									'background':'url("'+this.display.image+'") no-repeat center center'
								})
							)
					)
					.appendTo(parent);
			});
			return parent.children().not(':eq(0)').hide();
		},
		
		_buildPreviews: function(element){
		
			var parent = element,
				grid = ( this.options.grid / this.options.count ) + 1;

			$.each(this.options.data, function(index){
			
				$('<li/>').attr({ 'class':'grid_' + grid })
					.append(
						$('<a/>').attr({'href':this.link})
							.append($('<h3/>')
								.html(this.preview.header)
							)
							.append($('<div/>')
								.attr({ 'class' : 'img'})
								.css({ 
									'background':'url("'+this.preview.image+'") no-repeat center center'
								})
							)
					)
					.appendTo(parent);
			});
			
			// shift first preview to last
			return parent.children().eq(0).hide().detach().appendTo(parent);
			
		},

		_create: function(){	
		
			this.element.empty();
					
			// set information in regard to data object
			var count = 0;
			$.each(this.options.data, function(){ count++; });
			this._setOption('count', count);
			
			// build keys array
			for(key in this.options.data){
				this._keys.push(key);
			}
			
			// build out template
			this.element.addClass('tm-showcase');
			var caption = $('<div/>')
				.attr({ 'class' : 'tm-showcase-caption' })
				.appendTo(this.element);
				
			$('<h2/>')
				.html(this.options.data[this._keys[0]].display.header)
				.appendTo(caption);
			$('<p/>')
				.html(this.options.data[this._keys[0]].display.caption)
				.appendTo(caption);
			$('<a/>')
				.attr({ 
					'href' : this.options.data[this._keys[0]].link,
					'class' : 'button'
				})
				.html(this.options.data[this._keys[0]].display.button)
				.appendTo(caption);
			
			this._buildDisplay(
				$('<ul/>')
					.attr({ 'class' : 'tm-showcase-display tm-big-curl clearfix'})
					.appendTo(this.element)
			);
				
			this._buildPreviews(
				$('<ul/>')
					.attr({ 'class': 'tm-showcase-preview tm-curl clearfix' })
					.appendTo(this.element)
			);

			
		},
		
		_init: function(){
			this._interval();
		},
		
		_interval: function(){

			var showcase = this.element,
				previews = showcase.find('.tm-showcase-preview'),
				displays = showcase.find('.tm-showcase-display'),
				caption = showcase.find('.tm-showcase-caption'),
				count = this.options.count,
				displayHeight = displays.children().eq(0).css('height'),
				data = this.options.data,
				keys = this._keys,
				index = 1; // start at index 1 because 0 is already displaying
			
			setInterval(function(){
				
				var obj = data[keys[index]];
				// cycle previews
				previews.children().eq(count - 1).show();
				previews.children().eq(0).detach().appendTo(previews).hide();
				displays.children(':eq(1)').fadeIn().animate({
					// animation
				},
				$.ui.showcase.prototype.options.transition,
				function(){
					$(this).css('position','relative');
					displays.children().eq(0).hide().detach().appendTo(displays);
				});
				
				// caption
				$('<h2/>').html(obj.display.header).appendTo(caption.empty());
				$('<p/>').html(obj.display.caption).appendTo(caption);
				$('<a/>')
					.attr({ 
						'href' : obj.link,
						'class' : 'button'
					})
					.html(obj.display.button)
					.appendTo(caption);
				
				++index;
				
				// reset counter
				if(count === index){ index = 0 };
				
			}, this.options.duration);
			
		}

	
	});
	
	$.extend( $.ui.showcase, {
		version: "1.0"
	});
})(jQuery);
