﻿// drag_trivia.js - Drag-and-drop functions for BBeM trivia quiz
//
// These callback and support functions are dependent on the general functions in drag.js.
//
// - Duff Kurland
//   June 1, 2010

var lockCount = 0;										// Should be reset at each page load
var destCount = 0;										// Likewise
var topZ = 5;											// Each new drag goes in front of previous one

function getPosition (e) {   
	var left = 0, top  = 0;   

	while (e.offsetParent) {
		left += e.offsetLeft;   
		top  += e.offsetTop;   
		e     = e.offsetParent;   
	}   
	left += e.offsetLeft;   
	top  += e.offsetTop;   

	return { x:left, y:top };   
} 

function subXY (a, b) {
	return { x:a.x - b.x, y:a.y - b.y };
}

function trDragBegin (item, x, y) {
	if (destCount == 0) {									// The first time through...
		for (var i = 1; i <= 10; i++) {						// Count the number of destinations
			var dest = document.getElementById("dest"+i);
			if (dest)
				destCount++;
		}
	}	

	item.style.zIndex = topZ++;								// Pop item in front
	if (item.lock) {										// If item is currently locked to a field,
		document.getElementById(item.lock).value = '';		// Clear that field's lock
		item.lock = null;									//   and clear the item lock
		item.style.backgroundColor = "#ccc";
		lockCount = Math.max(lockCount - 1, 0);
		document.frm.submit.disabled = true;
	}
	return false;
}

function trDragEnd (item, x, y) {
	var srcPos  = getPosition(item);						// Need to compare apples with apples
	for (var i = 1; i <= 10; i++) {							// Can handle up to 10 destinations
		var dest = document.getElementById("dest"+i);
		if (dest) {
			var destPos = getPosition(dest);				// Find location of each destination
			var d       = subXY(destPos, srcPos);
			if (Math.abs(d.x) < 60 && Math.abs(d.y) < 20) {	// Have we dragged close to a destination?
				var hid = document.getElementById("a"+i);	// Yup, look for a similarly named "hidden" field
				if (hid && hid.value != '') {				// Does the field already have something locked to it?
					var old = document.getElementById(hid.value);
					if (old) {								// Something was previously there
						old.style.left = old.style.top = 0;	// Return that object to its origin
						old.style.backgroundColor = "#ccc";
						old.lock = null;					// Item is no longer locked to anything
						hid.value = '';						// Field no longer has anything locked to it
						lockCount = Math.max(lockCount - 1, 0);
						document.frm.submit.disabled = true;
					}
				}
				item.style.left = parseInt(item.style.left) + d.x + "px";
				item.style.top  = parseInt(item.style.top)  + d.y + 2 + "px";  // Fudge it just a bit
				if (hid) {
					hid.value = item.id;					// Field has this item locked to it
					item.lock = hid.id;						// Item is locked to the field
					item.style.backgroundColor = "#fff99d";
					lockCount = Math.min(lockCount + 1, destCount);
					if (lockCount == destCount)
						document.frm.submit.disabled = false;
				}
				break;
			}
		}
	}
	return false;
}

// End of drag_trivia.js
