/*
* Ao iniciar a página é executado
*/
window.onload = function() {
	if (document.getElementById("tab")){
		tmp = window.location.href.split('#');
		if (tmp.length > 1) {
			goTab(tmp[1]);
		}
	}
	createExternalLinks();
};

function showMsn(){
	msn = '<div id="Microsoft_Live_Messenger_PresenceButton_bbb1b65209433223"'; 
	msn += 'msgr:width="115" msgr:backColor="#DBDBDB"';
	msn += 'msgr:altBackColor="#FFFFFF"';
	msn += 'msgr:foreColor="#424542"';
	msn += 'msgr:conversationUrl="http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=bbb1b65209433223@apps.messenger.live.com&mkt=pt-br&useTheme=true&foreColor=676769&backColor=DBDBDB&linkColor=444444&borderColor=8D8D8D&buttonForeColor=99CC33&buttonBackColor=676769&buttonBorderColor=99CC33&buttonDisabledColor=F1F1F1&headerForeColor=729527&headerBackColor=B2B2B2&menuForeColor=676769&menuBackColor=BBBBBB&chatForeColor=99CC33&chatBackColor=EAEAEA&chatDisabledColor=B2B2B2&chatErrorColor=760502&chatLabelColor=6E6C6C">';
	msn += '</div>';
	document.write(msn);
}

function closeFloater(div){
	$j("#"+div).addClass("invisible");
	$j("#mapa-site").removeClass("invisible");
	$j("#"+div).removeClass("invisible");
}

function mostra(arq,x,y,title){
	openPopup(arq,"Wallpaper",x,y,'resizable,status');   
}

//############################################################################
function closeFloater(div){
	document.getElementById(div).style["display"]="none";
}
function setPhoto (img,img_zoom,value,alt) {
	if(value!=''){
		document.getElementById("card").innerHTML='<input class="invisible" type="text" name="card" value="'+value+'" />';
		document.getElementById("zoom").innerHTML='<img src="'+img+'" alt="'+alt+'" />';
	} else {
    	document.getElementById("zoom").innerHTML='<a href="'+img_zoom+'" title="'+alt+'" rel="lightbox"><img src="'+img+'" alt="'+alt+'" /></a>'; 
		for (i=0;i<$j("#galery a").length;i++){
			//alert($j("#galery #foto-zoom-"+i).attr("href")+ " == "+img_zoom);
			if ($j("#galery #foto-zoom-"+i).attr("href")==img_zoom ){
		 		$j("#galery #foto-zoom-"+i).attr("rel", "");
			} else {
				$j("#galery #foto-zoom-"+i).attr("rel", "lightbox");
			}
		}
		$j('a[@rel*=lightbox]').lightBox();
	}
}

function viewForm(form) { 
	document.getElementById(form).style.display = "block";
} 

/*
Combo-Box Viewer 
exibe apenas o mês selecionado no combo
*/
function expandOne(){
	if (document.getElementById){
		var selectedItem=document.getElementById("form-mes").mes.value; //document."name_form"."name_select".value
		var inc=0;
		while (document.getElementById("mes-"+inc)){
			document.getElementById("mes-"+inc).style.display="none";
			inc++;
		}
		document.getElementById("mes-"+selectedItem).style.display="block";
	}
}

function setCiTyCombo(id, target){
	if (target == parseFloat(target)){ //is_numeric
		target = document.getElementsByClassName('city')[target].getElementsByTagName("span")[0];
		comboName = target.getElementsByTagName("select")[0].name;
	} else {
		target = document.getElementById(target);
		comboName = "city_id";
	}
	var urlAjax = rootUrl+"/bercariovirtual/fale-conosco?id="+id+"&comboName="+comboName;
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", urlAjax, true);
	target.innerHTML = '<br/>Carregando...';
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}


/*
* Acha o target dos combos dos estados, isso ocorre quando clona o elemento e ele mantem o mesmo target ( setCiTyCombo(this.value,TARGET) )
*/
function getCityTarget(stateCombo){
	stateCombos = document.getElementsByName('state_id[]');
	nElement = stateCombos.length;
	for (i=0;i<nElement;i++){
		//alert(stateCombos[i].value+" - "+stateCombo.value);
		if (stateCombos[i]==stateCombo){ //combo selecionado
			return i;
		}
	}
	return 0;
}

function remover(msg){
	if(msg==1) {msg = "Tem certeza que deseja remover essa foto?";}
	if(msg==2) {msg = "Tem certeza que deseja remover essa mensagem?";}
	if(msg==3) {msg = "Tem certeza que deseja remover esse nascimento e seus bebês?";}
	if(!confirm(msg)){
		return false;
	}		
}

/*
* JQUERY
*/
var $j = jQuery.noConflict();
$j(document).ready(function() {

	var rapHeight = $j("div#rap").height();
	var htmlHeight = $j("html").height();
	if (rapHeight<htmlHeight){
		var headerHeight = $j("div#header").height();
		var footerHeight = $j("div#footer").height();
		var wrapperHeight = $j("div#wrapper").height();
		var newHeight = htmlHeight - (headerHeight+footerHeight); 
		var newFooterHeight = htmlHeight - (headerHeight+wrapperHeight+footerHeight)+footerHeight;
		//var newFooterHomeHeight = htmlHeight - (headerHeight+wrapperHeight)+10;
		$j("body").height(htmlHeight);
		//$j("div#content").height(newHeight);
		$j("div#footer").height(newFooterHeight-10);
		//$j("div.home div#footer").height(newFooterHomeHeight);
	}
							
	if (document.getElementById("entrada")){
		$j("#mapa-site").addClass("invisible");
	}
	setMask();	
	$j('a[@rel*=lightbox]').lightBox();
	$j("input.rmclone").addClass("invisible");
	$j(".clone").click(function(){	
	if ($j("div.bercario-cadastro-bebe fieldset.photosBox").length<10){
		var target = $j(this).attr("value");
		unMask();
		$j("."+target+":first").clone(true).insertAfter("."+target+":last");
		$j("."+target+" input.rmclone").removeClass("invisible");
		setMask();
	}
	});
	
	$j(".rmclone").click(function(){
		var target = $j(this).attr("value");
		var nElements = $j("."+target).length;
		if (nElements==2){
			$j("."+target+" input.rmclone").addClass("invisible");
		}
		if (nElements>1){						  
			$j(this).parent().parent().remove();
		}
	});  
	

	// default options
	$j("#sound10").jmp3();
	$j("#sound11").jmp3();
	$j("#sound12").jmp3();
	$j("#sound13").jmp3();
	$j("#sound20").jmp3();
	$j("#sound21").jmp3();
	$j("#sound22").jmp3();
	$j("#sound23").jmp3();
	// custom options
	/*$j("#sound").jmp3({
		backcolor: "",
		forecolor: "FFF",
		width: 200,
		showdownload: "true"
	});*/

	$j('#tabs').tabs({ fxFade: true, fxSpeed: 'fast' });	
	
	$j("input#goog-wm-sb").attr("value","");
	
	$j("form#form-resposta-0").validate();
	$j("form#form-resposta-1").validate();
	$j("form#form-resposta-2").validate();
	$j("form#form-resposta-3").validate();
	$j("form#form-resposta-4").validate();
	$j("form#form-resposta-5").validate();
	$j("form#form-resposta-6").validate();
	$j("form#form-resposta-7").validate();
	$j("form#form-resposta-8").validate();
	$j("form#form-resposta-9").validate();
	$j("form#form-resposta-10").validate();

	$j("form#form-mensagem").validate();
	$j("form#form-cadastro-bebe").validate();
	$j("form#form-cartao").validate();
	$j("form#form-cadastro-bebe").validate();
	$j("form#form-search").validate();
	$j("form#form-cadastro-cliente").validate();
	$j("form#form-cadastro-medico").validate();
	$j("form#form-newsletter").validate();
	$j("form#form-contactus").validate();
	$j("form#form-remember-pass").validate();
	$j("form#form-indication").validate();
	$j("form#form-login").validate();
	$j("form#form-enquete").validate();
	
	$j("#mapa-site select").change(function () {
		var url = $j(this).attr("value");
		if (url!=""){
			window.location.href = url;
		}
	});
	
	$j("form#form-cadastro-bebe label.birthday input.txt-date").blur(function () {
		$j.ajax({
			type: "GET",
			url: "./cadastro-bebe",
			data: "date="+$j(this).attr("value"),
			success: function(msg){
				if (msg=="false"){
					$j("form#form-cadastro-bebe label.birthday input.txt-date").attr("value","");
					$j("form#form-cadastro-bebe label.birthday label.error").remove();
					$j("form#form-cadastro-bebe label.birthday input.txt-date").parent().append('<label class="error">Somente bebê com menos de um ano de idade.</label>');
				} else {
					$j("form#form-cadastro-bebe label.birthday label.error").remove();
				}
			}
		});
	});
	
	/* Seta background dos inputs da busca da home
	*/
	$j("form#form-enquete label.name input").click( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 -22px'}); 
		} else {
			$j(this).css({'background-position' : '0 22px'}); 
		}
	});
	$j("form#form-enquete label.name input").blur( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 0'}); 
		} else {
			$j(this).css({'background-position' : '0 -22px'}); 
		}
	});
	$j("form#form-enquete label.name input").focus( function(){ 
		$j(this).css({'background-position' : '0 -22px'}); 
	});
	
	$j("form#form-enquete label.email input").click( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 -22px'}); 
		} else {
			$j(this).css({'background-position' : '0 22px'}); 
		}
	});
	$j("form#form-enquete label.email input").blur( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 0'}); 
		} else {
			$j(this).css({'background-position' : '0 -22px'}); 
		}
	});
	$j("form#form-enquete label.email input").focus( function(){ 
		$j(this).css({'background-position' : '0 -22px'}); 
	});
	
		$j("form#form-enquete label.message textarea").click( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 -72px'}); 
		} else {
			$j(this).css({'background-position' : '0 69px'}); 
		}
	});
	$j("form#form-enquete label.message textarea").blur( function(){ 
		if ($j(this).val()==''){
			$j(this).css({'background-position' : '0 0'}); 
		} else {
			$j(this).css({'background-position' : '0 -72px'}); 
		}
	});
	$j("form#form-enquete label.message textarea").focus( function(){ 
		$j(this).css({'background-position' : '0 -72px'}); 
	});
	
	/*
	* Mensagens de erro ou sucesso
	*/
	function displayMessage($alert) {
		var alerttimer = window.setTimeout(function () {
			$alert.trigger('click');
		}, 8000);
		$alert.animate({height:'83px'}, 800)
		.click(function () {
			window.clearTimeout(alerttimer);
			$alert.animate({height: '0'}, 800);
		});
	}
	
	if ($j('div#exception').length){
		displayMessage($j('div#exception'));
	}
	
	if ($j('div#sucess').length){
		displayMessage($j('div#sucess'));
	}
	
});

function unMask(){
	$j(".txt-date").unmask();	
	$j(".txt-phone").unmask();
	$j(".txt-cnpj").unmask();
	$j(".txt-cpf").unmask();
	$j(".txt-cep").unmask();
	$j(".txt-time").unmask();
	$j(".txt-number-card").unmask();
	$j(".txt-number-card-medico").unmask();
}

function setMask(){
	$j(".txt-date").mask("NN/NN/NNNN");
	$j(".txt-phone").mask("(NN) NNNN-NNNN");
	$j(".txt-cnpj").mask("NN.NNN.NNN/NNNN-NN");
	$j(".txt-cpf").mask("NNN.NNN.NNN-NN");
	$j(".txt-cep").mask("NNNNN-NNN");	
	$j(".txt-time").mask("NN:NN");
	$j(".txt-number-card").mask("0 005 NNNNNNNNNNNN-N");
	$j(".txt-number-card-medico").mask("0 975 NNNNNNNNNNNN-N");
}

function bannersInfo(name,url){
	$j.ajax({
		type: "POST",
		url: './banners-info',
		data: "name="+name,
		success: function(res){
			//alert(res);
			window.location=url;
		}
	});
}