
var hover_timeout_home;
var ajax_timeout;
var array_loaded = new Array();

$(document).ready(
	function() {

		//------------------
		// INPUT fields default values
		//------------------

		$('INPUT[@onempty]').focus(
			function() {
				var obj = $(this).get(0);
				if(obj.value == $(this).attr('onempty')) { obj.value = ''; $(this).removeClass('Dim');}
			}
		)

		$('INPUT[@onempty]').blur(
			function() {
				var obj = $(this).get(0);
				if(obj.value == '') { obj.value = $(this).attr('onempty'); $(this).addClass('Dim'); }
			}
		)

		// Home Tab
		$('.TabHome').hover(
			function() {
				this.style.cursor = 'pointer';
				var sTab = $(this).attr('reltab');
				var sSrc = this.style.backgroundImage;
				sSrc = sSrc.replace('url(','');
				sSrc = sSrc.replace('.gif)','_sel.gif');
				this.src = sSrc;
				$('.DestaquesHomeContainer').addClass('OffScreen');
				$('#' + sTab).removeClass('OffScreen');
			},
			function() {
				this.src = "/images/spacer.gif";
			}
		);

		$('.TabHomeKeepHover').hover(
			function() {
				clearTimeout(hover_timeout_home);
			},
			function() {
				hover_timeout_home = setTimeout(function() {
					$('.DestaquesHomeContainer').addClass('OffScreen');
				}, 1000);
			}
		);


		// Lawyer Tab Positioning
		try {
			var ARR_OFFSET = $(".AdvogadosDetail").offset();
			var ADVOGADOS_DETAIL_OFFSET_TOP = ARR_OFFSET.top
		}
		catch(e) {}

		if(ADVOGADOS_DETAIL_OFFSET_TOP) {
			$(window).scroll(function(){
					var iScrollTop = $(document).scrollTop();
					if(iScrollTop > ADVOGADOS_DETAIL_OFFSET_TOP) {
						var iMarginTop = iScrollTop - ADVOGADOS_DETAIL_OFFSET_TOP + 40;
						$(".AdvogadosDetail").css('margin-top', iMarginTop + 'px');
					} else { $(".AdvogadosDetail").css('margin-top','0px'); }
			});
		}

		// Lawyer Tab
		$(".Advogados").bind("mouseenter",function(){

			var aID = $(this).attr('artid');
			this.style.cursor = 'pointer';
			clearTimeout(ajax_timeout);

			if(array_loaded[aID]) {
				ajax_timeout = setTimeout(function() {
					$('#ajx_qs_equipa').hide();
					$('#ajx_qs_equipa').html(array_loaded[aID]);
					$('#ajx_qs_equipa').fadeIn('slow');
				}, 250);
			} else {
				ajax_timeout = setTimeout(function() {
					$('#ajx_qs_equipa').hide();
					$('#ajx_loading').show();
					$.post('ajx.qs_equipa.php', 'aID=' + aID, function(data) {
						array_loaded[aID] = data;
						$('#ajx_qs_equipa').html(data);
						$('#ajx_loading').hide();
						$('#ajx_qs_equipa').fadeIn('slow');
					});
				}, 250);
			}
	
		}).bind("mouseleave",function(){
			$('#ajx_loading').hide();
			//$('#ajx_qs_equipa').fadeOut('slow');
			if(ajax_timeout) { clearTimeout(ajax_timeout); }
		});


		DocumentReady(document);
	}
)

function DocumentReady(obj) {
	
	$(obj).find('.CycleContainer').cycle({
		fx:     'scrollUp', 
		timeout: 6000, 
		delay:  -2000
	});
	$(obj).find('.CycleContainer').removeClass('Hidden');

	$(obj).find('.CycleContainerHome').cycle({
		fx:     'scrollUp', 
		timeout: 3000, 
		delay:  -1000
	});
	$(obj).find('.CycleContainerHome').removeClass('Hidden');

}



//----------------------
// FAQ
//----------------------

function SwitchSummaryDetail(iQuestion, sNamespace) {
	var bHitOnSelected = false;
	if(!sNamespace) { sNamespace = ''; }
	if($(sNamespace + ' #Q' + iQuestion).hasClass('Sel')) { bHitOnSelected = true; }
	
	$(sNamespace + ' .Summary').removeClass('Sel');
	$(sNamespace + ' .Detail').hide('fast');
	
	if(!bHitOnSelected) {
		$(sNamespace + ' #Q' + iQuestion).addClass('Sel');
		$(sNamespace + ' #A' + iQuestion).show('fast');
	}
}


//----------------------
// Tooltip
//----------------------

$(function() {
	$('.Tooltip').tooltip({
		track: true, 
		delay: 0,
		fade: 250,
		showURL: false,
		bodyHandler: function() {
			return ($(this).attr("ttip"));
		}
	});
	$(".HelpTip").tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		opacity: 1, 
		fixPNG: true, 
		showBody: " - ", 
		extraClass: "fancy", 
		top: -5, 
		left: 25,
		bodyHandler: function() {
			tId = $(this).attr("tipid");
			return ($('.HelpTipText#'+tId).html());
		}
	}); 
});


//------------------
// Filter Stuff
//------------------

function onSubmitFilter() {
	var filter_year = $('.FilterForm SELECT[@name=filter_year] option:selected').attr('value');
	var filter_month = $('.FilterForm SELECT[@name=filter_month] option:selected').attr('value');
	if(((filter_year == '') || (filter_year == undefined)) &&
	   ((filter_month != '') && (filter_month != undefined))) {
		alert(MESSAGE_AnoValido);
		return(false);
	}
}

function onChangeFilter(iCategoryId) {
	var filter_year = $('.FilterForm SELECT[@name=filter_year] option:selected').attr('value');
	$('.FilterForm SELECT[@name=filter_month]').attr('disabled', 'disabled');

	$.get("ajx.get_months.php", { filter_year:filter_year, iCatId:iCategoryId },
		function(data){
			$('.FilterForm SELECT[@name=filter_month]').html(data);
			$('.FilterForm SELECT[@name=filter_month]').removeAttr('disabled');
		}
	);
}

