function addEvent(obj, type, fn) {
	if (obj.attachEvent) {
		obj['e' + type + fn] = fn;
		obj[type + fn] = function() { obj['e' + type + fn] (window.event); }
		obj.attachEvent('on' + type, obj[type + fn] );
	} else {
		obj.addEventListener(type, fn, false);
	}
}

function removeEvent(obj, type, fn ) {
	if (obj.detachEvent) {
		obj.detachEvent('on' + type, obj[type + fn] );
		obj[type+fn] = null;
	} else {
		obj.removeEventListener( type, fn, false);
	}
}

function newwindow(coverURL) {
	coverwin = window.open(coverURL,'cover','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=710,height=580');
}

function smwindow(smURL) {
	smwin = window.open(smURL,'cover','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=430,height=560');
}

function midiwindow(midiURL) {
	midiwin = window.open(midiURL,'midiwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=400,height=150');
}

function cdwindow(cdURL) {
	cdwin = window.open(cdURL,'cd','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=400,height=230');
}

function cartwindow(itemURL) {
	cartwin = window.open(itemURL,'cartwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=400');
}

function sampwindow(sampURL) {
	sampwin = window.open(sampURL,'sample','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=620,height=545');
}

function helpWindow(helpURL) {
	var helpwin = window.open(helpURL, '','toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=0,resizable=1,width=600,height=460');
}

function announce(annURL) {
	var s = getCookie('announce');
	if (s != 1) {
		announceWin = window.open(annURL,'announcement','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=500,height=320');
	}
}

function setScrollArea() {
	var h = (document.body.offsetHeight - (document.getElementById('contract').className != 'hide'? document.getElementById('bodyScroll').offsetTop : 24) -6);
	var hp = (h < 1?1:h) + "px";
	document.getElementById('bodyScroll').style.height = hp;
}

function initMenu() {
	document.getElementById('loading').className = "hide";
	document.getElementById('menuLinks').className = "menuLinks";
	document.getElementById('timeDisplay').className = "timeDisplay";
	var s = getCookie('menuStateAlbums');
	if (s == null) { s = 1; }
	showMenu(s);
	setScrollArea();
	if(document.location.href.indexOf('#')!=-1) {
		document.location.href=document.location.href;
	}
}

function showMenu(show) {
	if(show==0) { aI.hideAreas('navmap'); }
	document.getElementById('contract').className = (show == 1 ? "show": "hide");
	document.getElementById('expand').className = (show == 0 ? "show": "hide");
	if(brVersion == '') {
		document.getElementById('expand').style.marginTop = "-5px";
	}
	setScrollArea();
	if(show==1) { aI.showArea(pageRef,'navmap'); }
	document.cookie = "menuStateAlbums=" + show;
}

function setMenu() {
	var s = getCookie('menuStateAlbums');
	if(s==0) { aI.hideAreas('navmap'); }
	setScrollArea();
	if(s==1) { aI.showArea(pageRef,'navmap'); }
}

function showAbout(show) {
	if (document.getElementById('about') == null) { return }
	divAbout = document.getElementById('about');
	if(show==0) {
		divAbout.className = "hide";
	} else {
		divAbout.style.top = (document.getElementById('menuLinks').offsetTop) + 'px';
		divAbout.style.left = document.body.offsetWidth - 360 + 'px';
		divAbout.className = "dAbout";
	}
}

// Area for the AJAX container

var dom = (document.getElementById)? 1:0
var ua = navigator.userAgent
var ie4 = (document.all&&!dom)? 1:0
var ie5 = (document.all&&dom)? 1:0
var ff = (dom&&!ie5)? 1:0
var doc_root = ((ie5&&ua.indexOf("Opera")<0||ie4)&&document.compatMode=="CSS1Compat")? "document.documentElement":"document.body"
var timer1 = "";
var timer2 = "";
var mTimer = 0;
var move = 0;
var mouse_X, mouse_Y;
var mx, my, sc1, sct, ih, iw, sv, ww, wh, evlh, evlw, sbw, imgTop;
var divData;

document.onmousemove = mousePos;

if(ff) {
	mx = "e.pageX"
	my = "e.pageY"
	scl = "window.pageXOffset"
	sct = "window.pageYOffset"
}

if(ie4||ie5) {
	mx = "event.x"
	my = "event.y"
	scl = "eval(doc_root).scrollLeft"
	sct = "eval(doc_root).scrollTop"
	if(ie5) {
		mx = mx+"+"+scl
		my = my+"+"+sct
	}
}

if(ie4||dom){
	sl = "style.left"
	st = "style.top"
	ih = "offsetHeight"
	iw = "offsetWidth"
	sv = "style.visibility="
}

if(ie4||ie5) {
	ww = "eval(doc_root).clientWidth"
	wh = "eval(doc_root).clientHeight"
} else {
	ww = "window.innerWidth"
	wh = "window.innerHeight"
	sbw=15
}
evlh = eval(wh) - 20;
evlw = eval(ww)

function openAjax() {
	if(window.XMLHttpRequest) {
		var XMLHttpRequestObject = new XMLHttpRequest();
		if (XMLHttpRequestObject.overrideMimeType) {
			XMLHttpRequestObject.overrideMimeType('text/xml; charset=iso-8859-1');
		}
	} else if (window.ActiveXObject) {
		var XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return XMLHttpRequestObject
}

function getAlbumData(dataSource, albumImg) {
	divData = document.getElementById('showData');
	clearTimeout(timer1);
	clearTimeout(timer2);
	hideData();

	var XMLHttpRequestObject = openAjax();

	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", dataSource + ".htm");
		XMLHttpRequestObject.onreadystatechange = function() {
			if(XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200) {
				var dataStr = XMLHttpRequestObject.responseText;
				pObj= albumImg;
				var left1 = pObj.offsetLeft;
				var top1 = pObj.offsetTop;

				// Find the highest parent location for accurate placement
				while (pObj.offsetParent != null) {
					left1 += pObj.offsetParent.offsetLeft;
					top1 += pObj.offsetParent.offsetTop;
					pObj = pObj.offsetParent;
				}
				top1 -= document.getElementById("bodyScroll").scrollTop;
				imgTop = top1;
				top1 = (top1-30)<20?20:(top1-30);
				divData.style.top = top1 + 'px';
				divData.style.left = (left1 + 70) + 'px';

				var sHTML = '';
				var nMarker = (dataStr.indexOf('<tr><td width="200"')-1);
				var newWidth = (parseInt(dataStr.substr(dataStr.indexOf("width")+7,3)) + 20) + "px";
				sHTML += dataStr.slice(dataStr.indexOf("<table"), nMarker) + "<tr><td align='center' style='font-size:10.5pt;color:#bfbfff;'><b><u>" + dataStr.slice(dataStr.indexOf("<title>") + 7, dataStr.indexOf("</title>")) + "</u></b></td></tr>";
				sHTML += dataStr.slice(dataStr.indexOf("<td width=", nMarker +10), dataStr.indexOf('</tr></table>', nMarker +10)-9);
				sHTML = "<img align='left' src='http://ragpiano.com/images/callout.gif' id='callout' style='margin-top:" + (top1==20?20:50) + "px; margin-right:-3px;z-index:101;position:relative;' />" + sHTML;
				divData.innerHTML = sHTML + "<tr><td align='center' style='color:#bfb;font-size:8.0pt;'><b>Click Thumbnail Cover for Separate Window</b></td></tr></table>";
				divData.style.width=newWidth;
				timer1=setTimeout(function() {showAlbumData()}, 700);
			}
		}
		XMLHttpRequestObject.send(null);
	}
}

function mousePos(e) {
	if(e != null || (!ff && event != null)) { // added to trap for no mouse event when calling from keyboard
		MouseX = eval(mx),MouseY = eval(my);
	}
	if(move==1 && mTimer==0) hideData();
}

function showAlbumData() {
	mTimer=1;
	divData.className = "dAlbum";
	var divBottom = (divData.offsetHeight + parseInt(divData.style.top) - 20)
	if(divBottom > evlh) {
		divData.style.top = (parseInt(divData.style.top)-(divBottom - evlh)>19?parseInt(divData.style.top)-(divBottom - evlh):20) + "px";
		document.getElementById('callout').style.marginTop = (imgTop - parseInt(divData.style.top)  + 20) + "px";
	}
	timer2=setTimeout(function() { mTimer=0; hideData();},7999);
}

function hideData() {
	divData = document.getElementById('showData');
	mTimer=0;
	clearTimeout(timer1);
	clearTimeout(timer2);
	if(move==1) {
		if(MouseX > (divData.offsetLeft + 20) && MouseX < (divData.offsetLeft + divData.offsetWidth) - 8 && MouseY > (divData.offsetTop + 8) && MouseY < (divData.offsetTop + divData.offsetHeight) - 8) { return;}
	}
	divData.className="hide";
	move=0;
}

function cancelAlbum() {
	if(mTimer==0) clearTimeout(timer1);
}

function keyCancel() {
	move=0;
	cancelAlbum();
	hideData();
}

// end of Ajax Container code

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0)
			return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
    	end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

var sessionID;

function sessionCookie() {
//	deleteCookie("sessionID");
//	return;
	if (getCookie("sessionID")) {
		sessionID = getCookie("sessionID");
   } else {
		dDate = new Date();
		var expireDate = new Date(dDate.getTime() + (86400000));
		sessionID = Math.random();
		document.cookie = "sessionID=" + sessionID + "; expires=" + expireDate;
   }
}
