/*
 *
 *
 *
 */
 
 jQuery.fn.slideShow = function(options) {
	return this.each(function() {
	
		// Default settings
		settings = jQuery.extend({
			width:700,
			height:250,
			interval:10000,
			speed:'slow',
			opacity:0.7,
			displayNav:true,
			navEmptyImg:'./img/point_empty.png',
			navFullImg:'./img/point_full.png',
			navNextImg:'./img/ad_right.png',
			navPrevImg:'./img/ad_left.png',
			displayDesc:true,
			displayButtons:true,
			autoplay:true,
			changeOnImgClick:false
		}, options);
		
		// Set container style
		$(this).css({
			width:settings.width,
			height:settings.height,
			position:'relative',
			overflow:'hidden',
			listStyle:'none',
			border:'0px solid #000',
			margin:'0px',
			padding:0,
			textAlign:'left',
			color:'#fff',
			display:'inline-block',
			verticalAlign:'middle'
		});
		
        
		// Set elements style
		$('> * > *',this).css({
			position:'absolute',
			top:0
			//width:settings.width,
			//height:settings.height
		});
        
		
		// Elements
		Slides = $('> *', this).length-1;
		Current = 0;
		fssThis = this;
		$('> *:gt(0)', this).css('display', 'none');
		
		showDesc = function(desc) {
			$('#slide_desc').animate({
				top:(settings.height-50),
				opacity:settings.opacity
			},settings.speed, function() {
				$('> p', this).html(desc);
			});
		}
		
		hideDesc = function() {
			$('#slide_desc').animate({
				top:settings.height,
				opacity:0
			},settings.speed, function() {
				$('> p', this).html('');
			});
		}
		
		// Prev, next buttons
		if(settings.displayButtons && Slides > 0) {
			$(this).css('display', 'inline-block').wrap('<span id="slideshowWrapper" style="position:relative;display:inline-block;width:'+settings.width+';height:'+settings.height+';"></span>');
			
			$(this).before('<img src="'+settings.navPrevImg+'" id="slidePrev" alt="" style="display:inline-block;" />');
			$(this).after('<img src="'+settings.navNextImg+'" id="slideNext" alt="" style="display:inline-block;" />');
			
			$('#slidePrev').css({
				position:'absolute',
				top:((settings.height-$('#slidePrev').height())/2),
				left:0
			}).bind('click', function() {
				stopPlay();
				$('#slide_desc').clearQueue();
				changeSlide(Current-1);
				autoplay();
			});
			
			$('#slideNext').css({
				position:'absolute',
				top:((settings.height-$('#slideNext').height())/2),
				right:0
			}).bind('click', function() {
				stopPlay();
				$('#slide_desc').clearQueue();
				changeSlide(Current+1);
				autoplay();
			});
			
			$('#slidePrev').load(function() {
				$('#slideshowWrapper').css({
					width: function(index, value) {
							return parseFloat(value) + $('#slidePrev').width();
						}
				});
			});
			$('#slideNext').load(function() {
				$('#slideshowWrapper').css({
					width:function(index, value) {
							return parseFloat(value) + $('#slideNext').width();
						}
				});
			});
		}
		
		// Add navigation bar
		if(settings.displayNav && Slides > 0) {
			$(this).append('<span id="slide_nav"></span>');
			$('#slide_nav').css({
				background:'#000',
				//display:'block',
				opacity:settings.opacity,
				position:'absolute',
				top:0,
				width:settings.width,
				height:20,
				textAlign:'right'
			});
			
			for(i=0;i<=Slides;i++) {
				$('#slide_nav').append('<a href="javascript:void()" class="slideChange"></a>');
				$('.slideChange').css({
					width:15,
					height:15,
					background:'url("'+settings.navEmptyImg+'") no-repeat center',
					display:'inline-block'
				});
			}
			$('.slideChange:eq(0)').css('background','url("'+settings.navFullImg+'") no-repeat center');
			$('.slideChange').bind('click', function() {
				$('#slide_desc').clearQueue();
				stopPlay();
				newIndex = $('#slide_nav a').index(this);
				changeSlide(newIndex);
				autoplay();
			});
		}
		
		// Add description bar
		if(settings.displayDesc) {
			$(this).append('<span id="slide_desc"><p></p></span>');
			$('#slide_desc').css({
				background:'#000',
				color:'#fff',
				display:'block',
				opacity:settings.opacity,
				position:'absolute',
				width:settings.width,
				height:50,
				fontSize:20,
				top:settings.height,
				textIndent: 20
			});
			$('#slide_desc p').css({
				marginTop:0
			});
			
			if(settings.displayDesc && $('img:eq(0)', fssThis).attr('alt') != '') {
				showDesc($('img:eq(0)', fssThis).attr('alt'));
			}
		}
		
		// Slide change
		changeSlide = function(newIndex) {
			if(newIndex == Current) return false;
			if(newIndex < 0){newIndex = Slides;}
			else if(newIndex > Slides){newIndex = 0;}
			hideDesc();
			$('> *:eq('+Current+')', fssThis).fadeOut(settings.speed);
			$('.slideChange:eq('+Current+')').css('background','url("./img/point_empty.png") no-repeat center');
			$('> *:eq('+newIndex+')', fssThis).fadeIn(settings.speed);
			$('.slideChange:eq('+newIndex+')').css('background','url("./img/point_full.png") no-repeat center');
			
			Current = newIndex;
			
			if(settings.displayDesc && $('img:eq('+newIndex+')', fssThis).attr('alt') != '') {
				showDesc($('img:eq('+newIndex+')', fssThis).attr('alt'));
			}
		}
		
		if(settings.changeOnImgClick) {
			$('> li img', this).bind('click', function() {
				stopPlay();
				$('#slide_desc').clearQueue();
				changeSlide(Current+1);
				autoplay();
			});
		}
		
		autoplay = function() {
			intval = setInterval(function() {
				changeSlide(Current+1);
			}, settings.interval);
		}
		
		stopPlay = function() {
			clearInterval(intval);
			intval = false;
		}
		
		if(settings.autoplay && Slides > 0) {autoplay();} else {intval=false;}
	});
 };
