(function($) {
	$.fn.src = function(uri, callback) {
		callback = callback || $.noop;
		
		return this.each(function() {
			var $this = $(this);
			$this.attr('src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
			$this.one('load', callback);
			$this.attr('src', uri);
		})
	}
})(jQuery);

(function($) {
	$.fn.scoreboard = function(config) {
		config = $.extend({}, $.fn.scoreboard.defaults, config);
		config.chars = config.chars.split('');
			
		var charcode_map = {};
		$.each(config.chars, function(i, el) {
			charcode_map[el] = i;
		});
		
		return this.each(function(i, el) {
			var $this = $(this);
			var chars_orig = $this.text().split('');
			
			$this.text($.map(chars_orig, function(el, i) {
				if($.inArray(el, config.chars) != -1) {
					return config.chars[(charcode_map[el]+Math.round(Math.random()*100))%config.chars.length];
				} else {
					return el;
				}
			}).join(''));
			
			setTimeout(function() {
				var recurse=false;
				
				$this.text($.map($this.text().split(''), function(el, i) {
					if(el != chars_orig[i]) {
						recurse = true;
						return config.chars[(charcode_map[el]+1)%config.chars.length];
					} else {
						return el;
					}
				}).join(''));
				
				if(recurse) {
					setTimeout(arguments.callee, config.speed);
				} else {
					config.callback.call($this);
				}
			}, config.speed);
		});
	};
	
	$.fn.scoreboard.defaults = {
		callback:$.noop
		, chars:(function() {
			var chars=[], start=32, end=127;
			for(var i=start; i<end; ++i) {
				chars[i-start] = String.fromCharCode(i);
			}
			
			return chars.join('');
		})()
		, speed:$.fx.speeds.fast
	}
})(jQuery);



$(function() {
	
	if($(".videos").length)
	{
		top.$(top).trigger('pause.background-loop');
	}
	
	var peopleScrolling = function(dir) {
		var Scrollwrap = $('#people');
		var Scrollcontent = $('#scrolling_content', Scrollwrap);
		
		Scrollcontent.width(0);
		$('img', Scrollcontent).each(function() {
			Scrollcontent.width(Scrollcontent.width()+$(this).width());
		});
		var scrolling_width = Scrollcontent.width()/3;
		
		Scrollcontent.stop().clearQueue();
		
		if(Scrollcontent.position()) {
			Scrollcontent.animate(
					{
						left:(Scrollcontent.position().left+Scrollwrap.width()*dir)
					}
					, {
						duration:1000
						, step:function(now, fx) {
							fx.now = (fx.now%scrolling_width)-scrolling_width;
						}
					}
			);
		}
	};
	
	$('#people #scroll_next').live('mouseup', function(e) {
		clearInterval($('#people #scroll_next').data('autoScroll'));
		peopleScrolling(-1);
	}).data('autoScroll', setInterval(function() {
		peopleScrolling(-1)
	}, 6000));
	
	$('#people #scroll_prev').live('mouseup', function(e) {
		clearInterval($('#people #scroll_next').data('autoScroll'));
		peopleScrolling(1);
	});
	
	
	
	$('#flight_status.pictures #image_big').data('autoSlide', setInterval(function() {
		$('#flight_status.pictures #image_big').trigger('click');
	}, 6000));
	$('#flight_status.pictures .media_items a, #flight_status.pictures #image_big').live('mouseup', function() {
		clearInterval($('#flight_status.pictures #image_big').data('autoSlide'));
	});
	
	$('#flight_status.pictures .media_items a').live('click', function(e) {
		e.preventDefault();
		var a = $(this);
		
		$('#flight_status.pictures .media_items a.active').removeClass('active')
		a.addClass('active');
		
		$('#flight_status.pictures #image_big img').fadeOut('fast', function() {
			var $this = $(this);
			$this.src(a.attr('href'), function() {
				$this.fadeIn();
			});
		})
	});
	
	$('#flight_status.pictures #image_big').live('click', function(e) {
		e.preventDefault();
		
		if($('#flight_status.pictures .media_items a[href="'+$('img', this).attr('src')+'"]').parent().next().children('a').trigger('click').length == 0) {
			$('#flight_status.pictures .media_items li:first a').trigger('click');
		}
	})
	
	
	
	var autoPilot = function() {
		var $this = $('#start_plane');
		if($this.length > 0) {
			$this.css({left:-164, top:(Math.round(Math.random()*150)+100)+'px'});
			$this.animate(
					{
						left:1360
						, top:$this.position().top-60
					}
					, {
						duration:12000
						, easing:'linear'
					}
			)
		}
	};
	
	setTimeout(function() {
		autoPilot();
		$('#start_plane').data('autoPilot', setInterval(autoPilot, 15000));
	}, 4000);
	
	$('#sky .start_label').each(function() {
		var $this = $(this);
		var iv = function() {
			$this.fadeIn().delay(2000).fadeOut();
		};
		$this.data('autoBlend', setInterval(iv, Math.random()*7000+5000));
		
		$this.fadeOut(1);
	});
	
	
	
	$('.scrolling').jScrollPane();
	
	
	
	$("input[type=file]").filestyle({ 
		image:"/images/button_choose.png"
		, imageheight:45
		, imagewidth:132
		, width:180
	});
});

$(function() {
	$('.error_list').fadeTo(1, 0);
	
	setTimeout(function() {
		$('.error_list').fadeTo('fast', 1);
	}, 400);
});

