function OpenMeHideOthers( opendiv )
{
	var divtoopen = document.getElementById( opendiv );
	
	var overview  = document.getElementById( 'overview' );
	var borders   = document.getElementById( 'borders' );
	var text	  = document.getElementById( 'text' );
	var bgrounds  = document.getElementById( 'backgrounds' );
	
	if ( opendiv == 'overview' )
	{
		overview.style.display = '';
		borders.style.display  = 'none';
		text.style.display     = 'none';
		bgrounds.style.display = 'none';
	}
	
	if ( opendiv == 'borders' )
	{
		overview.style.display = 'none';
		text.style.display     = 'none';
		borders.style.display  = '';
		bgrounds.style.display = 'none';		
	}
	
	if ( opendiv == 'text' )
	{
		overview.style.display = 'none';
		text.style.display     = '';
		borders.style.display  = 'none';	
		bgrounds.style.display = 'none';		
	}
	
	if ( opendiv == 'backgrounds' )
	{
		overview.style.display = 'none';
		text.style.display     = 'none';
		borders.style.display  = 'none';	
		bgrounds.style.display = '';		
	}	
}

function do_update_colours( mid )
{
	mid 			= parseInt( mid );

	var atype   	= document.getElementById( 'css_default' ).checked;
	var btype		= document.getElementById( 'css_imageset_default' ).checked;
	var ctype		= document.getElementById( 'css_myown' ).checked;
	
	var borderclr   		= document.getElementById( 'fahms_css_border_colour' ).value;
	var borderstyle 		= document.getElementById( 'border_styles' ).value;
	var cntboxclr   		= document.getElementById( 'fahms_css_contentbox_border' ).value;
	var tabonclr  		 	= document.getElementById( 'fahms_css_pp_tabon_border' ).value;
	var taboffclr  			= document.getElementById( 'fahms_css_pp_taboff_border' ).value;
	var maintitleclr   		= document.getElementById( 'fahms_css_maintitle_border' ).value;
	var ppnameclr   		= document.getElementById( 'fahms_css_pp_name_border' ).value;
	
	var tabontxt			= document.getElementById( 'fahms_css_pp_tabon_txt' ).value;
	var tabofftxt			= document.getElementById( 'fahms_css_pp_taboff_txt' ).value;
	var maintitletxt		= document.getElementById( 'fahms_css_maintitle_txt' ).value;
	
	var myabackbg   		= document.getElementById( 'fahms_css_myaback_bg' ).value;
	var cntboxbg   			= document.getElementById( 'fahms_css_contentbox_bg' ).value;
	var tabonbg  		 	= document.getElementById( 'fahms_css_pp_tabon_bg' ).value;
	var taboffbg  			= document.getElementById( 'fahms_css_pp_taboff_bg' ).value;
	var maintitlebg   		= document.getElementById( 'fahms_css_maintitle_bg' ).value;
	var ppnamebg   			= document.getElementById( 'fahms_css_pp_name_bg' ).value;	
	
	//alert( skin );
	
	if ( atype == true )
	{
		var typea = 'ipb';
	}
	
	else if ( btype == true )
	{
		var typea = 'imgset';
	}
	
	else
	{
		var typea = 'own';
	}
	
	var url2      = ipb_var_base_url + 'autocom=socialiser&CODE=profile-xml&mid=' + mid + '&code2=colours&update=1&type=' + escape( typea ) + '&bcolour=' + escape( borderclr ) + '&bstyle=' + escape( borderstyle ) + '&tonclr=' + escape( tabonclr ) + '&toffclr=' + escape( taboffclr ) + '&mtclr=' + escape( maintitleclr ) + '&ppnaclr=' + escape( ppnameclr ) + '&cntboxclr=' + escape( cntboxclr ) + '&tofftxt=' + escape( tabofftxt ) + '&tontxt=' + escape( tabontxt ) + '&mttxt=' + escape( maintitletxt ) + '&tonbg=' + escape( tabonbg ) + '&toffbg=' + escape( taboffbg ) + '&mtbg=' + escape( maintitlebg ) + '&ppnabg=' + escape( ppnamebg ) + '&cntboxbg=' + escape( cntboxbg ) + '&myabg=' + escape( myabackbg );
	
	do_request_function2 = function()
	{
		if ( ! xmlobj2.readystate_ready_and_ok() )
		{
			xmlobj.show_loading( 'Updating settings..' );
			// dimmer2( true );
			return;
		}
			// dimmer2(false)		
			xmlobj.hide_loading();							
	}
	
	xmlobj2 = new ajax_request();
	xmlobj2.onreadystatechange( do_request_function2 );
	
	xmlobj2.process( url2 );	
}

function updatecolour( id )
{
	itm = document.getElementById( id );
	
	if ( itm )
	{
		eval( 'newcol = document.colours.f' + id + '.value' );
		itm.style.backgroundColor = newcol;
	}
}

function poppicker( initcolor, formfield )
{
	if ( initcolor )
	{
		document.colours.initcol.value = initcolor;
	}
	
	document.colours.initformval.value = formfield;
	
	PopUp( 'skin_acp/IPB2_Standard/colours.html', 'PopPicker', 400, 500 );
}

function do_update( mid )
{	
	mid 			= parseInt( mid );
	var skin      	= document.getElementById( 'ahms_skins' ).value;
	var imgset      = document.getElementById( 'ahms_imgset' ).value;
	var atitle      = document.getElementById( 'ahms_page_title' ).value;
	var no_skina   	= document.getElementById( 'ahms_no_skin' ).checked;
	
	//alert( skin );
	
	if ( no_skina == true )
	{
		var no_skin = 1;
	}
	else
	{
		var no_skin = 0;
	}
	
	//alert( no_skin );	
	
	var url2      = ipb_var_base_url + 'autocom=socialiser&CODE=profile-xml&mid=' + mid + '&code2=look&update=1&title=' + escape( atitle ) + '&imageset=' + escape( imgset ) + '&skin=' + escape( skin ) + '&no_skin=' + escape( no_skin );
	
	do_request_function2 = function()
	{
		if ( ! xmlobj2.readystate_ready_and_ok() )
		{
			xmlobj.show_loading( 'Updating settings..' );
			// dimmer2( true );
			return;
		}
			// dimmer2(false)		
			xmlobj.hide_loading();							
	}
	
	xmlobj2 = new ajax_request();
	xmlobj2.onreadystatechange( do_request_function2 );
	
	xmlobj2.process( url2 );
}

function do_update_dynamic( mid )
{	
	mid = parseInt( mid );
	
	var titlea      = document.getElementById( 'ahms_image_title' ).value;
	var desc		= document.getElementById( 'ahms_image_desc' ).value;
	var image       = document.getElementById( 'ahms_image' ).checked;
	
	//alert( skin );
	
	if ( image == true )
	{
		image = 1;
	}
	else
	{
		image = 0;
	}
	
	//alert( no_skin );	
	
	var url2      = ipb_var_base_url + 'autocom=socialiser&CODE=profile-xml&mid=' + mid + '&code2=dynamic&update=1&image=' + escape( image ) + '&title=' + escape( titlea ) + '&desc=' + escape( desc );
	
	do_request_function2 = function()
	{
		if ( ! xmlobj2.readystate_ready_and_ok() )
		{
			xmlobj.show_loading( 'Updating settings..' );
			// dimmer2( true );
			return;
		}
			// dimmer2(false)		
			xmlobj.hide_loading();							
	}
	
	xmlobj2 = new ajax_request();
	xmlobj2.onreadystatechange( do_request_function2 );
	
	xmlobj2.process( url2 );
}

function show_loading( message )
{
	//------------------------------------------------
	// Change text?
	//------------------------------------------------
	
	if ( message )
	{
		document.getElementById( 'social-loading-layer-text' ).innerHTML = message;
	}
	
	centerdiv         = new center_div();
	centerdiv.divname = 'social-loading-layer';
	centerdiv.move_div();
	
	//------------------------------------------------
	// Add the shadow
	//------------------------------------------------
	
	//xmlobj.add_shadow('social-loading-layer-shadow', 'social-loading-layer-inner');
	
	//xmlobj = new ajax_request();
	return;
}

function hide_loading()
{
	document.getElementById( 'social-loading-layer' ).style.display = 'none';
	return;
}

function do_disable_dynamic( truefalse )
{
	var divone = document.getElementById( 'ahms_image_title' );
	var divtwo = document.getElementById( 'ahms_image_desc' );
	
	if ( truefalse == true )
	{
		divone.disabled = '';
		divtwo.disabled = '';
		
		divone.className = '';
		divtwo.className = '';		
	}
	else
	{
		divone.disabled = 'disabled';
		divtwo.disabled = 'disabled';
		
		divone.className = 'i_am_disabled';
		divtwo.className = 'i_am_disabled';
	}
}

function object_hide( truefalse, divname )
{
	var what = document.getElementById( divname );
	//alert( what.innerHTML );
	
	if ( truefalse == true )
	{
		what.style.display = 'none';
	}
	else
	{
		what.style.display = '';
	}	
}

function object_disabled( truefalse, divname )
{
	var what = document.getElementById( divname );
	//alert( what.innerHTML );
	
	if ( truefalse == true )
	{
		what.disabled = 'disabled';
	}
	else
	{
		what.disabled = '';
	}
}

function dimmer2(dim)
{
	var shallwe = document.getElementById('mypop-dimmer');
	
	if( dim == true )
	{
		shallwe.style.display='';
	}
	else
	{
		shallwe.style.display='none';
	}
}

/*--------------------------------------------*/
// INIT VARS
/*--------------------------------------------*/

var divobj;
var divopen  = 0;
var xmlobj;


// Widgets
var widget_handle = new Array();
var widget_main = new Array();
var widget_position = new Array();
var widget_ph = new Array();
var widget_ph_orgh = new Array();
var widget_ph_orgx = new Array();
var widget_ph_orgy = new Array();
var widget_ph_dragging = new Array();
var widget_anim = new Array();
//var widget_shim = new Array();
var widget_newpos = 0;
var widgets_left = 0;
var widgets_right = 0;
var screen_width = 0;
var screen_height = 0;
var screen_middle = 0;
var pos_ind;
var left;
var right;

// XML Popup
var socialiser_main;
var social_loaded   = 0;
var loaded_non_xml = 0;

function psettings( url, socialisertitle, is_myassis )
{
	var in_social_loaded = 0;
	
	if( ! is_myassis )
	{
		socialiser_main    = document.getElementById( 'get-social' );
		socialiser_drag    = document.getElementById( 'social-drag' );
		socialiser_content = document.getElementById( 'social-content' );
		socialiser_title   = document.getElementById( 'social-title' );
		socialiser_title.innerHTML = socialisertitle;
		in_social_loaded = social_loaded;
	}
	else
	{
		socialiser_main    = document.getElementById( 'get-myassistant' );
		socialiser_drag    = document.getElementById( 'myass-drag' );
		socialiser_content = document.getElementById( 'myass-content' );
		in_social_loaded = myass_loaded;
	}

	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		var html = xmlobj.xmlhandler.responseText;
		
//		if ( is_ie )
//		{
//			 html = "<iframe id='social-shim' src='javascript;' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; right:0px; display: none;'></iframe>" + html;
//		}
		
		socialiser_content.innerHTML = html;
		
//		if ( is_ie )
//		{
//			socialiser_shim               = document.getElementById('social-shim');
//			socialiser_shim.style.width   = socialiser_content.offsetWidth;
//			socialiser_shim.style.height  = socialiser_content.offsetHeight;
//			socialiser_shim.style.zIndex  = socialiser_content.style.zIndex - 1;
//			socialiser_shim.style.top     = socialiser_content.style.top;
//			socialiser_shim.style.left    = socialiser_content.style.left;
//			socialiser_shim.style.display = "block";
//		}
	
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	xmlobj.process( ipb_var_base_url + url );
  	
  	socialiser_main.style.position = 'absolute';
	//socialiser_main.style.display  = 'block';
	socialiser_main.style.zIndex   = 99;
	
	new Effect.Appear( $( 'get-social' ), { duration: 2.0 } );
	
	if ( ! in_social_loaded )
	{
		var socialiser_width  = 0;
		var socialiser_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' )
		{
			socialiser_width  = window.innerWidth;
			socialiser_height = window.innerHeight;
		}
		else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			socialiser_width  = document.documentElement.clientWidth;
			socialiser_height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			socialiser_width  = document.body.clientWidth;
			socialiser_height = document.body.clientHeight;
		}
		
		var divheight = parseInt( socialiser_main.style.height );
		var divwidth  = parseInt( socialiser_main.style.width );
		
		divheight = divheight ? divheight : 500;
		divwidth  = divwidth  ? divwidth  : 600;
		
		if( ! is_myassis )
		{
			var extratop = get_scrolling();
		}
		else
		{
			Drag.cookiename = 'ipb-myass-div';
			var divxy = my_getcookie( 'ipb-myass-div' );
			var extratop = 0;
		}
		
		var co_ords;
		
		if ( divxy && divxy != null )
		{
			co_ords = divxy.split( ',' );
			
			if ( co_ords.length )
			{
				var final_width  = co_ords[0];
				var final_height = co_ords[1];
				
				if ( co_ords[0] > socialiser_width )
				{
					final_width = socialiser_width - divwidth;
				}
				
				if ( co_ords[1] > socialiser_height )
				{
					final_height = socialiser_height - divheight;
				}
				
				socialiser_main.style.left = final_width  + 'px';
				socialiser_main.style.top  = final_height + 'px';
			}
		}
		else
		{
			socialiser_main.style.left = socialiser_width  / 2  - (divwidth / 2)  + 'px';
			socialiser_main.style.top  = ((socialiser_height / 2 - (divheight / 2)) + extratop) + 'px';
		}
		
		if( is_myassis )
		{
			Drag.cookiename = 'ipb-myass-div';
		}
		
		Drag.init( socialiser_drag, socialiser_main );
		
		in_social_loaded = 1;
	}
}

function get_scrolling()
{
	var yaxis = 0;
	
	if ( document.documentElement.scrollTop && document.documentElement )
	{
		yaxis = document.documentElement.scrollTop;
	}
	else if ( document.body.scrollTop && document.body )
	{
		yaxis = document.body.scrollTop;
	}
	else if ( window.pageYOffset )
	{
		yaxis = window.pageYOffset;
	}
	else if ( window.yaxis )
	{
		yaxis = window.yaxis;
	}
	
	return yaxis;
}

/*--------------------------------------------*/
// Init Drag/Drop Vars
/*--------------------------------------------*/
function widget_init_dragdrop_vars()
{
	right = document.getElementById( 'widget_right' );
	left = document.getElementById( 'widget_left' );
}

/*--------------------------------------------*/
// Content Block DragDrop Initialisation
/*--------------------------------------------*/

function widget_dragdrop_init( wid, pos, mid )
{
	widget_anim[ wid ] = setTimeout('null',1);
	widget_main[ wid ] = document.getElementById( 'widget_'+wid );
	
//	if ( is_ie )
//	{
//		widget_main[ wid ].innerHTML = "<iframe id='widget_"+wid+"-shim' src='javascript: false;' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;'></iframe>" + widget_main[ wid ].innerHTML;
//		widget_shim[ wid ] = document.getElementById('widget_'+wid+'-shim');
//	}
	
	widget_handle[ wid ] = document.getElementById( 'widget_'+wid+'-handle' );
	widget_ph[ wid ] = document.getElementById( 'widget_'+wid+'-ph' );
	widget_ph_orgh[ wid ] = parseInt( widget_ph[ wid ].offsetHeight );
	
	if ( pos=='left' )
	{
		widgets_left++;
		widget_position[ wid ] = 'l';
	}
	else
	{
		widgets_right++;
		widget_position[ wid ] = 'r';
	}
	
	widget_ph_dragging[ wid ] = 0;

	Drag.keeponscreen = false;
	Drag.init( widget_handle[ wid ], widget_main[ wid ] );
	widget_main[ wid ].onDragStart = function(x, y) { widget_Start( wid, x, y, mid ) };
	widget_main[ wid ].onDragEnd = function(x, y) { widget_End( wid, x, y, mid ) };
	widget_main[ wid ].onDrag = function(x, y) { widget_Move( wid, x, y, mid ) };
}

/*--------------------------------------------*/
// Content Block DragDrop start
/*--------------------------------------------*/

function widget_Start( wid, x, y, mid )
{
	widget_ph_orgx[ wid ] = x;
	widget_ph_orgy[ wid ] = y;
	widget_ph_dragging[ wid ] = 0;

	if ( typeof( window.innerWidth ) == 'number' )
	{
		screen_width  = window.innerWidth;
		screen_height  = window.innerHeight;
	}
	else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		screen_width  = document.documentElement.clientWidth;
		screen_height  = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		screen_width  = document.body.clientWidth;
		screen_height  = document.body.clientHeight;
	}
	screen_middle = parseInt( ( screen_width / 2 ) - ( widget_main[ wid ].offsetWidth / 2) );
}

/*--------------------------------------------*/
// Clear the Block's space when dragging
/*--------------------------------------------*/

function ClearSpace( wid )
{
    clearTimeout( widget_anim[ wid ] );
	ph_obj = widget_ph[ wid ];
	
	if ( widget_ph_orgh[ wid ] == 0 )
	{
		widget_ph_orgh[ wid ] = parseInt( ph_obj.offsetHeight );
	}
	
    new_h = parseInt( ph_obj.offsetHeight ) - 5;
    
    if ( new_h < 0 )
    {
    	new_h = 0;
    }
    
    ph_obj.style.height = new_h + 'px';
    
	if ( new_h > 0 )
	{
		widget_anim[ wid ] = setTimeout('ClearSpace( ' + wid + ' )', 10);
	}
}

/*--------------------------------------------*/
// Content Block DragMove
/*--------------------------------------------*/

function widget_Move( wid, x, y, mid )
{
	if ( widget_ph_dragging[ wid ]==0 && ( (x-widget_ph_orgx[ wid ]) > 3 || (x-widget_ph_orgx[ wid ]) < -3 || (y-widget_ph_orgy[ wid ]) > 3 || (y-widget_ph_orgy[ wid ]) < -3 ) )
	{
		widget_ph_dragging[ wid ] = 1;

		widget_ph[ wid ].style.width = widget_ph[ wid ].offsetWidth + 'px';
		widget_ph[ wid ].style.height = widget_ph[ wid ].offsetHeight + 'px';

//		if ( is_ie )
//		{
//			widget_shim[ wid ].style.width = widget_main[ wid ].offsetWidth + 'px';
//			widget_shim[ wid ].style.height = ( widget_main[ wid ].offsetHeight - 15 ) + 'px';
//			widget_shim[ wid ].style.zIndex = -1;
//			widget_shim[ wid ].style.display = "block";
//		}

		widget_main[ wid ].style.width = widget_main[ wid ].offsetWidth + 'px';
		widget_main[ wid ].style.height = widget_main[ wid ].offsetHeight + 'px';
		widget_main[ wid ].style.left = _get_obj_leftpos(widget_main[ wid ]) + 'px';
		widget_main[ wid ].style.top = _get_obj_toppos(widget_main[ wid ]) + 'px';
		widget_main[ wid ].style.position = 'absolute';
		widget_main[ wid ].style.display = 'block';
		widget_main[ wid ].style.zIndex = 3;

		pos_ind = document.createElement("div");
		pos_ind.style.height = '3px';
		pos_ind.style.width = widget_main[ wid ].offsetWidth + 'px';
		pos_ind.style.fontSize = '0px';
		pos_ind.style.left = _get_obj_leftpos(widget_main[ wid ]) + 'px';
		pos_ind.style.top = (_get_obj_toppos(widget_main[ wid ]) - 9 ) + 'px';
		pos_ind.style.position = 'absolute';
		pos_ind.style.backgroundColor = '#000000';
		pos_ind.style.zIndex = 2;

		if ( widget_position[ wid ]=='l' )
		{
			left.appendChild( pos_ind );
		}
		else
		{
			right.appendChild( pos_ind );
		}

		x = _get_obj_leftpos( widget_main[ wid ] );
		y = _get_obj_toppos( widget_main[ wid ] );
		ClearSpace( wid );
	}

	if ( widget_ph_dragging[ wid ]==1 )
	{
		if ( x < screen_middle )
		{
			pos = 'l';
		}
		else
		{
			pos = 'r';
		}

		new_id = Get_NewWidgetPosition( wid, x, y, pos );
		
		if ( new_id > 0 )
		{
			pos_ind.style.left =  _get_obj_leftpos( widget_ph[ new_id ] ) + 'px';
			pos_ind.style.top = ( _get_obj_toppos( widget_ph[ new_id ] ) - 9 ) + 'px';
			pos_ind.style.width = widget_main[ wid ].offsetWidth + 'px';
			pos_ind.style.height = '3px';
		}
		else
		{
			bb_id = Get_Bottom( wid, pos );
			if ( bb_id > 0 )
			{
				pos_ind.style.width = widget_main[ wid ].offsetWidth + 'px';
				pos_ind.style.height = '3px';
				pos_ind.style.left = _get_obj_leftpos( widget_ph[ bb_id ]) + 'px';
				pos_ind.style.top = ( _get_obj_toppos( widget_ph[ bb_id ]) + widget_ph[ bb_id ].offsetHeight - 9 ) + 'px';
			}
			else
			{
				pos_ind.style.width = '3px';
				if ( pos == 'l' )
				{
					pos_ind.style.left = 8 + 'px';
				}
				else
				{
					pos_ind.style.left = ( screen_width - 8 ) + 'px';
				}
				if ( widget_position[ wid ] == 'l' )
				{
					pos_ind.style.top = ( _get_obj_toppos( left ) - 9 ) + 'px';
					pos_ind.style.height = ( screen_height - _get_obj_toppos( left ) + 9 ) + 'px';
				}
				else
				{
					pos_ind.style.top = ( _get_obj_toppos( right ) - 9 ) + 'px';
					pos_ind.style.height = ( screen_height - _get_obj_toppos( right ) + 9 ) + 'px';
				}
			}
		}
	}
}

function Get_Bottom( wid, pos )
{
	new_y = 0;
	bb_id = 0;
	
	for ( var cb_id in widget_ph )
	{
		if ( cb_id != wid && widget_position[ cb_id ] == pos )
		{
			ph_y = _get_obj_toppos( widget_ph[ cb_id ] );
			
			if ( ph_y > new_y )
			{
				//alert ( 'We passed the new_y' );
				new_y = ph_y;
				bb_id = cb_id;
			}
		}
	}
	
	return bb_id;
}

function Get_NewWidgetPosition( wid, x, y, pos )
{
	new_y = 0;
	new_id = 0;
	for (var cb_id in widget_ph)
	{
		if ( cb_id != wid && widget_position[cb_id]==pos )
		{
			ph_y = _get_obj_toppos( widget_ph[ cb_id ] );

			//alert( ph_y );
			
			if ( y < ph_y && (new_y > ph_y || new_id==0) )
			{
				//alert ( 'We passed the y' );
				new_y = ph_y;
				new_id = cb_id;
				
				//alert( new_y + '<br />' + new_id );
			}
		}
	}
	return new_id;
}

/*--------------------------------------------*/
// Content Block DragDrop end
/*--------------------------------------------*/

function widget_End( wid, x, y, mid )
{
	if ( widget_ph_dragging[ wid ] )
	{
		if ( x < parseInt( ( screen_width / 2 ) - ( widget_main[ wid ] . offsetWidth / 2 ) ) )
		{
			pos = 'l';
			bar = left;
		}
		else
		{
			pos = 'r';
			bar = right;
		}

		pos_ind.style.display = 'none';
		
		if ( widget_position[ wid ] == 'l' )
		{
			left.removeChild( pos_ind );
			left.removeChild( widget_ph[ wid ] );
			widgets_left--;
		}
		else
		{
			right.removeChild( pos_ind );
			right.removeChild( widget_ph[ wid ] );
			widgets_right--;
		}

		ph_obj = document.createElement("div");
		ph_obj.style.height = '20px';
		ph_obj.appendChild( widget_main[ wid ] );
		
		blank_div = document.createElement("div");
		blank_div.style.height = '10px';		

		new_id = Get_NewWidgetPosition( wid, x, y, pos );
		
		//alert( new_id );
		
		if ( new_id > 0 )
		{
			if ( ! is_ie )
			{
				//bar.insertBefore( blank_div, ph_obj );
			}
			
			bar.insertBefore( ph_obj, widget_ph[ new_id ] );
		}
		else
		{
			if ( ! is_ie )
			{
				//bar.insertBefore( blank_div, ph_obj );
			}
						
			bar.appendChild( ph_obj );
			//bar.insertBefore( ph_obj, widget_ph[ new_id ] );			
		}

		widget_ph[ wid ] = ph_obj;
		widget_position[ wid ] = pos;
		
		if ( pos == 'l' )
		{
			widgets_left++;
		}
		else
		{
			widgets_right++;
		}

//		if ( is_ie )
//		{
//			widget_shim[ wid ].style.display = "none";
//		}
		
		//alert( new_id );

		check_left_and_right();

		do_request_function = function()
		{
			if ( ! xmlobj.readystate_ready_and_ok() )
			{
				xmlobj.show_loading();
				return;
			}
			
			xmlobj.hide_loading();

			var widgetstatus = xmlobj.xmlhandler.responseText;
			
			if ( widgetstatus == "error" )
			{
				alert( 'The Widget would not save its new position!' );
			}
			
//			else
//			{
//				alert( xmlobj.xmlhandler.responseText );
//			}
		};

		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );

		xmlobj.process( ipb_var_base_url + 'autocom=socialiser&CODE=reorder-widgets&mid=' + mid + '&wid=' + wid + '&pos=' + pos + '&newid=' + new_id );

		ReturnWidget( wid );
		//alert ( new_id );
	}
}

function ReturnWidget( wid )
{
	clearTimeout( widget_anim[wid] );

	ph_obj = widget_ph[wid];
    new_h = parseInt(ph_obj.offsetHeight) + 10;
   // alert ( new_h );
    if ( new_h > widget_ph_orgh[wid] )
    {
    	new_h = widget_ph_orgh[wid];
    	//alert ( new_h );
    }
    ph_obj.style.height = new_h + 'px';

    orgx = _get_obj_leftpos( widget_ph[wid] );
    orgy = _get_obj_toppos( widget_ph[wid] );
    curx = parseInt( widget_main[wid].style.left );
    cury = parseInt( widget_main[wid].style.top );
    
    // alert (orgx);
    // alert (curx);

    if ( curx < orgx )
    {
    	curx += 15;
    	if ( curx > orgx ) { curx = orgx; }
    }
	else if ( curx > orgx )
	{
		curx -= 15;
    	if ( curx < orgx ) { curx = orgx; }
	}

    if ( cury < orgy )
    {
    	cury += 15;
    	if ( cury > orgy ) { cury = orgy; }
    }
	else if ( cury > orgy )
	{
		cury -= 15;
    	if ( cury < orgy ) { cury = orgy; }
	}
	
	widget_main[ wid ].style.left = curx + 'px';
	widget_main[ wid ].style.top = cury + 'px';

	if ( new_h < widget_ph_orgh[wid] || orgx != curx || orgy != cury )
	{
		widget_anim[wid] = setTimeout('ReturnWidget( '+wid+' )', 20);
	}
	else
	{
		widget_main[wid].style.position = 'static';
	}
}

/*--------------------------------------------*/
// Close Content Window!
/*--------------------------------------------*/
function close_widget( id )
{
	document.getElementById( "widget-" + id ).style.display = 'none';
}

/*--------------------------------------------*/
// Delete Widget
/*--------------------------------------------*/
function delete_a_widget( wid, mid )
{
	if ( confirm( 'Are you sure you want to remove this Widget?' ) )
	{	
		do_request_function = function()
		{
			if ( ! xmlobj.readystate_ready_and_ok() )
			{
				xmlobj.show_loading();
				return;
			}
			
			xmlobj.hide_loading();
	
			var widgetstatus = xmlobj.xmlhandler.responseText;
			
			if ( widgetstatus == 'error' )
			{
				alert( 'This Widget failed to remove itself!' );
			}
			
			else
			{
				if ( widget_position[ wid ] == 'l' )
				{
					new Effect.Fade( $( widget_ph[ wid ] ), { duration: 1.5 } );
					// left.removeChild( widget_ph[ wid ] );
					widgets_left--;
				}
				else
				{
					new Effect.Fade( $( widget_ph[ wid ] ), { duration: 1.5 } );
					//right.removeChild( widget_ph[ wid ] );
					widgets_right--;
				}
				
				ClearSpace( wid );
				
				check_left_and_right();
			}
		};
	}

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );

	xmlobj.process( ipb_var_base_url + 'autocom=socialiser&CODE=delete-widget&mid=' + mid + '&wid=' + wid );
}

function add_widget( wid, mid )
{
	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();

		// Retrieve our info
		var ahtml = xmlobj.xmlhandler.responseText;
		
		if ( ahtml != 'too_many' )
		{
			var output3 = ahtml.split('<!--SPLIT-->');
			// alert( ahtml );
							
			newa = document.createElement( 'div' );
			newa.innerHTML = output3[0];
			newa.id = 'widget_' + output3[1] + '-ph';
			
			//newa = "<br />" + newa;
			
			// Javascripts //
			left.appendChild( newa );
			new Effect.Appear( $( 'widget_' + output3[1] + '-ph' ), { duration: 1.5 } );
						
			widgets_left++;
			
			check_left_and_right();
			
			var item = document.getElementById( 'witem-' + wid );
			new Effect.Fade( $( 'witem-' + wid ), { duration: 1.5 } );
			
			// Remove the block from the XML menu
			// var menu = document.getElementById( 'widget_menu' );	
			// var item = document.getElementById( 'witem-' + wid );
			
			// menu.removeChild( item );
						
			widget_main[ output3[1] ] = newa;
			widget_dragdrop_init( output3[1], 'left', mid );						
		}
		
		else
		{
			alert( 'You cannot add any more widgets, you are at the maximum limit set by the Board Administrator' );
		}
	};

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );

	xmlobj.process( ipb_var_base_url + 'autocom=socialiser&CODE=add-widget&wid=' + wid + '&mid=' + mid );
}

function remove_this( item, menu )
{
	menu.removeChild( item );	
}

function check_left_and_right()
{
	if ( widgets_left > 0 && left.style.display == "none")
	{
		left.style.display="";
	}
	if ( widgets_left == 0 && left.style.display == "")
	{
		left.style.display="";
	}
	if ( widgets_right >= 0 && right.style.display == "none")
	{
		right.style.display="";
	}
	if ( widgets_right == 0 && right.style.display == "")
	{
		right.style.display="";
	}
}
