jQuery(function ($){
	$('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});

	$('#navigation .dd').wrapInner('<div class="dd-cnt" />').prepend('<div class="dd-t" />').append('<div class="dd-b" />');
	$('#dd-nav > ul > li').mouseover(function() {
			var $li = $(this),
				$dd = $(this).find('.dd:eq(0)');

			if ( $dd.width() >= $li.width()) {
				$dd.css({
					'left': -1 * ( $dd.width()/2 - $li.width()/2 )
				});
			} else {
				$dd.css({
					'left': $li.width()/2 - $dd.width()/2
				});
			};
	});
	
	$('.tenantsTalk, .tenantSpotlight').each(function(){
		var entries = $(this).find('.entry');
		entries.eq(Math.floor(Math.random() * entries.length)).show();
	});

	$('.top-search .btn-search').click(function() {
		$(this).next('.dd-search').fadeToggle();
		return false;
	});
	
	$('.items ul.items-list li:nth-child(4n+5)').css('clear','both');

	$(window).resize(function() {
		if ($('.header-image #image-holder').width() > $(document).width) {
			$('.header-image #image-holder').css({
				'left': -1 * ( $('.header-image #image-holder').width() - $(document).width())/2
			});	
		} else {
			$('.header-image #image-holder').css({
				'left': ( $(document).width() - $('.header-image #image-holder').width() )/2
			});
		};
		
	}).resize();

	
	var angle1 = 0,
		angle2 = 0,
		img1_src = $('img#earth').attr('src'),
		img2_src = $('img#planes').attr('src');
	$('#image-holder').html('');
	var R = Raphael("image-holder", 2304, 2304);
	var img1 = R.image(img1_src, 0, 0, 2304, 2304);
	var img2 = R.image(img2_src, 0, 0, 2304, 2304);

	var isFirstTime = true;
				
	$('.pick-destination .go-day, .pick-destination .go-night').click(function() {
		if ($(this).hasClass('active') || $('.header-image img#earth').is(':animated')) return false;

		$('.pick-destination .go-day, .pick-destination .go-night').removeClass('active');
		$(this).addClass('active');

		if ($(this).hasClass('go-day')) {

			angle1 = 90;
			angle2 = 180;
			img1.stop().animate({transform: "r" + angle1}, 5000, "<>");
			img2.stop().animate({transform: "r" + angle2}, 5500, "<>");

			$('.header-heading h2:visible').fadeOut(2500, function() {
				$('h2.txt-strategic-location').fadeIn(2500);
			});

			$('.pick-destination p.legend:visible').fadeOut(2500, function() {
				$('.txt-you-are-here-day').fadeIn(2500);
			});

			if ($('#logo').hasClass('logo-night')) {
				$('#logo').fadeOut(2500, function() {
					$('#logo').removeClass('logo-night');
					$('#logo').fadeIn(2500);
				});
			};

			$('.header-heading h3.txt-sctac').fadeOut(2500, function() {
				$('.header-heading h3.txt-sctac').css('color', '#122840').fadeIn(2500);
			});
			

		};
		if ($(this).hasClass('go-night')) {
			
			angle1 = -90;
			angle2 = -130;
			img1.stop().animate({transform: "r" + angle1}, 5000, "<>");
			img2.stop().animate({transform: "r" + angle2}, 5500, "<>");

			$('.header-heading h2:visible').fadeOut(2500, function() {
				$('h2.txt-connect-global-market').fadeIn(2500);
			});

			$('.pick-destination p.legend:visible').fadeOut(2500, function() {
				$('.txt-you-are-here-night').fadeIn(2500);
			});

			$('#logo').fadeOut(2500, function() {
				$('#logo').addClass('logo-night');
				$('#logo').fadeIn(2500);
			});

			$('.header-heading h3.txt-sctac').fadeOut(2500, function() {
				$('.header-heading h3.txt-sctac').css('color', '#82a52c').fadeIn(2500);
			});

			
		};	

		return false;
	});

	$('.bubbles-head .bubble').click(function() {
		if ($(this).hasClass('active')) return false;
		var href = $(this).attr('href');
		if ($(href).length == 0) return;

		$('.bubbles-head .bubble').removeClass('active');
		$(this).addClass('active');

		$('body').append('<div id="empty-space" style="height: 1000px;"/>');
		$('.bubbles-body').slideUp(400, function() {
			$('.bubble-tab').hide();
			$(href).show();	
			$('.bubbles-body').slideDown(400,function() {
				$('html, body').animate({
					scrollTop: $('.bubble-tabs').offset().top
				},400,function() {
					$('#empty-space').remove();
				});
			});
			$('.bubble-tabs .expand-collapse .btn').css('visibility', 'visible');
		});

		return false;
	});
	$('.bubbles-head .more-info a').click(function() {
		$(this).parents('li:eq(0)').find('a.bubble').click();
		return false;
	});
	$('.bubble-tabs .expand-collapse').click(function() {
		$('.bubbles-head .bubble').removeClass('active');
		$('.bubbles-body').slideUp(600).find('.bubble-tab').fadeOut();
		$(this).find('.btn').css('visibility', 'hidden');
		return false;
	});

	$('.form .row:eq(0)').addClass('medium left');
	$('.form .row:eq(1)').addClass('medium left last');
	$('.form .row:eq(2)').addClass('big');
	$('.form .row:eq(3)').addClass('medium left');
	$('.form .row:eq(4)').addClass('tiny left');
	$('.form .row:eq(5)').addClass('small left last');
	$('.form .row:eq(6)').addClass('medium left');
	$('.form .row:eq(7)').addClass('medium left last');
	$('.form .row:eq(8)').addClass('big');

	$(window).load(function() {
		$(window).resize(function() {
			if( $('.logos-list ul').length ) {
				var ul_w = $('.logos-list ul li').length * $('.logos-list ul li').outerWidth();

				ul_position = $('.logos-list').width() - ul_w + $('.logos-list ul li').outerWidth();

				$('.logos-list ul').css({ 'width' : ul_w + 'px', left: 0 });
				_animate();
			}
		}).resize();
	});

	$('.link-increase-altitude').click(function() {
		$('html, body').animate({
			scrollTop: 0
		}, 700);		


		return false;
	});
	
	$('a.cboxElement').colorbox({
		current: '({current} of {total})',
		previous: '',
		next: ''
	});

	$('.items .item:nth-child(4n)').after('<div class="cl" />');

});

function _stop_animation(){
	$('.logos-list ul').stop();
}

function _animate( to_pos ){
	
	//return false;
	
	var left = parseInt($('.logos-list ul').position().left);
	var pos;
	
	/*
	if( left == 0 ){
		pos = ul_position;
	}else {
		pos = 0;
	}
	*/
	pos = ul_position;
	
	if( typeof(to_pos) != 'undefined' ) {
		pos = to_pos;
	}
	current_animate_position = pos;
	
	var speed = (Math.abs(left - pos) + 1) * 10;
	speed = speed * 5;
	
	$('.logos-list ul').animate(
		{ 'left': pos + 'px' },
		speed,
		'linear',
		function(){
			$('.logos-list ul').css({ 'left' : '0' });
			_animate();
		}
	);
}


