var minis = Array();

function vote(vote_up, vote_down) {
	if (minis.length>3) {
		tmp = minis.shift();
	}
	minis.push(vote_up);
	
	
	$('votedfor').innerHTML = '';
	for (i = 0;i<minis.length;i++) {
		//alert('<a id="mini_'+minis[i]+'" href="uid/'+minis[i]+'/">'+'<img src="mini.php?uid='+minis[i]+'" alt="" />'+'</a>');
	 $('votedfor').innerHTML += '<a id="mini_'+minis[i]+'" href="uid/'+minis[i]+'/">'+'<img src="mini.php?uid='+minis[i]+'" alt="" />'+'</a>';
	}
	new Ajax.Request( 
		'vote.php',
		{
			postBody: 'up='+vote_up+'&down='+vote_down
		}	
	
	);

}


function abuse(uid) {
	new Ajax.Request( 
		'abuse.php',
		{
			postBody: 'uid='+uid,
			onComplete: function(t) {		
				if (t.responseText == 'OK') {
					$('status').innerHTML = 'Bilden abuseanmäld tillsammans med ditt IP';
				} else{
					$('status').innerHTML = 'Bilden EJ abuseanmäld, ett fel uppstod';
				}
				new Effect.Highlight($('status'));
			}
		}	
	
	);

}


function loadImages() {
	$('status').innerHTML = '<img src="images/loading.gif" alt="" /> Röstar...';
	new Ajax.Request(
		'get_images.php',
		{
			postBody: 'images='+location.hash,
			onComplete: function(t) {
				//alert(t.responseText);
				eval('res='+t.responseText);
				
				$('status').innerHTML = '';
				location.hash = escape(res.uid1 + ',' + res.uid2);
				$('img1').setAttribute('src','src/'+res.name1);
				$('infoimg1').setAttribute('href','uid/'+res.uid1+'/');
				
				$('ab1').onclick = function() {
					abuse(res.uid1);
					return false;
				}
				
				$('imglnk1').onclick = function() {
					vote(res.uid1,res.uid2);
					location.hash='#';
					loadImages()
					return false;
				}
				
				$('imglnk1b').onclick = $('imglnk1').onclick;
				
				$('img2').setAttribute('src','src/'+res.name2);
				$('infoimg2').setAttribute('href','uid/'+res.uid2+'/');
				
				$('imglnk2').onclick = function() {
					vote(res.uid2,res.uid1);
					location.hash='#';
					loadImages();
					return false;
				}
				
				$('imglnk2b').onclick = $('imglnk2').onclick;
				
				$('ab2').onclick = function() {
					abuse(res.uid2);
					return false;
				}
				
				// onmouseover-grejer
				cX = findPosX($('img1'));
				cY = findPosY($('img1'));
				
				$('info1').style.left = cX+15+'px';
				$('info1').style.top = cY+20+'px';

				cX = findPosX($('img2'));
				cY = findPosY($('img2'));
			
				$('info2').style.left = cX+98+'px';
				$('info2').style.top = cY+20+'px';

				
				// google analytics
				pageTracker._trackPageview("/imageReload");
			}
		}
		
	);
}


function checkFade(ev,ref,fade_ref) {
	/*
	console.log(ref);
	console.log(ev);
	console.log('ref.offsetLeft+ref.clientWidth: '+(ref.offsetLeft+ref.clientWidth));
	console.log('ref.offsetLeft: ' + ref.offsetLeft);
	console.log('ref.offsetTop+ref.clientHeight: '+(ref.offsetTop+ref.clientHeight));
	console.log('ref.offsetTop: '+ref.offsetTop);
	*/
	cX = findPosX(ref);
	cY = findPosY(ref);
	
	if ((ev.clientX > cX+ref.clientWidth) || 
			(ev.clientX < cX) || 
			(ev.clientY > cY+ref.clientHeight) || 
			(ev.clientY < cY)) {
				//console.log('fadar');
				//new Effect.Fade(fade_ref);
				fade_ref.hide();
	} else {
		//console.log('fadar ej');
	}
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}
