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

;(function($) {
	var elem, opts, titlebar, intervalID, numberSlides = 0, currentSlide = 0, slides = Array();

$.fn.simpleslide = function(settings) {
	$this = $(this);
	settings = $.extend({}, $.fn.simpleslide.defaults, settings);
	__initialize();

	function __initialize(){
		opts = settings;
		currentSlide = opts.startingSlide;
		var widthSettings = {
				'width'		:	opts.frameWidth  + 'px',
				'height'	:	opts.frameHeight + 'px'
			};
		var widthSettingsText = {
				'width'		:	(opts.frameWidth - 8)  + 'px',
				'height'	:	(opts.titlebarHeight - 4)  + 'px'
			};
		
		//wrap slides in simpleslide div
		$this.wrapAll('<div id="jquery_simpleslide"></div>');
		slides = $this.children();
		numberSlides = $this.children().length - 1;
		$this.children().addClass('simpleslide_slide');
		//set width of slides and slideshow area
		$("#jquery_simpleslide").css(widthSettings);
		$("#jquery_simpleslide .simpleslide_slide").css(widthSettings);
		$("jquery_simpleslide")
		if(opts.labels == true){
			var titlebar = '<div id="simpleslide_titlebar"><div class="simpleslide_title_bg"></div><div class="simpleslide_title_copy">foo</div></div>';
			$("#jquery_simpleslide").append(titlebar);
			$("#jquery_simpleslide #simpleslide_titlebar").css(widthSettings).css("height", opts.titlebarHeight + "px");
			$("#jquery_simpleslide .simpleslide_title_bg").css(widthSettings).css("height", opts.titlebarHeight + "px");
			$("#jquery_simpleslide .simpleslide_title_copy").css(widthSettingsText);
			var title_position = (opts.labelPosition == 'bottom' ? (opts.frameHeight - opts.titlebarHeight) : 0);
			$("#jquery_simpleslide #simpleslide_titlebar").css("margin-top", title_position + "px");
		}
		__setup();
	}
	
	function __setup(){
			$('#jquery_simpleslide .simpleslide_slide').css("z-index", "3");
			$(slides[currentSlide]).show();
			$(slides[currentSlide]).css("z-index", "10");
			$.fn.simpleslide.setTitle($(slides[currentSlide]).attr("title"));
			__start();
	}
	
	function __start(){
		setInterval('$.fn.simpleslide.animateSlide()', opts.timePerSlide);
	}
};

$.fn.simpleslide.setTitle = function(title){
	if(opts.labels == true){
		$('#jquery_simpleslide .simpleslide_title_copy').text(title);
	}
}

$.fn.simpleslide.animateSlide = function(settings) {
	var currentSlideStyle  = {
			'opacity'		:	'0',
			'z-index'		:	'10'
		};
	var backgroundSlidesStyle  = {
			'opacity'		:	'1',
			'z-index'		:	'3'
		};			

	currentSlide = (currentSlide < numberSlides ? currentSlide += 1 : 0);
	var elem = $(slides[currentSlide]);
	$.fn.simpleslide.setTitle($(slides[currentSlide]).attr("title"));

	if(currentSlide == 0){
		$('#jquery_simpleslide .simpleslide_slide').css(backgroundSlidesStyle);
		$(elem).css(currentSlideStyle).animate({opacity: 1}, 
												opts.transitionTime, 
												opts.transitionType
											   );
	}else
	{
		$(elem).css(currentSlideStyle).animate({opacity: 1},
												opts.transitionTime, 
												opts.transitionType, 
												function(){
															$('#jquery_simpleslide .simpleslide_slide').css('z-index', '3');
															$(elem).css('z-index', '10');
														}
											   );
	}
}

$.fn.simpleslide.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		:	5000,		//time per slide to feature
		transitionTime		:	500,		//time per transition
		transitionType		:	"swing"		//transition type
	};

})(jQuery);
