// abs_interface.js
// -------------------------------------------------------------------------------------------------
// Javascript functions to run the tabbed menu
// Uses Mootools
//
// Project    > dmABS
// Revision   > $Rev: 109 $
// Date		  > $Date: 2009-08-06 13:44:56 +0100 (Thu, 06 Aug 2009) $
// Author     > RAK for DML
// Copyright  > (C) Digital Mail Limited (2005-2010)
// -------------------------------------------------------------------------------------------------

// Add entry to library registration object, creating it if not present
if (typeof dmx_library != 'object') var dmx_library = new Object(); // Library registration object
if (typeof dmx_pg_init != 'object') var dmx_pg_init = new Object(); // Has page initialisation run?
dmx_library.abs_interface = true;
dmx_pg_init.abs_interface = false;

// -------------------------------------------------------------------------------------------------

var sm; // Object modelling active sub-menu

var menu_show;

var dally = new Object;		// Stores timing dependent events for menu mouseovers
dally.state;				// Set to true if the user dallies on a main menu tab (as opposed to skimming over it)
dally.tab;					// Holds the id of the last main menu tab the user dallied on
dally.time = 500;			// Defines the minimum time that constitues 'to dally', in milliseconds
dally.wait = 50;			// Defines the time before a mousover causes a submenu to change, in milliseconds
dally.timout_id;			// The id returned by the setTimeout call that might hide the dally

var abs_layer_store = new Array; // Stores references to DHTML objects
var abs_layer_count = 0;

var abs_editable = new Array; // Stores IDs of editable DIVS
var abs_edit_buttons;

// -------------------------------------------------------------------------------------------------

function abs_interface_init()
{
	var json_debug = Cookie.read('dmx_debug_settings');
	dmx_debug = json_debug ? JSON.decode(json_debug) : new Object;
	abs_toolbar_menu_init();
	abs_debug_display();
	var abs_content = Cookie.read('abs_content');
	abs_edit_buttons = abs_content.substring(abs_content.lastIndexOf('__') + 2);
	if (abs_edit_buttons == 'on') abs_highlight_editable();
}

// -------------------------------------------------------------------------------------------------

function sub_menu_show(id)
{
	if (dally.next != undefined) clearTimeout(dally.next);
	if (dally.timeout_id != undefined) clearTimeout(dally.timeout_id);
	if (id != 'sub_menu_content')
	{
		dally.next = setTimeout('dally.next_visible = true; do_sub_menu_show(\'' + id + '\'); ', dally.wait);
	}
	else
	{
		dally.next = setTimeout('dally.next_visible = true; sub_menu_hide(); ', dally.wait);
	}
	dally.state = false;
	dally.timeout_id = setTimeout('dally.state = true; dally.tab = \'' + id + '\';', dally.time);
}

function do_sub_menu_show(id)
{
	if (sm != undefined && sm.visible) sm.style.display ='none';
	if (id != 'sub_menu_content')
	{
		sm = $(id);
		sm.style.display = 'block';
	}
	sm.visible = true;
}

function sub_menu_hide()
{
	if (sm == undefined || !sm.visible) return;
	sm.style.display = 'none';
	sm.visible = false;
}

function abs_do_they_dally()
{
	// If the cursor enters the submenu bar just moments after showing a submenu, revert to the previously visible submenu
	// 'just moments' is defined by dally.time
	clearTimeout(dally.next);
	clearTimeout(dally.timeout_id);
	if (!dally.state && dally.next_visible)
	{
		sub_menu_hide();
		if (dally.tab != undefined) do_sub_menu_show(dally.tab);
	}
	dally.next_visible = false;
	dally.state = true;
}

// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------

function abs_show_sidepage(id, x, y)
{
	// Show the sidepage with the DOM ID id, after assigning it to abs_later_store
	// If x AND y are NOT specified, position the sidepage (depending on the
	// vertical scroll position of the browser window) so it is on screen
	
	if (!$defined(abs_layer_store[id]))
	{
		abs_layer_store[id] = $(id);
		if (!$defined(x)) var x = abs_layer_store[id].getStyle('left');
		if (!$defined(y)) var y = abs_layer_store[id].getStyle('top');
		abs_layer_store[id].dd = new Drag.Move(
			id,
			{
				handle: id + '_handle',
				onComplete: function(el)
				{
					if (parseInt(el.getStyle('top')) < -100) el.setStyle('top', '-100px');
				}
			}
		);
		abs_layer_store[id].hide = function()
		{
			this.setStyle('display', 'none');
		};
	}
	else
	{
		var x = abs_layer_store[id].getStyle('left');
		var y = abs_layer_store[id].getStyle('top');
	}
	
	if (parseInt(y) < window.getScrollTop())
	{
		y = window.getScrollTop();
	}
	
	abs_layer_store[id].setStyles( { display: 'block', visibility: 'visible', left: x, top: y , zIndex: 1000 + abs_layer_count } );
	abs_layer_count++;
}

// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// DUBUGGING TOOLBAR FUNCTIONS

function abs_toolbar_menu_init()
{
	var tbarmenu = $('tbarmenu_1');
	if (!tbarmenu) return;
	
	tbarmenu.getElements('a').each(
		function(ael)
		{
			ael.addEvent('click', function(event)
				{
					this.blur();
					if (this.href.slice(-1) != '#') return;
					event.preventDefault();
				}
			);
		}
	);
	
	// IE-specific fix
	if (Browser.Engine.trident)
	{
		tbarmenu.getElements('li').each(
			function(li)
			{
				li.addEvent('mouseover', function(event)
					{
						this.addClass('sfhover');
						var siblings = $(this).getAllPrevious('li');
						siblings.extend($(this).getAllNext('li'));
						siblings.each(
							function(li)
							{
								li.removeClass('sfhover');
							}
						);
					}
				);
				li.addEvent('mouseout', function(event)
					{
						this.removeClass('sfhover');
					}
				);
			}
		);
	}

}

// -------------------------------------------------------------------------------------------------

function abs_toolbar_show(bool)
{
	document.body.focus();
	bool == 1 ? 1 : 0;
	$('admin_toolbar').setStyle('display', bool ? 'block' : 'none');
	dmx_debug.toolbar_show 	= bool;
	abs_debug_update_cookie();
}

// -------------------------------------------------------------------------------------------------

function abs_debug_display()
{
	if (!$defined(dmx_debug.abs)) 	 	dmx_debug.abs   	= 0;
	if (!$defined(dmx_debug.xcom)) 	 	dmx_debug.xcom   	= 0;
	if (!$defined(dmx_debug.tst))  	 	dmx_debug.tst  		= 0;
	if (!$defined(dmx_debug.send_tstd))	dmx_debug.send_tstd	= 0;
	if (!$defined(dmx_debug.tstd))	 	dmx_debug.tstd		= '';
	if (!$defined(dmx_debug.pan))  	 	dmx_debug.pan  		= 0;
	if (!$defined(dmx_debug.s_goo_ip)) 	dmx_debug.s_goo_ip	= 1;
	if (!$defined(dmx_debug.s_goo_my)) 	dmx_debug.s_goo_my	= 0;
	
	var f = document.debug_options;
	f.abs.checked 	 			= dmx_debug.abs;
	f.xcom.checked   			= dmx_debug.xcom > 0;
	f.xcom_lev.selectedIndex 	= dmx_debug.xcom > 0 ? dmx_debug.xcom - 1 : 0;
	f.tst.checked 	 			= dmx_debug.tst;
	f.send_tstd.checked 		= dmx_debug.send_tstd;
	f.tstd.value 				= dmx_debug.tstd;
	f.pan.checked 	 			= dmx_debug.pan;
	f.s_goo_ip.checked 			= dmx_debug.s_goo_ip;
	f.s_goo_my.checked 			= dmx_debug.s_goo_my;
}

// -------------------------------------------------------------------------------------------------

function abs_debug_update()
{
	var f = document.debug_options;
	dmx_debug.abs 		= f.abs.checked		 	? 1 : 0;
	dmx_debug.xcom 		= f.xcom.checked * f.xcom_lev.options[f.xcom_lev.selectedIndex].value;
	dmx_debug.tst 		= f.tst.checked 		? 1 : 0;
	dmx_debug.send_tstd	= f.send_tstd.checked 	? 1 : 0;
	dmx_debug.tstd		= f.tstd.value;
	dmx_debug.pan 		= f.pan.checked 		? 1 : 0;
	dmx_debug.s_goo_ip	= f.s_goo_ip.checked	? 1 : 0;
	dmx_debug.s_goo_my	= f.s_goo_my.checked 	? 1 : 0;
	abs_debug_update_cookie();

	var devtag = $('devtag');
	if (devtag)
	{
		devtag.set('text', 'DEV TAG > ' + dmx_debug.tstd);
		devtag.setStyle('display', dmx_debug.send_tstd ? 'block' : 'none');
	}
}

// -------------------------------------------------------------------------------------------------

function abs_debug_update_cookie()
{
	var json_debug = JSON.encode(dmx_debug);
	Cookie.write( 'dmx_debug_settings', json_debug, { path: '/' } ); // SET PATH TO WHOLE DOMAIN
}

// -------------------------------------------------------------------------------------------------

function abs_clear_session(abs_root, e)
{
	// Stop TST ref being automatically added when link is followed
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	document.location.href = abs_root + '/clear.php?fwd=' + document.location.pathname;
}

// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------

function abs_toggle_edit_buttons()
{
	abs_edit_buttons == 'on' ? abs_unhighlight_editable() : abs_highlight_editable();
	// Store state, so whether edit buttons are on or off is persistent from page-to-page
	abs_content = Cookie.read('abs_content');
	abs_content = abs_content.substring(0, abs_content.lastIndexOf('__'));
	abs_content += '__' + abs_edit_buttons;
	Cookie.write('abs_content', abs_content, { duration: 1, path: abs_root ? abs_root : '/' });
}

// -------------------------------------------------------------------------------------------------

function abs_highlight_editable()
{
	var url_of_editor_script = abs_root + '/admin/editor.php';
	for (var i=0; i<abs_editable.length; i++)
	{
		var div = get_DOM_ref(abs_editable[i]);
		var id = div.getAttribute('id');
		var panel_path = id.substring(0, id.lastIndexOf('/') + 1);
		var panel_name = id.substring(id.lastIndexOf('/') + 1);
		var eb = document.createElement('a');
		eb.setAttribute('href', url_of_editor_script + '?panel_path=' + id + '&return_url=' + document.URL);
		eb.setAttribute('class', 'editbutton infotext');
		var tx = document.createTextNode('EDIT');
		var tip = document.createElement('span');
		tip.setAttribute('class', 'tipcontents');
		tip.innerHTML = '<b>' + panel_name + '</b><br />' + panel_path;
		eb.appendChild(tx);
		eb.appendChild(tip);
		switch(panel_name.substring(0,13))
		{
			case 'fragment--sp_':
				div = div.getElementsByTagName('div');
				div = div[0];
				div.className += ' editable_sp';
				break;
			default:
				div.className += ' editable';
		}
		div.insertBefore(eb, div.firstChild);
		if (b0_tips) b0_tips.attach(div.firstChild);
	}
	abs_edit_buttons = 'on';
}

// -------------------------------------------------------------------------------------------------

function abs_unhighlight_editable()
{
	for (var i=0; i<abs_editable.length; i++)
	{
		var div = get_DOM_ref(abs_editable[i]);
		var id = div.getAttribute('id');
		var panel_name = id.substring(id.lastIndexOf('/') + 1);
		switch(panel_name.substring(0,13))
		{
			case 'fragment--sp_':
				div = div.getElementsByTagName('div');
				div = div[0];
				break;
			default:
		}
		div.removeChild(div.firstChild);
		div.className = div.className.substring(0, div.className.lastIndexOf(' editable'));
	}
	abs_edit_buttons = 'off';
}

// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------

function abs_xajax(params, target_id)
{
	// Get an XComponent using AJAX
	
	// Common request nodes
	params['dmb1_SessRef'] 	= dmb1_session_ref;
	params['ajax']			= '1',
	params['response_type']	= 'html';
	
	// POST request
	var xajax = new Request.HTML( 
		{
			url: '/dmx/core/php/xcom_server.php',
			data: params,
			update: target_id
		}
	);
	xajax.post();
}
