(function ($) {
	var onDocumentReady = function (e) {
			var body = $('body'), dt = new Date()/*, hrs = dt.getHours(), nightMode = false*/;
			var isHome = body.is('#home');
			if (isHome) arvestHeadlineFix();
			nightMode = body.is('.night');
			if (nightMode) {
				$(window).bind('resize', initStars);
			}
			$(window).bind('resize', initClouds).trigger('resize');
			$('#login-small-frm input[type=checkbox], #frm-signin input[type=checkbox]').checkbox({empty: 'img/blank.gif'});
		}, 
		arvestHeadlineFix = function () {
			var hdln = $('#box-whats-new-headline');
			if (hdln.length > 0) {
				var hdlnHeight = hdln.height(), hdlnLineHeight = parseInt(hdln.css('line-height').replace('px', ''), 10); 
				if (hdlnHeight < hdlnLineHeight * 2) {
					hdln.css('line-height', (hdlnLineHeight*2) + 'px');
				}
			}
		},
		starsTimer = null,
		cloudsTimer = null,
		initStars = function () {
			if (starsTimer) {
				window.clearTimeout(starsTimer);
			}
			starsTimer = window.setTimeout(addStars, 500);
		},
		addStars = function () {
			var b = $('#wrap'), stars = '', container = b.find('div.stars');
			if (container.length == 0) {
				container = $('<div class="stars"></div>').appendTo(b);
			}
			else {
				container.html('');	
			}			
			var cWidth = 853,
				bWidth = $(document).width(),
				bHeight = $(document).height() - 215,
				el, x, y, t;
			for (var i = 0; i < 120; i++) {
				x = Math.round(Math.random() * bWidth);
				y = Math.round(Math.random() * bHeight);
				el = '<div class="star png';
				t = Math.round(Math.random() * 100);
				t = t < 5 ? 3 : (t < 15 ? 2 : 1);
				x = x - (t == 3 ? 16 : (t == 2 ? 12 : 1));
				y = y - (t == 3 ? 16 : (t == 2 ? 13 : 1));
				x = x < 0 ? 0 : x;
				y = y < 0 ? 0 : y; 
				el += (' star-' + t + '" style="top:' + y + 'px;left:' + x + 'px;"></div>');
				stars += el;
			}
			container.html(stars);
		},
		initClouds = function () {
			if (cloudsTimer) {
				window.clearTimeout(cloudsTimer);
			}
			cloudsTimer = window.setTimeout(addClouds, 500);
		},
		addClouds = function () {
			var b = $('#wrap');
			b.find('div.cloud').remove();
			var cWidth = 833, bWidth = $(document).width(), el, clouds = [], marginLeft = '-' + ((bWidth - 833) / 2) + 'px';
			for (var i = 1; i <= 5; i++) {
				el = $('<div class="cloud png" id="cloud-' + i + '"></div>');
				el.css('margin-left', marginLeft);
				clouds.push(el);
			}	
			clouds = $(clouds);
			clouds.appendTo(b.find('#main'));
			var cloudAnimate = function () {
					var posLeft = this.position().left, speed;
					if (posLeft != -this.data('width')) {
						speed = Math.round(this.data('speed') * (this.data('path') - this.data('width') - posLeft) / this.data('path')); 
					}
					else {
						speed = this.data('speed');
					}
					this.animate({left: bWidth + 'px'}, speed, 'linear', cloudAnimateCb);
				},
				cloudAnimateCb = function () {
					var $this = $(this);
					$this.css('left', '-' + $this.data('width') + 'px');
					cloudAnimate.apply($this);
				};
			clouds.each(function (idx) {
				var $this = $(this),
					speed = Math.round(30000 + 100000 * Math.random()),
					width = $this.width();
				$this.data('speed', speed).data('width', width).data('path', bWidth + 2 * width);
			//	cloudAnimate.apply($this);
			});
		},
		nightMode = false;
	$(document).bind('ready', onDocumentReady);
})(jQuery);