$(document).ready(function(){


/* -- Init do Height da Content Grid ------------------------------------------------------------------------------------------------- */
$('div.contentGrid').each(function(){
	$(this).find("li.itemConfig").height($(this).height());
});
/* ------------------------------------------------------------------------------------------------------------------------------------ */






/* -- Configuracao da Modal Window da GALERIA DE FOTOS E VIDEO ------------------------------------------------------------------------ */
$(".modalConfig").hide();
$(".contentGalleryGrid a").click(function (event) {
	event.preventDefault();
	// currentIndex = $(this).parent().prevAll().length;
	var currentIndex = $('.contentGalleryGrid li.itemConfig a').index(this);
	//alert ($(this).parent().prevAll().prevAll().length);
	
	if($(this).attr('rel')=="horizontal"){
	$(".modalConfig img").attr('src', $(this).attr('href'));
	
		$(".modalConfig").modal({
			containerCss:{
			height:550,
			width:574
		},
		overlayClose:true
		});
		$(".modalConfig .nav .count").css("width","420px");
		
		var thisTitle = $(this).find('span.title').html();
		var thisDesc = $(this).find('span.desc').html();
		setupModal(currentIndex,thisTitle,thisDesc);
	}
	if($(this).attr('rel')=="vertical"){
		$(".modalConfig img").attr('src', $(this).attr('href'));
		$(".modalConfig").modal({
			containerCss:{
			height:697,
			width:430
		},
		overlayClose:true
		});
		$(".modalConfig .nav .count").css("width","276px");
		
		var thisTitle = $(this).find('span.title').html();
		var thisDesc = $(this).find('span.desc').html();
		setupModal(currentIndex,thisTitle,thisDesc);
	}
	
	
	
	
	if($(this).attr('rel')=="video"){
		var thisHref = $(this).attr('href');
		var thisTitle = $(this).attr('title');
		var thisDesc = $(this).attr('rev');
		
		
		
		
		
		$(this).delay(50,function(){ //importante delay para o safari
			$(".modalConfig").modal({
				containerCss:{
				height:470,
				width:683,
          			onShow: function(){
					var flashvars = {videoURL:thisHref};
					var flashvars = {videoURL:thisHref,autoplay:true};
					var params = {menu:"false", allowfullscreen:"true", bgcolor:"#000000"};
					var attributes = {id:"videoPlayerId", name:"videoPlayerName"};
					swfobject.embedSWF("/flash/video_player.swf", "videoPlayerContainer", "683", "384", "9.0.0", false, flashvars, params, attributes);
					$(this).delay(1000,function(){
						$(".modalConfig .title").html(thisTitle);
						$(".modalConfig .desc").html(thisDesc);
					});
				}
			},
			overlayClose:true
			});
		});
	}
	
});
$(".modalConfig .close a").click(function (event) {
	event.preventDefault();
	$.modal.close();
});

function setupModal(modalID, modalTITLE, motalDESC ){
	//alert($('.contentGalleryGrid .itemConfig').size());
	var modalTOTAL = $('.contentGalleryGrid .itemConfig').size();
	$(".modalConfig .nav .count .total").html(modalTOTAL);
	$(".modalConfig .nav .count .active").html(modalID+1);
	$(".modalConfig .title").html(modalTITLE);
	$(".modalConfig .desc").html(motalDESC);
	$(".modalConfig .nav .prev").click(function (event) {
		var newModal = modalID-1;
		if(newModal<0){newModal=modalTOTAL-1;}
		brandNewModal(newModal);	
		event.preventDefault();

	});
	$(".modalConfig .nav .next").click(function (event) {				
		var newModal = modalID+1;
		if(newModal>modalTOTAL-1){newModal=0;}
		brandNewModal(newModal);
		event.preventDefault();

	});
}
function brandNewModal(nModal){
	$.modal.close();
	$(".contentGalleryGrid .itemConfig:eq("+nModal+") a").trigger('click');
}
/* ------------------------------------------------------------------------------------------------------------------------------------ */






/* -- Configuracao da Modal Window da Planta Interactiva ------------------------------------------------------------------------------- */
$(".modalConfig").hide();
$(".plantaInteractiva").click(function (event) {
	event.preventDefault();
	$(".modalConfig").modal({
		containerCss:{
		height:550,
		width:980
	},
	overlayClose:true
	});
});
$(".plantaInteractiva .close a").click(function (event) {
	event.preventDefault();
	$.modal.close();
});
/* ------------------------------------------------------------------------------------------------------------------------------------ */








/* -- Configuracao do Datepicker ----------------------------------------------------------------------------------------------------- */
$(".datepicker").datepicker({
	showAnim: 'slideDown',
	altFormat: 'yy-mm-dd',
	minDate: -90,
	maxDate: '90'
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */








/* -- Controla o Filtro das Noticias -------------------------------------------------------------------------------------------------- */
$(".contentFilterControl").show();
$(".contentFilter").hide();
$(".contentFilterControl a").click(function(event){
	event.preventDefault();
	$(".contentFilterControl a").toggle();
	$(".contentFilter").slideToggle(500);	
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */








/* -- Controla a Pesquisa Avancada -------------------------------------------------------------------------------------------------- */
//$(".contentFilterControl").show();
$(".pesquisageral").hide();
$(".avancada").hide();
$(".advancedContent").hide();

$(".advanced").click(function(event){
	event.preventDefault();
	$('input[type="text"]').attr("value","");
	$("#fldAdvancedEspecialidades option[value='1']").attr('selected', 'selected');

	$(".advanced").toggle();
	$(".searchlable").toggle();
	$(".advancedContent").slideToggle(500);
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */








/* -- Email Switching ---------------------------------------------------------------------------------------------------------------- */
$('a.email').each(function(){
	e = this.rel.replace('/','@');
	this.href = 'mailto:' + e;
	$(this).text(e);
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */








// -- Mask dos inputs de data e hora ------------------------------------------------------------------------------------------------- */
//$(".maskData").mask("99/99/9999");
$(".maskHora").mask("99:99");
/* ----------------------------------------------------------------------------------------------------------------------------------- */








// -- Comportamento global de focus e blur dos input ---------------------------------------------------------------------------------- */
$('input[type="text"]').focus(function() {
	(this.value == this.defaultValue) ? this.value = '' : this.select();
}).blur(function() {
	if(!$.trim(this.value))	this.value = this.defaultValue;  
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */







// -- Activa o input de upload de documentos ------------------------------------------------------------------------------------------ */
$(".uploadDoc").hide();
$('#fldMotivo').change(function(event){
	if($(this).val() == "Recrutamento"){
		$(".uploadDoc").show();
	}else{
		$(".uploadDoc").hide();
	}
});
/* ----------------------------------------------------------------------------------------------------------------------------------- */






// -- Manipulacao de dados no form [Marcacao de Consultas] ---------------------------------------------------------------------------- */
// Hora de Contacto
$('input[name="horarioPessoalInicio"], input[name="horarioPessoalFim"]').focus(function() {
	$("#horarioPessoal").attr("checked","checked");
});
$('input[id="horarioNormal"]').focus(function() {
	$('#horarioPessoalInicio, #horarioPessoalFim').val("");
});

// Data da Consulta
$('#fldDiaSemana').focus(function() {
	$("#diaSemana").attr("checked","checked");
});
$('input[id="dataASAP"]').focus(function() {
	$('#fldDiaSemana').val("");
});

// Hora da Consulta
$('input[name="horaPessoalInicio"], input[name="horaPessoalFim"]').focus(function() {
	$("#horaPessoal").attr("checked","checked");
});
$('input[id="horaQualquer"]').focus(function() {
	$('#horaPessoalInicio, #horaPessoalFim').val("");
});

// Fix do hover no ie6 do submit
$('.submitForm').hover(function(){
	$(this).css({'background-position' : 'left -25px'});
}, function(){
	$(this).css({'background-position' : 'left 0'});
});

/* ----------------------------------------------------------------------------------------------------------------------------------- */






// -- Manipulacao dos hightlights da homepage ---------------------------------------------------------------------------- */

// Activa a primeira thumb no estado activo
//$('.highlightThumbList ul li:first').removeClass('active').siblings().addClass('active');

var totWidth = 0;
var positions = new Array();

$('.highlightContent .highlight').each(function(i){
	positions[i] = totWidth;
	totWidth += $(this).width();
});
$('.highlightContent').width(totWidth);

// Autoscrolling
var current=1;
function autoAdvance(){
	if(current==-1) return false;
	$('.highlightThumbList ul li a').eq(current%$('.highlightThumbList ul li a').length).click();
	current++;
}
var changeEvery = 10;
var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

// Pause/Restart Autoscrolling
$('.highlightContent').hover(function() {
	clearTimeout(itvl);
}, function() {
	itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
});

// Muda o hightlight on click na thumb
$('.highlightThumbList a').click(function(event){
	event.preventDefault();
	//$('.highlightThumbList li').addClass('active');
	//$(this).parent().removeClass('active');
	
	var pos = $(this).parent().prevAll('li').length;
	current = pos;
	$('.highlightContent').stop().animate({marginLeft:-positions[pos]+'px'}, 850);
});

// Efeito do MouseOver nas thumbs
$(".highlightThumbList a").mouseover(function(){
	$('.highlightThumbList li').addClass('active');
	$(this).parent().removeClass('active');
}).mouseout(function(){
	$('.highlightThumbList li').removeClass('active');
});


/* ----------------------------------------------------------------------------------------------------------------------------------- */









// -- Manipulacao de zonas colapsible ---------------------------------------------------------------------------- */
$('.colapsible.colapsed').hide();
$('a.showColapsed').click(function(event){
 event.preventDefault();
 var colapsed = $(this).attr('href');
 $(colapsed).toggle(500);
 $('.colapsible:not('+colapsed+')').hide(500);

});


// -- Manipulacao de tabs ---------------------------------------------------------------------------- */
$('.tab.colapsed').hide();
$('a.showTab').click(function(event){
 event.preventDefault();
 var tabToShow = $(this).attr('href');
 $("a.showTab").removeClass("on");
 $(this).addClass("on");
 $(tabToShow).show(500);
 if ($(".googleMapsConfig")){setupMaps(); }
 $('.tab:not('+tabToShow+')').hide(500);
}); 

/* ----------------------------------------------------------------------------------------------------------------------------------- */ 



























/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* --  COOKIEs  -- */
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
function setCookie(name,value,days,path,domain,secure) {	
	var expires, date;
	if (typeof days == "number") {
		date = new Date();
		date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
	}
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
	var nameq = name + "=";
	var c_ar = document.cookie.split(';');
	for (var i=0; i<c_ar.length; i++) {
	var c = c_ar[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
	}
	return null;
}

function deleteCookie(name,path,domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */















/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* --  Resolve o problema das imagens em background onOver - NO IE  -- */
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
try{document.execCommand("BackgroundImageCache", false, true);}
catch(err){}
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------------------------- */















});














