/*****************************************************************
*
*	Generic rollover functions: now using JSON protocol mostly
*
*****************************************************************/

var dh = null;
var to = 1;
var posx = 0;
var posy = 0;
var targ = null;
var rollover_delay = 400;
var styles = [];
var exists = 0;

styles['HELP'] = 'help_div';
styles['HELPPROXY'] = 'help_div';

win_size();

//
// get the mouse coords for the event
//
function getMouseXY(e) {

	if ( !e ){ 
		var e = window.event
		targ = e.srcElement;
	}

	if ( e.srcElement ) targ = e.srcElement;
	else targ = e.target;
	
	// calculate mouse coordinates
	if ( e.pageX || e.pageY ) {
		posx=e.pageX;
		posy=e.pageY;
	} else if ( e.clientX || e.clientY ) {
		posx=e.clientX;
		posy=e.clientY;
		// check for scroll offsets in IE 6
		if(document.documentElement.scrollLeft || document.documentElement.scrollTop){
			posx+=document.documentElement.scrollLeft;
			posy+=document.documentElement.scrollTop;
		}
	}
}

function image_on(e,path) {
	var div = get_div_by_id(e,'generic_div');

	if ( div ) {
		div.innerHTML='<img src="'+path+'" alt="this" border="0px" />';
		// move pop-up DIV element
		div.style.top = posy-15 + 'px';
		if ( posx > (winRight * 0.75) ) {
			var x = posx - div.offsetWidth;
			div.style.left = x + 'px';
		}
		else div.style.left = posx+5 + 'px';
	}

}

//
// open a top-level div with the msg contents in it
//
function div_msg(e,content,style) {
	if ( !style ) var style = 'error_msg';
	var div = get_div_by_id(e,'generic_div');
	if ( div ) {
		div.className = style;
		div.innerHTML= content;
		if ( !exists ) position_div(div);
	}
}

function position_div(div) {
	div.style.top = posy-15 + 'px';
	if ( posx > (winRight * 0.5) ) {
		var x = posx - div.offsetWidth;
		div.style.left = x + 'px';
	}
	else div.style.left = posx+5 + 'px';
}

function get_div_by_id(e,id,style) {
	//
	// get mouse coordinates, then create a div and display it at that pt
	//
	if ( e ) getMouseXY(e);

	var div=document.getElementById(id);
	if( !div ) {
		exists = 0
		var div = document.createElement('div');
		div.setAttribute('id',id);
		div.onmouseover=kill_timer;
		div.onmouseout=delayed_hide_rollover;
		div.innerHTML='Loading....';
		div.className = style;
		document.getElementsByTagName('body')[0].appendChild(div);
	} else exists = 1;
	return div;
}

function rollover_on(e,type,extra) {
	var st = (styles[type] ? styles[type] : 'generic'); 
	var div = get_div_by_id(e,'generic_div',st);

	if ( div ) {
		IncludeJavaScript('/srv/json.php/'+type+'/'+extra+'/displayJSONContent');
		// move pop-up DIV element
		if ( !exists ) position_div(div);
	}
}

function hideRolloverDiv(){
	to = 0;
	var div=document.getElementById('generic_div');
	if(!div){return};
	div.parentNode.removeChild(div);
}

function kill_timer() { 
	clearTimeout(dh);
}

function delayed_hide_rollover() {
	dh = setTimeout('hideRolloverDiv()',rollover_delay);
	to = 1;
}

function displayJSONContent(json) {
	var popupdiv = document.getElementById('generic_div');
	if ( !popupdiv )return;
	if ( json.Error ) { popupdiv.innerHTML = json.Error; return; }
	popupdiv.innerHTML = json.Content;
}

 
