var steps = 20;
var bgPosX = -715;
var voteCount = 1;

function getOpponents()
{
	$('status').set('html', voteCount+' / '+steps);
	$('progressbar').setStyle('backgroundPosition', bgPosX+'px 0');
	
	var req = new Request.JSON(
		{
			link: 'ignore',
			url: '/HotOrNots/getOpponents/?'+Math.random(),
			onSuccess: function(jres)
			{
				if (jres.length > 0)
				{
					var arr_images = new Array();
					
					jres.each(function(obj, n)
					{
						arr_images.push(obj.src);
					});
					
					new Asset.images(arr_images,
					{
						onComplete: function()
						{
							$$('div.honWrap').each(function(item, i)
							{
								if ($chk('fav_'+i))
								{
									if (voteCount > 1)
									{
										$('fav_'+i).getElement('img').src = ($('fav_'+i).getElement('img').title == '') ? '/img/buttons/profil_merken.gif' : '/img/buttons/profil_merken_dis.gif';
										new Fx.Tween('fav_'+i).start('opacity', 0,1);
									}
									$('fav_'+i).getElement('img').id = jres[i].profile_id;
								}
								
								var div = new Element('div',
									{
										'class': 'honItem',
										'events':
										{
											'click': function()
											{
												if (voteCount < steps)
												{
													new Request(
													{
														link: 'ignore',
														url: '/HotOrNots/vote/'+jres[i].crs,
														onRequest: function()
														{
															$$('div.honItem').each(function(honItem, h)
															{
																honItem.dispose();
																new Fx.Tween('fav_'+h).start('opacity', 1, 0);
															});
														},
														onSuccess: function()
														{
															voteCount++;
															bgPosX += (700 / steps);
															
															getOpponents();
														}
													}).get();
												}
												else
												{
													document.location.href = '/HotOrNots/scoring';
												}
											}
										}
									});
								
								var img = new Element('img',
									{
										'src': jres[i].src,
										'width':350,
										'height':350,
										'border':0
									});
								
								img.inject(div);
								div.inject(item);
							});
						}
					});
				}
			}
		});
		
	req.get();
}

window.addEvent('domready', function()
{
	getOpponents();
});
