(function($){
	$.fn.veloxMembros = function(options){
		var zIndex = 3;
		var settings = $.extend({}, $.fn.veloxMembros.defaults, options);
		
		var cont = 0;
		$(this).find(".person").each(function(){
			var person = $(this);
			person.css("left", (cont++ * 120)+"px")
			person.data("background", person.find(".background"));
			person.data("details", person.find(".details"));
		});
		
		$(this).find(".person").hover(function(){
			_open($(this));
		},function(){
			_close($(this));
		});
		
		_open = function(items) {
			_setStyle(items, true);
		};
		
		_close = function(items) {
			_setStyle(items, false);
		};
		
		_setStyle = function(items, open) {
			return items.each(function(){
				var person = $(this);
				
				if(open){
					person.css("zIndex",zIndex++);
					person.addClass("open");
				} else {
					person.removeClass("open");
				}
				
				if($.support.opacity){
					person.data("details").stop().fadeTo(settings.speed / 2, open? 1 : 0);
					person.stop().fadeTo(settings.speed, 1);
					person.siblings().stop().fadeTo(settings.speed, open ? .3 : 1);
				} else {
					person.data("details")[open?"show":"hide"]();
				}
				
				var size = open ? 340 : 170;
				person.data("background").stop().animate({
					width : size,
					height : size,
					top : open ? (person.hasClass("top") ? -180: -140) : 0
				}, settings.speed, settings.easing);
			});
		};
	};
	
	$.fn.veloxMembros.defaults = {
		speed : 250,
		easing : "easeOutExpo"
	};
	
	$.fn.veloxGaleria = function(options){
		var settings = $.extend({}, $.fn.veloxGaleria.defaults, options);
		
		return this.each(function(){
			var $this = $(this);
			
			var tamanho = 0;
			var track = $this.find("div.thumbs div.track");
			var thumbs = $this.find("div.thumbs");
			var thumbs_a = $this.find("div.thumbs a");
			var descricao = $this.find("div.legenda");
			var setaesq = $this.find("div.imagens").children("div.setaesq");
			var setadir = $this.find("div.imagens").children("div.setadir");
			
			thumbs_a.each(function(){
				tamanho = tamanho + ($(this).find("img").width()+1);
			});
			
			track.width(tamanho);
			
			if(tamanho > 750){
				thumbs.css({ height: "135px", overflow: "auto" });
			}
			
			/*thumbs.mousemove(function(e){
				//var left = (e.pageX - thumbs.offset().left) * (((e.pageX - thumbs.offset().left)/thumbs.width()) * (tamanho/thumbs.width()));
				var position = (e.pageX - thumbs.offset().left);
				var percentage = Math.ceil((position*100)/thumbs.width());
				var left = tamanho * (percentage/100);
				thumbs.scrollLeft(left);
			});*/
			
			thumbs_a.click(function(){
				if(!$(this).hasClass("on")){
					setadir.hide();
					setaesq.hide();
					
					var idx = $(this).data("id");
					var index = thumbs_a.index($(this));
					var offset = track.offset()['left'];
					
					thumbs.animate({ scrollLeft: (thumbs_a.filter(":data(id,"+idx+")").offset().left-offset) }, 1000);
					
					if($this.find("div.imagens div.img").filter(":data(id,"+idx+")").size() > 0){
						var imagens = $this.find("div.imagens").children("div.img");
						var img = imagens.filter(":data(id,"+idx+")");
						var legenda = img.find("img").attr("alt");
						
						thumbs_a.removeClass("on");
						if(settings.showTitulo == "inside"){
							imagens.find("div.legenda").fadeOut(settings.speed).css({ right: -278 });
						} else {
							descricao.fadeOut(settings.speed);
						}
						imagens.fadeOut(settings.speed);
						
						thumbs_a.filter(":data(id,"+idx+")").addClass("on");
						img.fadeIn(settings.speed, function(){ 
							if(settings.showTitulo == "inside"){
								$(this).find("div.legenda").show().animate({ right: 0 }, settings.speed);
							} else {
								descricao.html(legenda).fadeIn(settings.speed);
							}
						});
						
						if(index < (thumbs_a.size()-1)){
							setadir.fadeIn(settings.speed);
						}
						
						if(index > 0){
							setaesq.fadeIn(settings.speed);
						}
					} else {
						$this.find("div.imagens").append('<div class="loading"><img src="imagens/fancybox_loading.gif" alt="" /></div>');
						
						var ajaxUrl = "ajax/instalacao.php";
						var ajaxData = "id="+idx;
						
						if(settings.galeria == "evento"){
							ajaxUrl = "ajax/evento_imagem.php";
							if(idx == "cartaz"){
								ajaxData = "evento="+$this.data("evento")+"&id=cartaz";
							} else {
								ajaxData = "evento="+$this.data("evento")+"&id="+idx;
							}
						}
						
						$.ajax({
							type: "POST",
							url: ajaxUrl,
							data: ajaxData,
							cache: false,
							success: function(msg){
								$this.find("div.imagens").append(msg);
								
								var legenda = $("img", msg).attr("alt");
								var imagens = $this.find("div.imagens").children("div.img");
								var img = imagens.filter(":data(id,"+idx+")");
								
								img.find("img").load(function(){
									$this.find("div.loading").remove();
									
									thumbs_a.removeClass("on");
									if(settings.showTitulo == "inside"){
										imagens.find("div.legenda").fadeOut(settings.speed).css({ right: -278 });
									} else {
										descricao.fadeOut(settings.speed);
									}
									imagens.fadeOut(settings.speed);
									
									thumbs_a.filter(":data(id,"+idx+")").addClass("on");
									img.fadeIn(settings.speed);
									
									if(settings.showTitulo == "inside"){
										img.find("div.legenda").show().animate({ right: 0 }, settings.speed);
									} else {
										descricao.html(legenda).fadeIn(settings.speed);
									}
									
									$(this).parent().css({ left: "50%", top: "50%", marginLeft: "-"+($(this).width()/2)+"px", marginTop: "-"+($(this).height()/2)+"px" });
									
									if(index < (thumbs_a.size()-1)){
										setadir.fadeIn(settings.speed);
									}
									
									if(index > 0){
										setaesq.fadeIn(settings.speed);
									}
								});
							}
						});
					}
				}
			});
			
			thumbs_a.eq(0).click();
			
			if(thumbs_a.size() > 1){
				setadir.fadeIn(settings.speed);
			}
			
			setaesq.find("a").click(function(){
				var idx = thumbs_a.index(thumbs_a.filter(".on"))-1;
				thumbs_a.eq(idx).click();
			});
			
			setadir.find("a").click(function(){
				var idx = thumbs_a.index(thumbs_a.filter(".on"))+1;
				thumbs_a.eq(idx).click();
			});
		});		
	};
	
	$.fn.veloxGaleria.defaults = {
		speed : 1000,
		showTitulo : "inside",
		galeria : "instalacao"
	};
	
	$.fn.veloxClube = function(options){
		var settings = $.extend({}, $.fn.veloxClube.defaults, options);
		
		return this.each(function(){
			var clube = $(this);
			var obj = $("#corpo div.clube div.vantagem");
			
			clube.mouseenter(function(){
				obj.find("h3").html(clube.attr("title"));
				if(clube.parent("a").size() > 0){
					obj.find("p").html(clube.parent().parent().find("p").html());
				} else {
					obj.find("p").html(clube.parent().find("p").html());
				}
				
				var top = (clube.offset().top + (clube.height() / 2)) - (obj.height() / 2);
				if(clube.data("pos") == 1 || clube.data("pos") == 2){
					obj.find("div.setavant").removeClass("setavantdir");
					obj.find("div.texto").removeClass("textodir");
					var left = (clube.offset().left + clube.width()) + 30;
				} else {
					obj.find("div.setavant").addClass("setavantdir");
					obj.find("div.texto").addClass("textodir");
					var left = (clube.offset().left - obj.width()) - 30;
				}
				
				obj.css({
					top: top,
					left: left
				}).show();
				//.fadeIn(settings.speed);
			});
			
			clube.mouseout(function(){
				obj.find("h3").html("");
				obj.find("p").html("");
				obj.css({
					top: 0,
					left: 0
				}).hide();
			});
		});		
	};
	
	$.fn.veloxClube.defaults = {
		speed : 500
	};
	
	$.fn.veloxServicos = function(options){
		var settings = $.extend({}, $.fn.veloxServicos.defaults, options);
		
		return this.each(function(){
			var servico = $(this);
			var img = servico.find("img");
			var obj = $("#corpo div.servicosvelox div.serv");
			var seta = obj.find("div.seta");
			var texto = obj.find("div.texto");
			var span = texto.find("span");
			var idx = $("#corpo div.servicosvelox .lista td a").index(servico);
			var table = $("#corpo div.servicosvelox table");
			var coluna = table.find("td").width();
			
			servico.mouseenter(function(){
				if(obj.css("display") == "none"){
					var left = img.offset().left;
					
					span.html(servico.attr("title"));
					texto.find("p").html(servico.parent().find("p").html());
					seta.css({ left: (img.width() / 2) - 14 });
					
					if(idx < 5){
						texto.css({ top: "auto", bottom: 0 });
						seta.removeClass("setatop");
						
						var top = img.offset().top - 25;
						obj.css({
							top: top,
							left: left
						}).show();
					} else {
						texto.css({ bottom: "auto", top: 0 });
						seta.addClass("setatop");
						
						var top = img.offset().top + (img.height() + 25);
						obj.css({
							top: top,
							left: left
						}).show();
					}
					
					if(span.width() > settings.tamHover){
						var dif = ((span.width()-settings.tamHover) / 2);
						var lft = parseInt(seta.css("left").replace("px", ""));
						
						if(idx > 0 && idx < 4 || (idx > 5 && idx < 9)){
							obj.css({ left: obj.offset().left - dif });
							seta.css({ left: lft + dif })
						} else if(idx == 4 || idx == 9){
							obj.css({ left: ((obj.offset().left - (span.width() + 50)) + img.width()) + 1 });
							seta.css({ left: (((span.width() + 25) - (img.width() / 2)) + 10) })
						}
					}
				}
			});
			
			servico.mouseleave(function(){
				span.html("").css({ textAlign: "center" });
				texto.find("p").html("").hide();
				texto.css({ width: "auto" });
				obj.hide();
			});
			
			servico.click(function(){				
				if(texto.width() < settings.tamBox){
					var dif = (((span.width()-settings.tamHover) / 2) + 2);
					if(idx == 2 || idx == 7){
						obj.css({ left: obj.offset().left - (((settings.tamBox / 2) - dif) - (img.width() / 2)) });
						seta.css({ left: (((settings.tamBox-4) / 2) - 14) });
					} else if(idx > 2 && idx < 5 || (idx > 7 && idx < 10)){
						var mlt = (idx == 3 || idx == 8 ? 2 : 3);
						obj.css({ left: table.offset().left + 197 });
						seta.css({ left: ((coluna*(mlt)) + (img.width() / 2) - 18) });
					}
					
					texto.css({ width: settings.tamBox });
					span.css({ textAlign: "left" }); 
					texto.find("p").show();
					texto.show();
				}
			});
		});		
	};
	
	$.fn.veloxServicos.defaults = {
		tamBox : 630,
		tamHover : 74
	};
	
	$.fn.veloxInput = function(){		
		return this.each(function(){
			var $this = $(this);
			
			if($this.is("[title]")){
				var title = $this.attr("title");
				
				$this.focus(function(){
					if($this.val() == title){
						$this.val("");
					}
				});
				
				$this.blur(function(){
					if($this.val() == ""){
						$this.val(title);
					}
				});
			}
		});		
	};
	
	$.fn.veloxTrack = function(){		
		return this.each(function(){
			var $this = $(this);
			
			var tamanho = 0;
			var track = $this.find("div.lista div.track");
			var thumbs = $this.find("div.lista");
			var thumbs_div = thumbs.find("div.evtn");
			
			thumbs_div.each(function(){
				tamanho = tamanho + ($(this).width()+16);
			});
			
			track.width(tamanho);
			
			thumbs.mousemove(function(e){
				var left = (e.pageX - thumbs.offset().left) * (((e.pageX - thumbs.offset().left)/thumbs.width()) * (tamanho/thumbs.width()));
				thumbs.scrollLeft(left);
			});
		});		
	};
})(jQuery);
