/*	steam and human animation */
var steamobjs = new Array();
function reinitobj(i) {
	steamobjs[i].$el.css({
		top: 90,
		left: Math.floor(Math.random() * 50)
	});
	
	steamobjs[i].$el.find('img').fadeTo(0, 0);

	steamobjs[i].step = 0;
	steamobjs[i].steps = Math.floor(30 + Math.random() * 60);
}
function opacitytrans(phase) {
	return 0.75 * Math.sin(phase * Math.PI);
}
function periodicanim() {
	for (var i = 0; i < steamobjs.length; i++) {
		steamobjs[i].step++;
		if (steamobjs[i].step >= steamobjs[i].steps)
			reinitobj(i);
		else {
			var phase = steamobjs[i].step / steamobjs[i].steps;
					
			steamobjs[i].$el.css({
				top: (90 - phase * 70)
			});
			
			steamobjs[i].$el.find('img').css( {
				left: ('-' + ((4 - Math.abs(Math.floor(steamobjs[i].step / 2) % 8 - 4)) * 70) + 'px'),
				top: ('-' + (steamobjs[i].sprite * 70) + 'px')
			}).fadeTo(0, opacitytrans(phase));
		}
	}
	
	setTimeout(periodicanim, 50);
}

var curfoldata = [10,10,9,9,9,10,11,12,14,16,17,19,20,21,22,22,23,23,23,23,23,23,22,21,
20,18,16,13,10,9,7,6,6,5,5,4,4,4,4,4,4,5,6,7,9,10,10,9,8,7,6,7,9,12,16,18,20,21,21,21,21,20,17];

function imgpreload(path) {
	var img = new Image();
	img.src = path;
}

function switchgal() {
	var nminigal = (cminigal + 1) % minigal.length;

	$('#minigal1').attr('src', minigal[cminigal]);
	
	$('#minigal0').hide();
	$('#minigal0').attr('src', minigal[nminigal]);
	$('#minigal0').fadeIn(1000);
	
	cminigal = nminigal;
}

function checkIePngOpacityCompat() {
	if ($.browser.msie && parseInt($.browser.version, 10) < 9) return false;
	return true;
}

$(function() {
	$('.js-ext').click(function() {
		window.open(this.href);
		return false;
	});	
	
	if (checkIePngOpacityCompat()) {
		imgpreload(jsbase + 'stat/img/steam.png');
		imgpreload(jsbase + 'stat/img/steamalt.png');
		
		var $cup = $('#header .cup');

		for (var n = 0; n < 4; n++) {
			var $steamobj = $('<div class="steam"><img src="' + jsbase + 'stat/img/steam.png" /></div>');
			$steamobj.appendTo($cup);
			steamobjs[n] = { $el: $steamobj, sprite: n };
			reinitobj(n);
			steamobjs[n].step = Math.floor(Math.random() * steamobjs[n].steps);
		}
		periodicanim(1000);
	}
	
	var steamclickcount = 5;
	$('.steam img').click(function() {
		if (0 == --steamclickcount )
			$('.steam img').attr('src', jsbase + 'stat/img/steamalt.png');
		return false;
	});

	var $curfol = $('<img class="cursor-follower" src="' + jsbase + 'stat/img/ba-cursor.png" />');
	$curfol.appendTo('#header');
	$curfol.hide().css({ 'z-index': '9999', position: 'absolute' });
	
	$('body')
		.mousemove(function(event) {
			var wrapx = 372;
			var cfx = event.pageX - $('#header').offset().left - wrapx;
			if (cfx < 0) cfx = 0;
			if (cfx > 600) cfx = 600;
			
			var cfy = 235
				- curfoldata[Math.floor(cfx / 10)] * (1 - (cfx % 10) / 10)
				- curfoldata[Math.floor(cfx / 10) + 1] * ((cfx % 10) / 10);
				
			$curfol.show().css({ left: (Math.floor(cfx + wrapx) + 'px'), top: (Math.floor(cfy) + 'px') });
		});
		
	$('#nav > li, #add-nav > li').hover(function() {
		$(this).children('ul').hide().css('left', 0).slideDown(200);
	}, function() {
		$(this).children('ul').slideUp(200);
	});
	
	if (typeof(minigal) != 'undefined' && minigal.length > 1) {
		for (var i = 0; i < minigal.length; i++)
			imgpreload(minigal[i])

		setInterval('switchgal()', 3000);
	}
	
	var newtags = new Array();
	var newtagscnt = 0;

	var mincount = 100500;
	var maxcount = 0;

	$('#tags ul li').each(function() {
		var curcount = 0 + $(this).find('span').text();
		mincount = mincount < curcount? mincount: curcount;
		maxcount = maxcount > curcount? maxcount: curcount;
		newtags[newtagscnt++] = $(this).html();
	});
	
	newtags.sort(function() { return Math.random() - 0.5 }); //randomize kw order

	var newtagshtml = '<div class="newtags">';
	for (var n in newtags)
		newtagshtml += newtags[n] + ' ';
	newtagshtml += "</div>";

	$('#tags').append(newtagshtml);
	$('#tags ul').hide();
	
	if (mincount < maxcount) {
		var minfontsize = 10;
		var maxfontsize = 20;
		
		$('#tags .newtags a').each(function() { 
			var curcount = 0 + $(this).find('span').text();
			$(this).css('font-size', minfontsize + Math.floor((curcount - mincount) / (maxcount - mincount) * (maxfontsize - minfontsize)));
		});
	}
});
