

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// Copyright (c) 2006-2010 Skeletal Software Ltd, England.              //
// All rights reserved.                                                 //
//                                                                      //
// Redistribution or modification of the source is prohibited without	//
// the written consent of Skeletal Software Ltd.                        //
//                                                                      //
// This software is provided by Skeletal Software Ltd 'as is' and any	//
// express or implied warranties, including, but not limited to, the    //
// implied warranties of merchantability and fitness for a particular	//
// purpose are disclaimed.  In no event shall Skeletal Software Ltd. be //
// liable for any direct, indirect, incidental, special, exemplary, or	//
// consequential damages (including, but not limited to, procurement of //
// substitute goods or services; loss of use, data, or profits;         //
// or business interruption) however caused and on any theory of        //
// liability, whether in contract, strict liability, or tort (including //
// negligence or otherwise) arising in any way out of the use of this   //
// software, even if advised of the possibility of such damage.         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
// Panel Code
/////////////////////////////////////////////////////////////////////////////////
// establist scrolls

function ss_prevent_default(e)
{
	// prevent default action
	if (e)
	{
		if (e.preventDefault)
			e.preventDefault();		//DOM2
		else
			e.returnValue=false;	//IE
	}
}

/////////////////////////////////////////////////////////////////////////////////

function ss_add_listener(name,funct)
{
	if (document.addEventListener)
		document.addEventListener(name,funct,true); // netscape
	else if (document.attachEvent)
		document.attachEvent("on"+name,funct);
	else
		return false;
	return true;
}

/////////////////////////////////////////////////////////////////////////////////

function ss_remove_listener(name,funct)
{
	if (document.removeEventListener)
		document.removeEventListener(name,funct,true); // netscape
	else if (document.detachEvent)
		document.detachEvent("on"+name,funct);
	else
		return false;
	return true;
}

/////////////////////////////////////////////////////////////////////////////////
function ss_panel_kill(panel)
{
	panel.id='dead';
	panel.innerHTML='';
    panel.style.display='none';
	return null;
}
/////////////////////////////////////////////////////////////////////////////////

function ss_panel_create(x,y,width,html,title,on_close)
{
	var div=document.createElement('div');
	div.style.position='absolute';

	if (x==-1)
	{
		div.style.top='1px';
		div.style.left='1px';
	}
	else
	{	
		div.style.top=y+'px';
		div.style.left=x+'px';
	}

	// need to set the width 0 for IE so 
	div.style.width='0px';

	document.body.appendChild(div);
	var s="<table width='100%' cellspacing='0' cellpadding='0'>" +
			"<tr>" +
				"<td class='box_hl' height='20'></td>" +
				"<td class='box_hm' style='padding-right:4px;cursor:default' ><b>" + title + "</b></td>" +
				"<td class='box_hm'  align='right'>" +
					"[<span style='cursor:pointer'>x</span>]</td>" +
				"<td class='box_hr'></td>" +
			"</tr>" +
		"</table>" +
		"<table class='box_sides' cellspacing='0' cellpadding='0'>" +
			"<tr><td>" +
				"<div width='0px'>" +	
				html +
				"</div>" +
			"</td></tr>" +
		"</table>" +
		"<table width='100%' cellspacing='0' cellpadding='0'>" +
			"<td class='box_bl' height='8px'></td>" +
			"<td class='box_bm'></td>" +
			"<td class='box_br'></td>" +
		"</table>";

	// create panel
	div.innerHTML=s;

	// add drag event  on top table
	var t1=div.firstChild;
	t1.onmousedown=ss_panel_drag;

	// find kill event
	//           <table>    <tbody>    <tr>        <td>       <td>       <td>         [           x
	var kill=div.firstChild.firstChild.firstChild.firstChild.nextSibling.nextSibling.firstChild.nextSibling;
	kill.onclick=ss_panel_kill;

	// reset all elements to correct width because IE can not do it itself  
	//           <table>    <table>   <table>      
	var t2=div.firstChild.nextSibling;
	var t3=div.firstChild.nextSibling.nextSibling;
	//	         <tbody>    <tr>        <td>       <td>      
	var td3=t3.firstChild.firstChild.firstChild.nextSibling;
	if (t2.offsetWidth>t1.offsetWidth)
	{
		// re
		t1.style.width=t2.offsetWidth+'px';
		t3.style.width=t2.offsetWidth+'px';
		td3.style.width=(t2.offsetWidth-12)+'px'; // account for side sizes
	}

	if (x==-1)
	{
		var cx=(ss_client_width()-width)/2;
		var cy=(ss_client_height()-div.offsetHeight)/2+ss_scroll_top();
		if (cx<1) cx=1;
		if (cy<1) cy=1;
		div.style.top=cy+'px';
		div.style.left=cx+'px';
	}

	// kill box
	function ss_panel_kill(e)
	{
		if (div)
		{
			div.style.display='none';
			div.id='';
		}	
		div=null;	
		if (on_close)
		    on_close();
	}

	// drag panel by top bar
	function ss_panel_drag(event)
	{
		if (!event)	event=window.event;		
	    var x=event.clientX-parseInt(div.style.left);
	    var y=event.clientY-parseInt(div.style.top);

	    var ok=ss_add_listener("mousemove",ss_mouse_move_hander);
	    if (ok==true)
		    ok=ss_add_listener("mouseup",ss_mouse_up_hander);

	    if (ok)
	    {
		    ss_stop_propagation(event);
		    ss_prevent_default(event);
	    }
		// handler
		function ss_mouse_move_hander(e)
		{
			if (!e)	e=window.event;
			// prevent losing it off the screen	
			if ((e.clientX-x)>0 && (e.clientY-y)>0)
			{
				div.style.left=(e.clientX-x)+"px";
				div.style.top=(e.clientY-y)+"px";
				ss_stop_propagation(e);
			}
			else
			{
				ss_mouse_up_hander(e);
			}
		}

		// handle up event
		function ss_mouse_up_hander(e)
		{
			if (!e)	e=window.event;	
			ss_remove_listener("mousemove",ss_mouse_move_hander);
			ss_remove_listener("mouseup",ss_mouse_up_hander);
			ss_stop_propagation(e);
		}
	}

    return div;
}

/////////////////////////////////////////////////////////////////////////////////
// use with onmouseover='ss_popup_create(this,event,"html text")';
var g_popup_list=new Array();
function ss_popup_create(element,event,html,id)
{
	// defautl cursor type
	element.style.cursor='default';

	// create div
	var loc=ss_find_object_loc(element);
	// reuse same id if possible
	var div=null;
	if (id)         div=ss_get_object('_ss_popup_' +id);
	if (div==null)  div=document.createElement('div');
	div.style.position='absolute';
	div.style.display='none';
	// if set then avoid 2 up at same time
	if (id)
	{
	    g_popup_list[id]=div;   
	    div.id='_ss_popup_' +id;
	}
	document.body.appendChild(div);
	var s="<table class='box' cellspacing='0' cellpadding='0'>" +
			"<tr><td>" +
				html +
			"</td></tr>" +
		"</table>";

	// create popup
	div.innerHTML=s;

	// pop mouse on first entry
	ss_popup_mouseover(event);

	// mouse out call
	function ss_popup_mouseover(e)
	{
		if (!e)	e=window.event;

		// kill mouse event so that we don't get another one
		element.onmouseover=null;
		// activate popup in 2-3 seconds
		setTimeout(ss_popup_activate,500);		
	}
	
	// stop propoggation of div over
	function ss_popup_div_mouseover(e)
	{
		if (!e)	e=window.event;
		ss_stop_propagation(e);
	}

	// activate popup after 500ms
	function ss_popup_activate()
	{
		// if we are not waiting for mouse over then reveal
		if (element.onmouseover==null)
		{
			div.style.display='';

			// get locaition
			var loc=ss_find_object_loc(element);

			// find appropriate location for popup - chnage lcoation if off scrren
			var x=loc.x+(loc.width/2);
			if (x+div.offsetWidth>ss_client_width()+ss_scroll_left())
				x-=div.offsetWidth;
			var y=loc.y+(loc.height/2);
			if (y+div.offsetHeight>ss_client_height()+ss_scroll_top())
				y-=div.offsetHeight;

			div.style.top=y+'px';
			div.style.left=x+'px';

			ss_add_listener("mousemove",ss_mouse_move_hander);
			
			// remove other popups			
			for (var i in g_popup_list)
	        {	
		        var popup=g_popup_list[i];
		        if (i!=id)
		            popup.style.display='none';
	        }
		}
	}

	// actives when mouse leaves
	function ss_popup_mouseout()
	{
		div.style.display='none';
		element.onmouseover=ss_popup_mouseover;
		ss_remove_listener("mousemove",ss_mouse_move_hander);
	}

	// handler for mouse movement
	function ss_mouse_move_hander(e)
	{
		if (!e)	e=window.event;
		var x=e.clientX;
		var y=e.clientY;
		if (x>(loc.x+loc.width) || x<loc.x || y<loc.y || y>(loc.y+loc.height) )
		{
			// detach mouse
			ss_popup_mouseout();
		}
	}
	
}

/////////////////////////////////////////////////////////////////////////////////

