jQuery(document).ready(function() {
	$(".ajax-form").submit(function() {
		var form = $(this);
		// IF NEWSLETTER
		if($(this).hasClass('newsletter'))
			form.parent('div.sysform').find('.newsltr-go').hide();
		
	    form.parent('div.sysform').find('#load').append('<img src="/includes/imgs/ajax-loader.gif" width="16" height="16" alt="sending..." id="cloading" />');
	    var fem = $(this).serialize(),
			note = form.parent('div.sysform').find('#note');
	
		note.click(function() {
			$(this).slideUp(1000, function() { $(this).hide(); });
		});
	    
	    $.ajax({
	        type: "POST",
	        url: form.attr('action'),
	        data: fem,
	        success: function(msg) {
				if ( note.height() ) {			
					note.slideUp(0, function() { $(this).hide(); });
				} 
				else note.hide();
	
	
				form.parent('div.sysform').find('#cloading').fadeOut(300, function() {
					$(this).remove();
					form.parent('div.sysform').find('.newsltr-go').show();
					var php = msg.split('<|>');
	
					var i = setInterval(function() {
						if ( !note.is(':visible') ) {
							note.html(php[0]).slideDown(1000);
							clearInterval(i);
						}
					}, 40);
					
					if(php[1] == 'OK'){
						$(':input','#ajax-form')
						 .not(':button, :submit, :reset, :hidden')
						 .val('')
						 .removeAttr('checked')
						 .removeAttr('selected');
						form.hide();
					}else{
						$("input[name="+php[1]+"], textarea[name="+php[1]+"]").focus();
					}
					if(form.parents('div.content').find('#titulo').length > 0){
						$('html, body').animate({
						    scrollTop: form.parents('div.content').find('#titulo').offset().top
						}, 1000);
					}
				}); // end loading image fadeOut
	
	        }
	    });
	
	
	   return false;
	});
	
	$('div#meteo').load('/tempo_yr.php');
	
	
	$('p.custom-radio').click(function(){
		if(!$(this).find('label').hasClass('checked')){
			$(this).find('label').addClass('checked');
			$(this).find('input[type=radio]').attr('checked', true);
			if($(this).find('input[type=radio]').attr('name') == 'empresa'){
				$('p#designsocial').show();
			}else{
				$('p#designsocial').hide();
			}
			$('p.custom-radio').not(this).each(function(){
				$(this).find('label').removeClass('checked');
				$(this).find('input[type=radio]').attr('checked', false);
			});
		}
	});
	
	$('p.custom-checkbox').click(function(){
		if(!$(this).find('label').hasClass('checked')){
			$(this).find('label').addClass('checked');
			$(this).find('input[type=checkbox]').attr('checked', true);
			$(this).parents('form:first').find('p.ed').hide();
		}else{
			$(this).find('label').removeClass('checked');
			$(this).find('input[type=checkbox]').attr('checked', false);
			$(this).parents('form:first').find('p.ed').show();
		}
	});
	
	$('.data').datepicker({
		changeYear: true,
		changeMonth: true
	});
	$('.datanasc').datepicker({
		defaultDate: '-30y',
		changeYear: true,
		changeMonth: true
	});
	
	$('.btnreservar').aToolTip({
		clickIt: true,
		tipContent: $('#reserva-form').html(),
		toolTipClass: 'emTheme',
		xOffset: -($('#reserva-form').width() + 110),
		yOffset: -($('#reserva-form').height() + 25),
		onShow: function(tip, node){
			var params = tip.attr('id').split('-');
			$('html, body').animate({
			    scrollTop: $('#aToolTip').position().top - 100
			}, 1000);
			$('#aToolTip').find('form').find('input[name=grupoid]').val(params[0]);
			$('#aToolTip').find('form').find('input[name=viaturaid]').val(params[1]);
		}
	});	
	
	$('.detalhes, .pagamento').aToolTip({
		clickIt: true,
		tipContent: $('#reserva-dados').html(),
		toolTipClass: 'emTheme',
		xOffset: -545,
		yOffset: -53,
		onShow: function(tip, node){
			$('#aToolTip').find('.aToolTipContent #aToolTipCloseBtn').after('<label id="load" style="display: block; width: 500px; height: 150px; padding-top: 100px; text-align: center;"><img src="/includes/imgs/ajax-loader.gif" width="16" height="16" alt="load" id="cloading" style="margin-bottom: 5px;" /><br/>A carregar...</label>');
			$.post('/rentacar/myaccount/'+(tip.hasClass('pagamento') ? 'pagamento' : 'detalhes_reserva')+'_ajax.php', {reservaid: tip.attr('id')}, function(response){
				$('#aToolTip').find('.aToolTipContent').find('#load').fadeOut(300, function() {
					$(this).remove();
					$('#aToolTip').find('.aToolTipContent #aToolTipCloseBtn').after(response);
				});
			});
			$('html, body').animate({
			    scrollTop: $('#aToolTip').position().top - 75
			}, 1000);
			
			tip.parents('table:first').find('td').css('background', '#fff');
			tip.parents('tr:first').find('td').each(function(){
				$(this).css('background', '#DCF2CD');
			});
		},
		onHide: function(tip, node){
			tip.parents('tr:first').find('td').each(function(){
				$(this).css('background', '#fff');
			});
		}
	});
	
	$('a.att').aToolTip();
	/*
	$().piroBox({
		width: 500,
		my_speed: 400, //animation speed
		bg_alpha: 0.1, //background opacity
		slideShow : false, // true == slideshow on, false == slideshow off
		slideSpeed : 4, //slideshow duration in seconds(3 to 6 Recommended)
		close_all : '.piro_close,.piro_overlay'// add class .piro_overlay(with comma)if you want overlay click close piroBox
	});
	
	
	if('http://' + window.location.hostname + '/' == window.location)
		$('a#natal').trigger("click");
		*/

});
