// ---------------------------------------------
// popinfo v1.0
// written by Bernhard Rieder
// GNU public licence
// ---------------------------------------------


// main parameters

var mouse_distance = 10;
var layer_width = 640;
var layer_height = 300;
var link_style = 'font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;font-size: 9px;color: #ffffff;text-decoration:none;';
var bar_color = '#000000';
var bar_height = 14;
var search_string = 'http://www.google.com/search?hl=en&btnG=Google+Search&q=';
//var search_string = 'http://dictionary.reference.com/search?q=';


// ---------------------------------------------
// functions
// ---------------------------------------------

var drag_state = 'off';
var x_offset,y_offset;

document.onmousemove = mouse_monitor;			// start the mouse monitor at the very beginning



function mouse_monitor(event) {					// the mouse monitor provides the coordinates of the cursor at all times
	
	if(window.event) {							// path IE							
    	x = window.event.clientX;
    	y = window.event.clientY;
  	} else {									// path Mozilla
   		x = event.clientX;
		y = event.clientY;
  	}
	
	if(drag_state == 'on') {
		document.getElementById('barlayer').style.left = (x - x_offset) + "px";
		document.getElementById('barlayer').style.top = (y - y_offset) + "px";
	}
	
}



function pop_info(event) {
		
	if (window.getSelection) { var query = window.getSelection();	} 
	
	else if (document.getSelection) { var query = document.getSelection(); } 
	
	else if (document.selection) {  var query = document.selection.createRange().text;	} 
	
	else { return; }
	
	if(document.getElementById('framelayer') || document.getElementById('barlayer')) {
		document.body.removeChild(document.getElementById('framelayer'));
		document.body.removeChild(document.getElementById('barlayer'));
	}
	
	
	my_bar = document.createElement("div");		// the layer containing the popframe
	
	my_bar.id = "barlayer";

	my_bar.style.position = "absolute";
	my_bar.style.top = y + "px";
	my_bar.style.left = x + "px";
	my_bar.style.width = layer_width + "px";
	my_bar.style.height = bar_height + "px";
	my_bar.style.zIndex = 1000;			
	
	var inner_html = '<table width="'+layer_width+'" height="'+bar_height+'" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="'+bar_color+'" onmousedown="pop_drag();" onmouseup="pop_release();"><a href="javascript:pop_close()" style="'+link_style+'">&nbsp;close</a></td></tr></table>';

	my_bar.innerHTML = inner_html;
									
	document.body.appendChild(my_bar);
	
	
	my_frame = document.createElement("div");		// the layer covering the popframe
	
	my_frame.id = "framelayer";

	my_frame.style.position = "absolute";
	my_frame.style.top = y + bar_height + "px";
	my_frame.style.left = x + "px";
	my_frame.style.width = layer_width + "px";
	my_frame.style.height = layer_height +  "px";
	my_frame.style.visibility = "visible";
	my_frame.style.zIndex = 1000;

	var inner_html = '<iframe width="'+(layer_width-4)+'" height="'+layer_height+'" src="'+search_string+query+'"></iframe>';
	
	my_frame.innerHTML = inner_html;
	
	document.body.appendChild(my_frame);

}


function pop_close() {

	document.body.removeChild(document.getElementById('barlayer'));
	document.body.removeChild(document.getElementById('framelayer'));
	
}

function pop_drag() {

	document.body.removeChild(document.getElementById('framelayer'));

	drag_state = 'on';
	
	x_offset = x - parseInt(document.getElementById('barlayer').style.left.replace(/px/,""));
	y_offset = y - parseInt(document.getElementById('barlayer').style.top.replace(/px/,""));
	
}

function pop_release() {
	
	document.body.appendChild(my_frame);
	
	document.getElementById('framelayer').style.left = (x - x_offset) + "px";
	document.getElementById('framelayer').style.top = (y - y_offset + bar_height) + "px";
	
	drag_state = 'off';
	
}