var user_conts = new Array();
function new_draw(win_id, is_cur)
{	
    clearInterval(u_timer);
    clearInterval(animate_timer);    
    shuffle_participants('#draw_div');
    $('#head_winner').hide();
    open_draw_area();
	$('#draw_div_cont').animate({'opacity': '0.95'}, 500);
	setTimeout('start_animation('+win_id+')', 2000);	
}

function shuffle_participants(container)
{
	$('#draw_div_cont').css({'opacity': '0'});
	$('#draw_div_cont').show();
	
	var width = $(container).width();
	var height = $(container).height();
	var b_width = 90;
	var b_height = 25;
	//debugger;
	while (user_conts.length > 0)
	{
		user_conts.pop();
	}
	$(container).html('');
	for (id in cur_ids)
	{
		if (isNaN(parseInt(id)))
		{
			continue;
		}
		var top = height / 2 - Math.ceil(b_height / 2);
		var left = width / 2 - Math.ceil(b_width / 2);
		var red = 50;
		var green = 50;
		var blue = 50;
		for (i = 0; i < 1/*cur_chances[cur_ids[id]]*/; i++)
		{		
			//while ((Math.abs(top - (height / 2 - 13)) < 50) && (Math.abs(left - (width / 2 - 100)) < 200))
			if (i == 0)
			{  
				top = Math.floor(Math.random() * (height - b_height + 1));
				left = Math.floor(Math.random() * (width - b_width + 1));
				red = Math.floor(Math.random() * (151)) + 50;
				green = Math.floor(Math.random() * (151)) + 50;
				blue = Math.floor(Math.random() * (151)) + 50;
			}
			else
			{
				top += (Math.floor(Math.random() * (b_height + 1)) + 1 * b_height) * ((Math.random() > 0.5) ? 1 : -1);
				left += Math.floor(Math.random() * (2 * b_width + 1)) - 1 * b_width;
				if (top < 0) top = 0;
				if (left < 0) left = 0;
				if (top > (height - b_height)) top = height - b_height;
				if (left > (width - b_width)) left = width - b_width;
			}
			var num = id;
			var cont_id = 'c_user_' + (cur_ids[id]) + ((i > 0) ? ('_' + i) : '');
			$(container).prepend('<div id="' + cont_id + '" class="u_drawer" style="position: absolute; width: auto; height: ' + b_height + 'px; text-align: center; line-height: ' + b_height + 'px; margin-top: ' + top + 'px; margin-left: ' + left + 'px; color: rgb(' + red + ', ' + green + ', ' + blue + '); z-index: 15;">' + cur_users[cur_ids[id]] + ' (' + cur_chances[cur_ids[id]] + ')' + '</div>');
			user_conts.push(cont_id);
		}
	}
}

function start_animation(win_id)
{
	var winner = win_id;	
	if ( typeof animate_autodraw.counter == 'undefined' ) {
        animate_autodraw.counter = 0;
    }
	adr_timer = setInterval('animate_autodraw(30, '+winner+')', 300);
}

function animate_current_parties()
{
	if ( typeof animate_current_parties.inited == 'undefined' ) {
        animate_current_parties.inited = false;
    }
	if ((!animate_current_parties.inited) && (cur_ids.length > 0))
	{
		shuffle_participants('#draw_div');
		$('#draw_div_cont').addClass('hide_cont');
		$('#draw').html('');
		$('#draw_div_cont').animate({'opacity': '1'}, 500);
		animate_current_parties.inited = true;
		$('.restore_link').click(open_draw_area);
		$('.restore_link').css('zIndex', '11');
		$('.restore_link').show();
		//$('.close_link').click(close_draw_area);
	}	
	if (user_conts.length > 0)
	{
		var user = user_conts[Math.floor(Math.random()*user_conts.length)];
	    if (user != '______array')
	    {
	        var draw = $('#'+user);
	        var col = draw.css('color');
	        draw.removeClass('transparent');
	        draw.css('fontWeight', 'bold');
	        draw.animate({backgroundColor: '#3399FF', color : '#FFFFFF'}, 1500);//.animate({'opacity': 'hide'}, 150);
	        //cur_animate_uid = user;
	        //$('#draw_'+user+'').animate({'top': '304px'}, 1000);
	        draw.animate({backgroundColor: 'transparent', color : col}, 500);
	        //draw.animate({backgroundColor: 'transparent', color : col}, 100);	        
	        setTimeout("$('#"+user+"').css('fontWeight', 'normal')", 1750);
	        setTimeout("$('#"+user+"').addClass('transparent')", 1950);
	    }
    }
}

function open_draw_area()
{
	$('.restore_link').css('zIndex', '0');
	$('#draw_div_cont').animate({'opacity': '0'}, 250);
	setTimeout("$('#draw_div_cont').removeClass('hide_cont')", 245);
	$('#draw_div_cont').animate({'opacity': '0.95'}, 500);
}
function close_draw_area()
{
	$('.restore_link').css('zIndex', '11');
	$('#draw_div_cont').animate({'opacity': '0'}, 500);
	setTimeout("$('#draw_div_cont').addClass('hide_cont')", 495);
	$('#draw_div_cont').animate({'opacity': '1'}, 250);
}

function animate_autodraw(count, winner)
{	
	var id;
    
    var u = '';
    if (animate_autodraw.counter < count)
    {
/*        id = cur_ids[Math.floor(Math.random()*cur_ids.length)];
        //alert($('#draw_'+id+'').css('top'));
        $('#draw_'+id+'').animate({'top': '152px'}, 150). animate({'opacity': 'hide'}, 150);
        $('#draw_'+id+'').css({'top': '0px', 'opacity': 'show'});*/
		
		
	    var user = user_conts[Math.floor(Math.random()*user_conts.length)];
	    if (user != '______array')
	    {
	        var draw = $('#'+user);
	        var col = draw.css('color');
	        draw.removeClass('transparent');
	        draw.css('fontWeight', 'bold');
	        draw.animate({backgroundColor: '#EE4570', color : '#FFFFFF'}, 200);//.animate({'opacity': 'hide'}, 150);
	        //cur_animate_uid = user;
	        //$('#draw_'+user+'').animate({'top': '304px'}, 1000);
	        draw.animate({backgroundColor: 'transparent', color : col}, 100);
	        draw.animate({backgroundColor: 'transparent', color : col}, 100);	        
	        setTimeout("$('#"+user+"').css('fontWeight', 'normal')", 1000);
	        setTimeout("$('#"+user+"').addClass('transparent')", 1000);
	    }
    }
    else
    {
    	//u += '<div class="draw' + current + '" id="draw_winner" style="margin-top: -152px; font-weight: bold;" ><img src="' + cur_photos[winner] + '" style="width: 105px; height: 78px; padding-right: 40px;" />' + cur_users[winner] + '</div>';
    	//$('#draw').html(u);
        //$('#draw_winner').animate({'top': '152px'}, 300);
        //alert($('#draw_winner').css('top'));
        var draw = $('#c_user_'+winner);
        clearInterval(adr_timer);
        //$('.last_winners_list').append('<div class="winner_' + draw_from + '" style="display: none;"><img src="' + cur_photos[winner] + '" style="" /><span class="username">'+cur_users[winner]+'</span></div>');
        draw.css('fontWeight', 'bold');
        draw.css('zIndex', '20');
        draw.removeClass('transparent');
		draw.animate({backgroundColor: '#EE4570', color : '#FFFFFF'}, 200);//.animate({'opacity': 'hide'}, 150);
		draw.animate({fontSize: '40px', backgroundColor: '#3399FF', color : '#FFFFFF', lineHeight: '50px', height: '50px', marginTop: '250px', marginLeft: '-200px', width: '400px', left: '50%'}, 2000);
		$('#head_winner').animate({'opacity': 'show'},2000);
		$('.last_winners_list').append('<div class="winner_' + draw_from + '" style="display: none;"><img src="' + cur_photos[winner] + '" style="" /><span class="username">'+cur_users[winner]+'</span></div>');
        last_draw_from = draw_from;
        animate_autodraw.counter = 0;
        if (draw_from != main_draw)
        {		
			draw_from++;
		}
        else
        {
        	
        }
        setTimeout(get_results_of_draw, 5000);
    }
    animate_autodraw.counter++;
}

