var searchText = "Search...";

function focusSearch(id) {	
	if (document.getElementById(id).value == searchText) {
		//document.getElementById(id).style.fontStyle = "normal";
		document.getElementById(id).style.color = "#aaa";
		document.getElementById(id).value = '';
	}
}

function blurSearch(id, ignore) {
	if (document.getElementById(id).value.length == 0 || ignore) {
		//document.getElementById(id).style.fontStyle = "italic";
		document.getElementById(id).style.color = "#444";
		document.getElementById(id).value = searchText;
	}
	fadeOutSearch();
}

function highlightSearch(id) {
	document.getElementById(id).style.background = "#1d1d1d";
	document.getElementById(id).style.color = "#555";
	document.getElementById(id).style.border = "1px solid #262626";
}

function unhighlightSearch(id) {
	document.getElementById(id).style.background = "#1a1a1a";
	document.getElementById(id).style.color = "#444";
	document.getElementById(id).style.border = "1px solid #222";
}

function show(id) {
	document.getElementById(id).style.display = "block";
}

function hide(id) {
	document.getElementById(id).style.display = "none";
}

function submitSearch(q) {
	var string = q.replace(/\s/g, "+");
	window.location = "/search/" + string;
}

function search(q, scope) {
	resultDiv = document.getElementById('searchResults');
	if (q.length < 2) {
		closeSearchResults();
	}
	else {
		var req = "action=search&q=" + q + "&scope=" + scope;
		var xmlHTTP = new XMLHttpRequest();
		xmlHTTP.open("POST", "/admin/adminprocess.php", true);
		
		xmlHTTP.onreadystatechange = function(){
			if (xmlHTTP.readyState == 4) {
				if (resultDiv) {
					openSearchResults();
					resultDiv.innerHTML = xmlHTTP.responseText;
				}
			}
		};
		
		xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHTTP.setRequestHeader("Content-length", req.length);
		xmlHTTP.setRequestHeader("Connection", "close");
		xmlHTTP.send(req);
	}
}

function incrementDlCounter(id) {
	var req = "action=incrementdlcounter&id=" + id;
	var xmlHTTP = new XMLHttpRequest();
	xmlHTTP.open("POST", "/admin/adminprocess.php", true);
	
	xmlHTTP.onreadystatechange = function(){
		if (xmlHTTP.readyState == 4) {
			// Do nothing			
		}
	};
	
	xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHTTP.setRequestHeader("Content-length", req.length);
	xmlHTTP.setRequestHeader("Connection", "close");
	xmlHTTP.send(req);	
}

var infoFadeSpeed = 800;				// Info/caption box fade time period in milliseconds
var infoFreq = 10; 						// How many frames in this time period
var infoFadeOutFreq = infoFreq;			// How many frames in this time period during fade out (default is same as infoFreq, change if needed)
var infoOpacity = 1;					// Opacity of each slide's caption box.

function fadeOut(id, level) {
	var inverseOpacity;
	if (id == 'ssNav')
		inverseOpacity = 1 - (parseFloat(1 / infoFadeOutFreq) * level);
	else
		inverseOpacity = 1 - (parseFloat(infoOpacity / infoFadeOutFreq) * level);
	var fadeOut = document.getElementById(id);
	
	if (fadeOut) {
		fadeOut.style.opacity = inverseOpacity;
		fadeOut.style.filter = "alpha(opacity = " + (inverseOpacity * 100) + ")";
	}
	
	if (level < infoFreq) {
		var nextLevel = parseInt(level) + 1;
		setTimeout("fadeOut('" + id + "', '" + nextLevel + "')", infoFadeSpeed / infoFreq);
	}
	else {
		if (fadeOut)
			fadeOut.style.display = "none";
		return;
	}	
}

var searchResultsOpen = 0;

function openSearchResults() {
	if (document.getElementById('siteSearch').value != searchText && document.getElementById('siteSearch').value.length > 1) {
		searchResultsOpen = 1;
		document.getElementById('searchResults').style.display = "block";
	}
}

function closeSearchResults() {
	searchResultsOpen = 0;
	document.getElementById('searchResults').style.display = "none";
}

function fadeInSearch() {
	if (!searchResultsOpen)
		genInterFade('search2', 'search', 1, 1);
}

function fadeOutSearch(ignore) {
	//alert(document.getElementById('siteSearch').value + " != " + searchText + " -> " + (document.getElementById('siteSearch').value != searchText));
	if (document.getElementById('siteSearch').value == searchText || document.getElementById('siteSearch').value.length == 1 || ignore) {
		if (document.getElementById('siteSearch').value.length == 1)
			blurSearch('siteSearch');
			
		genInterFade('search', 'search2', 1, 1);
		closeSearchResults();		
	}
}

function forceFadeOutSearch() {
	fadeOutSearch(1);
	blurSearch('siteSearch', 1);
}

var bnFadeSpeed = 250;			// Fade time period in milliseconds (1000 ms = 1 second)
var bnFreq = 5; 				// How many frames in this time period (higher number will produce "smoother" fades, but will also be rougher on the browser)


function genInterFade(fadeInId, fadeOutId, level, hide) {
	
	var opacity = parseFloat(1 / bnFreq) * level;
	var inverseOpacity = 1 - (parseFloat(1 / bnFreq) * level);
	var fadeIn = document.getElementById(fadeInId);
	var fadeOut = document.getElementById(fadeOutId);	

	if (fadeIn) {
		if (opacity > '0') 
			fadeIn.style.display = "block";
		
		fadeIn.style.opacity = opacity;
		fadeIn.style.filter = "alpha(opacity = " + (opacity * 100) + ")";
	}
	
	if (fadeOut) {
		fadeOut.style.opacity = inverseOpacity;
		fadeOut.style.filter = "alpha(opacity = " + (inverseOpacity * 100) + ")";
	}
	
	if (level < freq) {
		var nextLevel = parseInt(level) + 1;
		setTimeout("genInterFade('" + fadeInId + "', '" + fadeOutId + "', '" + nextLevel + "', '" + hide + "', '" + noFadeOut + "')", bnFadeSpeed / bnFreq);
	}
	else {
		if (hide == 1 && fadeOut) {
			fadeOut.style.display = "none";
		}
		return;
	}	
}

function viewSlide(id) {
	var req = "id=" + id;	
	var xmlHTTP = new XMLHttpRequest();		
	xmlHTTP.open("POST", "/admin/getslide.php", true);	
	
	xmlHTTP.onreadystatechange = function() {
		if (xmlHTTP.readyState == 4) {	
			if (xmlHTTP.responseText.length > 0) {	
				if (document.getElementById('viewSlide')) {
					document.getElementById('viewSlide').innerHTML = "<div class=\"slideCloseLink\"><a href=\"javascript:disablePopup();\"><img src=\"/images/close.png\"/></a></div>" + xmlHTTP.responseText;
					centerPopup();
					loadPopup();
				}
			}			
		}		
	};
		
	xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHTTP.setRequestHeader("Content-length", req.length);
	xmlHTTP.setRequestHeader("Connection", "close");
	xmlHTTP.send(req);
}

function makeBalloon(divId, linkText) {
	var div = document.getElementById(divId);
	div.className = "explanation";
	div.innerHTML = "<a href=\"javascript:hide('" + divId + "');\" class=\"closeExplanationLink\" onClick=\"document.getElementById('" + divId + "Link').className='explanationLink';\">Close</a>" + div.innerHTML;
}
