// 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 fotoShowGalleryOptions = new Array();

jQuery.fn.fotoShowNextSlide = function(){
	var id = fotoShowFindElementInArray($(this));
	if(!fotoShowGalleryOptions[id][7]) return true;
	fotoShowGalleryOptions[id][7] = false;
	if(fotoShowGalleryOptions[id][8]) clearTimeout(fotoShowGalleryOptions[id][8]);
	fotoShowSlide(id,1);
	return false;
};
jQuery.fn.fotoShowPrevSlide = function(){
	var id = fotoShowFindElementInArray($(this));
	if(!fotoShowGalleryOptions[id][7]) return true;
	fotoShowGalleryOptions[id][7] = false;
	if(fotoShowGalleryOptions[id][8]) clearTimeout(fotoShowGalleryOptions[id][8]);
	fotoShowSlide(id,0);
	return false;
};
jQuery.fn.fotoShowGotoSlide = function(slidenum){
	var id = fotoShowFindElementInArray($(this));
	if(!slidenum) slidenum = 0;
	if(!fotoShowGalleryOptions[id][7]) return true;
	fotoShowGalleryOptions[id][7] = false;
	if(fotoShowGalleryOptions[id][8]) clearTimeout(fotoShowGalleryOptions[id][8]);
	var melemento = $(this).children(":eq("+slidenum+")");
	if(melemento.length != 0){
		fotoShowGalleryOptions[id][6] = melemento;
		fotoShowSlide(id,1);
	}
	return false;
};
jQuery.fn.fotoShowGetSlide = function(){
	var id = fotoShowFindElementInArray($(this));
	return fotoShowGalleryOptions[id][5].index();
};
jQuery.fn.fotoShow = function(showFirst,slideTime,fadeTime,fadeStyle,elementType) {
	if($(this).children().length < 1) return false;
	$(this).children(elementType).hide();
	if(!showFirst) showFirst = false;
	if(!slideTime) slideTime = 0;
	if(!fadeTime) fadeTime = 1000;
	if(!fadeStyle || fadeStyle > 3 || fadeStyle < 1) fadeStyle = 1;
	if(!elementType) elementType = "img";
	
	$(this).children(elementType).css("position","relative");
	$(this).children(elementType).css("z-index","1");
	
	
	
	var Eu = $(this);
	var melemento = $(this).children(elementType+":first");
	var Antes = melemento;
	
	if(showFirst){
		Antes.css("z-index","2"); 
		Antes.fadeIn(10); 
		melemento = melemento.next(elementType);
	}else Antes = false;
	
	var Options = new Array($(this),slideTime,fadeTime,fadeStyle,elementType,Antes,melemento,true,false);
	var id = fotoShowGalleryOptions.push(Options);
	id--;
	fotoShowSlide(id,1);	//fotoShowFn(Eu,slideTime,fadeTime,Antes,melemento,fadeStyle,elementType);
	return true;
};


function fotoShowFindElementInArray(elemento){
	for(var i=0; i< fotoShowGalleryOptions.length;i++){
		if(fotoShowGalleryOptions[i][0].index() == elemento.index()){
			return i;
		}
	}
	return -1;
}
function fotoShowSlide(id,dir){
	if(id != -1){
		var elemento = 		fotoShowGalleryOptions[id][0];
		var slideTime = 	fotoShowGalleryOptions[id][1];
		var fadeTime = 		fotoShowGalleryOptions[id][2];
		var fadeStyle = 	fotoShowGalleryOptions[id][3];
		var elementType = 	fotoShowGalleryOptions[id][4];
		var lastElemento = 	fotoShowGalleryOptions[id][5];
		var proxElemento = 	fotoShowGalleryOptions[id][6];
		
		if((fadeStyle == 1 && !lastElemento) || fadeStyle != 1){
			var melemento = proxElemento.next(elementType);
			if(dir == 0) melemento = proxElemento.prev(elementType);
			if(melemento.length == 0){
				melemento = elemento.children(elementType+":first");
				if(dir == 0) melemento = elemento.children(elementType+":last");
			}
			fotoShowGalleryOptions[id][5] = proxElemento;
			fotoShowGalleryOptions[id][6] = melemento;
		}
		
		if(fadeStyle == 1){
			if(!lastElemento){
				proxElemento.css("z-index","2");
				proxElemento.fadeIn(fadeTime,function(){
					fotoShowGalleryOptions[id][7] = true;
					if(slideTime != 0 ){
						fotoShowGalleryOptions[id][8] = setTimeout(function(){
							if(proxElemento){
								proxElemento.css("z-index","1");
								proxElemento.fadeOut(fadeTime,function(){
									fotoShowSlide(id,dir);
								});
							}else{
								fotoShowSlide(id,dir);
							}
						},slideTime);
					}
				});
			}else if(lastElemento){
				lastElemento.css("z-index","1");
				lastElemento.fadeOut(fadeTime,function(){
					fotoShowGalleryOptions[id][5] = false;
					fotoShowSlide(id,dir);
				});
				return true;
			}
		}else if(fadeStyle == 2){
			proxElemento.css("z-index","2");
			if(lastElemento){
				lastElemento.css("z-index","1");
				var pos = lastElemento.position();
				if(proxElemento.index() == 0){
					lastElemento.css("top","-"+(lastElemento.height()+pos.top)+"px");
				}else proxElemento.css("top","-"+(lastElemento.height()+pos.top)+"px");
				
				lastElemento.fadeOut(fadeTime);
			}		
			proxElemento.fadeIn(fadeTime,function(){
				proxElemento.css("top","0");
				lastElemento.hide();
				fotoShowGalleryOptions[id][7] = true;
				if(slideTime != 0){
					fotoShowGalleryOptions[id][8] = setTimeout(function(){
						fotoShowSlide(id,dir);
					},slideTime);
				}
			});
		}else if(fadeStyle == 3){
			proxElemento.css("z-index","2");
			if(lastElemento){
				lastElemento.css("z-index","1");
				var pos = lastElemento.position();
				if(proxElemento.index() == 0){
					lastElemento.css("top","-"+(lastElemento.height()+pos.top)+"px");
				}else proxElemento.css("top","-"+(lastElemento.height()+pos.top)+"px");
			}
			proxElemento.fadeIn(fadeTime,function(){
				proxElemento.css("top","0");
				if(lastElemento) lastElemento.hide();
					fotoShowGalleryOptions[id][7] = true;
				if(slideTime != 0){
					fotoShowGalleryOptions[id][8] =setTimeout(function(){
						fotoShowSlide(id,dir);
					},slideTime);
				}
			});
		}
	}
}
