



$.BackdropSlideshow = function(target, settings) {  this.init( $(target), settings );  };

$.BackdropSlideshow.prototype = {

    init: function(target, settings) {  
	
		this.container = target;
		this.settings = settings;

		this.contentWrap =  this.container.find(this.settings.contentWrap);
		
		this.imageWrap = this.container.find(this.settings.imageWrap);
		this.images = this.imageWrap.find('img');
		this.imageQty = this.images.length;
		
		this.rotatingcontentWrap = this.container.find(this.settings.rotatingcontentWrap);
		this.rotatingcontent = this.rotatingcontentWrap.find(this.settings.contentItemWrap);
		this.rotatingcontentQty = this.rotatingcontent.length;
		
		this.navlinkWrap = this.container.find(this.settings.navlinkWrap);
		
		this.slideIndex = 1;

		/* -- run -- */
		this.images.eq(0).show();
		this.rotatingcontent.eq(0).show();
		
		clearTimeout(this.timer); 
		
		this.buildNavLinks();
		this.setUserEvents();

    },
	
	buildNavLinks : function() {
		
		var $inner = $('<div/>', { className: 'navlinks_inner' });
		
		for ( var i = 0; i < this.imageQty; i++ ) { 
			var navlink = $('<a/>',{ html : i+1, href : '#' });
			if ( i === 0 ) { navlink.addClass('on'); };
			if ( i + 1 === this.imageQty ) {  navlink.addClass('last')  }
			navlink.appendTo($inner);
		}
		$inner.appendTo(this.navlinkWrap);
		this.navlinks = this.navlinkWrap.find('a');
		this.autoCycle();

	},

	autoCycle : function() {
		var _this = this;
		this.timer = setTimeout(function() {   _this.goNext();    }, _this.settings.delay)
	},
	
	
	changeSlide : function(nextSlide, changeIndexTo) {

		this.imageWrap.find('img:visible')
			.add( this.rotatingcontentWrap.find(this.settings.contentItemWrap + ':visible')  ) 
			.fadeOut(this.settings.fadeOutSpeed);
			
		this.navlinkWrap.find('a.on').removeClass('on');

		this.rotatingcontent.eq(nextSlide)
			.add(this.images.eq(nextSlide))
			.stop(true,true).fadeIn(this.settings.fadeInSpeed);
		
		this.navlinks.eq(nextSlide).addClass('on');
		this.slideIndex = changeIndexTo;
		
	},
	
	goNext : function() {
		
		
		var isNotLast = ( this.slideIndex !== this.imageQty )  ? true : false; 
		var nextSlide = ( isNotLast === true ) ? this.slideIndex : 0;
		var changeIndexTo = ( isNotLast === true ) ? this.slideIndex + 1  : 1;
		
		this.changeSlide(nextSlide, changeIndexTo); 
		
		this.autoCycle();	
		
	},
	
	setUserEvents : function() {
		
		var _this = this;
		var timer;
		
		this.container
			.mouseover(function() { 	clearTimeout(_this.timer); 	})
			.mouseleave(function() {
				_this.timer = setTimeout(function() {   _this.goNext();    }, _this.settings.delay)
			});
		
		this.navlinkWrap.live('click',function(event) {
			var $wrap = $(this);
			var $clicked = $(event.target);
			
			if ( $clicked.is('a:not(.on)') ) {  
				var linkEq = $wrap.find('a').index($clicked);
				var activeImageEq = _this.images.index(_this.imageWrap.find('img:visible')  )
				
				_this.changeSlide(linkEq, linkEq + 1);

			}
			return false;
		
		});
	
	}

	
	
};

$.fn.backdropSlideshow = function(options) {

	var settings = { 
		imageWrap : '.ss_images',
		contentWrap : '.ss_content',
		rotatingcontentWrap: '.ss_rotatingcontent',
		navlinkWrap: '.ss_navlinks',
		contentItemWrap : 'li',
		fadeOutSpeed : 500,
		fadeInSpeed : 1200,
		delay: 7000
	}
	
    this.each(function() {
					   
		if (options) { $.extend(settings,options) };
		var backdropslideshow = new $.BackdropSlideshow(this, settings);
    });
    return this;
};

$(document).ready(function() {
	$('#slideshow').backdropSlideshow();
});











