jQuery(function(){
	$('#tieser').each(function(){
		var BgSpeed = 800;
		var BgDuration = 9000;
		var bgs = $(this).children('ul').find('li');
		$(bgs).fadeOut(0).first().fadeIn(0);
		var Interval = setInterval(function(){
			var current = $(bgs).filter(':visible').index();
			var newF = current;
			if(++newF==$(bgs).size())
				newF = 0;
			$(bgs).eq(newF).addClass('new').fadeIn(BgSpeed,function(){
				$(bgs).eq(current).fadeOut(0);
				$(bgs).eq(newF).removeClass();
			});
			
			$('#tieser').find('h2 a').fadeOut(BgSpeed/2,function(){
				$(this).html($(bgs).eq(newF).html()).fadeIn(BgSpeed/2);
			});
		},BgDuration);
		
		$('#tieser .chat i').each(function(){
			var i = 0;
			var root = this;
			var int = setInterval(function(){
				
				
				switch(i){
					case 0:
						$(root).css('backgroundPosition','0px -78px');
						break;
						
					case 1:
						$(root).css('backgroundPosition','1px -78px');
						break;
					case 2:
						$(root).css('backgroundPosition','1px -77px');
						break;
					case 3:
						$(root).css('backgroundPosition','1px -76px');
						break;
					case 4:
						$(root).css('backgroundPosition','0px -76px');
						break;
					case 5:
						$(root).css('backgroundPosition','-1px -76px');
						break;
					case 6:
						$(root).css('backgroundPosition','-1px -77px');
						break;
					case 7:
						$(root).css('backgroundPosition','-1px -78px');
						break;
				}
				i++;
				if(i==8)
					i = 0;
			},200);
		});
	});
	//Forms select--------------------------------------------------------------------
	$('.forms > ul a').first().parent().addClass('active');
	$('.forms > form').hide().first().show();
	$('.forms > ul a').click(function(){
		
		if(!$(this).parent().hasClass('active')){
			$('.forms > form:visible').hide();
			$('.forms > ul .active').removeClass('active');
			var newform = $(this).attr('href');$(this).parent().addClass('active');
			
			$('.forms '+newform).show();
		}
		return false
	});
	
	$('input[name^="leave"]').click(function(){
		if($(this).attr('value')==='Leaving from...')
			$(this).attr('value','');
	});
	$('input[name^="leave"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Leaving from...');
	});
	$('input[name^="go"]').click(function(){
		if($(this).attr('value')==='Going to...')
			$(this).attr('value','');
	});
	$('input[name^="go"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Going to...');
	});
	$('input[name^="email"]').click(function(){
		if($(this).attr('value')==='E-mail')
			$(this).attr('value','');
	});
	$('input[name^="email"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','E-mail');
	});
	/*$('input[name^="password"]').click(function(){
		if($(this).attr('value')==='Password')
			$(this).attr('value','');
	});
	$('input[name^="password"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Password');
	});*/
	$('input[name^="res_code"]').click(function(){
		if($(this).attr('value')==='Reservation code...')
			$(this).attr('value','');
	});
	$('input[name^="res_code"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Reservation code...');
	});
	$('input[name^="last_name"]').click(function(){
		if($(this).attr('value')==='Last name...')
			$(this).attr('value','');
	});
	$('input[name^="last_name"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Last name...');
	});
	
	$('input[name^="first_name"]').click(function(){
		if($(this).attr('value')==='First Name...')
			$(this).attr('value','');
	});
	$('input[name^="first_name"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','First Name...');
	});
	
	$('input[name^="birthday"]').click(function(){
		if($(this).attr('value')==='Birthday...')
			$(this).attr('value','');
	});
	$('input[name^="birthday"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Birthday...');
	});
	$('input[name^="username"]').click(function(){
		if($(this).attr('value')==='Username (e-mail)...')
			$(this).attr('value','');
	});
	$('input[name^="username"]').blur(function(){
		if($(this).attr('value')==='')
			$(this).attr('value','Username (e-mail)...');
	});
	/*--------------------------------------------------------------*/
	$('input[type="password"]').after('<input type="text" value="Password"/>').hide();
	
	if($('input[type="password"]').val()!=='')
		$('input[type="password"] + input').hide().prev().show();
	
	$('input[type="password"] + input').focus(function(){
		$(this).hide().prev().show().focus();
	});
	$('input[type="password"]').blur(function(){
		if($(this).val()==='')
			$(this).hide().next().show();
	});
	$('input.date').datepicker({dateFormat: 'dd / MM / yy'});
	//Faq-----------------------------------------------------------------------------
	$('.faq').each(function(){
		var FaqAnimationSpeed = 300;
		var FaqAnimationBlocked = false;
		
		$('.faq dd dd').slideUp(0).parent().removeClass('open');
		$('.faq dd').slideUp(0).parent().removeClass('open');
		$('.faq h3,h4,a.ctrl').click(function(){
			if(!FaqAnimationBlocked){
				if($(this).parent().parent().hasClass('open')){
					FaqAnimationBlocked = true;
					$(this).parent().next().slideUp(FaqAnimationSpeed, function(){
						$(this).parent().removeClass('open');
						FaqAnimationBlocked = false;
					});
				}else{
					$(this).parent().parent().addClass('open').children('dd').slideDown(FaqAnimationSpeed,function(){
						FaqAnimationBlocked = false;
					});
				}
			}
			return false;
		});
		$('.faq li').click(function(){
			if(!FaqAnimationBlocked){
				FaqAnimationBlocked = true;
				if($(this).hasClass('open')){
					$(this).children('p,ul').slideUp(FaqAnimationSpeed,function(){
						$(this).parent().removeClass('open');
						FaqAnimationBlocked = false;
					});
				}else{
					$(this).addClass('open');
					$(this).children('p,ul').slideDown(FaqAnimationSpeed,function(){FaqAnimationBlocked = false;});
				}
			}
			
			
			return false;
		});
	});
	//Flight Search Deals Tab-----------------------------------------------
	$('.other_deals').parent().tabs();
	$('<a href="#mainarea"></a>').click();
	$('.other_deals li:first-child a').click();
	//Offers------------------------------------------------------------------
	$('.offers').each(function(){
		var offersSlideSpeed = 400;
		var offersSlideBlocked = false;
		
		$(this).find('h3').each(function(){
			if($(this).height()>30)
				$(this).addClass('double')
		});
		$(this).find('h4').each(function(){
			
			
			if($(this).height()>26)
				$(this).addClass('double')
		});
		$(this).children('dl').children('dd').slideUp(0).parent().removeClass('open');
		$(this).find('dl h3, dl a.ctrl').click(function(){
			if(!offersSlideBlocked){
				offersSlideBlocked = true;
				var root = $(this).parent().parent();
				if($(root).hasClass('open')){
					
					$(root).children('dd').slideUp(offersSlideSpeed,function(){
						$(root).removeClass('open');
						offersSlideBlocked = false;
					});
				}else{
					
					$(root).addClass('open').children('dd').slideDown(offersSlideSpeed,function(){
						offersSlideBlocked = false;
					});
				}
			}
			
		});
		$(this).children('dl').find('dl').click(function(){
			return false;
		});
		
	});
	$('.block').each(function(){
		$(this).css('min-height',$(this).next().height()-20);
		if($(this).hasClass('block_2'))
			$(this).css('min-height',$(this).next().height()-92);
	});
	//Tickets------------------------------------------------------------------
	//$('#content').remove();
	$('.booking_inside').each(function(){
		$(this).tabs();
		var	TicketsAnimationSpeed = 500;
		var TicketsAnimationBlocked = false;
		$('.booking_inside > div > dl ul a').click(function(){return false;});
		$('.booking_inside > div > dl > dd').find('p,dt,dd').css('display','none');
		$('.booking_inside > div > dl').click(function(){
			//alert(0);
			var root = this;
			if(!TicketsAnimationBlocked){
				TicketsAnimationBlocked = true;
				if($(this).hasClass('open')){
					$(root).children('dd').find('p,dt,dd').fadeOut(TicketsAnimationSpeed,function(){
						$(root).removeClass('open');
						TicketsAnimationBlocked = false;
					});
				}else{
					$(this).addClass('open');
					var elem = $(this).children('dd').find('p,dt,dd');
					var idx = 0;
					
					var Size = $(elem).size();
					function ShowNext(){
						if(idx>=Size) return;
						$(elem).eq(idx++).fadeIn(Time,function(){
							ShowNext();
						});
					}
					var Time = TicketsAnimationSpeed/Size*2;
					$(elem).eq(idx++).fadeIn(Time,function(){
						TicketsAnimationBlocked = false;
						ShowNext();
					});
					
					
				}
			}
			return false;
		});
		
	});
	
	//Popups--------------------------------------------------------------------------
	
	$('#panel .bookings').click(function(){
		$('#popup').addClass('open').find('.check').addClass('open');
		return false;
	});
	$('#panel a:contains(Register Now)').click(function(){
		$('#popup').addClass('open').find('.booking_reg').addClass('open');
		return false;
	});
	$('#panel a:contains(Forgot password?)').click(function(){
		$('#popup').addClass('open').find('.password').addClass('open');
		return false;
	});
	$('#popup').click(function(){
		$(this).removeClass('open').children().removeClass('open');
		
	});
	$('.booking').click(function(){
		return false;
	});
	$('#popup a.close').click(function(){
		$('#popup').removeClass('open').children().removeClass('open');
		return false;
	});
	
});
