$(document).ready(function(){

	css_load('slide/slide.css');

	slide.ini('#dv_slide');
	
	$.getJSON('slide/slide.php',function(lista){
		$.each(lista,function(indice,valor){
			
			var titulo = valor.titulo;
			var texto = valor.texto;
			var img = valor.img;
			var hiperlink = valor.hiperlink;
			var hiperlink = valor.hiperlink;

			slide.add(img,titulo,texto,hiperlink);

			});
		
		slide.definir_efeito('lado');
		slide.play();
		
		});
	});
	
//--->	
	
slide = {
		alvo:'',
		slide_foto:new Array(),
		slide_titulo:new Array(),
		slide_texto:new Array(),
		slide_link:new Array(),
		tipo_efeito:'',
		tempo_efeito:1000,
		atual:0,
		anima:1,
		tempo:5000,
		intervalo:0,
		
		//INI
				'ini':function(alvo){
					
					if(typeof(alvo)=='undefined'){
						this.alvo = '#dv_slide';
						} else {
						this.alvo = alvo;
						}
					
					var conteudo='<div class="seta_esq"></div><div class="seta_dir"></div><div class="palco"><ul></ul></div>';
					$(this.alvo).html(conteudo);
					
					$(this.alvo+' .seta_esq').bind("click", function(){
						slide.ant();
						});
					
					$(this.alvo+' .seta_dir').bind("click", function(){
						slide.prox();
						});
					
					},
		
		//ADD
				'add':function(foto,titulo,texto,hiperlink){
					this.slide_foto.push(foto);
					this.slide_titulo.push(titulo);
					this.slide_texto.push(texto);
					this.slide_link.push(hiperlink);
					
					if(hiperlink==''){
						
							var cod = '<li>\
							<span class="foto"><img src="'+foto+'"></span>\
							<span class="texto">\
								<h1>'+titulo+'</h1>\
								<p>'+texto+'</p>\
							</span>\
							</li>';
						
						} else {
							
							var cod = '<li onclick="location.href=\''+hiperlink+'\'" style="cursor:pointer">\
							<span class="foto"><img src="'+foto+'"></span>\
							<span class="texto">\
								<h1>'+titulo+'</h1>\
								<p>'+texto+'</p>\
							</span>\
							</li>';
						
						}
						
					$(cod).appendTo(this.alvo+' div.palco ul');
					
					},
		
		//REM
				'rem':function(i){
					this.slide_foto.remove(i);
					this.slide_titulo.remove(i);
					this.slide_texto.remove(i);
					this.slide_link.remove(i);
					},
		
		//PLAY
				 'play':function(){
					var local = this;
					if(this.anima==1){
						var duracao = this.tempo;
						local.intervalo = setInterval(function(){
										local.prox();
										},duracao);
						}
					},
		
		//STOP
				'stop':function(){
					alert('Parando a execução');
					},
		
		//PROX
				'prox':function(){
					
					if(this.intervalo){
						clearInterval(this.intervalo);	
						}
					
					var total_slides = this.slide_foto.length;
					if(this.atual<(total_slides-1)){
						this.atual++;
						slide.goto(this.atual);
						}
					},
		
		//ANT
				'ant':function(){
					
					if(this.intervalo){
						clearInterval(this.intervalo);	
						}
					
					if(this.atual>0){
						this.atual--;
						slide.goto(this.atual);
						this.anima = 0;
						}
					},
					
		//GOTO
				'goto':function(i){
					
					this.atual=i;
					
					if(this.tipo_efeito==''){
						var altura_item = $(this.alvo+' .palco ul li:eq(0)').height();
						var pos_mover = i*altura_item;
						pos_mover = pos_mover - (2*pos_mover);						
						$(this.alvo+' .palco ul').animate({"top": +pos_mover+"px"}, 500);
						}
					
					if(this.tipo_efeito=='lado'){
						var largura_item = $(this.alvo+' .palco ul li:eq(0) span.foto').width();
						var pos_mover = i*largura_item;
						pos_mover = pos_mover - (2*pos_mover);						
						$(this.alvo+' .palco ul').animate({"left": +pos_mover+"px"}, 500);
						}
					
					if(this.play==1){
							setTimeout(function(){
								//comandos
								this.prox();
								},this.tempo);
							}
					
					//$(this.alvo).html(this.slide_foto[i]);
					},
		
		//DEFINIR EFEITO
				'definir_efeito':function(tipo){
					this.tipo_efeito=tipo;
					
					if(this.tipo_efeito=='lado'){
						var total_slides = this.slide_foto.length;
						var largura_item = $(this.alvo+' .palco ul li:eq(0)').width();
						var largura_total = total_slides * largura_item;
						
						$(this.alvo+' .palco ul').css('width',largura_total+'px');
						$(this.alvo+' .palco ul li').css('display','block');
						$(this.alvo+' .palco ul li').css('float','left');
						
						}
					
					}
					
	}

