//Javascript Document

(function($){
    $.fn.webdoor = function(opcoes) 
	{
		
		var $this = $(this);

		 // opções padrão
		 var defaults = {
			 width: 		500,
			 height: 		350,
			 animate: 		"slide",
			 direction: 	"left",
			 autoAnimate: 	true,
			 timer: 		(1000 * 3),
			 legenda: 		true,
			 positionLegend: "TR",
			 startWith: 	0
		 }

		 $this.opcoes = $.extend(defaults, opcoes);
		 
		 $this.wrap("<div class=\"jwebdoor\"></div>");
		 
		 wrapper = $this.parent();
		 
		 //Customização de estilos Css
		 wrapper.css({
			width: 		$this.opcoes.width,
			height: 	$this.opcoes.height,
			overflow: 	"hidden",
			position: 	"relative"
		 });
		 
		 $("li img", $this).css({
			border: "none"
		 });
		 
		 $this.css({
			margin:		0,
			padding:	0,
			listStyle:	"none",
			position:	"relative"
		 });
		 
		 //Verifica o tipo de animação escolhida
		 switch($this.opcoes.animate){
			case "slide":
				animate.slide($this);
			break;
		 }
		 
		 //Verifica se existe animação automatica
		 if($this.opcoes.autoAnimate){

			$this.atualItem = $this.opcoes.startWith;
			
			clearInterval($this.timer);
			
			$this.timer = setInterval(function(){
				autoAnimate.slide($this);
			}, $this.opcoes.timer);
			
		 }
		 
		 //Verifica se existe legenda
		 if($this.opcoes.legenda){
			legenda.make($this);
			legenda.update($this);
		 }

	};
	
	//Classe de animações na transição
	var animate = {
		slide : function(object)
		{
			opcoes = object.opcoes;
			
			switch(opcoes.direction){
				case "left":
					$("li", object).css({
						float: "left"
					});
					
					object.css({
						width: $("li", object).size() * opcoes.width
					});
				break;
			}
		}
	}
	
	//Classe de animação automatica
	var autoAnimate = {
		slide : function(object)
		{
			opcoes = object.opcoes;
			
			if(object.atualItem < $("li", object).size() - 1){
				object.atualItem ++;
			} else {
				object.atualItem = opcoes.startWith;
			}
			
			autoAnimate.execute(object);
		},
		
		execute : function(object)
		{
			opcoes = object.opcoes;
			
			legenda.update(object);
			
			object.animate({
				left: -(object.atualItem * opcoes.width)
			}, 500);
		},
		
		updateTimer : function(object)
		{
			clearInterval(object.timer);
			
			object.timer = setInterval(function(){
				autoAnimate.slide(object);
			}, object.opcoes.timer);
		}
	}
	
	//Classe de legenda
	var legenda = {
		make : function(object)
		{
			opcoes = object.opcoes;
			
			legendDiv = $("<div class=\"legendaWebdoor\"><ul></ul></div>").appendTo(object.parent());
			
			for(i = 0; i < $("li", object).size(); i ++){
				var li = $("<li>"+ (i + 1) + "</li>").appendTo($("ul", legendDiv));
				
				li.mouseenter(function(){
					if(!$(this).hasClass("ativo")){
						$(this).animate({
							opacity: 1,
							filter:"alpha(opacity=100)"
						});
					}
				}).mouseleave(function(){
					if(!$(this).hasClass("ativo")){
						$(this).animate({
							opacity: 0.5,
							filter:"alpha(opacity=50)"
						});
					}
				}).click(function(){
					if(!$(this).hasClass("ativo")){
						object.atualItem = $(this).index();
						autoAnimate.updateTimer(object);
						autoAnimate.execute(object);						
					}
				});;
			}
			
			$("ul", legendDiv).css({
				listStyle: 	"none",
				margin:		0,
				padding:	0
			});
			
			$("ul li", legendDiv).css({
				width:		15,
				height:		15,
				float:		"left",
				margin: 	"0 5px",
				background: "#FFF",
				textAlign: 	"center",
				fontSize: 	10,
				cursor: 	"pointer",
				opacity:	0.5,
				filter: 	"alpha(opacity=50)"
			});
			
			switch(opcoes.positionLegend){
				case "TL":
					legendDiv.css({
						position: 	"absolute",
						top: 		10,
						left: 		10
					});
				break;
				case "TR":
					legendDiv.css({
						position: 	"absolute",
						top: 		10,
						right: 		10
					});
				break;
				case "BL":
					legendDiv.css({
						position: 	"absolute",
						bottom: 	10,
						left: 		10
					});
				break;
				case "BR":
					legendDiv.css({
						position: 	"absolute",
						bottom: 	10,
						right: 		10
					});
				break;
			}
			
		},
		
		update : function(object)
		{

			$(".legendaWebdoor ul li", object.parent()).each(function(){
				$(this).animate({
					opacity:	0.5,
					filter: 	"alpha(opacity=50)"
				}).removeClass("ativo");
			});

			legendaAtual = $(".legendaWebdoor ul li", object.parent()).eq(object.atualItem);
			
			legendaAtual.animate({
				opacity: 1,
				filter:"alpha(opacity=100)"
			}).addClass("ativo");
			
		}
	}
	
})(jQuery);
