var openPopup = ""; //Is always PHP-URL-encoded name
var popupBox;

function i() {
	//MSIE currently has atrocious handling of the popup panel
	if (navigator.userAgent.indexOf('MSIE') == -1) {
		document.searchform.j.value='1';
		document.getElementById('indexadvlink').href='advanced.php?j=1';
	}
	document.searchform.idxsearchedit.focus();
}
function g(l) {
	window.location.replace('http://commons.wikimedia.org/wiki/Image:'+l);
}
function l(i) {
	var loc = window.location.href.replace(/[\?&]il=[a-z][a-z]/g,'');
	if (loc.indexOf('?') != -1) {
		loc = loc + '&il=' + i;
	} else {
		loc = loc + '?il=' + i;
	}
	window.location.replace(loc);
}
function h() { document.getElementById('lclbtn').style.display = 'none'; }

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//BEGIN POPUP CODE

function showPopup(i) {
	//i is phpUrlEncoded
	//popupData: Icons, Cats, Width, Height, ThumbWidth, MediaType, ThumbHeight, prettyName
	
	//Show the box
	document.getElementById('jsPopupHead').style.display = 'block';
	document.getElementById('jsPopupPreview').style.display = 'block';
	document.getElementById('jsPopupIcons').style.display = 'block';
	document.getElementById('jsPlzHover').style.display = 'none';
	
	//Populate the box
	//'<a href="http://commons.wikimedia.org/wiki/Image:' + i + '">' + 
	document.getElementById('jsFilename').innerHTML  = popupData[i][7].replace(/[_\-]/g, ' ');
	document.getElementById('jsPopupIcons').innerHTML = popupData[i][0];
	var catsInnerHTML = langNoCategories;
	if (trim(popupData[i][1]) != "") {
		catsInnerHTML = "<strong>" + langCatPrefix + "</strong><br /><ul>";
		var cats = popupData[i][1].split(", ");
		for (var k = 0; k < cats.length; k++) {
			catsInnerHTML  += "<li>" + cats[k] + "</li>";
		}
		catsInnerHTML += "</ul>";
	}
	document.getElementById('jsPopupCats').innerHTML = catsInnerHTML;
	if ((popupData[i][4] != 0) && (popupData[i][6] != 0)) {
		document.getElementById('jsPopupPreview').innerHTML = '<a href="http://commons.wikimedia.org/wiki/Image:' + i + '">'
		+"<img src=\"http://commons.wikimedia.org/w/thumb.php?f=" + i + "&w="+popupData[i][4]
		+"\" alt=\"Preview\" width=\""+popupData[i][4]+"\" height=\""+popupData[i][6]+"\" /></a>";
		var dimension = popupData[i][2] + "&times;" + popupData[i][3] + " ";
	} else {
		var dimension = "";
		document.getElementById('jsPopupPreview').innerHTML = langNoPreview;
	}
	document.getElementById('jsPopupSpecs').innerHTML =  dimension + popupData[i][5];
	
	openPopup = i;
}
function closePopup() {
	document.getElementById('jsPopupHead').style.display = 'none';
	document.getElementById('jsPopupPreview').style.display = 'none';
	document.getElementById('jsPopupIcons').style.display = 'none';
	document.getElementById('jsPlzHover').style.display = 'block';
	document.getElementById('jsPopupCats').innerHTML = '';
	
	openPopup = "";
}
function setUpPopups() {
	//Assign events to all the thumbs
	var thumbs = document.getElementsByTagName("div");
	for (var ti = 0; ti < thumbs.length; ti++) {
		if (thumbs[ti].className == "tm") {
			thumbs[ti].onmouseover = tmOnMouseOver;
			thumbs[ti].onmouseout = tmOnMouseOut;
		}
	}
	//Launch stuff
	popupBox = document.getElementById('jsPopupDetails');
}
function tmOnMouseOver() {
	if (openPopup != this.id) {
		showPopup(this.id);
	}
}
function tmOnMouseOut() {
	closePopup();
}