/*** this file does some js setup after the page has finished loading ***/

// bind unload handler. this has the side-
// effect of making sure the browser runs
// document.ready when the user goes to a
// page via the back button.
$(window).bind("unload", function() {});

$(document).ready(function() {
	$('#logobanner').css({
		display:'block',
		opacity:1,
		left:-2000
	});
	Tempest.repositionLogoImage();

	var finishedAnimations = 0;
	var gridElements = $('.grid');
	gridElements.each(function() {
		var el = $(this);
		if(el.hasClass('slideoff')) {
			el.css({
				display:'block',
				opacity:1
			});
			return;
		}
		el.css({
			display:'block',
			opacity:0
		});
		var position = el.position();
		var top = position.top;
		var left = position.left;
		var timeout = parseInt(Math.random()*600);
		if(el.hasClass('slideup')) {
			top += 200;
			timeout += 600;
		} else if(el.hasClass('slidedown')) {
			top -= 200;
			timeout += 600;
		} else if(el.hasClass('slideleft')) {
			left += 200;
			timeout += 600;
		} else if(el.hasClass('slideright')) {
			left -= 200;
			timeout += 600;
		} else {
			var changetop = Math.round(Math.random()) == 1;
			var changeplus = Math.round(Math.random()) == 1;
			if(changetop) {
				if(changeplus) top += 200;
				else top -= 200;
			} else {
				if(changeplus) left += 200;
				else left -= 200;
			}
		}
		el.css({
			left:left,
			top:top
		});
		setTimeout(function() {
			el.animate({
				opacity:1,
				left:position.left,
				top:position.top
			}, 600, function() {
				finishedAnimations++;
				if(finishedAnimations >= gridElements.size()) {
					if($.isFunction(window.infinitNewsScrolling)) {
						infinitNewsScrolling();
					}
				}
			});
		}, timeout+100);
	});
	
	$('a').bind('click', function(e) {
		e.preventDefault();
		Tempest.goToUrl($(this).attr('href'));
		return false;
	});
	var repotimeout = false;
	$(window).resize(function(e) {
		if(repotimeout) clearTimeout(repotimeout);
		repotimeout = setTimeout(Tempest.repositionLogoImage,200);
	});
	$('#donotclick').bind('mouseenter', function() {
		var el = $(this);
		if(el.hasClass('moved')) {
			el.removeClass('moved');
			el.animate({
				left:0
			}, 200);
		} else {
			el.addClass('moved');
			el.animate({
				left:192
			}, 200);
		}
	
	});
});
