// JavaScript Document
/*
	Foto Slider , Por Henrique Pauli.
	
	Para usar:
	Chame por este script após chamar pelo jquery.
	use:
	
	$("#exemplo").fotoShow();
	
	Coloque elementos do tipo escolhido dentro do elemento pai.
	
	Variáveis:
	showFirst (true / false) 				: Mostrar ou não a primeira imagem ao carregar;
	slideTime (milisegundos) 				: Tempo entre slides;
	fadeTime (milisegundos)  				: Tempo para dar transparencia.
	fadeStyle (1 / 2 / 3)    				: Estilo de fade*
	elementType (qualquer elemento html)	: O tipo de elemento usado.
	
	**
	1: A primeira imagem some, entao a outra aparece.
	2: As duas imagens fazem o efeito juntas.
	3: A nova imagem aparece por cima da outra, entao a outra some.
	**
*/

	
var dpmenuOverElement = $(document);
var dpmenuElementoAberto = $(document);
jQuery.fn.dropMenu = function(elemento,posicao,ativacao,efeito,tempoEfeito) {
	if(!elemento) return false;
	if(!posicao) posicao = 0;
	if(!ativacao) ativacao = 0;
	if(!efeito) efeito = 0;
	if(!tempoEfeito) tempoEfeito = 1000;
	
	elemento.css("position","absolute");
	elemento.css("z-index","-1");
	elemento.fadeOut(0);
	
	if(ativacao == 0 || ativacao == "hover"){
		$(this).hover(function(){
			if(elemento.attr("moved") != "true"){
				//elemento.attr("pai",$(this).parent().parent().attr('id'));
				if(posicao == 0 || posicao == "right"){
					elemento.css("left",parseInt($(this).offset().left)+parseInt($(this).width()));
					elemento.css("top",parseInt($(this).offset().top));
				}else if(posicao == 1 || posicao == "bottom"){
					elemento.css("left",parseInt($(this).offset().left));
					elemento.css("top",parseInt($(this).offset().top)+parseInt($(this).height()));
				}else if(posicao == 2 || posicao == "left"){
					elemento.css("left",parseInt($(this).offset().left)-parseInt($(this).width())-parseInt(elemento.width()));
					elemento.css("top",parseInt($(this).offset().top));
				}else if(posicao == 3 || posicao == "top"){
					elemento.css("left",parseInt($(this).offset().left));
					elemento.css("top",parseInt($(this).offset().top)-parseInt($(this).height())-parseInt(elemento.height()));
				}
				elemento.css("z-index","999999");
				
				if(efeito == 0 || efeito == "show"){
					elemento.show(tempoEfeito);
				}else if(efeito == 1 || efeito == "fade"){
					elemento.fadeIn(tempoEfeito);
				}
				elemento.attr("moved","true");
				elemento.hover(function(){
					elemento.attr("aberto","true");
				},function(){
					if(!$("[pai="+elemento.attr('id')+"]").length){
						elemento.attr("moved","false");
						elemento.attr("aberto","false");
						if(efeito == 0 || efeito == "show"){
							elemento.hide(tempoEfeito);
						}else if(efeito == 1 || efeito == "fade"){
							elemento.fadeOut(tempoEfeito);
						}
					}
				});
			}
		},function(){
			setTimeout(function(){
				if(elemento.attr("aberto") != "true"){
					//elemento.attr("pai","");
					elemento.attr("moved","false");
					elemento.attr("aberto","false");
					if(efeito == 0 || efeito == "show"){
						elemento.hide(tempoEfeito);
					}else if(efeito == 1 || efeito == "fade"){
						elemento.fadeOut(tempoEfeito);
					}
				}
			},50);
		});
	}else if(ativacao == 1 || ativacao == "click"){
		$(this).click(function(){
			if(elemento.attr("aberto") != "true")
			{
				if(elemento.attr("moved") != "true"){
					if($("[pai="+$(this).parent().parent().attr('id')+"]").length){
						$("[pai="+$(this).parent().parent().attr('id')+"]").each(function(){
							$(this).attr("moved","false");
							$(this).attr("aberto","false");
							if(efeito == 0 || efeito == "show"){
								$(this).hide(tempoEfeito);
							}else if(efeito == 1 || efeito == "fade"){
								$(this).fadeOut(tempoEfeito);
							}
						});
					}
					elemento.attr("pai",$(this).parent().parent().attr('id'));
					if(posicao == 0 || posicao == "right"){
						elemento.css("left",parseInt($(this).offset().left)+parseInt($(this).width()));
						elemento.css("top",parseInt($(this).offset().top));
					}else if(posicao == 1 || posicao == "bottom"){
						elemento.css("left",parseInt($(this).offset().left));
						elemento.css("top",parseInt($(this).offset().top)+parseInt($(this).height()));
					}else if(posicao == 2 || posicao == "left"){
						elemento.css("left",parseInt($(this).offset().left)-parseInt($(this).width())-parseInt(elemento.width()));
						elemento.css("top",parseInt($(this).offset().top));
					}else if(posicao == 3 || posicao == "top"){
						elemento.css("left",parseInt($(this).offset().left));
						elemento.css("top",parseInt($(this).offset().top)-parseInt($(this).height())-parseInt(elemento.height()));
					}
					elemento.css("z-index","999999");
					
					if(efeito == 0 || efeito == "show"){
						elemento.show(tempoEfeito);
					}else if(efeito == 1 || efeito == "fade"){
						elemento.fadeIn(tempoEfeito);
					}
					elemento.attr("moved","true");
					elemento.attr("aberto","true");
				}
			return false;
		}else{
			$("[pai="+elemento.attr('id')+"]").each(function(){
				$(this).attr("moved","false");
				$(this).attr("aberto","false");
				if(efeito == 0 || efeito == "show"){
					$(this).hide(tempoEfeito);
				}else if(efeito == 1 || efeito == "fade"){
					$(this).fadeOut(tempoEfeito);
				}
			});
			elemento.attr("pai","");
			elemento.attr("moved","false");
			elemento.attr("aberto","false");
			if(efeito == 0 || efeito == "show"){
				elemento.hide(tempoEfeito);
			}else if(efeito == 1 || efeito == "fade"){
				elemento.fadeOut(tempoEfeito);
			}
			return false;
		}
		});
	}
	//$(this).dropMenuKeepWatching(elemento,posicao,ativacao,efeito,tempoEfeito);
	return true;
};

jQuery.fn.dropMenuKeepWatching = function(elemento,posicao,ativacao,efeito,tempoEfeito) {
	var Eu = $(this);
	if(dpmenuOverElement.attr("id") == $(this).attr("id") && dpmenuElementoAberto.attr("id") != $(this).attr("id")){
		dpmenuElementoAberto = $(this);
		if(posicao == 0 || posicao == "right"){
			elemento.css("left",parseInt($(this).offset().left)+parseInt($(this).width()));
			elemento.css("top",parseInt($(this).offset().top));
		}else if(posicao == 1 || posicao == "bottom"){
			elemento.css("left",parseInt($(this).offset().left));
			elemento.css("top",parseInt($(this).offset().top)+parseInt($(this).height()));
		}else if(posicao == 2 || posicao == "left"){
			elemento.css("left",parseInt($(this).offset().left)-parseInt(elemento.width()));
			elemento.css("top",parseInt($(this).offset().top)+parseInt($(this).height()));
		}else if(posicao == 3 || posicao == "top"){
			elemento.css("left",parseInt($(this).offset().left));
			elemento.css("top",parseInt($(this).offset().top)-parseInt(elemento.height()));
		}
		elemento.css("z-index","999999");
		if(efeito == 0 || efeito == "show"){
			elemento.show(tempoEfeito);
		}
	}
	else if(dpmenuOverElement.attr("id") != $(this).attr("id") && dpmenuElementoAberto.attr("id") == $(this).attr("id")){
		if(efeito == 0 || efeito == "show"){
			if(dpmenuElementoAberto.attr("id") == Eu.attr("id"))dpmenuElementoAberto = $(document);
			elemento.hide(tempoEfeito,function(){
				elemento.css("z-index","-1");
			});
		}
	}
	setTimeout(function(){
		Eu.dropMenuKeepWatching(elemento,posicao,ativacao,efeito,tempoEfeito);
	},100);
};
