/* url app */
var hostname = window.location.hostname;
if(hostname == '127.0.0.1')
	var urlApp = "http://"+hostname+"/building_record/";
else
	var urlApp = "http://"+hostname;
/* -------- */
function mainmenu(){
	$(" #nav ul "). css({display: "none"});
	$(" #nav li").hover(function(){
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(200);
	},function(){
	$(this).find('ul:first').css({visibility: "hidden"});
	});
}
$(document).ready( function() {	
	mainmenu();
	
	//-----------------------------------------------//
	// SCRIPT para TV da HOME
	//-----------------------------------------------//
	var tvAtivo = 0;
	var tvSetTime = 0;
	function tvRandom(){
		if( tvAtivo < $('.canais li').length - 1 ) { tvAtivo ++; }
		else { tvAtivo = 0; }
		tvTrans();
	};
	function tvTrans(){		
		$('.lista li','.tv').removeClass('ativo');
		$('.lista li:eq('+tvAtivo+')','.tv').addClass('ativo');
		$('.canais li','.tv').removeClass('ativo');
		$('.canais li:eq('+tvAtivo+')','.tv').addClass('ativo');
		tvSetTime = setTimeout(function() { tvRandom() }, 5000);
	};
	$('.tv').ready(function() {						
		tvTrans();
	});
	$('.canais li','.tv').click(function() {
		clearTimeout(tvSetTime);
		tvAtivo = $('.canais li').index(this);
		tvTrans();
	});	
	
	//-----------------------------------------------//
	// SCRIPT validação
	//-----------------------------------------------//
	$('form').submit( function() {
		$('.campo :input', this).each( function() { $(this).blur(); });
		
		var conta_erro = 0;
		$('.erro',this).each( function () { conta_erro ++; });
		if(conta_erro>0) { return false; }
		
		$('.c_captcha',this).hide();
		$('.campo span', this).removeClass('ok');
		$('p.alerta',this).text('Mensagem enviada com sucesso!');
	});
	
	$('.campo :input','form').blur( function() {
		var parent = $(this).parent();
		var label = $('label',parent).text();
		var value = jQuery.trim($(this).val());
		$(this).val(value);
		if (value == null) { value = '' }
		
		if (label.indexOf('*') > -1) {
			if((value == 0) || (value == '') || (value == 'off')) {
				$('span',parent).removeClass('ok');
				$('span',parent).addClass('erro');
			}
			else {
				$('span',parent).removeClass('erro');
				$('span',parent).addClass('ok');
			}
			
			if( $(this).attr('type') == 'checkbox' ){
				if( $(this).attr('checked') ){
					$('span',parent).removeClass('erro');
					$('span',parent).addClass('ok');
				}
				else{
					$('span',parent).removeClass('ok');
					$('span',parent).addClass('erro');
				}
			}
			
			if( $(this).attr('name').indexOf('email') > -1 ){
			  var regmail = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
			  if (! regmail.test(value) ) {
					$('span',parent).removeClass('ok');
					$('span',parent).addClass('erro');
			  }				
			}			
		}
		
	});
		
	$('.campo select','form').change( function() {
		var parent = $(this).parent();
		var label = $('label',parent).text();
		var value = $(this).attr('value');
		
		if (label.indexOf('*') > -1) {
			if((value == 0) || (value == '')) {
				$('span',parent).removeClass('ok');
				$('span',parent).addClass('erro');
			}
			else {
				$('span',parent).removeClass('erro');
				$('span',parent).addClass('ok');
			}
		}
		
	});
})


function ValidaCad(obj, acao){
	if(obj.senha.value != obj.repita_senha.value){
		alert('Por favor preencha a senha e confirmação de senha corretamente.');
		return false;
	}
	else
		return true;

}

//--------------------------------------------------------//

function openNovoTop(){
	document.getElementById("novos_topicos").style.display="block";
	document.getElementById("topicos").style.display="none";
}

function closeNovoTopico(){
	document.getElementById("novos_topicos").style.display="none";
	document.getElementById("topicos").style.display="block";
}	

// ------------------------------------Custom----------------------------------------

function homeCatalogo(tipo){
	$("#catalogo").html("Carregando...");
	$.get(urlApp+'?/home/bloco_catalogo/'+tipo, function(data){
		$("#catalogo").html(data);
	
	});
}

function ExibeGrupo(artid, tabela, tipo, iddetalhe){

	$("#detalhe_artista").html('');
	$("#detalhe_artista").html('Carregando...');
	$.get(urlApp+'?/artista/grupo/'+artid+'/'+tabela+'/'+tipo+'/'+iddetalhe, function(data){
		$("#detalhe_artista").html(data);
		$("#detalhe_artista").fadeIn("show");	
	});
}

function Preview_grupo(artid, tabela, tipo, iddetalhe){
	$("#video_preview").hide();
	$.get(urlApp+'?/artista/grupo/'+artid+'/'+tabela+'/'+tipo+'/'+iddetalhe, function(data){
		$("#"+tabela+"_preview").html(data);
	});
	$("#"+tabela+"_preview").fadeIn("show");
}



//Ajax PAGE
var bustcachevar=1; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
if(rootdomain.indexOf("127.0.0.1")!= -1)
	rootdomain+= "/building_record";

var bustcacheparameter="";
function ajaxpage(url, containerid){

	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
		
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
	catch (e){
		try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){}
		}
	}
	else
		return false
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid)
	}
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	
	page_request.open('GET', rootdomain+url, true)
	page_request.send(null)
}

function loadpage(page_request, containerid){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			document.getElementById(containerid).innerHTML=page_request.responseText;
			
		}
}



