// JavaScript Document
(function($) {
		  
	$.fn.jcarbanner = function(o) {
		o = $.extend({
				btnPrev: null,
				btnNext: null,
				marginsAuto: false,
				labels: false,
				autoScale: false,
				time: 3000,
				speed: 1000,
				easing: null,
				mouseOver: false,
				stopOnOver: true,
				loop: 'normal',
				visible: 3,
				scrollNum : 1,
				start: 1,
				strech: false,
				ratio: 1.1,
				highlight: false,
				fading: true,
				vertical: true,
				autoplay: false,
				imgToLi: false,
				onEnd:	null
				}, o || {});
			
		return this.each(function() {
				
				var div = $(this);
				var pDiv = $(this).parent();
				var li = $('li',div );
				
				var liTempWidth = $('li',div ).css('width');

				var ul = div.find('ul').first();
				var totalLi = div.find('li').length;
				var totalPg = Math.ceil(totalLi / o.visible);
				
				pDiv.append('<span class="debug"></span>');
				var debug = pDiv.find('.debug');
				
								
				function firstPass() {
					
					var marginPrev = 0;
					var marginNext = 0;
					
					if ( o.marginsAuto ) {
						if ( o.btnPrev ) { marginPrev = o.btnPrev.width(); }
						if ( o.btnNext ) { marginNext = o.btnNext.width(); }
						//alert( marginPrev + ' ' + marginNext );
					}
					
					if ( o.autoScale) {
						if ( !o.marginsAuto ) {
							
						o.vertical == false ? li.width( (pDiv.width() / o.visible) ) 
						: li.height( (pDiv.height() /o.visible));	
							
						} else {
							if ( $.browser.moz ) {
								o.vertical == false ? li.width( Math.round((pDiv.width() / o.visible) - (marginPrev + marginNext) / o.visible)  ) 
								: li.height( Math.round(pDiv.height() /o.visible) - ((marginPrev + marginNext) / o.visible));	
							} else {
								
															
								o.vertical == false ? li.width( Math.ceil( (pDiv.width() / o.visible) - (marginPrev + marginNext) / o.visible )  ) 
								: li.height( Math.ceil( (pDiv.height() /o.visible) - (marginPrev + marginNext) / o.visible )  );									
							}
						}
					}
					
					var liW = li.width();
					
					var liH = li.height();
					
					
					var stageS = o.vertical == false ? liW * o.visible : liH * o.visible ;
					
					var totalW = totalLi * liW;
					var totalH = totalLi * liH;
					
								
					div.css({ 
						'position'	: 'relative',
						'display'	: 'block',
						'overflow'	: 'hidden',
						'min-height' : li.height(),
						'z-index'	: o.zIndex,
						'padding'	: 0
					});
					
					if ( o.vertical == false ) {
						div.css ({ 'width'		: stageS > pDiv.width() ? pDiv.width() :  (o.loop == 'circular' ? stageS - liW : stageS), 'margin-left' : marginPrev, 'margin-right' : marginNext });
					} else {
						div.css ({ 'height'		: stageS });
					}
					
					if ( $.browser.webkit ) {
						o.vertical == false ? div.width( Math.ceil(div.width())) : div.height( Math.ceil(div.height()));
					}
					
					li.css({
                        'display'   : 'block',
						'float'		: o.vertical == false ? 'left' : 'none'
					});
					
					ul.css({
						'position'	 : 'absolute',
						'min-height' : li.height()
					});
					
					o.highlight == true ? li.first().addClass('active') : '';
					
					if ( o.visible > totalLi ){
					
						if ( o.loop == 'circular' ) {
							for ( i = 0; i < ( o.visible - totalLi ) ; i++ ){
							ul.find('li:nth-child('+i+')').clone().appendTo(ul);    
							}
						} else {
							o.visible = totalLi;    
						}
						ul.finished = true; 
						totalW = o.visible * liW;
						totalH = o.visible * liH;
						
					
					} else if ( o.visible == 1 ) {
					
						li.first().addClass('active');
						ul.finished = true; 
					
					}
					
					o.vertical == false ? ul.width(totalW) : ul.height(totalH);
                    
                    //li.height(liH);

				    if (o.imgToLi ) {

    					li.each( function () { 

                            if ($(this).find('img').length) { 
    	    					var bgImg = $(this).find('img').attr('src');
	    	    				$(this).find('img').remove();
		    	    			$(this).css({ 'background' : 'url('+ bgImg + ') center center no-repeat' });									   
                            }
				    	});
						
    				}
	
				}
				
				firstPass();
			
				
				var pos = o.start;				
				var posEnd;
				var page = 0;
				var direction = 1;        
				var t = 0;                
				var move = true;          
				var resume = false;       
				var enableButtons = true;
				
				
				if (o.loop == 'circular' || o.mouseOver )  { o.easing = 'linear'; o.time = 1; }
				
							
				if (o.labels) {
					pDiv.append('<div class="labels"></div>');
					var labelct = pDiv.find('.labels');
					
					if ( totalPg > 1 ){
						labelct.append('<ul></ul>');
						var labelul = labelct.find('ul');
						for ( i = 0; i < totalPg; i++){	
							labelul.append('<li><span></span><h2>'+ (i + 1) +'</h2></li>');							
						}
						labelul.find('li').first().addClass('active');
						labelul.find('li').bind('click', function () {
							pos = o.visible  * $(this).find('h2').text() - o.visible + 1;
							page = $(this).find('h2').text();
														
							labelul.find('.active').removeClass('active');
							$(this).addClass('active');
							checkPos();															   
						});
					}
				}
				
		
				
				if (o.btnPrev) {
					
					o.start == 1 ? o.btnPrev.addClass('disabled') : o.btnPrev.removeClass('disabled');
					if ( totalLi > o.visible ) {
					o.btnPrev.bind(o.mouseOver == true ? 'mouseenter' : 'click', function () { 	
																			   
						direction = -1;
						posTreat();	
						
						if (o.mouseOver == true) {
							o.autoplay = true 
							o.btnNext.bind('mouseleave', function () { o.autoplay = false });
						}
						
					});
					}
				}
				if (o.btnNext) {
					
					if ( totalLi > o.visible ) {
					
					o.btnNext.bind(o.mouseOver == true ? 'mouseenter' : 'click', function () { 
																						   
						direction = 1;
						posTreat();
						
						if (o.mouseOver == true) {
							o.autoplay = true;
							o.btnNext.bind('mouseleave', function () { o.autoplay = false });
						}
						
					});
					
					
					} else {
						
						o.btnNext.addClass('disabled');
					}
				}
				
				function posTreat() {
					
					var tmpPos = pos ;
					
					
					
					if ( direction < 0 ){
						
						
						if ( pos > 1 ) {
							pos -= o.scrollNum;

						}
						
						if ( pos <= 0 || tmpPos <= 0 ) {
							pos = 1;	
						}
						
						if  ( pos == o.visible + 1 ){
							pos == 1;
						}
						
						
						
						
					
						
						
					} else {
						
						if ( pos <= totalLi - o.visible ) {
							pos += o.scrollNum;
							
						}
						
						if ( pos + o.visible - 1 >= totalLi ){
							pos = totalLi - o.visible + 1;
						}
						
						
						
					}
					
					
					
					if ( o.btnPrev ) {
						pos == 1  ? o.btnPrev.addClass('disabled') : o.btnPrev.removeClass('disabled');
					}
					if ( o.btnNext ) {
						pos < totalLi - o.visible + 1 ? o.btnNext.removeClass('disabled') : o.btnNext.addClass('disabled');
					}
					
					
					page = Math.round( pos / o.visible );
					
					
					checkPos();
				}
				
				function checkPos() {				
					
					if ( labelul ) {					
						labelul.find('.active').removeClass('active');
						labelul.find('li:nth-child('+page+')').addClass('active');						
					}
					
						
					ul.find('li').removeClass('active');
					o.highlight == true ? ul.find('li:nth-child('+pos+')').addClass('active') : '';
					
					
					posEnd = o.vertical == false ? ul.find('li:nth-child('+pos+')').position().left : ul.find('li:nth-child('+pos+')').position().top;
					
					//alert(pos + ' ' + page + ' ' + direction);
					go(-posEnd);
					
					if (o.autoplay) {
						if ( page == totalLi ) {
							if ( o.loop == 'normal' ) {
								pos = 0; page = 0; 
							} else if ( o.loop == 'swing' ) {
								direction = direction * -1;
							}
						} else if ( pos == 1 ) {
							direction = 1;	
						}
					}
						
					if ( o.autoplay == true ) { 
						t = setTimeout( posTreat,o.time);
					}
					
					
					
				}
				
				function vis() {
					return li.slice(pos).slice(0,o.visible);
				};
				
				function go(to) {
					clearTimeout(t);
					ul.stop(true,false);
                    if ($.browser.msie){
                     if (  parseInt($.browser.version) <= 7 ) { 
                        var animateMotion = false;
                     }
                    } else {
                        var animateMotion = true;
                    }


					if ( animateMotion == false ) {	
					    ul.css( o.vertical == false ? {'left' : to}  :  {'top' : to} );	
				    } else {  ul.animate(
						o.vertical == false ? {'left' : to}  :  {'top' : to}, o.speed, o.easing, function () { if ( o.onEnd ) { o.onEnd.call(this) } } );
						
				    }
				}
				
				o.autoplay == true ? t = setTimeout(posTreat,o.time) : ' ';
				
				if ( o.autoScale ) {
					$(window).resize(function() {
						firstPass();				  
					});
				}
				
				
									  
		});
	}

})(jQuery);

