preloadImg1 = new Image();
preloadImg1.src='/images/search_focus.gif';
preloadImg2 = new Image();
preloadImg2.src = '/images/download_btn_hover.png';

window.addEvent('domready', function() {
	new Autocompleter.Request.JSON('searchBox', 'http://' + document.domain + '/PopDown/Autocomplete', {
        'postVar': 'q', 
        'delay': 0,
        'autoSubmit': true
    });
});

function adjustInnerDiv(innerDivId) {
	document.getElementById(innerDivId).style.width = (document.getElementById(innerDivId).offsetWidth - 4) + 'px';
	document.getElementById(innerDivId).style.height = (document.getElementById(innerDivId).offsetHeight - 4) + 'px';
}
function adjustContentDiv(contentDivId) {
	document.getElementById(contentDivId).style.width = (document.getElementById(contentDivId).offsetWidth - 24) + 'px';
	document.getElementById(contentDivId).style.height = (document.getElementById(contentDivId).offsetHeight - 24) + 'px';
}
function tabMouseOver(tabDivId) {
	document.getElementById(tabDivId).style.textDecoration = 'underline';
	document.getElementById(tabDivId).style.color = '#00a8b7';
}
function tabMouseOut(tabDivId) {
	document.getElementById(tabDivId).style.textDecoration = 'none';
	document.getElementById(tabDivId).style.color = '#777777';
}
function thumbnailImageMouseOver(linkId) {
	linkId.style.borderColor = '#222222';
}
function thumbnailImageMouseOut(linkId) {
	linkId.style.borderColor = '#FFFFFF';
}
function screenshotsLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function screenshotsLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function demoLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function demoLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function sortLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function sortLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function isolateLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function isolateLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function viewLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function viewLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function searchTermMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function searchTermMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function breadcrumbLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function breadcrumbLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function errorMiscLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function errorMiscLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function reSearch(searchTerm) {
	document.getElementById('searchBox').value = searchTerm;
	document.getElementById('searchBox').focus();
}
function footerLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
	linkId.style.color = '#00a8b7';
}
function footerLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
	linkId.style.color = '#AAAAAA';
}
function typeMenuLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function typeMenuLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function thumbnailItemLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function thumbnailItemLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function detailInfoLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
}
function detailInfoLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
}
function detailDownloadMouseOver(buttonId, linkId) {
	document.getElementById(buttonId).src = '/images/download_btn_hover.png';
	document.getElementById(linkId).style.color = '#222222';
}
function detailDownloadMouseOut(buttonId, linkId) {
	document.getElementById(buttonId).src = '/images/download_btn.png';
	document.getElementById(linkId).style.color = '#75a432';
}
function adminLinkMouseOver(linkId) {
	linkId.style.textDecoration = 'underline';
	linkId.style.color = '#00a8b7';
}
function adminLinkMouseOut(linkId) {
	linkId.style.textDecoration = 'none';
	linkId.style.color = '#777777';
}
function PaginateMenuBar(node, imageDetails, minimumSize, maximumSize, range) {
	var iconsNode = document.createElement('div');
	node.appendChild(iconsNode);
	iconsNode.style.textAlign = 'center';
	iconsNode.style.height = maximumSize + 'px';
	var maximumWidth = 0;
	var scale  = 0;
	var closeTimeout  = null;
	var closeInterval = null;
	var openInterval  = null;
	var images = [];
	var iconNodes = [];
	var iconSizes = [];
	for (var i = 0; i < imageDetails.length; i++) {
		iconNodes[i] = document.createElement('img');
		if (imageDetails[i].name.indexOf('prev') != -1) {
			iconNodes[i].alt = '...previous';
//THIS SECTION SHOULD CHECK THE pageNum VARIABLE TO MAKE SURE THAT IT'S A NUMBER
			iconNodes[i].onclick = function() {window.location = searchPath + '?searchFilter=' + searchFilter + '&searchTerm=' + searchTerm + '&pageNumber=' + (parseInt(pageNum)-1).toString() + '&totalPages=' + totalPages;};
		}
		else if (imageDetails[i].name.indexOf('next') != -1) {
			iconNodes[i].alt = 'next...';
//THIS SECTION SHOULD CHECK THE pageNum VARIABLE TO MAKE SURE THAT IT'S A NUMBER
			iconNodes[i].onclick = function() {window.location = searchPath + '?searchFilter=' + searchFilter + '&searchTerm=' + searchTerm + '&pageNumber=' + (parseInt(pageNum)+1).toString() + '&totalPages=' + totalPages;};
		}
		else {
			var altNum = imageDetails[i].name.substring(imageDetails[i].name.indexOf('-')+1);
			altNum = altNum.substring(0,altNum.indexOf('-'));
			try {
				altNum = parseInt(altNum);
				iconNodes[i].alt = 'page ' + altNum;
				iconNodes[i].id = altNum;
				iconNodes[i].onclick = function() {window.location = searchPath + '?searchFilter=' + searchFilter + '&searchTerm=' + searchTerm + '&pageNumber=' + this.id + '&totalPages=' + totalPages;};
			}
			catch(err) {
				iconNodes[i].alt = '';
			}
		}
		iconNodes[i].style.cursor = 'pointer';
		iconNodes[i].style.position = 'relative';
		iconSizes[i] = minimumSize;
		updateIconProperties(i);
		iconsNode.appendChild(iconNodes[i]);
		if (iconNodes[i].addEventListener) {
			iconNodes[i].addEventListener('mousemove', processMouseMove, false); 
			iconNodes[i].addEventListener('mouseout', processMouseOut, false);
			iconNodes[i].addEventListener('click', imageDetails[i].onclick, false);
		}
		else if (iconNodes[i].attachEvent) {
			iconNodes[i].attachEvent('onmousemove', processMouseMove);
			iconNodes[i].attachEvent('onmouseout', processMouseOut);
			iconNodes[i].attachEvent('onclick', imageDetails[i].onclick);
		}
		for (var j = 0; j < imageDetails[i].sizes.length; j++) {
			var image = document.createElement('img');
			image.setAttribute('src',imageDetails[i].name + imageDetails[i].sizes[j] + imageDetails[i].extension);
			images.push(image);
		}
	}
	function updateIconProperties(index) {
		var size = minimumSize + scale * (iconSizes[index] - minimumSize);
		var sizeIndex = 0;
		while (imageDetails[index].sizes[sizeIndex] < size && sizeIndex + 1 < imageDetails[index].sizes.length) {
			sizeIndex++;
		}
		if (size == maximumSize) {
			iconNodes[index].setAttribute('src',imageDetails[index].name + maximumSize + '-full' + imageDetails[index].extension);
		}
		else {
			iconNodes[index].setAttribute('src',imageDetails[index].name + imageDetails[index].sizes[sizeIndex] + imageDetails[index].extension);
		}
		iconNodes[index].setAttribute('width',  size);
		iconNodes[index].setAttribute('height', size);
		iconNodes[index].style.marginTop = (maximumSize - size) + 'px';
	}
	function processMouseMove(e) {
		window.clearTimeout(closeTimeout);
		closeTimeout = null;
		window.clearInterval(closeInterval);
		closeInterval = null;
		if (scale != 1 && !openInterval) {
			openInterval = window.setInterval(function() {
				if (scale < 1) scale += 0.125;
				if (scale >= 1) {
					scale = 1;
					window.clearInterval(openInterval);
					openInterval = null;
				}
				for (var i = 0; i < iconNodes.length; i++) {
					updateIconProperties(i);
				}
			},
			1);
		}
		if (!e) e = window.event;
		var target = e.target || e.srcElement;
		var index = 0;
		while (iconNodes[index] != target) index++;
		var across = (e.layerX || e.offsetX) / iconSizes[index];
		if (across) {
			var currentWidth = 0;
			for (var i = 0; i < iconNodes.length; i++) {
				if (i < index - range || i > index + range) {
					iconSizes[i] = minimumSize;
				}
				else if (i == index) {
					iconSizes[i] = maximumSize;
				}
				else if (i < index) {
					iconSizes[i] = minimumSize + Math.round((maximumSize - minimumSize - 1) * (Math.cos((i - index - across + 1) / range * Math.PI) + 1) / 2);
					currentWidth += iconSizes[i];
				}
				else {
					iconSizes[i] = minimumSize + Math.round((maximumSize - minimumSize - 1) * (Math.cos((i - index - across) / range * Math.PI) + 1) / 2);
					currentWidth += iconSizes[i];
				}
			}
			if (currentWidth > maximumWidth) maximumWidth = currentWidth;
			if (index >= range && index < iconSizes.length - range && currentWidth < maximumWidth) {
				iconSizes[index - range] += Math.floor((maximumWidth - currentWidth) / 2);
				iconSizes[index + range] += Math.ceil((maximumWidth - currentWidth) / 2);
			}
			for (var i = 0; i < iconNodes.length; i++) updateIconProperties(i);
		}
	}
	function processMouseOut() {
		if (!closeTimeout && !closeInterval) {
			closeTimeout = window.setTimeout(function() {
				closeTimeout = null;
				if (openInterval) {
					window.clearInterval(openInterval);
					openInterval = null;
				}
				closeInterval = window.setInterval(function() {
					if (scale > 0) scale -= 0.125;
					if (scale <= 0) {
						scale = 0;
						window.clearInterval(closeInterval);
						closeInterval = null;
					}
					for (var i = 0; i < iconNodes.length; i++) {
						updateIconProperties(i);
					}
				},
				20);
			},
			100);
		}
	}
}

