/*
by 王志刚（QQ:770721020 blog:http://www.itxiaozu.com  weibo:http://www.weibo.com/itxiaozu）
 */

jQuery.fn.g_slide=function(options){return this.each(function(){settings=jQuery.extend({speed:'slow',interval:4500,PlayPauseElement:false,PlayText:'开始',PauseText:'暂停',NextElement:false,PrevElement:false,ListElement:false,ListLi:'li',ListLiActive:'on',addListToId:false,allowKeyboardCtrl:true,autoplay:true},options);var Slides=jQuery('> *',this).length-1;var ActSlide=0;var jQslide=jQuery('> *',this);var fssThis=this;jQuery('> *',this).each(function(index){$(this).css('z-index',20-index)});autoplay=function(){intval=setInterval(function(){jQslide.eq(ActSlide).fadeOut(settings.speed);if(settings.ListElement){setActLi=ActSlide+1;if(setActLi==Slides+1){setActLi=0}
jQuery('#'+settings.ListElement+' li').removeClass(settings.ListLiActive);jQuery('#'+settings.ListElement+' li').eq(setActLi).addClass(settings.ListLiActive)}
if(ActSlide==Slides){jQslide.fadeIn(settings.speed);ActSlide=0}else{ActSlide++}},settings.interval);if(settings.PlayPauseElement){jQuery('#'+settings.PlayPauseElement).html(settings.PauseText)}}
stopAutoplay=function(){clearInterval(intval);intval=false;if(settings.PlayPauseElement){jQuery('#'+settings.PlayPauseElement).html(settings.PlayText)}}
jumpTo=function(newIndex){if(newIndex>ActSlide){jQuery('> *:lt('+newIndex+')',fssThis).fadeOut(settings.speed)}else if(newIndex>0&&newIndex<ActSlide){jQuery('> *:lt('+(newIndex)+')',fssThis).fadeOut(settings.speed);jQuery('> *:gt('+(newIndex-1)+')',fssThis).fadeIn(settings.speed)}else if(newIndex==0){jQuery('> *',fssThis).fadeIn(settings.speed)}
ActSlide=newIndex;if(settings.ListElement){jQuery('#'+settings.ListElement+' li').removeClass(settings.ListLiActive);jQuery('#'+settings.ListElement+' li').eq((newIndex)).addClass(settings.ListLiActive)}
clearInterval(intval);autoplay()}
if(settings.ListElement){i=0;li='';while(i<=Slides){if(i==0){li=li+'<li class="'+settings.ListLi+(i+1)+' '+settings.ListLiActive+'"><a href="#">'+(i+1)+'<\/a><\/li>'}else{li=li+'<li class="'+settings.ListLi+(i+1)+'"><a href="#">'+(i+1)+'<\/a><\/li>'}
i++}
jQuery('#'+settings.ListElement).append(li);jQuery('#'+settings.ListElement+' a').bind('click',function(){index=jQuery('#'+settings.ListElement+' a').index(this);jumpTo(index);return false})}
if(settings.PlayPauseElement){if(!jQuery('#'+settings.PlayPauseElement).css('display')){jQuery(this).after('<a href="#" id="'+settings.PlayPauseElement+'"><\/a>')}
if(settings.autoplay){jQuery('#'+settings.PlayPauseElement).html(settings.PauseText)}else{jQuery('#'+settings.PlayPauseElement).html(settings.PlayText)}
jQuery('#'+settings.PlayPauseElement).bind('click',function(){if(intval){stopAutoplay()}else{autoplay()}
return false})}
if(settings.NextElement){jQuery('#'+settings.NextElement).bind('click',function(){nextSlide=ActSlide-1;jumpTo(nextSlide);return false})}
if(settings.PrevElement){jQuery('#'+settings.PrevElement).bind('click',function(){prevSlide=ActSlide+1;jumpTo(prevSlide);return false})}
if(settings.allowKeyboardCtrl){jQuery(document).bind('keydown',function(e){if(e.which==39){nextSlide=ActSlide-1;jumpTo(nextSlide)}else if(e.which==37){prevSlide=ActSlide+1;jumpTo(prevSlide)}else if(e.which==32){if(intval){stopAutoplay()}else{autoplay()}
return false}})}
if(settings.autoplay){autoplay()}else{intval=false}})};
