// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Hote array
var hote;

var randomHote;

function setRandomHote(){
	var chosenHote = getChoseHote();

	if(chosenHote){
		randomHote = chosenHote;
	}
	else{
		randomHote = hote.random();
	}
}

function getChoseHote(){

    if(typeof(urlParam["id_hote"]) != "undefined"){

    	for (var i = 0; i < hote.length; i++) {
    		if(urlParam["id_hote"] == hote[i]["id"]){
    			return hote[i];
    		}
		}
    }

	return 0;
}

function setBigImage(imageID){
	try {
	  document.getElementById(imageID).onerror = function(){
	   var syn = (!HOTE_IS_GAY || location.href.match(/hetero/)) ? 'hetero' : 'gay';
	   document.getElementById(imageID).src = 'http://media.eurolive.com/hote/images/default/big/nopic_'+syn+'.gif';
    }
		document.getElementById(imageID).src = randomHote['image'];
	}
	catch(e){}

	document.getElementById(imageID).alt = randomHote['name'];
	document.getElementById(imageID).title = randomHote['name'];

	imageLinkID = imageID + "_link";

	if(urlParam["site_url"] == "info"){
		document.getElementById(imageLinkID).href = HOTE_INFO_URL + randomHote['id'] + euroParams;
	}
	else {
		document.getElementById(imageLinkID).href = HOTE_INDEX_URL + euroParams;
	}
}

function setThumbs(imagePrefix,maxImages){

		var imageID = null;
		var i=0;

		while (i < maxImages){
			imageID = imagePrefix + "_" + i;
			imageLinkID = imagePrefix + "_link_" + i;

		  //NEW: hide the whole link with the image
		  eval('\
		  document.getElementById(imageID).onerror = function(){\
		    document.getElementById("'+imageLinkID+'").style.display = "none";\
      }\
      ');
			try {
				document.getElementById(imageID).src = randomHote['thumbs'][i];
			}
			catch (e) {
				// No thumb - hide the picture
				document.getElementById(imageID).style.display = "none";
			}

			try {
				document.getElementById(imageID).alt = randomHote['name'];
				document.getElementById(imageID).title = randomHote['name'];

				if(urlParam["site_url"] == "info"){
					document.getElementById(imageLinkID).href = HOTE_INFO_URL + randomHote['id'] + euroParams;
				}
				else {
					document.getElementById(imageLinkID).href = HOTE_INDEX_URL + euroParams;
				}
			}
			catch(e){
				// No name, id (shouldnt happen) alert(e);
			}

			i++;
		}
}