/*
bxCarousel v1.0
Plugin developed by: Steven Wanderski
http://bxcarousel.com
http://stevenwanderski.com

Released under the GPL license:
http://www.gnu.org/licenses/gpl.html
*/

(function($){
	$.fn.bxCarousel = function(options){
		var defaults = {
			move          : 1,
			display_num   : 4,
			speed         : 500,
			easing        : 'customInOut',
			auto          : true,
			auto_interval : 10000,
			auto_hover    : true,
			active        : ''
		};
		var options = $.extend(defaults,options);
		
		//return this.each(function(){
			var $this      = this;
			var target     = this;
			var li         = $this.find('li');
			var first      = 0;
			var fe         = 0;
			var last       = options.display_num-1;
			var le         = options.display_num-1;
			var is_working = false;
			var clicked    = false;
			var timer      = '';
			var ow         = li.outerWidth(true);
			var wrap_width = ow*options.display_num;
			var seg        = ow*options.move;
			
			$this.wrap('<div class="bx_container"></div>').width(999999);
			$this.parent('.bx_container').css({'position':'relative','overflow':'hidden','width':wrap_width});
			$this.css({'position':'relative', 'left':-(seg)});
			
			var w = li.slice(0,options.display_num).clone();
			$this.empty().append(w);
			get_p();
			get_a();
			ChangeMainImg($this.find('li').eq(1));
			
			if(options.auto){
				AutoSlider();
				$(document).everyTime(options.auto_interval, "autoPlay", autoPlay);
			}
			
			function autoPlay(){
				slide_next();
			}
			
			$this.parent().parent().parent().find('.next a').click(function(){
				$(document).stopTime("autoPlay");
				slide_next();
				return false;
			});
			
			$this.parent().parent().parent().find('.prev a').click(function(){
				$(document).stopTime("autoPlay");
				slide_prev();
				return false;
			});
			if(options.active != ''){
				$this.find('li').each(function(){
					if($(this).hasClass(options.active)){
						$(this).find('a').addClass('active');
					}
				});
			}
			
			function AutoSlider(){
				if(options.auto_hover){
					var def = $this.parent().parent().parent().find('.thum').position().left;
					var listWidth = $this.parent().parent().parent().find('.thum ul li').outerWidth({margin:true});
					
					$this.find('li').stop(false,true).hover(function(){
						$(document).stopTime("autoPlay");
						ChangeMainImg($(this));
						var ArrowLeft = ($(this).position().left-def)+(listWidth/2)-5;
						ArrowLeft = ArrowLeft-(listWidth);
						$this.parent().parent().parent().find('p.main span').stop(false,true).animate({'left':ArrowLeft},{duration:options.duration,easing:'easeOutCirc'});
					},function(){
						$(document).everyTime(options.auto_interval, "autoPlay", autoPlay);
						ChangeMainImg($this.find('li').eq(1));
						$this.parent().parent().parent().find('p.main span').stop(false,true).animate({'left':'102px'},{duration:options.duration,easing:'easeOutCirc'});
					});
					
				}
			}
			
			function slide_next(){
				if(!is_working){
					if(!$this.is(':animated')){
						is_working=true;
						set_pos('next');
						$this.animate({
							left:'-='+seg
						},options.speed,options.easing,function(){
							$this.find('li').slice(0,options.move).remove();
							$this.css('left',-(seg));
							get_a();
							is_working=false;
							ChangeMainImg($this.find('li').eq(1));
							$(document).everyTime(options.auto_interval, "autoPlay", autoPlay);
						});
					}
				}
			}
			function slide_prev(){
				if(!is_working){
					if(!$this.is(':animated')){
						is_working=true;
						set_pos('prev');
						$this.animate({
							left:'+='+seg
						},options.speed,options.easing,function(){
							$this.find('li').slice(-options.move).remove();
							$this.css('left',-(seg));
							get_p();
							is_working=false;
							ChangeMainImg($this.find('li').eq(1));
							$(document).everyTime(options.auto_interval, "autoPlay", autoPlay);
						});
					}
				}
			}
			
			function get_a(){
				var str=new Array();
				var lix=li.clone();
				le=last;
				for(i=0;i<options.move;i++){
					le++;
					if(lix[le]!=undefined){
						str[i]=$(lix[le]);
					}else{
						le=0;
						str[i]=$(lix[le]);
					}
				}
				
				$.each(str,function(index){
					$this.append(str[index][0]);
				});
				AutoSlider();
			}
			function get_p(){
				var str=new Array();
				var lix=li.clone();
				fe=first;
				for(i=0;i<options.move;i++){
					fe--;
					if(lix[fe]!=undefined){
						str[i]=$(lix[fe]);
					}else{
						fe=li.length-1;
						str[i]=$(lix[fe]);
					}
				}
				$.each(str,function(index){
					$this.prepend(str[index][0]);
				});
				AutoSlider();
			}
			function set_pos(dir){
				if(dir=='next'){
					first+=options.move;
					if(first>=li.length){
						first=first%li.length;
					}
					last+=options.move;
					if(last>=li.length){
						last=last%li.length;
					}
				}else if(dir=='prev'){
					first-=options.move;
					if(first<0){
						first=li.length+first;
					}
					last-=options.move;
					if(last<0){
						last=li.length+last;
					}
				}
			}
			function ChangeMainImg($targetList){
				var Duration = 200;
				var Easing   = 'linear';
				
				var dot = $targetList.find('img').attr('src').lastIndexOf('.');
				var imgsrcL = $targetList.find('img').attr('src').substr(0, dot) + '_l' + $targetList.find('img').attr('src').substr(dot, 4);
				$this.parent().parent().parent().find('p.main img#sub').attr('src',imgsrcL);
				
				$this.parent().parent().parent().find('p.main img#visual').stop(false,false).animate({'opacity':'0'},{duration:Duration,easing:Easing});
				$this.parent().parent().parent().find('p.main img#sub').stop(false,false).animate({'opacity':'1'},{duration:Duration,easing:Easing,complete:function(){
					$this.parent().parent().parent().find('p.main a').attr('href',$targetList.find('a').attr('href'));
					$this.parent().parent().parent().find('p.main a').attr('target',$targetList.find('a').attr('target'));
					$this.parent().parent().parent().find('p.main img#visual').attr('src',imgsrcL).css('opacity','1');
				}});
				$this.parent().parent().parent().find('p.main span').stop(false,false).animate({'opacity':'1'},{duration:Duration,easing:Easing});
				
			}
		//});
	}
})(jQuery);

