function ChangeStars(itemId, votes,big)
{
	
    if (!getElmById('star' + itemId + '1'))
	{
		return;
	}
	
    if(big == null){
        zero='00';
        one='11';
        two='22';
    }else{
        zero='0';
        one='1';
        two='2';        
    }
	for (var i = 1; i <= 5; ++i)
	{
		getElmById('star' + itemId + String(i)).src = 'http://www.addict3d.org/content/d/s' + String((votes >= i) ? two : ((i - votes) < 1 ? one : zero)) + '.gif'
	};
}

var rateReqObject = null;
var rateItemId = null;

function RateDownload(itemId, vote)
{	
	
	if (rateReqObject != null || rateItemId != null)
	{
		return;
	}
	
	rateItemId = itemId;
	rateReqObject = createReqObject()
	rateReqObject.onreadystatechange = rateStateChange;
	rateReqObject.open("GET", "http://www.addict3d.org/ajax_handler.php?a=ratedl&iid=" + rateItemId + "&vote=" + vote, true);
	rateReqObject.send(null);
}

function returnStarsHTML(votes)
{
    var innerHTML = '';
    
   
    for (var i = 1; i <= 5; ++i)
    {
	    innerHTML += '<img src="http://www.addict3d.org/content/d/s' +
		    String((votes >= i) ? two : ((i - votes) < 1 ?
		    one : zero)) + '.gif" />';
    }
    
    innerHTML += '<br />' + String(votes).substring(0, 3) + '/5.0';
    
    return innerHTML;
}

function staticateStars(rateItemId, votes)
{
    if (getElmById('td' + rateItemId) == null || getElmById('value' + rateItemId) == null)
    {
        return;
    }
    
    if (!votes)
    {
        votes = Number(getElmById('value' + rateItemId).innerHTML);
    }
    
	getElmById('td' + rateItemId).innerHTML = returnStarsHTML(votes);
	getElmById('td' + rateItemId).onmouseout = '';
}

function removeMouseActions(rateItemId)
{
    getElmById('td' + rateItemId).innerHTML
};

function rateStateChange()
{
	// if xmlhttp shows "loaded"
	if (rateReqObject.readyState == 4)
	{
		if (rateReqObject.status == 200)
		{
			if (rateReqObject.responseText.indexOf('ERROR') != -1)
			{
				alert('Cannot rate twice!');
			}
			else
			{
			    // Retrieve the current votes from the webserver
				var votes = Number(rateReqObject.responseText);
				staticateStars(rateItemId, votes);
				createCookie('rate' + rateItemId, 'da', 0);
				
				alert('Thank you for voting!');
			}
		}
		
		rateReqObject = null;
		rateItemId = null;
	} 
}

var voteReqObject = null;
var voteItemId = null;

function Vote(itemId, vote)
{
	if (voteReqObject != null || voteItemId != null)
	{
		return;
	}
	
	voteItemId = itemId;
	voteReqObject = createReqObject()
	voteReqObject.onreadystatechange = voteStateChange;
	voteReqObject.open("GET", "http://www.addict3d.org/ajax_handler.php?a=rate&iid=" + voteItemId + "&vote=" + vote, true);
	voteReqObject.send(null);
}

function voteStateChange()
{
	// if xmlhttp shows "loaded"
	if (voteReqObject.readyState == 4)
	{
		if (voteReqObject.status == 200)
		{
			if (voteReqObject.responseText.indexOf('ERROR') != -1)
			{
				alert('Cannot vote twice!');
			}
			else
			{
				var votes = Number(voteReqObject.responseText);
				getElmById('bad' + voteItemId).onclick = function() {};
				getElmById('bad' + voteItemId).src = 'http://www.addict3d.org/content/vote_bred_disabled.gif';
				getElmById('good' + voteItemId).onclick = function() {};
				getElmById('good' + voteItemId).src = 'http://www.addict3d.org/content/vote_bgreen_disabled.gif';
				
				if (votes == 0)
				{
					getElmById('vote' + voteItemId).className = "neutral";
					getElmById('vote' + voteItemId).innerHTML = "0";
				}
				else if (votes > 0)
				{
					getElmById('vote' + voteItemId).className = "green";
					getElmById('vote' + voteItemId).innerHTML = "+" + String(votes);
				}
				else
				{
					getElmById('vote' + voteItemId).className = "red";
					getElmById('vote' + voteItemId).innerHTML = String(votes);
				}
				
				createCookie('vote' + voteItemId, "da", 0);
			}
		}
		
		voteReqObject = null;
		voteItemId = null;
	} 
}

function getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  
  while((inputObj = inputObj.offsetParent) != null)
  {
  	if(inputObj.tagName!='HTML')
  	{
  		returnValue += inputObj.offsetTop;
  	}
  }
  
  return returnValue;
}

function getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  
  while((inputObj = inputObj.offsetParent) != null)
  {
  	if(inputObj.tagName!='HTML')
  	{
  		returnValue += inputObj.offsetLeft;
  	}
  }
  
  return returnValue;
}

function resizeEffect(currWidth, currHeight, addToWidth, addToHeight, targetWidth, targetHeight)
{
	var thumbImage = getElmById('thumbImage');
	currWidth += addToWidth;
	currHeight += addToHeight;
	
	if ((currWidth >= targetWidth) || (currWidth >= targetWidth))
	{
		thumbImage.style.width = String(targetWidth) + 'px';
		thumbImage.style.height = String(targetHeight) + 'px';
	}
	else
	{
		thumbImage.style.width = String(currWidth) + 'px';
		thumbImage.style.height = String(currHeight) + 'px';
		
		// Continue looping
		setTimeout('resizeEffect(' + currWidth + ', ' + currHeight + ', ' +
			addToWidth + ', ' + addToHeight + ', ' +
			targetWidth + ', ' + targetHeight + ')', 20);
	}
}

function resizeThumb(thumbId, width, height)
{
	var thumbSlate = getElmById('thumbSlate');
	var thumbImage = getElmById('thumbImage');
	var thumbLink = getElmById('thumbLink');	

	thumbImage.src = lastThumbObject.src;
	thumbImage.style.width = String(lastThumbObject.width) + 'px';
	thumbImage.style.height = String(lastThumbObject.height) + 'px';
	thumbSlate.style.left = String(getLeftPos(lastThumbObject) - 5) + 'px';
	thumbSlate.style.top = String(getTopPos(lastThumbObject) - 5) + 'px';
	thumbSlate.style.display = 'block';
	thumbLink.href = 'http://www.addict3d.org/downloads/' + thumbId + '/';
	
	resizeEffect(lastThumbObject.width, lastThumbObject.height,
			(width - lastThumbObject.width) / 5, (height - lastThumbObject.height) / 5,
			width, height);
		
	clearThumbVariables();
}

var lastTimerId = null;
var lastThumbObject = null;
var lastLinkCancelTimerId = null;

function startResize(thumbId, width, height)
{
	if (lastTimerId == null)
	{
		lastTimerId = setTimeout('resizeThumb(' + thumbId + ', ' + width + ', ' + height + ')', 500);
		lastThumbObject = getElmById('thumb' + String(thumbId));
		lastThumbObject.style.border = '1px white solid';
	};
}

function cancelResize()
{
	if (lastTimerId != null)
	{
		clearTimeout(lastTimerId);
		clearThumbVariables();
	}
}

function delayedCloseSlate()
{	
	if (lastLinkCancelTimerId == null)
	{
		lastLinkCancelTimerId = setTimeout('hideSlate()', 100);
	}
}

function cancelDelayedCloseSlate()
{
	if (lastLinkCancelTimerId != null)
	{
		clearTimeout(lastLinkCancelTimerId);
		lastLinkCancelTimerId = null;
	}
}

function hideSlate()
{
	getElmById('thumbSlate').style.display = '';
	clearThumbVariables();
}

/**
  * Clear the global variables
  */
function clearThumbVariables()
{	
	lastTimerId = null;
	lastLinkCancelTimerId = null;
	
	if (lastThumbObject != null)
	{
		lastThumbObject.style.border = '1px black solid';
	}
}

//function submitSearch(baseUrl)
//{
//    var baseUrl=baseUrl.split("-");
//    location.href = baseUrl[0] + ((getElmById('advancedSearch').checked) ?
//        'A-' : '-') + baseUrl[1]+'/' + getElmById('sb_tf').value;
//}

function submitSearch(baseUrl)
{
    var baseUrl=baseUrl.split("-");
    location.href ='http://www.addict3d.org/'+baseUrl[0] + ((getElmById('advancedSearch').checked) ?
      'A-' : '-') + baseUrl[1]+'/' + getElmById('sb_tf').value;
}

function searchKP(baseUrl, charCode)
{
    if (charCode == 13)
    {
        submitSearch(baseUrl);
    };
}

/* HInfras lib by Hostile (c) 2007 */
function getElmById(aID)
{
	var retVal = null;

	if (document.getElementById)
	{
		retVal = document.getElementById(aID);
	}
	else if (document.layers)
	{
		retVal = document.layers[aID];
	}
	else if (document.all)
	{
		retVal = document.all[aID];
	}

	return retVal;
}

function createReqObject()
{
	var reqObject;
	
	if (window.XMLHttpRequest)
	{
		reqObject = new XMLHttpRequest();			
	}
	else if (navigator.userAgent.toLowerCase().indexOf('msie 5') == -1)
	{
		reqObject = new ActiveXObject("Msxml2.XMLHTTP");
	}
	else
	{
		reqObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return reqObject;
}