var click_selected_diamond;

function rollover(table, div, maxsize, color, timer) {
	this.table = table;
	this.div = div;
	this.maxsize = maxsize;
	this.color = color;
	this.timer = timer;

	this.tmrMouseover = new Array;
	this.tmrSize = new Array;
	this.tmrShrink = new Array;
	this.tmrMouseout = new Array;
	
	this.request;
	
	var table = document.getElementById(this.table);
	
	// Method of adding an event for EOMB
	if ( table.addEventListener ) {
		// Triggers a timer when you roll over the table
		table.addEventListener('mouseover', this.mouseoverTimer, false);
		// Triggers a timer when you roll out of the table
		table.addEventListener('mouseout', this.mouseoutTimer, false);
		table.addEventListener('click', this.mouseclick, false);
	// Method of adding an event for IE
	} else if ( table.attachEvent ) {
		// Triggers a timer when you roll over the table
		table.attachEvent("onmouseover", this.mouseoverTimer);
		// Triggers a timer when you roll out of the table
		table.attachEvent("onmouseout", this.mouseoutTimer);
		table.attachEvent("onclick", this.mouseclick);
	}
}

rollover.prototype.mouseclick = function (e)
{
	var element;
	var id;
	var row;
	var table;
	var center;
	var top;
	var bottom;
	
	if ( e.target ) {
		element = e.target
	} else {
		element = e.srcElement;
	}
	
	
	if ( element.tagName == 'TD') {
		id = element.parentNode.diamonds_id;
		row = element.parentNode;
		table = row.parentNode;
		center = get_offsetTop(row) + (row.offsetHeight / 2);
		
		for ( x=0; x<table.childNodes.length; x++ ) {
			if ( table.childNodes[x].diamonds_id ) {
				top = get_offsetTop(table.childNodes[x]);
				break;
			}
		}
		
		for ( x=table.childNodes.length-1; x>=0; x-- ) {
			if ( table.childNodes[x].diamonds_id) {
				bottom = get_offsetTop(table.childNodes[x]) + table.childNodes[x].offsetHeight;
				break;
			}
		}
	}
	
	if ( id) {
		if ( element.firstChild.tagName == "INPUT" ) {
			if ( element.firstChild.checked ) {
				element.firstChild.checked = false;
			} else {
				element.firstChild.checked = true;
			}
			var evt = new fake_event();
			evt.target = element.firstChild;
			compare_checked(evt);
		} else {
			/*var url = 'product_detail.php?product_id='+id;
			if ( customizing ) {
				url += '&place';
			} else if ( studcustomizing ) {
				url += '&place';
			}
			window.location = url;*/
			add_diamond(id);
		}
	}
}

// Delays the loading of the div when you roll over the target
rollover.prototype.mouseoverTimer = function (e)
{
	var id;
	var row;
	var table;
	var center;
	var top;
	var bottom;
	
	// Determine how we can grab the target element
	if ( e.target ) {
		// Make sure it's a table cell
		if ( e.target.tagName == 'TD' ) {
			// Store the ID of the table row
			id = e.target.parentNode.diamonds_id;
			
			row = e.target.parentNode;
			table = row.parentNode;
			
			center = get_offsetTop(row) + (row.offsetHeight / 2);
			
			for ( x=0; x<table.childNodes.length; x++ ) {
				if ( table.childNodes[x].diamonds_id ) {
					top = get_offsetTop(table.childNodes[x]);
					break;
				}
			}
			
			for ( x=table.childNodes.length-1; x>=0; x-- ) {
				if ( table.childNodes[x].diamonds_id ) {
					bottom = get_offsetTop(table.childNodes[x]) + table.childNodes[x].offsetHeight;
					break;
				}
			}
		}
	} else if ( e.srcElement ) {
		// Make sure it's a table cell
		if ( e.srcElement.tagName == 'TD') {
			if ( e.srcElement.parentNode.parentNode.id == 'diamonds_list') {
				e.srcElement.parentNode.style.background = "D0D5D9";
				e.srcElement.parentNode.style.cursor = 'hand';
				e.srcElement.parentNode.attachEvent('onmouseout', function(e) {e.srcElement.parentNode.style.background = ""; e.srcElement.parentNode.style.cursor = '';});
			}
			// Store the ID of the table row
			id = e.srcElement.parentNode.diamonds_id;
			
			row = e.srcElement.parentNode;
			table = row.parentNode;
			
			center = get_offsetTop(row) + (row.offsetHeight / 2);
			
			for ( x=0; x<table.childNodes.length; x++ ) {
				if ( table.childNodes[x].diamonds_id ) {
					top = get_offsetTop(table.childNodes[x]);
					break;
				}
			}
			
			for ( x=table.childNodes.length-1; x>=0; x-- ) {
				if ( table.childNodes[x].diamonds_id ) {
					bottom = get_offsetTop(table.childNodes[x]) + table.childNodes[x].offsetHeight;
					break;
				}
			}
		}
	}
	
	// Only execute the following if we got an ID
	if ( id ) {
		// If the timer is currently set, clear it
		if ( rollover.tmrMouseover[id] ) {
			clearTimeout(rollover.tmrMouseover[id]);
		}

		// Grab the info div
		var info_div = document.getElementById(rollover.div);
		// Make sure the div is not hidden
		if ( info_div.style.display != 'none' ) {
			// If the div is not associated with the current row, add the loading timer
			if ( info_div.rowid != id ) {
				// Set the timeout to load the div				
				rollover.tmrMouseover[id] = setTimeout('rollover.loaddiv("'+id+'", '+center+', '+top+', '+bottom+')', rollover.timer);
			}
		// Otherwise just add the loading timer
		} else {
			// Set the timeout to load the div
			rollover.tmrMouseover[id] = setTimeout('rollover.loaddiv("'+id+'", '+center+', '+top+', '+bottom+')', rollover.timer);
		}
	}

};

// Delays the sizing of the div during its loading process
rollover.prototype.sizeTimer = function (div, size)
{	
	// If the timer is currently set, clear it
	if ( rollover.tmrSize[div.rowid] ) {
		clearTimeout(rollover.tmrSize[div.rowid]);
	}
	
	// If the size is under our limits, keep sizing it up
	if ( size <= rollover.maxsize ) {
		// Calls the sizing function
		rollover.sizediv(size);
		
		// For some reason div doesn't work, it has to be set to a new variable.
		newdiv = div;
		size += 50;
		rollover.tmrSize[div.rowid] = setTimeout('rollover.sizeTimer(newdiv, '+size+')', 0);
	// Otherwise we are done
	} else {
		var info_div = document.getElementById(rollover.div);
		
		if ( info_div.style.display != 'none' ) {
			if ( rollover.request ) {
				rollover.request.abort();
			}
			if ( window.XMLHttpRequest ) {
				rollover.request = new XMLHttpRequest();
			} else if ( window.ActiveXObject ) {
				rollover.request = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				alert("You need to update your browser");
				throw("No Browser support for XMLHTTP");
			}
			
			rollover.request.onreadystatechange = function () {rollover.receive_request();};
		
			url = 'diamondinfo.php?id='+div.rowid;
			if ( customizing ) {
				url += '&cust';
			} else if ( studcustomizing ) {
				url += '&custstud';
			} else if ( loose_studs ) {
				url += '&loose_studs';
			}
			rollover.request.open("GET", url, true);
			rollover.request.send(null);
		}
	}
};

rollover.prototype.receive_request = function ()
{
	if ( rollover.request.readyState == 4) {
		if ( rollover.request.responseText ) {
			var info_div = document.getElementById(rollover.div);

			if ( info_div.style.display != 'none' ) {
				// Add the text
				info_div.innerHTML = rollover.request.responseText;
			}
		}
	}
};

// Delays the shrinking of the div during its removal process *not functional*
rollover.prototype.shrinkTimer = function (id, size)
{	
	// If the timer is currently set, clear it
	if ( tmrShrink[id] ) {
		clearTimeout(tmrShrink[id]);
	}
	
	var div = document.getElementById(rollover.div);
	if ( div ) {
		div.innerHTML = '';
		
		// If the size is above 0, keep shrinking it
		if ( size > 0 ) {
			// Calls the sizing function
			sizediv(rollover.div, size);
			
			size -= 50;
			tmrShrink[id] = setTimeout('shrinkTimer('+id+', '+size+')', 1);
		// Otherwise we are done
		} else {
			removediv(id);
		}
	}
};

// Delays the removal of a div after you have lost focus of it
rollover.prototype.mouseoutTimer = function (e)
{	
	var id;

	// Determine how we can grab the target element
	if ( e.target ) {
		// Make sure it's a table cell
		if ( e.target.tagName == 'TD' ) {
			// Store the ID of the table row
			id = e.target.parentNode.diamonds_id;
		} else if ( e.target.tagName == 'DIV' ) {
			// Store the row ID associated with the div
			id = e.target.rowid;
		}
	} else if ( e.srcElement ) {
		// Make sure it's a table cell
		if ( e.srcElement.tagName == 'TD') {
			// Store the ID of the table row
			id = e.srcElement.parentNode.diamonds_id;
		} else if ( e.srcElement.tagName == 'DIV' ) {
			// Store the row ID associated with the div
			id = e.srcElement.rowid;
		}
	}

	// Only execute the following if we got an ID
	if ( id ) {

		// If the timer is currently set, clear it
		if ( rollover.tmrMouseover[id] ) {
			clearTimeout(rollover.tmrMouseover[id]);
		}
		
		/*// If the timer is currently set, clear it
		if ( rollover.tmrShrink[id] ) {
			clearTimeout(rollover.tmrShrink[id]);
		}*/
		
		// If the timer is currently set, clear it
		if ( rollover.tmrMouseout[id] ) {
			clearTimeout(rollover.tmrMouseout[id]);
		}
		
		// Set the timeout to remove the div
		rollover.tmrMouseout[id] = setTimeout('rollover.removediv('+id+')', rollover.timer);
		
		//var info_div = document.getElementById('info_div');
		//tmrMouseout[id] = setTimeout('shrinkTimer('+id+', 300)', 500);
	}
};

// Generates the div of info
rollover.prototype.loaddiv = function (id, center, top, bottom)
{
	
	var table = document.getElementById(rollover.table);
	
	var div = document.getElementById(rollover.div);
	// Assign the row's ID so we can easily access it later on
	div.rowid = id;
	
	// Add some styles
	div.style.width = '0px';
	div.style.height = '0px';
	div.style.display = '';
	//div.style.border = "1px solid "+rollover.color;

	var cont_table = document  .getElementById('return_cont_table');
	tableleft = get_offsetLeft(cont_table);

	divleft = tableleft - rollover.maxsize;
	
	if ( divleft < 0 ) {
		divleft = 0;
	}
	
	div.style.left = divleft + 'px';
	div.style.position = 'absolute';

	var divtop = center - ((rollover.maxsize * rollover_multiplier) / 2);
	var divbottom = center + ((rollover.maxsize * rollover_multiplier) / 2);
	var divpos;
	
	if ( divtop < top ) {
		divpos = top + table.offsetTop;
	} else if ( divbottom > bottom ) {
		divpos = bottom - (rollover.maxsize * rollover_multiplier) + table.offsetTop;
	} else {
		divpos = divtop;
	}
	div.style.top = divpos+"px";
	// Trigger our sizing timer
	rollover.sizeTimer(div, 0);
	
	// Method of adding an event for EOMB 
	if ( table.addEventListener ) {
		// Handles the mouseover event of the table
		table.addEventListener('mouseover', rollover.mouseover, false);
	// Method of adding an event for IE
	} else {
		// Handles the mouseover event of the table
		table.attachEvent('onmouseover', rollover.mouseover);
	}
	
	// Method of adding an event for EOMB
	if ( div.addEventListener ) {
		// Handles the mouseover event of the div
		div.addEventListener('mouseover', rollover.mouseover, false);
		// Triggers a timer when you roll out of the div
		div.addEventListener('mouseout', rollover.mouseoutTimer, false);
	// Method of adding an event for IE
	} else {
		// Handles the mouseover event of the div
		div.attachEvent('onmouseover', rollover.mouseover);
		// Triggers a timer when you roll out of the div
		div.attachEvent('onmouseout', rollover.mouseoutTimer);
	}
};

// Our mouseover event function
rollover.prototype.mouseover = function (e)
{	
	var id;
	
	// Determine how we can grab the target element
	if ( e.target ) {
		// Make sure it's a table cell
		if ( e.target.tagName == 'TD' ) {
			// Store the ID of the table row
			id = e.target.parentNode.diamonds_id;
		} else {
			if ( e.target.id == rollover.div ) {
				// Store the row ID associated with the div
				id = e.target.rowid;
			} else {
				// Loop through all of the parent nodes in an attempt to find the div
				e = e.target.parentNode;
				while ( e ) {
					if ( e.tagName == 'DIV' ) {
						if ( e.id == rollover.div ) {
							// Store the row ID associated with the div
							id = e.rowid;
							break;
						}
					}
					
					e = e.parentNode;
				}
			}
		}
	} else if ( e.srcElement ) {
		// Make sure it's a table cell
		if ( e.srcElement.tagName == 'TD') {
			// Store the ID of the table row
			id = e.srcElement.parentNode.diamonds_id;
		} else {
			if ( e.srcElement.id == rollover.div ) {
				// Store the row ID associated with the div
				id = e.srcElement.rowid;
			} else {
				// Loop through all of the parent nodes in an attempt to find the div
				e = e.srcElement.parentNode;
				while ( e ) {
					if ( e.tagName == 'DIV' ) {
						if ( e.id == rollover.div ) {
							// Store the row ID associated with the div
							id = e.rowid;
							break;
						}
					}
					
					e = e.parentNode;
				}
			}
		}
	}

	// Only execute the following if we got an ID
	if ( id ) {
		// If the timer is currently set, clear it
		if ( rollover.tmrMouseover[id] ) {
			// Grab the info div
			var info_div = document.getElementById(rollover.div);
			
			// Make sure the div is not hidden
			if ( info_div.style.display != 'none' ) {
				// If the div is associated with the current row, remove it
				if ( info_div.rowid == id ) {
					clearTimeout(rollover.tmrMouseover[id]);
				}
			}
		}
		
		/*// If the timer is currently set, clear it
		if ( rollover.tmrShrink[id] ) {
			clearTimeout(rollover.tmrShrink[id]);
		}*/
		
		// If the timer is currently set, clear it
		if ( rollover.tmrMouseout[id] ) {
			clearTimeout(rollover.tmrMouseout[id]);
		}
	}
};
// The function that actually sizes the div
rollover.prototype.sizediv = function (x)
{
	// Grab the info div
	var info_div = document.getElementById(rollover.div);
	
	if ( info_div.style.display != 'none' ) {
		// Apply the styles
		info_div.style.width = x+'px';
		info_div.style.height = (rollover_multiplier * x)+'px';
	}
};

// The function that removes the div
rollover.prototype.removediv = function (id)
{	
	// Grab the info div
	var info_div = document.getElementById(rollover.div);
	
	// Make sure the div exists first
	if ( info_div.style.display != 'none' ) {
		// If the div is associated with the current row, remove it
		if ( info_div.rowid == id ) {
			// Hide it
			info_div.innerHTML = '';
			info_div.style.display = 'none';
			
			// Grab the table so we can remove one of the mouseover event handlers
			var table = document.getElementById(rollover.table);
	
			// Method of removing an event for EOMB
			if ( table.removeEventListener ) {
				// Removes the current mouseover event handler
				table.removeEventListener('mouseover', rollover.mouseover, false);
			// Method of removing an event for IE
			} else {
				// Removes the current mouseover event handler
				table.detachEvent('onmouseover', rollover.mouseover);
			}
		}
	}
};

function fake_event () {
	var target;
}