// jQuery code to control form display and widgets

$(document).ready(function(){
	
	$('#book-flight').click(function() {
		$('#promo-centre-img').attr('src','media/aerlinguscomhomepage/styleassets/images/promocentre/pics/book-flight-1.jpg');
		hideEngineForms('book-flight');
		$('.controller').show();
		return false;
	});
						   
	$('#checkin').click(function() {
		$('#promo-centre-img').attr('src','media/aerlinguscomhomepage/styleassets/images/promocentre/pics/manage-booking.jpg');
		hideEngineForms('checkin');
		$('.controller').show();
		return false;
	});
						   
	$('#manage-booking').click(function() {
		$('#promo-centre-img').attr('src','media/aerlinguscomhomepage/styleassets/images/promocentre/pics/manage-booking.jpg');
		hideEngineForms('manage-booking');
		$('.controller').show();
		return false;
	});
						   
	$('#wherefly').click(function() {
		$('#promo-centre-img').attr('src','media/aerlinguscomhomepage/styleassets/images/promocentre/pics/where-fly.jpg');
		hideEngineForms('wherefly');
		$('.controller').show();
		return false;
	});
	
	// simple accordion
	jQuery('#ancillaries').accordion({
		selectedClass: 'active',
		header: 'h3'
	});
	
	
	// promo-centre controller
	var centrePromoInterval = window.setInterval(
		function() {
			runCentrePromo(1);
		}, 6000
	);
	
	$('#promo-centre .controller .pause').click(
		function() {
			clearInterval(centrePromoInterval);
		}
	);
	
	$('#promo-centre .controller .prev').click(
		function() {
			clearInterval(centrePromoInterval);
			runCentrePromo(-1);
		}
	);
	
	$('#promo-centre .controller .next').click(
		function() {
			clearInterval(centrePromoInterval);
			runCentrePromo(1);
		}
	);
	
	function runCentrePromo(direction) {
		$('#promo-centre .image-area li.active').each(
			function() {
				
				var position = $('#promo-centre .image-area li').index(this);
				var size = $('#promo-centre .image-area').children().size();

				if (size < 2) {
					return;
				}
				
				$('#promo-centre .pics img').each(function(i){
					if (i == position + direction || (i == 0 && direction > 0 && position == size -1) || (i == size-1 && direction < 0 && position == 0)) {
						$(this).attr('src', $(this).data('onpic'));
					}
					else {
						$(this).attr('src', $(this).data('offpic'));
					}
				});
				
				$(this).fadeTo(500, 0.0,
					function() {
						$(this).removeClass('active');
                        $(this).css('zIndex', 1);						
					}
				);

				if (direction > 0) {
					var nextEl = $(this).next();
					
					if ($(this).is(":last-child")) {
						nextEl = $(this).parent().children().eq(0);
					}
				} else if(direction < 0) {
					var nextEl = $(this).prev();
					
					if ($(this).is(":first-child")) {
						nextEl = $(this).parent().find(':last-child');
					}
				}
				
				$(nextEl).fadeTo(500, 1.0,
					function() {
						$(nextEl).addClass('active');
						$(this).css('zIndex', 9000);
					}
				);
			}
		);
	}
	
	// promo-centre dots controller
	$('#promo-centre .image-area').each(
		function() {
			
			if ($(this).children().size() == 1) {
				
				$('#promo-centre .controller').hide();
				
			} else {
				
				var pics = document.createElement('ul');
				var offPic = 'media/aerlinguscomhomepage/styleassets/images/promocentre/player-pic-icon.gif';
				var onPic = 'media/aerlinguscomhomepage/styleassets/images/promocentre/player-pic-icon-active.gif';
				
				$(pics).addClass('pics');
				
				$(this).find('li').each(function(i){
				
					var picItem = document.createElement('img');
					$(picItem).data('index', i);
					$(picItem).data('offpic',offPic);
					$(picItem).data('onpic',onPic);
					$(picItem).attr('src', (i == 0) ? onPic : offPic);
					
					$(picItem).bind('click', function(){
					
						clearInterval(centrePromoInterval);
						
						if ($(picItem).attr('src') == offPic) {
							var el = this;
							
							$('#promo-centre .image-area li.active').each(function(){
								$(this).fadeTo(500, 0.0, function(){
									$(this).removeClass('active');	
									$(this).css('zIndex', 1);	
								});
							});
							
							$('#promo-centre .image-area li').eq($(this).data('index')).fadeTo(500, 1.0, function(){
									$(this).addClass('active');	
									$(this).css('zIndex', 9000);
							});
								
							
							$('#promo-centre .pics img').each(function(){
								$(this).attr('src', offPic);
							})
							
							$(this).attr('src', onPic);
						}
					});
					
					$(pics).append($(document.createElement('li')).append(picItem));
				})
				
				$('#promo-centre .controller').append(pics);
			}
		}
	);

});


