function CallAhah(url, method, dest, cursr) {	// url = page you want to call, method = GET or POST, dest = id of element you want the response to be in
	var ajaxPost;
	
	if(method == null || method == "") {
		method = "GET";
	}
	
	if(dest == "") {
		dest = null;
	}
	
	try	{
		// Initialize all current browsers
		ajaxPost = new XMLHttpRequest();
	}
	catch(tryms) {
		try	{
			// Initialize Internet Explorer
			ajaxPost = new ActiveXObject('msxml2.XMLHTTP');
		}
		catch(tryotherms) {
			try {
				// Initialize older Internet Explorer
				ajaxPost = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(failed){
				alert('XmlHttp not supported');
				return false;
			}
		}
	}
	
	if (url.indexOf("?") >= 0) {
		url = url + "&rand=" + getRandom();
	} else {
		url = url + "?rand=" + getRandom();
	}
	
	var ReqParams = new String("");
	
	if (url.indexOf("?") >= 0 && method.toUpperCase() == "POST") {
		ReqParams = url.substring(url.indexOf("?") + 1, url.length);
		url = url.substring(0, url.indexOf("?"));
	}
	
	ajaxPost.onreadystatechange=function(){
		if(ajaxPost.readyState == 4){
			if(ajaxPost.status == 200){
				if(dest != null) {
					document.getElementById(dest).innerHTML = ajaxPost.responseText;
					if(dest == "LightBox") {
						showLightBox();	
					}
				} else {
					eval(ajaxPost.responseText);
				}
				if(cursr != null && cursr != "") {
					document.body.style.cursor = cursr;
				}
			} else {
				if(dest != null) {
					alert("Ajax Error!");
				}
			}
		}
	}
	
	ajaxPost.open(method.toUpperCase(), url, true);
	if (method.toUpperCase() == "POST") {
		ajaxPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	ajaxPost.send(ReqParams);
	
	return true;
}

/******************************************************************/
// light box functions
function doLightBox(url, dl, dl_url) { // pass the url and if its a download, flag the dl to true (dl = true), pass the download url to the function if we are downloading
	// create grayed out div for underneath lightbox
	lbop = document.createElement("div");
	lbop.id = "OpacityLB";
	lbop.className = "grayOpacity";
	
	// create the lightboxcont layer
	lbc = document.createElement("div");
	lbc.id = "LightBoxCont";
	lbc.className = "lightBoxCont";
	
	// create the lightbox layer
	lb = document.createElement("div");
	lb.id = "LightBox";
	lb.className = "lightBox";
	lb.style.display = "none";
	
	//add the lightbox to lightboxcont layer
	lbc.appendChild(lb);
	// add the opacity to the document
	document.body.appendChild(lbop);
	// add lightbox to the document
	document.body.appendChild(lbc);
	// call the page that will populate the light box trough an AHAH call
	CallAhah(url, "GET", "LightBox");
	// do the call to show the lightbox
	//showLightBox();
	// are we downloading a song?
	if (dl != null && dl == true) {
		CallAhah(dl_url, "POST");
	}
	return false;
}

function showLightBox() {
	var scrollTop = 0;
	
	// resize the gray opacity layer to the full width and height that is scollable in the browser
	lbop = document.getElementById("OpacityLB");
	if (lbop != null) {
		lbop.style.width = document.documentElement.scrollWidth + "px";
		lbop.style.height = document.documentElement.scrollHeight + "px";
	}
	
	// resize the lightbox layer to the width and height to the client area that is viewable/visible
	lbc = document.getElementById("LightBoxCont");
	if (lbc != null) {
		lbc.style.width = document.documentElement.clientWidth + "px";
		lbc.style.height = document.documentElement.clientHeight + "px";
		// move the lightbox layer off the client viewable area
		lbc.style.left = document.documentElement.clientWidth + "px";
	}
	
	if (lbop != null) {
		// display the gray opacity layer on the page
		lbop.style.display = "block";
	}
	
	if (lbc != null) {
		// move the lightbox layer at the top left corner, change its size, and then display it
		lbc.style.left = "0px";
		lbc.style.display = "block";
	}
	
	lb = document.getElementById("LightBox");
	if (lb != null) {
		lb.style.display = "block";
		if (self.pageYOffset) {
			scrollTop = window.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scrollTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scrollTop = document.body.scrollTop;
		}
		
		if (lb.offsetHeight <= document.documentElement.clientHeight) {
			lb.style.top = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop) + "px";
		} else {
			lb.style.top = parseInt(scrollTop) +"px";
		}
	}
	
	lastScrollTop = scrollTop;
	
	// add events to window
	if (window.addEventListener) { // FF
		window.addEventListener("scroll", scrolled, false);
	} else if (window.attachEvent) { // IE
		window.attachEvent("onscroll", scrolled);
	}
	
	return false;
}

var lastScrollTop = 0;

function scrolled() {
	var scrollTop = 0;
	var moveLightbox = true;
	var lb = null;
	
	// get the lightbox
	lb = document.getElementById("LightBox");
	if (lb != null) {
		if (window.pageYOffset) {
			scrollTop = window.pageYOffset;
		} else {
			scrollTop = document.documentElement.scrollTop;
		}
		if (parseInt(scrollTop) > parseInt(lastScrollTop)) {
			// Scrolling down...
			if (lb.offsetHeight <= parseInt(document.documentElement.clientHeight)) {
				// center vertically
				position = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop);
			} else if ((parseInt(getNumericPart(lb.style.top)) + lb.offsetHeight) < (parseInt(scrollTop) + parseInt(document.documentElement.clientHeight))) {
				// align bottom to bottom
				position = parseInt(scrollTop) + document.documentElement.clientHeight - lb.offsetHeight;
			} else {
				// don't move
				moveLightbox = false;
			}
		} else if (parseInt(scrollTop) < parseInt(lastScrollTop)) {
			// Scrolling up...
			if (lb.offsetHeight <= parseInt(document.documentElement.clientHeight)) {
				// center vertically
				position = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop);
			} else if (parseInt(scrollTop) <= parseInt(getNumericPart(lb.style.top))) {
				// align top to top
				position = parseInt(scrollTop);
			} else {
				// don't move
				moveLightbox = false;
			}
		} else {
			// No scrolling...
			moveLightbox = false;
		}
		if (moveLightbox) {
			lb.style.top = position + "px";
			lastScrollTop = scrollTop;
		}
	}
	return false;
}

function hideLightBox() {
	lbc = document.getElementById("LightBoxCont");
	oplb = document.getElementById("OpacityLB");
	
	document.body.removeChild(lbc);
	document.body.removeChild(oplb);
	
	if (window.removeEventListener) {
		window.removeEventListener("scroll", scrolled, false);
	} else if (window.detachEvent) {
		window.detachEvent("onscroll", scrolled);
	}
	
	return false;
}

function getRandom() {	// this is used to break the caching issues in IE
   	var minutes = 1000*60;
	var hours = minutes*60;
	var days = hours*24;
	var years = days*365;
	var date = new Date();
	var t = date.getTime();
	var myrandom = Math.round(t/years);

	var d = new Date()
	var ranNum= (Math.round((Math.random()* myrandom)+1)) + d.getTime();
    return ranNum;
}
