// Disk Reunion v1.4 Copyright (c)2003-2006 Yuji Adachi, 9031.com. All rights reserved. //

mac = (navigator.appVersion.indexOf("Mac")>-1) ? true : false; // Mac
ie = (document.all && !window.opera) ? true : false; // IE
safari = (navigator.appVersion.indexOf("Safari")>-1) ? true : false; // Safari
gecko = (navigator.userAgent.indexOf("Gecko/") > -1) ? true : false; // Gecko
opera = (window.opera) ? true : false; // Opera;
add = (!mac && ie)? 2:0;

grab = null; // grabbed disk (object name);a
grabX = null;
grabY = null;
grabNX = null;
grabOld = null;

xMax = 4;			// main horizontal max cols
rMax = 30;			// main depth
xPitch = 131;		// main width (px)
yPitch = 200;		// main height (px)
mTop = 96;			// margin top (px)
mLeft = 160; 		// margin left (px)

tooltipW = 250;		// Tooltip Width

timer_diskUp = null;		// Timer (Disk Up)
timer_showTooltip = null;		// Timer (Show Tooltip)
timer_hideTooltip = null;		// Timer (Hide Tooltip)
timer_httpTimeout = null;		// Timer (XMLHTTP Timeout)
xmlhttpIsBusy = false;
mDown = false;
upped = 0;
hand = 0;
reviewmax = 400; // Reviews Max

if (ie && !mac && !opera) document.documentElement.style.overflow = "auto";

//var asin;

// ----- Add UI ----- //
function addUI() {
	// add Cursor // div is buggy on Win IE
	document.write('<img src="images/hand_0.gif" id="cur_0" alt="" width="16" height="16" onload="this.style.visibility=\'visible\'" />');
	document.write('<img src="images/hand_1.gif" id="cur_1" alt="" width="16" height="16" onload="this.style.visibility=\'visible\'" />');
	// add Tooltip
	document.write('<div id="bal"><'+'/div>');

}

// ----- Disk Positioning ----- //
function preload() {

	curs0 = document.getElementById("cur_0");
	curs1 = document.getElementById("cur_1");
	tooltip = document.getElementById("bal");
	
	// Count Disks and Layout
	obj = document.getElementById("main").childNodes;

	rZ = xMax;
	rX = 0; // X
	rY = 0; // Y
	no = 0; // ID No.
	mY = 0;
	
	curs0.style.zIndex = curs1.style.zIndex = tooltip.style.zIndex = rZ+1;

	for (i=0; i<obj.length; i++) {
		if (obj[i].tagName == "DIV") { // Disk
			
			with (obj[i]) {
				style.left = mLeft+(rX*xPitch)+(rY*2)+"px";
				style.top = mTop+mY+(rY*2)+"px";
				style.zIndex = rZ;
				style.visibility = "visible";
				id = "disk_"+no;
				getElementsByTagName("span")[0].id = "nam_"+no;
				getElementsByTagName("a")[0].onfocus = function() { this.blur(); }
				getElementsByTagName("a")[0].onclick = function() { return false; }
				if (opera) getElementsByTagName("a")[0].onmouseover = function() { return false; }
				asin = getElementsByTagName("a")[0].getAttribute("HREF").substring(41,51);
				if (ie) { // Generate Img Tag (Win IE, Mac IE)
					getElementsByTagName("a")[0].innerHTML='<img src="images/'+ asin + '.jpg" alt="" width="128" height="128" onload="this.style.visibility=\'visible\'; this.parentNode.parentNode.style.background=\'none\'" />' + getElementsByTagName("a")[0].innerHTML;
				} else { // Generate Background-Image (Faster)
					getElementsByTagName("a")[0].style.backgroundImage = "url(images/" + asin + ".jpg)";
					
				}
			}

			if (rY == rMax-1) {
				rX++; rZ--;
				if (rX>=xMax) { // change global row
					rX = 0;
					mY = mY+yPitch;
					rZ = rZ+(xMax*2);
				};
			}
			rY = (rY+1)%rMax;
			no++;

		} else if (obj[i].tagName == "H3" || obj[i].tagName == "H4") { // Tab
			// Tab Offset
			if (rY == rMax-1) {
				rX++; rZ--;
				if (rX>=xMax) { // change global row
					rX = 0;
					mY = mY+yPitch;
					rZ = rZ+(xMax*2);
				};
				rY = (rY+1)%rMax;
				no++;
			}
			// Tab Offset
			
			with (obj[i]) {
				style.left = mLeft+(rX*xPitch)+(rY*2)+"px";
				style.top = mTop+mY+(rY*2)-8+"px";
				style.zIndex = rZ;
				style.visibility = "visible";
			}
			if (rY == rMax-1) {
				rX++; rZ--;
				if (rX>=xMax) { // change global row
					rX = 0;
					mY = mY+yPitch;
					rZ = rZ+(xMax*2);
				};
			}
			rY = (rY+1)%rMax;
			no++;
		}	
	}
	
	bodyObj = (mac && ie) ? document.body:document.documentElement;
	
	document.onmousemove = mMove;
	document.onclick = mClick;
	document.onmousedown = function() { mDown = true; }
	document.onmouseup = function() { mDown = false; }
	document.ondragstart = document.onselectstart = function() { return false };
}

// ----- Mouse Move ----- //
function mMove(e) {
	if (mDown) {
		hideTooltip();
		return false;
	}
	// get mouse loc
	nx = (ie) ? window.event.clientX : e.pageX;
	ny = (ie) ? window.event.clientY - add : e.pageY;
	
	gy = Math.max(0,Math.floor((ny-mTop)/yPitch)); // grobal row
	wy = Math.min(rMax, Math.floor((ny-(gy*yPitch)-mTop)/2));
	wx = Math.floor((nx-mLeft-(wy*2))/xPitch);
	oNum = ((gy*rMax*xMax)+(rMax*wx)+wy);
	oNam = "disk_" + oNum;
	if (grab) { // mouseout
		if (document.getElementById(oNam)!=grab){
			clearTimeout(timer_httpTimeout);
			clearTimeout(timer_hideTooltip);
			clearTimeout(timer_showTooltip);
			
			clearInterval(timer_diskUp);
			lY = Math.floor(grab.id.split("_")[1] / (rMax *xMax))*yPitch;
			grabX = parseInt(grab.style.left);
			grabY = mTop+lY+(grab.id.split("_")[1]%rMax *2);
			updateDiskPos();
			
			if (xmlhttpIsBusy && xmlhttp) {
				xmlhttp.abort();
				//xmlhttp.onreadystatechange = null; iranai
				xmlhttp = null;
				xmlhttpIsBusy = false;
			} //!
			if (tooltip.style.visibility != "hidden") hideTooltip();
			
			grabOld = grab;
			grab = null;
			upped = 0;
		}
	} else { // mouseover
		if (document.getElementById(oNam) && (wx< xMax) && ((rMax*wx)+wy) < (wx*rMax)+rMax && ((rMax*wx)+wy) >= (wx*rMax) && oNum>=(gy*rMax*xMax)){
			grab = document.getElementById(oNam);
			if (grab!=grabOld) hand = (hand+1)%2;
			grabNX = nx;
			diskUp();
			timer_diskUp = setInterval('diskUp()',50);
		} else {
			if (curs0.style.visibility != "hidden") curs0.style.visibility = "hidden";
			if (curs1.style.visibility != "hidden") curs1.style.visibility = "hidden";
		}
	}
}

function diskUp() {
	if (grab == document.getElementById(oNam)) {
		grabX = grab.offsetLeft;
		grabY = grab.offsetTop - 30;
		
		updateDiskPos();

		if (grab.offsetTop < mTop+(gy*yPitch)+(grab.id.split("_")[1]%rMax *2)-60) {
			upped = 1;
			clearInterval(timer_diskUp); // Stop Disk Up
			initTooltip();
		}
	}
}

// ----- Mouse Click ----- //
function mClick() {
	if (upped) {
		hideTooltip();
		if (grab) window.open(grab.getElementsByTagName("a")[0].getAttribute("HREF"),"_blank");
	}
}


function updateDiskPos() {
	// Update Cursor Position
	curs1.style.left = Math.max(Math.min(grabNX+12, grabX + grab.offsetWidth-8), grabX+18) + "px";
	curs0.style.left = Math.min(Math.max(grabNX-28, grabX+2), grabX + grab.offsetWidth - 24) + "px";
	with (document.getElementById("cur_"+hand).style) {
		top = grabY - 6 + "px";
		if (visibility != "visible") visibility = "visible";
	}
	with (document.getElementById("cur_"+(hand+1)%2).style) {
		if (visibility != "hidden") visibility = "hidden";
	}

	// Update Disk Position
	grab.style.top = grabY + "px";
}

function initTooltip() {
	// Split
	atitle = grab.getElementsByTagName("span")[0].firstChild.nodeValue.split(" ~ ");
	artist = atitle[0]; // Artist
	title = atitle[1].split(" [")[0]; // Title
	released = atitle[1].split(" [")[1].substring(0,4); // Released
	price = ""; // Init Price
	usedprice = ""; // Init Used Price
	availability = ""; // Init Availability
	desc = ""; // Init Reviews
	atext = "Artist: " + artist + "<br />Title: " + title + "<br />Released: " + released;
	asin = grab.getElementsByTagName("a")[0].getAttribute("HREF").substring(41,51); // ASIN
	
	timer_showTooltip = setTimeout("showTooltip()",500);
	loadXML();
}

function showTooltip() {
	if (!grab || xmlhttpIsBusy) {
		if (timer_showTooltip) { clearTimeout(timer_showTooltip); timer_showTooltip = null; }
		return;
	}
	clearTimeout(timer_httpTimeout);
	clearTimeout(timer_showTooltip);
	clearTimeout(timer_hideTooltip);

	wWidth = (ie) ? bodyObj.clientWidth : window.innerWidth;
	wHeight = (ie) ? bodyObj.clientHeight : window.innerHeight;

	atext += ((price || usedprice) ? ("<br />Price: " + price + usedprice) : "") ;
	atext += (availability != "") ? ("<br />Availability: " + availability) : "";
	atext += (desc != "") ? ("<br />" + desc) : "";
	
	
	tooltip.style.left = -9999+"px";
	tooltip.style.top = -9999+"px";
	tooltip.style.visibility = 'visible';
	
	tooltip.innerHTML = grab.atext = atext;
	tooltip.style.width = Math.min(tooltipW, (ie || safari || gecko) ? tooltip.offsetWidth : tooltip.firstChild.offsetWidth) +"px";
	tooltip.style.left =  Math.min(nx, wWidth - tooltip.offsetWidth) + "px";
	tooltip.style.top =  Math.min(ny + 8, wHeight - tooltip.offsetHeight) + "px";
	
	timer_hideTooltip = setTimeout("hideTooltip()",30000);
}

// ----- Hide Tooltip ----- //
function hideTooltip() {
	tooltip.style.visibility = "hidden";
	tooltip.innerHTML = "";
	tooltip.style.width = "auto";
}

// ----- Load XML ----- //
function loadXML() {
	if (xmlhttpIsBusy && xmlhttp) {
		//xmlhttp.onreadystatechange = null;
		xmlhttp.abort();
		xmlhttpIsBusy = false;
	}
	xmlhttp = null;
	
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp && xmlhttp.readyState == 4) { // Ready
				if (timer_httpTimeout) clearTimeout(timer_httpTimeout);
				try {
					if (xmlhttp && xmlhttp.status == 200) { // Successful
						try {
							xmld = xmlhttp.responseXML;
							if (xmld.getElementsByTagName("ErrorMsg").length==0) {
								parseXML();
							} 
						} catch (e) { // Error
							xmlhttp = null;
						}
					} else { // Error
						xmlhttp = null;
					}
				} catch (e) { // Error
					xmlhttp = null;
				}
				xmlhttpIsBusy = false;
				if (grab && !timer_showTooltip) showTooltip();
			}
		}
		timer_httpTimeout = setTimeout("timeOut()",2000);
		try {
			xmlhttp.open('GET', 'http://www.9031.com/diskreunion/asin.php?type=heavy&asin=' + asin);
			xmlhttpIsBusy = true;
			xmlhttp.send(null);
		} catch (e) { // Error
			xmlhttp.abort();
			if (grab && !timer_showTooltip) showTooltip();
		}
	} else {
		if (grab && !timer_showTooltip) showTooltip();
	}
}

// ----- Time Out ----- //
function timeOut() {
	if (xmlhttp) {
		xmlhttp.abort();
		xmlhttp = null;
		xmlhttpIsBusy = false;
	} //!Imp
	if (grab && !timer_showTooltip) showTooltip();
}

// ----- Parse XML ----- //
function parseXML() {
	// Price
	if (xmld.getElementsByTagName("OurPrice").length) {
		price = xmld.getElementsByTagName("OurPrice")[0].firstChild.nodeValue + " (税込)";
	} else if (xmld.getElementsByTagName("ListPrice").length)  {
		price = xmld.getElementsByTagName("ListPrice")[0].firstChild.nodeValue + " (税込)";
	}
	price = price.replace(/￥\s*/, "&yen;");
	
	// Availability T|F
	if (xmld.getElementsByTagName("Availability").length) {
		aVal = xmld.getElementsByTagName("Availability")[0].firstChild.nodeValue;
	} else {
		aVal = null;
	}
	available = (aVal == "This item is currently not available." || aVal == "Not Available" || !aVal)?false:true;
	
	// Used Price
	if (xmld.getElementsByTagName("UsedPrice").length) {
		usedprice = xmld.getElementsByTagName("UsedPrice")[0].firstChild.nodeValue + "より";
		usedprice = usedprice.replace(/￥\s*/, "&yen;");
		usedprice = (price) ? (" / Used "+ usedprice) : ("Used "+ usedprice);
	}
	
	// Availability
	if (!available) {
		 if (!usedprice) {
		 	availability = "現在お取り扱いできません";
		 } else {
		 	availability = unescape("%u51FA%u54C1%u8005%u304B%u3089%u901A%u5E382%u55B6%u696D%u65E5%u4EE5%u5185%u306B%u767A%u9001"); //出品者から通常2営業日以内に発送
		 }
	} else {
		availability = aVal;
	}
	
	// Reviews
	if (xmld.getElementsByTagName("ProductReviews").length) {
		desc = 'Editorial Reviews: ' + xmld.getElementsByTagName("ProductReviews")[0].firstChild.nodeValue;
	} else if (xmld.getElementsByTagName("Comment").length) {
		desc = 'Customer Reviews: ' + xmld.getElementsByTagName("Summary")[0].firstChild.nodeValue + ' <span class="summary">——</span> ' +  xmld.getElementsByTagName("Comment")[0].firstChild.nodeValue;
	}
	if (desc != "") {
		desc = desc.replace(/　/g, "");
		desc = desc.replace(/(<p>)/gi, "");
		desc = desc.replace(/(<br>)/gi, "");
		desc = desc.replace(/&nbsp;/gi, "");
		if (desc.length>reviewmax) {
			f = desc.lastIndexOf(unescape("%u3002"), reviewmax);
			if (f>-1) desc = desc.substr(0,f+1);
			else desc = desc.substr(0,reviewmax) + "...";
		}
	}
}

// ----- eXTReMe Tracking ----- //
function extremeTracking() {
	//return;
	EXs = screen;
	EXw = EXs.width;
	navigator.appName!="Netscape"?EXb=EXs.colorDepth:EXb=EXs.pixelDepth;
	var EXlogin='dskrnn' // Login ID
	var EXvsrv='s9' // VServer
	navigator.javaEnabled()==1?EXjv="y":EXjv="n";
	EXd = document;
	EXw?"":EXw="na";
	EXb?"":EXb="na";
	EXd.write('<img src="http://e0.extreme-dm.com/' + EXvsrv + '.g?login=' + EXlogin + '&amp;','jv=' + EXjv + '&amp;j=y&amp;srw=' + EXw + '&amp;srb=' + EXb + '&amp;','l=' + escape(EXd.referrer) + '" height="1" width="1" />');

}
