/* $Id: rollovers.js,v 1.3 2009/11/09 14:25:55 pmorrill Exp $ */
/*****************************************************************
*
*	Generic rollover functions: now using JSON protocol mostly
*
*****************************************************************/

var asynch_r = null;
var dh = null;
var to = 1;
var posx = 0;
var posy = 0;
var targ = null;
var rollover_delay = 400;
var styles = [];
var exists = 0;
var wrapped = null;
var wrapper = null;
var drag_bar = null;
var dragging = null;
var start_dragX = 0;
var start_dragY = 0;
var select_kludge = '<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';

styles['HELP'] = 'help_div';
styles['HELPPROXY'] = 'help_div';
styles['IMAGE'] = 'image_div';
styles['GET_POSTING'] = 'post_div';

win_size();

document.onmouseup = stop_drag;

//
// 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';
	}

}

function start_drag(e,n) {
	dragging = document.getElementById(n);
	if ( dragging ) {
		getMouseXY(e);
		start_dragX = posx;
		start_dragY = posy;
		start_posX = dragging.offsetLeft;
		start_posY = dragging.offsetTop;
		// tell our code to start moving the element with the mouse
		document.onmousemove = drag;
		// cancel out any text selections
		document.body.focus(); 
	}
}

//
// turn off the current dragging pointer
//
function stop_drag() { 
	if ( dragging ) {
		dragging.style.cursor = 'default';
		document.onmousemove = null;
		dragging = null; 
	}
}

//
// if there is an element 'in drag' mode, it gets repositioned here
// to follow the mouse move
//
function drag(e) {
	if ( dragging ) {
		getMouseXY(e);
		dragging.style.left = (start_posX + (posx - start_dragX)) + 'px';
		dragging.style.top = (start_posY + (posy - start_dragY)) + 'px';
	}
}

//
// wrap the passed div in absolute position wrapper
//
function wrap_div(div,title) {
	var odiv = document.getElementById('div_wrapper');
	if ( !odiv ) {
		odiv = document.createElement('div');
		odiv.setAttribute('id','div_wrapper');
		odiv.className = 'div_wrapper';
		odiv.innerHTML = '<div id="drag_bar" class="drag_bar" onmousedown="this.style.cursor=\'move\'; start_drag(event,\'div_wrapper\')" onmouseup="this.style.cursor=\'default\'; stop_drag()"><div id="drag_title" >'+title+'</div><img src="/images/cross.png" alt="Close" border="0px" onclick="hide_wrapper()" /></div>'+select_kludge;
		document.getElementsByTagName('body')[0].appendChild(odiv);
	}
	odiv.style.visibility = 'visible';
	odiv.style.width = (div.offsetWidth)+'px';
	odiv.style.height = (div.offsetHeight + 20)+'px';
	div.style.visibility = 'visible';
	odiv.appendChild(div);
	wrapped = div;
	wrapper = odiv;
	return odiv;
}

//
// hide a wrapped div
//
function hide_wrapper() {
	var odiv=document.getElementById('div_wrapper');
	if ( odiv ) {
		document.getElementsByTagName('body')[0].removeChild(odiv);
		odiv.style.visibility = 'hidden';
//		wrapped.style.visibility = 'hidden';
	}
}

//
// find a hidden div, move to mouse location, and show
//
function show_div(e,id,title,ops) {
	var div = get_div_by_id(id);
	if ( div ) {
		var odiv = wrap_div(div,title);
		if ( ops == 'center' ) {
			odiv.style.top = (winBottom - div.offsetHeight)/2 + 'px';
			odiv.style.left = (winRight - div.offsetWidth)/ 2 + 'px';
		}
		else position_div(e,div);
	}
}

//
// find a div, and hide
//
function hide_div(id) {
	var div=document.getElementById(id);
	if ( div ) div.style.visibility = 'hidden';
}

//
// 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('generic_div');
	if ( div ) {
		div.className = style;
		div.innerHTML = content;
		position_div(e,div);
	}
}

function position_div(e,div) {
	if ( e ) getMouseXY(e);

	if ( posy > (winBottom * 0.5) ) div.style.top = posy - 25 + 'px';
	else div.style.top = posy + 5 + 'px';

	if ( posx > (winRight * 0.5) ) {
		var x = posx - div.offsetWidth-15;
		div.style.left = x + 'px';
	}
	else div.style.left = posx + 15 + 'px';
}

function get_div_by_id(id,style) {
	var div=document.getElementById(id);
	if( !div ) {
		exists = 0
		var div = document.createElement('div');
		div.setAttribute('id',id);
		div.innerHTML='Loading....';
		document.getElementsByTagName('body')[0].appendChild(div);
	} else exists = 1;
	div.className = style;
	return div;
}

//
// find an existing div, then use json srv to get content and replace
//
function set_div_content(e,d_id,func,extra) {
	var div = get_div_by_id(d_id);
	if ( div ) IncludeJavaScript('/srv/json.php/'+func+'/'+extra+'/setJSONContent/'+d_id);
}

//
// use a custom callback func to handle the json structure
//
function set_div_content_ex(e,func,cb,extra) {
	IncludeJavaScript('/srv/json.php/'+func+'/'+extra+'/'+cb);
}

function rollover_on(e,type,extra) {
	var st = (styles[type] ? styles[type] : 'generic'); 
	if ( !asynch_r ) asynch_r = new Rwin();
	asynch_r.open_div('generic_div',st);
	asynch_r.wrap('Loading....');
	if ( st != 'help_div' ) asynch_r.add_timer();
	asynch_r.asynch(type,'displayJSONContent',extra);
	position_div(e,asynch_r.wrapper);
}

function hideRolloverDiv(){
	to = 0;
	hideDiv('generic_div');
}

function hideDiv(id) {
	var div=document.getElementById(id);
	if(!div){return};
	div.parentNode.removeChild(div);
}

function kill_timer() { 
	clearTimeout(dh);
}

function delayed_hide_rollover() {
	dh = setTimeout('hideRolloverDiv()',rollover_delay);
	to = 1;
}

function delayed_hide_wrapper() {
	dh = setTimeout('hide_wrapper()',rollover_delay);
	to = 1;
}

function setJSONContent(json) {
	if ( json.Error ) { alert(json.Error); return; }
	if ( json.Content != '' ) {
		var cdiv = document.getElementById(json.DivId);
		if ( !cdiv ) { alert("No div for content: "+json.DivId+"!"); return; }
		cdiv.innerHTML = json.Content;
	}
}

function displayJSONContent(json) {
	if ( json.Error ) { asynch_r.set_html(json.Error); return; }
	asynch_r.set_html(json.Content);
	asynch_r.set_title(json.Title);
}

//
// Display asynch content in generic div
//
function display_json_div(type,extra) {
	if ( !asynch_r ) asynch_r = new Rwin();
	var st = (styles[type] ? styles[type] : 'generic'); 
	t_div = asynch_r.open_div('jsContent',st);
	asynch_r.wrap('Content...',100,document.documentElement.scrollTop+20);
	asynch_r.set_html('Loading....');
	asynch_r.add_timer();
	asynch_r.asynch(type,'displayJSONContent',extra);
}

function db_div(s) {
	var div = get_div_by_id('db_div');
	if ( div ) {
		div.innerHTML = s;
//		div.onmouseout = hideDiv('db_div');
	}
}
/******************************************************
*
* OO interface to div open and manipulate funcs
*
******************************************************/

function Rwin() {}

Rwin.prototype.open_div = function(id,cl) {
	this.active_div = get_div_by_id(id,cl);
	return this.active_div;
}

Rwin.prototype.wrap = function(title,left,top) {
	var odiv = document.getElementById('div_wrapper');
	if ( !odiv ) {
		odiv = document.createElement('div');
		odiv.setAttribute('id','div_wrapper');
		odiv.className = 'div_wrapper';
		odiv.innerHTML = '<div id="drag_bar" class="drag_bar" onmousedown="this.style.cursor=\'move\'; start_drag(event,\'div_wrapper\')" onmouseup="this.style.cursor=\'default\'; stop_drag()"><div id="drag_title" >'+title+'</div><img src="/images/cross.png" alt="Close" border="0px" onclick="asynch_r.hide_wrapper()" /></div>'+select_kludge;
		document.getElementsByTagName('body')[0].appendChild(odiv);
	}
	odiv.style.width = (this.active_div.offsetWidth)+'px';
	odiv.style.height = (this.active_div.offsetHeight + 17)+'px';
	odiv.appendChild(this.active_div);
	this.wrapped = this.active_div;
	this.wrapper = odiv;
	if ( left ) this.position_div(this.wrapper,left,top);
	this.wrapper.style.visibility = 'visible';
	this.wrapped.style.visibility = 'visible';
	return odiv;
}


Rwin.prototype.position_div = function(div,x,y) {
	div.style.left = x+'px';
	div.style.top = y+'px';
}

Rwin.prototype.add_timer = function() {
	if ( this.wrapped ) {
		this.wrapper.onmouseout=delayed_hide_wrapper;
		this.wrapper.onmouseover=kill_timer;
	}
}

Rwin.prototype.asynch = function(handler,callback,params) {
	IncludeJavaScript('/srv/json.php/'+handler+'/'+params+'/'+callback);
}

Rwin.prototype.set_html = function(str) {
	this.active_div.innerHTML = str;
	if ( this.wrapper ) {
		this.wrapper.style.width = (this.active_div.offsetWidth)+'px';
		this.wrapper.style.height = (this.active_div.offsetHeight + 17)+'px';
	}

}

Rwin.prototype.set_title = function(str) {
	var db = get_div_by_id('drag_title');
	if ( db ) db.innerHTML = str;
}

Rwin.prototype.hide_wrapper = function() {
	if ( this.wrapper ) {
		document.getElementsByTagName('body')[0].removeChild(this.wrapper);
		this.wrapper.style.visibility = 'hidden';
		this.wrapped.style.visibility = 'hidden';
	}
}
