$(function(){ 

	//LOGIN FORM
	$('#block-sata_login #edit-sata-login-user-name, #block-sata_login #edit-sata-login-password').val('').focus(function(){
		$(this).prev().hide();
	}).blur(function(){
		if(!$(this).val()) $(this).prev().show();
	});

	//SEARCH FORM
	$("#edit-search-theme-form-1").focus(function(){
		if($(this).val()==$(this).attr('title')) $(this).val('');
	}).blur(function(){
		if(!$(this).val()) $(this).val($(this).attr('title'));
	});

	//SERVICES MENU
	if($('#services').is(':visible')){
		$('#block-menu-menu-services li:first').addClass('active-trail');
		$('.services_content').load($('#block-menu-menu-services a:first').attr('href'), function(){
			//$(this).fadeOut();
			if(typeof($.ifixpng)=='function'){
				$('[type=submit], [type=image]').ifixpng();
			}
			
		}).append('<div class="clear"></div>');
	}
	$('#block-menu-menu-services li a').click(function(event){
		event.preventDefault();
		elm=this;
		if($('#services').is(':hidden')){
			$('#services').slideDown(function(){
				$('#block-menu-menu-services li.active-trail').removeClass('active-trail');
				$(elm).parents('li').addClass('active-trail');
			});
		} else {
			$('#block-menu-menu-services li.active-trail').removeClass('active-trail');
			$(elm).parents('li').addClass('active-trail');
		}
		$('.services_content').fadeOut(function(){
			$(this).load($(elm).attr('href'),function(){
				$(this).fadeIn();
				if(typeof($.ifixpng)=='function'){
					$('[type=submit], [type=image]').ifixpng();
				}
				
			});
		});
	});

	//CONTENT MENU
	/*
	$('#block-menu-content-menu ul').hide();
	$('#block-menu-content-menu a.active').parents('li').find('a:first').addClass('active').next('ul').show();
	$('#block-menu-content-menu a').click(function(evt){
		evt.preventDefault();
		if($(this).is('.active')){
			$(this).parent().parent().find('a').removeClass('active').next('ul').slideUp();
		} else {
			$(this).parent().parent().find('a').removeClass('active').next('ul').slideUp();
			$(this).addClass('active').next('ul').slideDown();
		}
	});
	*/
	$('#block-menu-menu-sata').find('li.start-collapsed a').bind('click.sata', function(){
		if($(this).parent().is('.expanded')) $(this).parent().removeClass('start-collapsed');
	});

	//TABS
	if(typeof($.fn.tabs)!='undefined'){
		$('#tabs_schedule_response, .tabs').tabs();
	}

	/*
	$('.modal_trigger').live('click',function(evt){
		evt.preventDefault();
		var modal = $(this).parent().find('.modal_wrapper');
		var newModal = modal.clone();
		newModal.attr('id','newModal').css({'left':'50%','top':'50%','margin-left':-modal.width()/2+'px','margin-top':-(modal.height()/2)-20+'px'}).appendTo('body');
		$('#newModal').show();
		function hideModal(){
			$('#newModal').remove();
			$('body').unbind('keydown');
		}
		$('.modal_wrapper .modal_close, .modal_wrapper .modal_overlay').click(function(evt){
			evt.preventDefault();
			hideModal();
		});
		$('body').keydown(function(evt){
			if(evt.keyCode==27) hideModal();
		});
	});

	/* CUSTOM MAP MODAL
	$('#showMapLink').live('click',function(evt){
		evt.preventDefault();
		var html = $('#SataMapContainer').html();
    $('#origin').val(0);
    $('#destiny').val(0);
		$('#SataMapContainer').html('<div class="modal_overlay"></div><a class="modal_close">fechar</a><div class="modal_content">'+html+'<div class="submit_bg"><input class="submit" type="image" src="/sites/all/themes/sata/images/layout/'+siteLang+'/bt_confirmar.png" /></div><div class="clear"></div></div>').show();
		function hideMapLink(){
			$('#SataMapContainer').hide().html(html);
			$(window).unbind('keydown');
		}
		$('#SataMapContainer .modal_close, #SataMapContainer .modal_overlay, #SataMapContainer .submit').click(function(evt){
			evt.preventDefault();
			hideMapLink();
		});
		$('body').keydown(function(evt){
			if(evt.keyCode==27) hideMapLink();
		});
	});*/

	//CHECK OPTION
	$('#sata_check_my_trip input[type=text]').live('click',function(){
		$(this).parent().parent().find('input[type=radio]').attr('checked','checked');
	});
	$('#sata-schedule-flight-status-form input[type=text], #sata-schedule-flight-status-form select').live('click',function(){
		$(this).parent().parent().find('input[type=radio]').attr('checked','checked');
	});
	
	//TOOLTIPS
	if(typeof(ie6)=='undefined'){
		$('.tooltip').live('mouseover',function(){
			$('body').append('<div class="tooltip_wrapper"><div class="tooltip_arrow"></div><!--iframe class="tooltip_iframe" src="about:blank" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></iframe--><div class="tooltip_content"><p align="center"><img src="/sites/all/themes/sata/images/layout/icon_loading.gif" border="0" /></p></div></div>');
			var offset = $(this).offset();
			var width = $('.tooltip_wrapper').width();
			if($(document).width()-offset.left>width){
				$('.tooltip_wrapper').css({'left':offset.left+$(this).outerWidth()+'px','top':offset.top+'px'}).addClass('tooltip_right'); //top right
			} else {
				$('.tooltip_wrapper').css({'left':offset.left-width+'px','top':offset.top+'px'}).addClass('tooltip_left'); //top left
			}
			$.get($(this).attr('href'),function(data){
				if(data) $('.tooltip_content').html(data);
				//$('.tooltip_iframe').css({'width':$('.tooltip_content').outerWidth()+'px','height':$('.tooltip_content').outerHeight()+'px'});
			});
		}).live('mouseout',function(){
			$('.tooltip_wrapper').remove();
		}).live('click',function(evt){
			evt.preventDefault();
		});
	} else {
		$('.tooltip').live('click',function(evt){
			evt.preventDefault();
			window.open($(this).attr('href'),'Info','height=400,width=300');
		});
	}

	//STORE LOCATION
	var sata_store_location_enabled = true;
	$('.sata_store_location li a').live('click',function(evt){
		evt.preventDefault();
		elm = $(this);
		if(elm.is('.organization-unit')){
			$('.sata_store_location li a.active').removeClass('active');
			elm.addClass('active');
		} else if(sata_store_location_enabled){
			sata_store_location_enabled = false;
			elm.addClass('loading');
			$.get(elm.attr('href'),function(data){
				if(data){
					elm.parents('.sata_store_location').next().html(data);
					$('.sata_store_location_wrapper:not(:animated)').animate({'left':'-='+$('.sata_store_location').width()+'px'},function(){
						elm.removeClass('loading');
						sata_store_location_enabled = true;
					});
				}
			});
		}
	});
	$('.sata_store_prev').live('click',function(evt){
		evt.preventDefault();
		$('.sata_store_location_wrapper:not(:animated)').animate({'left':'+='+$('.sata_store_location').width()+'px'});
		sata_store_location_enabled = true;
	});

	//SATA FLEET
	var sata_fleet_enabled = true;
	$('.sata_fleet_config_nav .prev').addClass('prev_disabled disabled');
	if($('.sata_fleet_config_wrapper').height()==$('.sata_fleet_config').height()) $('.sata_fleet_config_nav .next').addClass('next_disabled disabled');
	$('.sata_fleet_config_nav a').click(function(evt){
		evt.preventDefault();
		if(sata_fleet_enabled && $(this).is('.disabled')==false){
			sata_fleet_enabled = false;
			($(this).is('.prev')) ? dir = '+=' : dir = '-=';
			$('.sata_fleet_plan_wrapper:not(:animated)').animate({'top':dir+$('.sata_fleet_plan_wrapper img').height()+'px'},function(){});
			$('.sata_fleet_config_wrapper:not(:animated)').animate({'top':dir+$('.sata_fleet_config').height()+'px'},function(){
				($('.sata_fleet_config_wrapper').position().top<0) ? $('.sata_fleet_config_nav .prev').removeClass('prev_disabled disabled') : $('.sata_fleet_config_nav .prev').addClass('prev_disabled disabled');
				($('.sata_fleet_config_wrapper').position().top==-$('.sata_fleet_config_wrapper').height()+$('.sata_fleet_config').height()) ? $('.sata_fleet_config_nav .next').addClass('next_disabled disabled') : $('.sata_fleet_config_nav .next').removeClass('next_disabled disabled');
				sata_fleet_enabled = true;
			});
		}
	});

	$('#flight_status_results_submit').click(function(evt){
		evt.preventDefault();
		$('.flight_status').click();
	});

	$('#flight_schedule_results_submit').click(function(evt){
		evt.preventDefault();
		$('.schedule').click();
	});
});

$(document).ready(function(){
  try {
  $('.banner-cycle').show();
  $('.banner-cycle').cycle({fx: 'fade'});
  } catch(e){}
});
