/**
 * @author Ruan Candido
 */
	$.widget("nmk.carrossel" , {
		options: {
			animation:'slideH',
			pagination:true,
			paginationType:"cont",
			arrows:true,
			itensPage:3,
			auto:false,
			timeInterval:7000,
			ativarKeydown:true,
			ease:'easeOutSine',
			duration:50
		},
		
		
		_create: function(){
			var $this , $next , $prev , $linkPage , $atualPage , $itens , itensPage , $animation , ativarKeydown , auto , timeInterval , duration , ease , intervalo , time , pages , page , pagination , paginationType , arrows , effectWidth , effectheight , flagNext , flagPrev , pageWidth , pageHeight , target , animation,
				
				start = function(element , options){
					//setando variaveis
					$this  			= element;
					$itens 			= $this.find('.item');
					itensPage 		= options.itensPage;
					pages  	   		= Math.ceil($itens.length / itensPage);
					pageWidth  		= $itens.outerWidth(true) * itensPage;
					pageHeight 		= $itens.outerHeight(true) * itensPage;
					page 	   		= 0;
					target 			= 1;
					animation 		= options.animation;
					auto 			= options.auto;
					timeInterval    = options.timeInterval;
					ativarKeydown 	= options.ativarKeydown;
					arrows 			= options.arrows;
					pagination 		= options.pagination;
					paginationType 	= options.paginationType;
					ease 			= options.ease;
					duration 		= options.duration;
					//Methods
					if (pagination){
						$this.append('<div class="pagination"/>');

						switch (paginationType){
							case "link":
								for (var i = 0 ; i < pages ; i++){
									$this.find('.pagination').append('<a href="javascript:;" class="page">'+ (i+1) +'</a>')
								}
								$linkPage = $this.find('.page');
								$linkPage.first().addClass('cur');
								$linkPage.click(function(){
									target = $linkPage.index(this);
									if(target != page){
										changePage (target , page);
									};
									page = target;
								});
								$linkPage.parent().css({'width': ($linkPage.outerWidth(true)*$linkPage.length) , 'margin':"0 0 0 " + (-($linkPage.outerWidth(true)*$linkPage.length)/2)+"px"});
							break;
							case "cont":
								$this.find('.pagination').append('<p style="display:block; height:100%; overflow:hidden; position:relative;"><span class="maskPages" style="float:left; display:block; left:0; top:0; position:relative;"></span> de '+pages+' páginas</p>')
								for (var i = 0 ; i < pages ; i++){
									$this.find('.pagination .maskPages').append('<span class="atual" style="display:block;">'+(i+1)+'</span>');
								}
								$atualPage = $this.find('.pagination .atual')
								
							break;
						}
					}
					if (arrows){
						$this.append('<a href="javascript:;" class="bts prev">Anterior<span class="effect"><span class="out"></span><span class="hover"></span><span class="disable"></span></span></a><a href="javascript:;" class="bts next">Próxima<span class="effect"><span class="out"></span><span class="hover"></span><span class="disable"></span></span></a>')
						$next = $this.find('.next');
						$prev = $this.find('.prev');
						effectWidth = $this.find('a.bts span.effect').outerWidth(true);
						effectheight = $this.find('a.bts span.effect').outerHeight(true);
						flagNext = true; 
						flagPrev = true;
						$prev.live({
							click: function(){
								target = page - 1;
								if (target >= 0){
									changePage (target , page);
								}else{
									target = page;
								};
								page=target;
								return false;
							},
							mouseenter: function(){
								flagPrev = false;
								if (!$(this).hasClass('disable')){
									if (animation == "slideH"){
										$(this).find('.effect').animate({left:-effectWidth*1/3} , duration , ease);
									}else{
										$(this).find('.effect').animate({top:-effectheight*1/3} , duration , ease);
									};
								}
							},
							mouseleave: function(){
								flagPrev = true; 
								if (!$(this).hasClass('disable')){
									if (animation == "slideH"){
										$(this).find('.effect').animate({left:'0'} , duration , ease);
									}else{
										$(this).find('.effect').animate({top:'0'} , duration , ease);
									}
								}
							}
						});
						$next.live({
							click : function(){
								target = page + 1;
								if (target < pages){
									changePage (target , page);
								}else{
									target = pages-1;
								};
								page=target;
								return false;
							},
							mouseenter: function(){
								flagNext = false; 
								if (!$(this).hasClass('disable')){
									if (animation == "slideH"){
										$(this).find('.effect').animate({left:-effectWidth*1/3} , duration , ease);
									}else{
										$(this).find('.effect').animate({top:-effectheight*1/3} , duration , ease);
									};
								}
							},
							mouseleave: function(){
								flagNext = true; 
								if (!$(this).hasClass('disable')){
									if (animation == "slideH"){
										$(this).find('.effect').animate({left:-effectWidth*2/3} , duration , ease);
									}else{
										$(this).find('.effect').animate({top:-effectheight*2/3} , duration , ease);
									}
								}
							}
						});

						if(pages>1){
							$prev.addClass('disable');
							$next.removeClass('disable');
							if (animation == "slideH"){
								$this.find('a.bts span.effect').css({left:-effectWidth*2/3});
							}else{
								$this.find('a.bts span.effect').css({top:-effectheight*2/3});
							}
						}else{
							$prev.addClass('disable');
							$next.addClass('disable');
							if (animation == "slideH"){
								$prev.find('.effect').css({left:-effectWidth*2/3});
								$next.find('.effect').css({left:0});
							}else{
								$prev.find('.effect').css({top:-effectheight*2/3});
								$next.find('.effect').css({top:0});
							}
						};
					}
					if (auto){
						time = function(){
							intervalo =  setTimeout( function(){
										target = page + 1;
										if (target < pages){
											changePage (target , page);
										}else{
											target = 0;
											changePage (target , page);
										};
										page=target;
									}, timeInterval);
						};
						
						time();
						
						$this.hover( function(){
							clearTimeout(intervalo);						
						},function(){
							time();						
						});
					}
					
					if (ativarKeydown){
						$this.hover (function(){
							$(document).bind( 'keydown' , function(e){
								if (e.keyCode == 39){
									//NEXT
									target = page + 1;
									if (target < pages){
										changePage (target , page);
									}else{
										target = pages-1;
									};
									page=target;
								}else if (e.keyCode == 37){
									//PREV
									target = page - 1;
									if (target >= 0){
										changePage (target , page);
									}else{
										target = page;
									};
									page=target;
								}
							});
						},function(){
							$(document).unbind('keydown');
						});
					};
					
					//Animations
					switch (options.animation){
						case 'slideV':
							$this.append('<div class="mask" style="width:100%; height:100%; position:relative; overflow:hidden;"><div class="animation" style="position:absolute;" /></div>');
							$animation = $this.find('.animation');
							$animation.append($itens).height($itens.length * pageHeight);
						break;
						default:
							$this.append('<div class="mask" style="width:100%; height:100%; position:relative; overflow:hidden;"><div class="animation" style="position:absolute;" /></div>');
							$animation = $this.find('.animation');
							$animation.append($itens).width($itens.length * pageWidth);
						break;
					};
					
					
				},
				
				
				
				changePage = function(target , page){
					if (auto){
						clearTimeout(intervalo);						
					};
					
					var moveH = -pageWidth*(target);
					var moveV = -pageHeight*(target);
					var falta = $itens.length - (target * itensPage);
					if ( falta < itensPage ){
						moveH = moveH + ((itensPage - falta) * $itens.outerWidth(true));
						moveV = moveV + ((itensPage - falta) * $itens.outerHeight(true));
					};
					if (pagination){
						switch(paginationType){
							case "link":
								$linkPage.removeClass('cur').eq(target).addClass('cur');
							break;
							case "cont":
								$this.find('.pagination .maskPages').stop().animate({"top":-target*$atualPage.outerHeight()} , 200 , ease);
							break;
						}
					}
					if (arrows){
		                switch(target){
		                	case 0:
								if (animation == "slideH"){
									$prev.addClass('disable').find('.effect').animate({left:-effectWidth*2/3} , duration , ease);
									if (flagNext){
										$next.removeClass('disable').find('.effect').animate({left:-effectWidth*2/3} , duration , ease);
									}else{
										$next.removeClass('disable');
									}
								}else{
									$prev.addClass('disable').find('.effect').animate({top:-effectheight*2/3} , duration , ease);
									if (flagNext){
										$next.removeClass('disable').find('.effect').animate({top:-effectheight*2/3} , duration , ease);
									}else{
										$next.removeClass('disable');
									}
								};
		                	break;
		                	case pages-1:
								if (animation == "slideH"){
									if (flagPrev){
										$prev.removeClass('disable').find('.effect').animate({left:0} , duration , ease);
									}else{
										$prev.removeClass('disable');
									}
									$next.addClass('disable').find('.effect').animate({left:0} , duration , ease);
								}else{
									if (flagPrev){
										$prev.removeClass('disable').find('.effect').animate({top:0} , duration , ease);
									}else{
										$prev.removeClass('disable');
									}
									$next.addClass('disable').find('.effect').animate({top:0} , duration , ease);
								};
		                	break;
		                	default:
								if (animation == "slideH"){
									if (flagPrev){
										$prev.removeClass('disable').find('.effect').animate({left:0} , duration , ease);
									}else{
										$prev.removeClass('disable');
									}
									if (flagNext){
										$next.removeClass('disable').find('.effect').animate({left:-effectWidth*2/3} , duration , ease);
									}else{
										$next.removeClass('disable');
									}
								}else{
									if (flagPrev){
										$prev.removeClass('disable').find('.effect').animate({top:0} , duration , ease);
									}else{
										$prev.removeClass('disable');
									}
									if (flagNext){
										$next.removeClass('disable').find('.effect').animate({top:-effectheight*2/3} , duration , ease);
									}else{
										$next.removeClass('disable');
									}
								};
		                	break;
		                };
					}
	                					
					switch(animation){
						case 'slideV':
							$animation.stop().animate({top:moveV+'px'},500 , ease);
						break;
						default:
							$animation.stop().animate({left:moveH+'px'},500 , ease);
						break;
					};
					if (auto){
						time();						
					};
				};
				
				
				
				start(this.element , this.options);
			this._update();
		},
		

		
		
		_setOption: function(key, value) {
			this.options[key] = value;
			this._update();
		},
		
		_update: function(){
		}
		
	});

