(function( $ ){
	
	var settings = {
		onOver:null,
		onOut:null,
		slideshowSpeed:2000
	};

	var methods = {
		init : function( options ) {
			return this.each(function(){
				
				var $this = $(this);
				var data = $this.data('projectsProjectPane');
							
				if ( options ) $.extend( settings, options );
								
				// If the plugin hasn't been initialized yet
				if (!data) {	
					$this.data('projectsProjectPane', {
						expanded:null,
						slideshow:null,
						slideshowSpeed:null,
						functions:{
							onOver:null,
							onOut:null
						}
					});
					data = $this.data('projectsProjectPane');
					data.slideshowSpeed = settings.slideshowSpeed;
					data.functions.onOver = settings.onOver;
					data.functions.onOut = settings.onOut;
				}
				
				$this.addClass('jq-projectsProjectPane');
				$this.addClass('compact');
				
				$this.projectsProjectPane('createColorCodes');
				$this.projectsProjectPane('createExpandedPane');
				
				$this.mouseenter(function(){
					if(data.functions.onOver) data.functions.onOver($this);
					return false;
				});
			});
		},
		createColorCodes:function(){
			return this.each(function(){
				
				var $this = $(this);
				var data = $this.data('projectsProjectPane');
			
				var $colorCodes = $('<div class="work-type-color-codes"></div>"');
				var $filters = $this.find('ul.filters li.Type');
				var $filter;
				
				var eachWidth = Math.floor(((1/$filters.length) * ($this.width() - ($filters.length-1))));
				$filters.each(function(){
					if($(this).attr('class') == 'Type') {
						$filter = $('<div class="color-code"></div>');
						$filter.addClass($(this).attr('alt'));
						$filter.css('width', eachWidth + 'px');
						$colorCodes.append($filter);
					}
				});
				$colorCodes.find('.color-code:last').addClass('last');
				$this.find('ul.filters').after($colorCodes).hide();
			});
		},
		createExpandedPane:function(){
			return this.each(function(){
				
				var $this = $(this);
				var data = $this.data('projectsProjectPane');
				var href = $this.find('a:eq(0)').attr('href');
				data.expanded = $('<div class="project-pane expanded"></div>');
				var $content = $('<div class="project-content"></div>');
				var $img = $('<div class="project-image outer"><div class="project-image inner"><a href="' + href + '"></a></div></div>');
				var $excerpt = $('<div class="project-excerpt"></div>');
				var $colorCodes = $('<ul class="work-type-color-codes"></ul>');				
				$excerpt.append('<h3>A Brief History...</h3>').append('<p>' + $this.find('p.description').remove().text() + '</p>').append('<a href="' + href + '" class="more-link">See&nbsp;&amp;&nbsp;Read More&nbsp;&rarr;</a>');
				$this.find('.project-image.inner > img').each(function(){
					$(this).attr('src', $(this).attr('longdesc')).removeAttr('longdesc').remove().appendTo($img.find('.project-image.inner a')).addClass('slideshow-image');
				});
				data.slideshow = $img.find('.project-image.inner a').cycle({
					timeout:data.slideshowSpeed,
					containerResize:false,
					slideResize:false
				}).cycle('pause');
				$content.append('<h2><a href="' + href + '">' + $this.find('h2').text() + '</a></h2>').append('<div class="project-url"><p>Link:&nbsp;</p></div>').append($img).append($excerpt).appendTo(data.expanded);
				$content.find('.project-url p').append($this.find('.project-link').remove());
				$this.find('ul.filters li.Type').each(function(){
					$colorCodes.append($(this).clone().addClass($(this).attr('alt')).removeClass('Type'));
				});
				$content.find('h2').after($colorCodes);
				
				$this.after(data.expanded);
				data.expanded.mouseleave(function(){
					if(data.functions.onOut) data.functions.onOut($this);
					return false;
				});
			});
		},
		pauseSlideshow:function(){
			return this.each(function(){
				var $this = $(this);
				var data = $this.data('projectsProjectPane');
				data.slideshow.cycle('pause');
			});
		},
		resumeSlideshow:function(){
			return this.each(function(){
				var $this = $(this);
				var data = $this.data('projectsProjectPane');
				data.slideshow.cycle('resume');
			});
		},
		destroy:function(){
			return this.each(function(){
				var $this = $(this);
				var data = $this.data('projectsProjectPane');

				data.slideshow.cycle('destroy').remove();
				data.expanded.unbind();
				$this.unbind();
				$this.removeData('projectsProjectPane');
			});
		}
	};
		
	$.fn.projectsProjectPane = function( method ) {
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.projectsProjectPane' );
		}    
	
	};	
})( jQuery );

