	
	function flagMouseOver(id)
	{
		var object = document.getElementById(id);
		if(object != null)
		{
			object.setAttribute('animate','false');
			
			object.height = flag_start_height;
			object.width = flag_start_width;
		}
	}

	function rand ( n )
	{
	  return ( Math.floor ( Math.random ( ) * n + 1 ) );
	}


	function flagMouseOut(id) {
		var percentage = 0;
		var object = document.getElementById(id);
		var timer = null;
		
		if(object != null) {
			object.setAttribute('animate','true');
			var height = parseInt(object.height);
			var width = parseInt(object.width);
			var _height = 0;
			var _width = 0;
			var new_height = 0;
			var new_width = 0;
			
			var height_total_decrement = (height - flag_height);
			var width_total_decrement = (width - flag_width);
			
			var step = function() {
				if(object.getAttribute('animate') == 'true') {
					_height = (height_total_decrement/100)*percentage;
					new_height = (height-_height);
					_width = (width_total_decrement/100)*percentage;
					new_width = (width-_width);
					object.height = new_height;
					object.width = new_width;
					
					if((percentage+flag_animation_speed) <= 100) {
						percentage += flag_animation_speed;
					}
					else {
						percentage = 100;
					}
					
					if(percentage >= 100)
					{
						window.clearTimeout(timer);
						object.setAttribute('animate','false');
					}
				}
				else {
					window.clearTimeout(timer);
					object.setAttribute('animate','false');
				}
			}
			timer = window.setInterval(step, 20);
		}
	}

	var unique_num = 1;
	function newflag(flag_container, flag) {
		var flag_container = document.getElementById(flag_container);
		if(flag_container != null) {
			var flag_name = flag.toLowerCase()+'.gif';
			var link = '?setlang='+flag;
			var flag_image = flags_root+flag_name;
			var img_id = 'flag_'+flag+'_'+unique_num;
			unique_num++;
			
			var div = document.createElement('div');
			div.className = 'flag';
			
			var img = document.createElement('img');
			img.src = flag_image;
			img.title = flag;
			img.id = img_id;
			img.height = flag_start_height;
			img.width = flag_start_width;
			img.setAttribute('animate','false');
			img.setAttribute('onMouseOver','flagMouseOver(\''+img_id+'\');');
			img.setAttribute('onMouseOut','flagMouseOut(\''+img_id+'\');');
			img.setAttribute('onClick', 'window.location=\''+link+'\';');
			
			div.appendChild(img);
			flag_container.appendChild(div);
			
			flagMouseOut(img_id);
		}
	}


	var start_timer = null;
	var i = 0; //Starting index count
	var newflag_init = function() {				
		newflag(flag_container, flags[i]); //Initialize a new flag
		
		i++;
		if(i >= flag_amount) {
			window.clearTimeout(start_timer);
		}					
	}

	window.onload = function() {
		start_timer = window.setInterval(newflag_init, appear_timeout);
	}
