/**
 * @author mutexkid
 * @version 1.1
 * @copyright copyright (c)2010 Josh Skeen
 * @website http://www.joshskeen.com
 * @license MIT License : http://en.wikipedia.org/wiki/MIT_License requires
 *          jquery 1.3+
 */

(function($) {
	var methods = {
			settings: function(passed_settings){
				$(this).data('settings', passed_settings);
				$(this).data("opts", '');
				$(this).data("titlebar", '');
				$(this).data("intervalID", null);
				$(this).data("numberSlides", 0);
				$(this).data("currentSlide", 0);
				$(this).data("slides", Array());
				$(this).data("defaults", {
						startingSlide : 0, // first slide to feature
						frameWidth : 640, // width of slideshow
						frameHeight : 480, // height of slideshow
						titlebarHeight : 20, // height of titlebar
						labels : true, // true or false
						labelPosition : 'bottom', // 'top' or 'bottom'
						timePerSlide : 6500, // time per slide to feature
						transitionTime : 500, // time per transition
						transitionType : "swing" // transition type
					});
				$(this).data("opts", $.extend( {}, $(this).data("defaults"), $(this).data("settings")));
				$(this).simpleslide('__initialize');
			},
			
			__start: function(){
						var $this = this;
		                setInterval(function(){
		                	$(this).simpleslide('animateSlide', $this);
		                }, 
		                $this.data('opts').timePerSlide
		                );
			},
			
			__setup: function(){
				$(this).parent('.jquery_simpleslide').find('.simpleslide_slide').css("z-index", "3");
				var slides = $(this).data("slides");
				var currentslide = $(this).data("currentSlide");
				
				$(slides[currentslide]).show();
				$(slides[currentslide]).css("z-index", "10");
				$(this).simpleslide('setTitle', $(slides[currentslide]).attr("title"));
				$(this).simpleslide('__start');
			},
			
			__initialize: function(){
				$(this).data("currentSlide", $(this).data("opts").startingSlide);
				var widthSettings = {
					'width' : $(this).data("opts").frameWidth + 'px',
					'height' : $(this).data("opts").frameHeight + 'px'
				};
				var widthSettingsText = {
					'width' : ($(this).data("opts").frameWidth - 8) + 'px',
					'height' : ($(this).data("opts").titlebarHeight - 4) + 'px'
				};

				// wrap slides in simpleslide div
				$(this).wrapAll('<div class="jquery_simpleslide"></div>');
				$(this).data("slides", $(this).children("img"));
				$(this).data("numberSlides", $(this).children("img").length - 1);
				$(this).children("img").addClass('simpleslide_slide');
				// set width of slides and slideshow area
				$(this).parent('.jquery_simpleslide').css(widthSettings);
				$(this).parent('.jquery_simpleslide').find('.simpleslide_slide').css(widthSettings);
				
				if ($(this).data("opts").labels === true) {
					var titlebar = '<div class="simpleslide_titlebar"><div class="simpleslide_title_bg"></div><div class="simpleslide_title_copy"></div></div>';
					$(this).parent('.jquery_simpleslide').append(titlebar);
					$(this).parent('.jquery_simpleslide').find(".simpleslide_titlebar")
						    .css(widthSettings)
							.css("height", $(this).data("opts").titlebarHeight + "px");
					
					$(this).parent('.jquery_simpleslide').find(".simpleslide_title_bg")
						    .css(widthSettings)
							.css("height", $(this).data("opts").titlebarHeight + "px");
					
					$(this).parent('.jquery_simpleslide').find(".simpleslide_title_copy")
						    .css(widthSettingsText);
					
					var title_position = ($(this).data("opts").labelPosition == 'bottom' ? ($(this).data("opts").frameHeight - $(this).data("opts").titlebarHeight): 0);
				
					$(this).parent('.jquery_simpleslide').find(".simpleslide_titlebar").css("margin-top", title_position + "px");
				}
				$(this).simpleslide('__setup');
			},
			
		setTitle: function(title) {
				if ($(this).data("opts").labels === true) {
					$('.jquery_simpleslide .simpleslide_title_copy').text(title);
				}
			},
			
		animateSlide: function($this) {
				var currentSlideStyle = {
					'opacity' : '0',
					'z-index' : '10'
				};
				var backgroundSlidesStyle = {
					'opacity' : '1',
					'z-index' : '3'
				};

				var slides = $this.data("slides");
				var currentslide = $this.data("currentSlide");
				if(typeof(currentslide) == "undefined"){
					$this.data("currentSlide", 0);
				}
				
				$this.data("currentSlide", (currentslide < $this.data("numberSlides") ? currentslide + 1 : 0));
				currentslide = $this.data("currentSlide");
				$this.data("elem", $(slides[currentslide]));
				$this.simpleslide('setTitle', $(slides[currentslide]).attr("title"));
				
				if ($this.data("currentSlide") === 0) {
					$this.parent('.jquery_simpleslide').find(".simpleslide_slide").css(backgroundSlidesStyle);
					$this.data("elem").css(currentSlideStyle).animate( {
						opacity : 1
					}, $this.data("opts").transitionTime, $this.data("opts").transitionType);
				} else {
					$this.data("elem").css(currentSlideStyle).animate(
							{
								opacity : 1
							},
							$this.data("opts").transitionTime,
							$this.data("opts").transitionType,
							function() {
								$this.parent('.jquery_simpleslide').find(".simpleslide_slide").css('z-index', '3');
								$this.data("elem").css('z-index', '10');
							});
				}
			}
	}
	
	//var elem, opts, titlebar, intervalID, numberSlides = 0, currentSlide = 0, slides = Array();

	$.fn.simpleslide = function(method) {
		// Method calling logic
	    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.simpleslide' );
	    }  
		return $(this);
	};

})(jQuery);

