$(function(){
	var queryid=new String(window.location);
	queryid=queryid.slice(queryid.indexOf("id=")+3);
	if(isNaN(queryid)){
		queryid=""
	}
	/*Alimentação, Loja, Serviço*/
	var alser;
	if($(".alimentacao").length){
		alser="alimentacao";
		$.getJSON("json/loja-json.asp?page=alimentacao&buscaAlfabeto=everything",function(data){montaResultado(data);});
	}
	else if($(".servico").length){
		alser="servico";
		$.getJSON("json/loja-json.asp?page=servico&buscaAlfabeto=everything",function(data){montaResultado(data);});
	}
	else if($(".loja").length){
		alser="loja";
		$.getJSON("json/loja-json.asp?page=loja&buscaAlfabeto=a",function(data){montaResultado(data);});
		$.getJSON("json/loja-json.asp?page="+alser+"&montaRamo=true",function(data){
			$(".busca-ramo").append("<option value=''></option>");
			$.each(data,function(){
				$(".busca-ramo").append("<option value='"+this.ramo+"'>"+this.ramoNome+"</option>");
			});
		});
	}
	$(".busca-alfabeto li").click(function(){
		$(".busca-ramo").attr("value","");
		$(".busca-nome").attr("value","");
		$.getJSON("json/loja-json.asp?page="+alser+"&buscaAlfabeto="+$(this).html(),function(data){montaResultado(data);});
	});
	$(".busca-nome").keyup(function(){
		$(".busca-ramo").attr("value","");
		$.getJSON("json/loja-json.asp?page="+alser+"&buscaNome="+$(this).val(),function(data){montaResultado(data);});
	});
	$(".busca-ramo").change(function(){
		$(".busca-nome").attr("value","");
		$.getJSON("json/loja-json.asp?page="+alser+"&buscaRamo="+$(this).val(),function(data){montaResultado(data);});
	});
	$(".busca-resultado li").live("click",function(){	
		$.getJSON("json/loja-json.asp?page="+alser+"&buscaLoja="+$(this).attr("value"),
			function(data){
				$(".loja-imagem").attr("src","img/loja/"+data[0].lojaImagem);
				$(".loja-nome").html(data[0].lojaNome);

				$(".loja-telefone").html("");
				$(".loja-email").html("");
				$(".loja-site").html("");
				$(".loja-ramo").html("");
				$(".loja-texto").html("");
				
				if( Trim( data[0].lojaEndereco )!="" && data[0].lojaEndereco!=null && data[0].lojaEndereco != void(null) ){
					$(".loja-endereco").html("<strong>Loja: </strong>"+data[0].lojaEndereco);
				}else{
					$(".loja-endereco").html("");
				}
				
				if(data[0].lojaTelefone!=""){
					$(".loja-telefone").html("<strong>Telefone: </strong>"+data[0].lojaTelefone);
				}
				if(data[0].lojaEmail!=""){
					$(".loja-email").html("<strong>E-mail: </strong><a href='mailto:"+data[0].lojaEmail+"'>"+data[0].lojaEmail+"</a>");
				}
				if(data[0].lojaSite!=""){
					$(".loja-site").html("<strong>Site: </strong><a href='"+data[0].lojaSite+"' target='_new'>"+data[0].lojaSite+"</a>");
				}
				if(data[0].lojaRamo!=""){
					$(".loja-ramo").html("<strong>Ramo: </strong>"+data[0].lojaRamo);
			}
			$(".loja-texto").html(data[0].lojaTexto);
		});
	});
	function montaResultado(data){
		$(".busca-resultado").html("");
		if(data.length>0){
			$(".busca-n-resultado").html(data.length+" Lojas Encontradas:");
			$.each(data,function(){
				$(".busca-resultado").append("<li value='"+this.loja+"'>- "+this.lojaNome+"</li>");
			});
		}
		else{
			$(".busca-n-resultado").html("Nenhuma Loja Encontrada");
		}
	}
	/*Cinema*/
	if($(".cinema").length){
		$.getJSON("json/cinema-json.asp",function(data){
			$.each(data,function(){
				$(".cinema-cartaz").append("<li value='"+this.cinema+"'><img src='http://www.madnezz.com.br/cinema-g/upload/"+this.cinemaCartaz+"' alt='' /></li>");
			});
		});
		$.getJSON("json/cinema-json.asp?buscaCinema="+queryid,function(data){
            montaCinema(data);
		});
	}
    $(".cinema-cartaz li").live("click",function(){
        $.getJSON("json/cinema-json.asp?buscaCinema="+$(this).val(),function(data){
			montaCinema(data);
		});
    });
    function montaCinema(data){
        $(".cinema-titulo").html(data[0].cinemaTitulo);
		$(".cinema-trailer").html("<object type='application/x-shockwave-flash' data='swf/cinema.swf' width='440' height='290'><param name='movie' value='swf/cinema.swf' /><param name='FlashVars' value='trailer=http://www.madnezz.com.br/cinema-g/upload/"+data[0].cinemaTrailer+"' /></object>");
		$(".cinema-sinopse").html(data[0].cinemaSinopse);
		$(".cinema-ator").html(data[0].cinemaAtor);
		$(".cinema-genero").html(data[0].cinemaGenero);
        if(data[0].cinemaCensura==0){
		    $(".cinema-censura").html("Livre");
        }
        else{
            $(".cinema-censura").html(data[0].cinemaCensura+" anos");
        }
		$(".cinema-duracao").html(data[0].cinemaDuracao+" min");
        $(".cinema-horario").html(data[0].cinemaHorario);
    }
	/*Index*/
	if($(".index").length){
		var bannerLargura=1173;
		var bannerTotal=$(".banner>li>a>img").length;
		$(".banner>li>a>img").load(function(){
			bannerLargura+=2+$(this).width();
			if(!--bannerTotal){
				bannerLargura/=3;
				animaBanner();
			}
		});
		$(".banner").hover(
			function(){
				$(".banner").stop();
			},
			function(){
				animaBanner();
			}
		);
		function animaBanner(){
			$(".banner").animate({"margin-left":-bannerLargura},(bannerLargura+parseInt($(".banner").css("margin-left").replace('px','')))*40,"linear",function(){
				$(".banner").css({"margin-left":0});
				animaBanner();
			});
		};
		var cinemaTotal=$(".cinema-center img").length/3-1;
		var cinemaAtual=0;
		$(".cinema-top").click(function(){
			if(cinemaAtual==cinemaTotal){
				cinemaAtual=0;
				$(".cinema-center").css("top",cinemaAtual*-300+50);
			}
			$(".cinema-center").stop().animate({"top":++cinemaAtual*-300+50});
		});
		$(".cinema-bottom").click(function(){
			if(cinemaAtual==0){
				cinemaAtual=cinemaTotal;
				$(".cinema-center").css("top",cinemaAtual*-300+50);
			}
			$(".cinema-center").stop().animate({"top":--cinemaAtual*-300+50});
		});
	}
	/*Evento, Novidade, Imprensa*/
	data=new Date();
	mes=data.getMonth()+1;
	ano=data.getFullYear();
	if($(".evento").length){
		$(".evento-mes").val(mes);
		$(".evento-ano").val(ano);
		montaEvento(queryid,mes,ano);
	}
	$(".evento-mes,.evento-ano").change(function(){
		montaEvento("",$(".evento-mes").val(),$(".evento-ano").val());
	});
	function montaEvento(id,month,year){
		$(".evento-resultado").html("");
		$.getJSON("json/evento-json.asp?id="+id+"&mes="+month+"&ano="+year,function(data){
			if(data.length==0){
				$(".evento-resultado").html("<div><h4>Nenhum evento nessa data.</h4></div>");
			}
			$.each(data,function(){
				$(".evento-resultado").append("<div><h4>"+this.eventoNome+"</h4><p>De "+this.eventoEntrada+" até "+this.eventoSaida+"</p><br />"+this.eventoTexto+"</div>");
			});
		});
	}
	if($(".novidade").length){
		$(".novidade-mes").val(mes);
		$(".novidade-ano").val(ano);
		montaNovidade(queryid,mes,ano);
	}
	$(".novidade-mes,.novidade-ano").change(function(){
		montaNovidade("",$(".novidade-mes").val(),$(".novidade-ano").val());
	});
	function montaNovidade(id,month,year){
		$(".novidade-resultado").html("");
		$.getJSON("json/novidade-json.asp?id="+id+"&mes="+month+"&ano="+year,function(data){
			if(data.length==0){
				$(".novidade-resultado").html("<div><h4>Nenhum novidade nessa data.</h4></div>");
			}
			$.each(data,function(){
				$(".novidade-resultado").append("<div><h4>"+this.novidadeNome+"</h4><p>De "+this.novidadeEntrada+" até "+this.novidadeSaida+"</p><br />"+this.novidadeTexto+"</div>");
			});
		});
	}
	if($(".imprensa").length){
		$(".imprensa-mes").val(mes);
		$(".imprensa-ano").val(ano);
		montaImprensa(mes,ano);
	}
	$(".imprensa-mes,.imprensa-ano").change(function(){
		montaImprensa($(".imprensa-mes").val(),$(".imprensa-ano").val());
	});
	function montaImprensa(month,year){
		$(".imprensa-resultado").html("");
		$.getJSON("json/imprensa-json.asp?mes="+month+"&ano="+year,function(data){
			$.each(data,function(){
				$(".imprensa-resultado").append("<h4>- De "+this.imprensaEntrada+" até "+this.imprensaSaida+"</h4><br /><a href='pdf/imprensa/"+this.imprensaPDF+"' target='_new'>"+this.imprensaNome+"</a>");
			});
		});
	}
	/*Localização*/
	var directionDisplay;
	var directionsService;
	var map;
	var coordenadas;
	if($(".localizacao").length){
		directionsDisplay=new google.maps.DirectionsRenderer();
		directionsService=new google.maps.DirectionsService();
		coordenadas=new google.maps.LatLng(-22.186886,-49.96765);
		var myOptions={
			zoom:16,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center:coordenadas,
			streetViewControl:true,
		}
		map=new google.maps.Map(document.getElementById("localizacao-mapa"),myOptions);
		directionsDisplay.setMap(map);
		directionsDisplay.setPanel(document.getElementById("localizacao-rota"));
		var marker=new google.maps.Marker({position:coordenadas,map:map,title:"Marília Shopping"});
	}
	$(".localizacao-endereco").keyup(function(){
		calcRoute();
	});
	function calcRoute() {
		directionsService.route({origin:$(".localizacao-endereco").val(),destination:coordenadas,travelMode:google.maps.DirectionsTravelMode.DRIVING},function(response,status){
			if (status==google.maps.DirectionsStatus.OK){
				directionsDisplay.setDirections(response);
			}
		});
	}
	/*Shopping*/
	if($(".shopping").length){
		function animaShopping(){
			$(".right div").animate({"margin-top":-($(".right div").height())/2},120000,"linear",function(){
				$(".right div").css({"margin-top":0});
				animaShopping();
			});
		};
		animaShopping();
	}
});
function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
