
	/**
	 *	Cidade THÁ -	Library
	 *	@author			Felipe Ricieri
	 */

	/*
	ready:
	inicia menus, inputs, configurações iniciais
	e demais....
	*/
	$(document).ready(function(){
		//tempo padrão das 
		//"animações" e "transições"
		var defaulttime = 300;
		//variável responsável pelo cálculo do
		//alinhamento da altura
		var valign;
		POSITIONS.calculate();
		//scrolls...
		//$('.divComScroll').jScrollPane({showArrows:true});

		//temperatura ambiente...
		//var temperatura = (Math.floor(Math.random()*8)) + 23;
		//$('#temperatura').text(temperatura);
		
		//menu: "TORRE THÁ"
		$('#cidadetha_menu_torre').click(function(){
			torreTHA(defaulttime);
		});
		
		//menu: "MUSEU THÁ"
		$('#cidadetha_menu_museu').click(function(){
			museuTHA(defaulttime);
		});
		
		//menu: "SHOPPING THÁ"
		$('#cidadetha_menu_shopping').click(function(){
			shoppingTHA(defaulttime);
		});
		
		//menu: "CLUBE THÁ"
		$('#cidadetha_menu_clube').click(function(){
			clubeTHA(defaulttime);
		});
		
		//menu: "NEWS THÁ"
		$('#cidadetha_menu_news').click(function(){
			newsTHA(defaulttime);
		});
		
		//menu: "NÚMEROS THÁ"
		$('#cidadetha_menu_numeros').click(function(){
			numerosTHA(defaulttime);
		});

		//menu: "NÚMEROS THÁ"
		$('#cidadetha_menu_legal').click(function(){
			legalTHA(defaulttime);
		});		
		
		
		//fechar modal
		//$('#cidadetha_modal').click(function(){
		//	fecharModal(defaulttime);
		//});
		
		$('.botao_fechar_modal').click(function(){
			history_reset();
			//fecharModal(defaulttime);
		});
		
		//pula para cadastro de cidadão
		$('.botao_cidadao').click(function(){
			clubePaginaCadastro();
		});
		
		var msg_busca 	= 'Buscar empreendimentos', 
			msg_login 	= 'Login',
			msg_senha 	= 'Senha',
			msg_shop	= 'Digite o nome do parceiro desejado';
		var $ipt_busca	= $('.mask_busca_topo'), 
			$ipt_login	= $('.mask_login'),
			$ipt_senha	= $('.mask_senha'),
			$ipt_shop	= $('.mask_busca_shopping');
		
		//buscas
		$ipt_busca.attr('value', msg_busca);		
		$ipt_busca
		.focus(function(){ if($ipt_busca.val() == msg_busca) $ipt_busca.attr('value', '');	})
		.blur(function(){ if($ipt_busca.val() == '') $ipt_busca.attr('value', msg_busca); });
		//login
		$ipt_login.attr('value', msg_login);		
		$ipt_login
		.focus(function(){ if($ipt_login.val() == msg_login) $ipt_login.attr('value', '');	})
		.blur(function(){ if($ipt_login.val() == '') $ipt_login.attr('value', msg_login); });
		//senha
		$ipt_senha.attr('value', msg_senha);		
		$ipt_senha
		.focus(function(){ if($ipt_senha.val() == msg_senha) $ipt_senha.attr('value', '');	})
		.blur(function(){ if($ipt_senha.val() == '') $ipt_senha.attr('value', msg_senha); });
		//busca shopping
		$ipt_shop.attr('value', msg_shop);		
		$ipt_shop
		.focus(function(){ if($ipt_shop.val() == msg_shop) $ipt_shop.attr('value', '');	})
		.blur(function(){ if($ipt_shop.val() == '') $ipt_shop.attr('value', msg_shop); });
		
		$().mousemove( function(e) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		});
		// carrega a home do shopping
		Shopping.loadHome();		
		
		$(document).click(function(e){
		    mouseX = e.pageX;
		    mouseY = e.pageY;
		});
		
		getQueryString();
		
	});
	
	var mouseX = 0;
	var mouseY = 0;
	
	/*
	ativaMenu:
	"ativa" (altera imagem para a condição "over")
	de um determinado menu
	*/
	function ativaMenu(id){
		/*
		var menu = $('.menuAtivo').attr('id');
		//retira do antigo...
		$('.menuAtivo').attr('src', _urlBase + 'images/'+menu+'.jpg');
		$('.menuAtivo').removeClass("menuAtivo");
		//... e adiciona ao novo
		//$('img#'+id).attr('src',_urlBase + 'images/'+id+'_over.jpg');
		$('img#'+id).addClass("menuAtivo");
		*/
	}
	
	/*
	desativaMenu:
	"desativa" (altera imagem para a condição normal)
	de um determinado menu
	*/
	function desativaMenu(id){
		$('img#'+id).removeClass("menuAtivo");
		$('img#'+id).attr('src', _urlBaseAssets + 'images/'+id+'.jpg');
	}
	
	/*
	desativaTodosMenu:
	"desativa" (altera imagem para a condição normal)
	de todos os menus
	*/
	function desativaTodosMenus(){
		$('.menuAtivo').removeClass("menuAtivo");
		$('img#menu_torre').attr('src',_urlBaseAssets + 'images/menu_torre.jpg');
		$('img#menu_museu').attr('src',_urlBaseAssets + 'images/menu_museu.jpg');
		$('img#menu_shopping').attr('src',_urlBaseAssets + 'images/menu_shopping.jpg');
		$('img#menu_clube').attr('src',_urlBaseAssets + 'images/menu_clube.jpg');
		$('img#menu_news').attr('src',_urlBaseAssets + 'images/menu_news.jpg');
	}
	
	/*
	moverEsquerda:
	move listas horizontais para a esquerda
	*/
	function moverEsquerda(elemento, valor){
		$elemento 	= $('.'+elemento);
		$elemento.animate({
			marginLeft:'-='+valor
		}, 250);
	}
	
	/*
	moverDireita:
	move listas horizontais para a direita
	*/
	function moverDireita(elemento, valor){
		$elemento 	= $('.'+elemento);
		$elemento.animate({
			marginLeft:'+='+valor
		}, 250);
	}
	
	/*
	moverBaixo:
	move listas horizontais para baixo
	*/
	function moverBaixo(elemento, valor){
		$elemento 	= $('.'+elemento);
		$elemento.animate({
			marginTop:'-='+valor
		}, 250);
	}
	
	/*
	moverCima:
	move listas horizontais para cima
	*/
	function moverCima(elemento, valor){
		$elemento 	= $('.'+elemento);
		$elemento.animate({
			marginTop:'+='+valor
		}, 250);
	}
	
	/*
	desceHeader:
	função responsável pelo tween do header,
	quando este aparece no "palco"
	*/
	function desceHeader(time){
		$('#cidadetha_modal_header').show();
		$('#cidadetha_modal_header')
		.animate({
			top:POSITIONS.head_y
		},time);
	}
	
	/*
	escondeHeaders:
	função responsável por esconder
	todos os headers
	*/
	function escondeWrappers(){
		$('#cidadetha_modal_torre').hide();
		$('#cidadetha_modal_museu').hide();
		$('#cidadetha_modal_shopping').hide();
		$('#cidadetha_modal_clube').hide();
		$('#cidadetha_modal_news').hide();
	}
	
	/*
	mostraHeader:
	função responsável por mostrar
	um header específico
	*/
	function mostraHeader( header ){
		fadeActive( 'header_ativo', '.'+header );
	}
	
	/*
	sobeWrappers:
	função responsável pelo tween dos wrappers,
	quando estes aparecem no "palco"
	*/
	function sobeWrappers(id, distancia, tempo){
		$elemento = $('#'+id);
		//$elemento.addClass('pagina_ativa');
		$elemento.addClass('modal_ativo');
		
		$elemento.show();
		$elemento
		.animate({
			top:POSITIONS.body_y+"px"
		}, tempo);
	}
	
	/*
	abrirModal:
	função responsável pelo tween
	de abertura do modal
	*/
	function abrirModal(time){
		fecharDetalhesEmpreendimento();
		//div wrappers...
		diminuirDivWrappers();
		//volta para a cidade...
		fecharModalMuseuTHA();
		/*
		if( $('.pagina_ativa').attr('id') == 'cidadetha_modal_museu' && $('#flash_cidadetha').css('display') == 'none' )
		{
			$('#flash_cidadetha').show();
			$('#flash_linha_do_tempo').hide();
		}
		*/
		if($('#cidadetha_modal').css('display') != 'none')
		{
			//hack para a torre thá
			if( $('.modal_ativo').attr('id') != 'cidadetha_modal_torre' ){
				//comando para todos os outros
				$('.modal_ativo').hide();
				$('.modal_ativo').css('top','800px');
				
			} else {
				//para a torre..
				$('.modal_ativo').fadeOut('fast');
			}
			
			$('.modal_ativo').removeClass('modal_ativo');
		}
		else
		{
			$('#cidadetha_modal_header').fadeIn(time);
			$('#cidadetha_modal')
			.css({opacity:0.8})
			.fadeIn(time);
		}
	}
	
	/*
	reiniciar:
	reinicia a área que estava ativa
	*/
	function reiniciar(id){
		switch(id){
			case 'cidadetha_modal_torre':
				reiniciaTorreTHA();
				break;
			case 'cidadetha_modal_museu':
				reiniciaMuseuTHA();
				break;
			case 'cidadetha_modal_shopping':
				reiniciaShoppingTHA();
				break;
			case 'cidadetha_modal_clube':
				reiniciaClubeTHA();
				break;
			case 'cidadetha_modal_news':
				reiniciaNewsTHA();
				break;
		}
	}
	
	/*
	abrirModal:
	função responsável pelo tween
	de fechamento do modal (e retorna à posição inicial)
	*/
	function fecharModal(time){
		var t = parseInt(time) / 2;
		$('#cidadetha_modal_header').fadeOut(t);
		$('#cidadetha_modal_torre').fadeOut(t);
		$('#cidadetha_modal_museu').fadeOut(t);
		$('#cidadetha_modal_shopping').fadeOut(t);
		$('#cidadetha_modal_clube').fadeOut(t);
		$('#cidadetha_modal_news')
		.fadeOut(t)
		.delay(t, function(){
			$('#cidadetha_modal').fadeOut(t);
			desativaTodosMenus();
			retornarPosicaoInicial();
		});
	}
	
	/*
	retornarPosicaoInicial:
	retorna todos os elementos do modal
	para as suas respectivas posições iniciais
	*/
	function retornarPosicaoInicial(){
		$('#cidadetha_modal_header').css('top','-100px');
		//$('#cidadetha_modal_torre').css('top','800px');
		$('#cidadetha_modal_museu').css('top','800px');
		$('#cidadetha_modal_shopping').css('top','800px');
		$('#cidadetha_modal_clube').css('top','800px');
		$('#cidadetha_modal_news').css('top','800px');
	}
	
	/*
	configurarInput:
	configura focus e blur de inputs
	*/
	function configurarInput(input, mensagem){
		$input = $('.'+input);
		$input.attr('value', mensagem);
		
		$input
		.focus(function(){
			if($input.val() == mensagem) $input.attr('value', '');
		})
		.blur(function(){
			if($input.val() == '') $input.attr('value', mensagem);
		});
	}
	
	/*
	fade:
	esconde um elemento da página e depois mostra o outro
	com um tween de "fade"
	*/
	function fade(elementoOut, elementoIn){
		$(elementoOut)
		.fadeOut('fast',function(){
			$(elementoIn).fadeIn('fast');
		});
	}
	
	/*
	slide:
	esconde um elemento da página e depois mostra o outro
	com um tween de "slide"
	*/
	function slide(elementoOut, elementoIn){
		$(elementoOut)
		.slideUp('fast',function(){
			$(elementoIn).slideDown('fast');
		});
	}
	
	/*
	fadeActive:
	esconde um elemento da página e depois mostra o outro
	com um tween de "fade" (o elemento que desaparece é uma classe)
	*/
	function fadeActive( classElementoOut, elementoIn, callback ){
		$("."+classElementoOut)
		.fadeOut('fast',function(){
			$("."+classElementoOut).removeClass(classElementoOut);
			$(elementoIn).addClass(classElementoOut);
			$(elementoIn).fadeIn('fast');
			//callback...
			if( callback != null ) callback();
		});
	}
	
	/*
	aumenta div "wrappers"
	em 100% da tela
	*/
	function aumentarDivWrappers(){
		$('#wrappers').css('width','100%');
	}
	
	/*
	diminui div "wrappers"
	para o seu tamanho original (980px)
	*/
	function diminuirDivWrappers(){
		$('#wrappers').css('width','980px');
	}
	
	
	function contentLoading(element){
		$(element).html('<div align="center"><img src="' + IMG_CONTENT_LOADING + '"/></div>');
	}
	
	function abrirDetalhesEmpreendimento(slug){
		var defaulttime = 300;
		fecharModal(defaulttime);
		switch(slug){
			case 'rr_torre_tha':
				torreTHA(defaulttime);
				fecharDetalhesEmpreendimento();
			break;
			case 'rr_news_tha':
				newsTHA(defaulttime);
				fecharDetalhesEmpreendimento();
			break;
			case 'rr_museu_tha':
				museuTHA(defaulttime);
				fecharDetalhesEmpreendimento();
			break;
			case 'rr_clube_tha':
				clubeTHA(defaulttime);
				fecharDetalhesEmpreendimento();
			break;
			case 'rr_shopping_tha':
				shoppingTHA(defaulttime);
				fecharDetalhesEmpreendimento();
			break;

			default:
				contentLoading('#empreendimento_detalhes');
				$('#empreendimento_detalhes').show();
				var position = $('#cidadetha_menu').position();
				var newTop = mouseY;
				var newLeft = mouseX;
				var boxHeight = 340;
				var boxWidth = 545;
				var breath = 15;
				var window_width = $(window).width();
				var window_height = $(window).height();
				if(mouseX < (window_width / 2)){
					var centro_x = (window_width/4) - boxWidth/ 2;
				}else{
					var centro_x = (window_width/2) + (window_width/4) - boxWidth/ 2;
				}
				var centro_y = (window_height/2) - boxHeight/ 2;
		
				$('#empreendimento_detalhes').css('top', centro_y);
				$('#empreendimento_detalhes').css('left', centro_x);
				contentLoading('#empreendimento_detalhes');
				$.ajax({
				  url: _urlSite  + 'empreendimentos/abrir/' + slug,
				  success: function(data){
				  		$('#empreendimento_detalhes').html(data);
			  		}
				});
			break;	
		}
	}

	function fecharDetalhesEmpreendimento(){
		$('#empreendimento_detalhes').hide();
		sendToActionScript('fechado');
	}
	
	var jsReady = false;
     function isReady() {
         return jsReady;
     }
     function pageInit() {
         jsReady = true;
     }

     function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
     function sendToActionScript(value) {
     	if(!isMuseuOpened){
     		try{
         		thisMovie("flash_cidadetha").sendToActionScript(value);
         	}catch(e){}
         }
     }

	function abrirEmpreendimentoFlash(alias){
     	if(!isMuseuOpened){
     		try{
     			fecharModal(300);
     			thisMovie("flash_cidadetha").abrirEmpreendimentoFlash(alias);
         	}catch(e){}
         }
	}

	var IMG_CONTENT_LOADING = _urlBaseAssets + 'images/content-loading.gif';
	var USER_DATA = {logged : false};
	
	var isMuseuOpened = false;
	function showMuseu(){
		fecharModal(300);
		isMuseuOpened = true;
		$('#flash-cidade').hide();
		$('#flash-museu').show();
	}
		
	function showCidade(){
		isMuseuOpened = false;
		$('#flash-cidade').show();
		$('#flash-museu').hide();
	}
	
	
var _history_hash = new Array();
_history_hash.push('#home');

function hash_history_save(hash){
	_history_hash.push(hash);
}

function history_back(){
	var trash   = _history_hash.pop();
	var destiny = _history_hash.pop();
	if(_history_hash.length == 0){
		hash_history_save('#home');
	}
	hash_goto(destiny);
}

function history_reset() {
	_history_hash = new Array();
	_history_hash.push('#home');
	hash_goto('#home');
}
	
function getQueryString(){
	var where = window.location.hash;
	hash_goto(where);
}

function hash_goto(where){
	var defaulttime = 300;
	switch(where){
		case '#torre':
			var cidadetha_modal_torre = $('#cidadetha_modal_torre');
			if(cidadetha_modal_torre.length > 0){
				torreTHA(defaulttime);	
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
		break;
		case '#museu':
			museuTHA(defaulttime);
		break;
		case '#shopping':
			if(Shopping.isLoaded){
				shoppingTHA(defaulttime);	
			}else{
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
			
		break;
		case '#clube':
			var clube_inicio = $('#clube_inicio');
			if(clube_inicio.length > 0){
				clubeTHA(defaulttime);	
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
		break;
		case '#news':
			newsTHA(defaulttime);
		break;
		case '#tha-em-numeros':
			numerosTHA(defaulttime);
		break;
		case '#cadastro':
		case '#clube-tha-seja-cidadao':
			var clube_form_cadastro = $('#clube_form_cadastro');
			if(clube_form_cadastro.length > 0){
				clubePaginaCadastro();	
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
			
		break;
		case '#home':
			fecharModal(defaulttime);
		break;
		case '#clube-empreendimentos':
			var div = $('#clube_empreendimentos');
			if(div.length > 0){
				clubeEscondido()
				setTimeout(function(){
					var trash = _history_hash.pop();
					openClubePage('#clube_empreendimentos');
				}, 400);
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
		break;
		case '#clube-historias':
			var div = $('#clube_historias');
			if(div.length > 0){
				clubeEscondido()
				setTimeout(function(){
					var trash = _history_hash.pop();
					clubePaginaHistorias();
				}, 400);
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}
		break;
		case '#clube-cidadaos':
			var div = $('#clube_cidadaos');
			if(div.length > 0){
				clubeEscondido()
				setTimeout(function(){
					var trash = _history_hash.pop();
					clubePaginaCidadaos();
				}, 400);
			} else {
				setTimeout(function(){
					hash_goto(where)
				}, 500);
			}

		break;
		case '#clube-form-envio':
			clubePaginaEnviarFoto();
		break;

	}
}
