/*
	Copyright 2010 Atanas Laskov
*/
var x_min 	= [6, 50, 40,   20,    20, 50];
var x_max 	= [50,  80,  85,  80,  80, 80];
var y_base 	= [-362, -276, -360, -380, -290, -165];
var speed	= [1, 1.2, 2.6, 5, 7.0, 2.5];
var hirez	= [false, false, false, true, true, true];
var sway	= [0, 0, 1, 1, 0, 0];
var dir 	= [], x = [], a = [], n_clouds = 5;

var lowrez_tresh = 680, hirez_tresh = 1300;

function init_clouds(){
	preload("cloud_1.gif", "cloud_2.gif");
	for(var i=0; i<n_clouds; i++){
		x[i] = x_min[i] + Math.random()*(x_max[i]-x_min[i]);
		if( Math.random() >= 0.5) dir[i] = +1; else dir[i] = -1;
		a[i] = Math.random()*3.14;
	}
	$(window).resize(function(){anim_sz();}); anim_sz(); setInterval('anim_clouds();', 60);
}

function isLowRez() { return ($(document).height() <= lowrez_tresh) || ( $(document).width() < 620 ); }
function isHiRez() { return ($(document).width() >= hirez_tresh); }

function anim_sz(){
	anim_clouds();
	for(i=0; i<n_clouds; i++) 
		if( isLowRez() || (!isHiRez() && hirez[i]) ) $('#cloud_'+i).fadeOut(); 
		else $('#cloud_'+i).fadeIn();
}

function anim_clouds(){
	var speedmod = 1400.0 / $(document).width(), ymod = 1 - ($(document).height() / 1024.0), i;
	
	for(i=0; i<n_clouds; i++) {
		x[i] = x[i] + speed[i]*dir[i] * speedmod * 0.06;
		a[i] += 0.08;
		
		if( x[i] > x_max[i] ) { x[i] = x_max[i]; dir[i] = dir[i] * (-1); }
		else if( x[i] < x_min[i] ) { x[i] = x_min[i]; dir[i] = dir[i] * (-1); }
			
		var ofsy = (Math.sin(a[i])*10*sway[i] + y_base[i] ) + 100 * ymod; 
		$('#cloud_'+i).css("left", x[i] + '%'); $('#cloud_'+i).css("top", ofsy + 'px');
	}
}