//*********************************************************************************************************
//	Function: siteeditresort()
//
//*********************************************************************************************************
//
function toggleselectbuttons()
{

}

function siteeditresort()
{
	//	Default size minimized
	listview_width	= 0;
	listview_height = 0;
	url				= '#';
	target			= 'resort';
		
	//	Open hidden window to process site edit sorting
	contentDetails( url, target, listview_width, listview_height );
	return false;
}

//*********************************************************************************************************
//	Function: selectEditAction()
//
//*********************************************************************************************************
CustomCommands = new Array();

function selectEditAction( se_id, cmd )
{
//---------------------------------------------------------------------------------------------------------
//	Get the action list information
//---------------------------------------------------------------------------------------------------------
	se_obj				= document.getElementById( se_id );
	article_id			= se_obj.getAttribute('articleid');
	article_type		= se_obj.getAttribute('articletype');
	uid					= se_obj.getAttribute('uid');
	target				= "se_details_" + article_id;
	usescrollbars		= '';
	
	//	Is the edit window already opened?
	//	If so focus it
	if ( contentDetailsFocus( target ) )
	{
		return;
	}

//---------------------------------------------------------------------------------------------------------
//	Determine if height and width needed
//---------------------------------------------------------------------------------------------------------
	switch ( cmd )
	{
		//	Edit or Copy need a details page height and width
		case 'E':
		case 'C':
			listview_height	= se_obj.getAttribute('dh');
			listview_width	= se_obj.getAttribute('dw');
		break;

		//	History
		case 'H':
			listview_width	= 600;
			listview_height	= 500;
		break; 

		//	Any other command needs nothing
		default:
			listview_width	= 0;
			listview_height = 0;
	}

//---------------------------------------------------------------------------------------------------------
//	Process the action
//---------------------------------------------------------------------------------------------------------
	//	Is the user doing an edit?
	switch( cmd )
	{
		//	Edit
		case 'E':
			//	No confirmation needed just do it
			url = "/contentmgr/article_details.php?id=" + article_id + '&uid=' + uid;
		break;

		//	History
		case 'H':
			//	No confirmation needed just do it
			url				= "/contentmgr/history_list.php?id=" + article_id;
			usescrollbars	= '1';
			target			= "ahist_" + article_id;
		break;

		//	Needs to be confirmed
		default:
			//	Otherwise, confirm action
			desc = getCmdDesc( cmd );
			if ( !confirm("Are you sure you want to " + desc.toLowerCase() + " this content?") )
			{
				return;
			}
			url = "/contentmgr/article_details.php?actions=" + cmd + "_" + article_id + "_" + article_type  + '&uid=' + uid;
		break;
	}

	//	Is there a custom command set for this action?
	if (CustomCommands[cmd])
	{
		url = CustomCommands[cmd];
	}

	//	Open the details window
	contentDetails( url, target, listview_width, listview_height, usescrollbars );
}

//*********************************************************************************************************
//	Function: contentDetailsFocus()
//
//*********************************************************************************************************
contentwindows = new Array();

function contentDetailsFocus( target )
{
	winobject = contentwindows[ target ];

	if ( winobject )
	{
		if ( !winobject.closed )
		{
			winobject.focus();
			return true;
		}
	}

	return false;
}

//*********************************************************************************************************
//	Function: contentDetails()
//
//*********************************************************************************************************
function contentDetails(content,target,sizeX,sizeY,usescrollbars,resizable)
{
	winobject = contentwindows[ target ];

	if ( winobject )
	{
		if ( !winobject.closed )
		{
			winobject.focus();
			return;
		}
	}

	if ( sizeX == 0 )
	{
		leftpos=2000;
		toppos =2000;
		
		//	Define the window size
		widthVar  = 'width=' + sizeX + ',';
		heightVar = 'height=' + sizeY + ',';
	}
	else
	{
		//	Center the window
		leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
		toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

		//	Define the window size
		widthVar  = 'width=' + sizeX + ',';
		heightVar = 'height=' + sizeY + ',';
	}

	//	Open the window
	winobject				= window.open(content,target,"menubar=0,statusbar=0,scrollbars=" + usescrollbars + ",toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos + ",resizable=" + resizable );
	contentwindows[target]	= winobject;
	winobject.focus();
}

//*********************************************************************************************************
//	Function: toggleSelectEdit()
//
//*********************************************************************************************************
var last_se_id = '';
var timeout_id = 0;

function toggleSelectEdit( se_obj, article_id )
{
//---------------------------------------------------------------------------------------------------------
//	Get the select edit list
//---------------------------------------------------------------------------------------------------------
	se_list_obj	= document.getElementById( 'se' );

//---------------------------------------------------------------------------------------------------------
//	
//---------------------------------------------------------------------------------------------------------
	//	No article id?
	if ( !article_id )
	{
		//	Celar any timeout if one active
		if ( timeout_id )
		{
			clearTimeout( timeout_id );
			timeout_id = 0;
		}

		//	Nope, is it being displayed?
		if ( se_list_obj.style.visibility == 'visible' );
		{
			//	Yep, close it
			se_list_obj.style.visibility = 'hidden';
			last_se_id  = '';
		}

		//	Do not allow zero article id
		return;
	}

//---------------------------------------------------------------------------------------------------------
//	Get the select edit list object, one for all select edit links
//---------------------------------------------------------------------------------------------------------
	se_id	= 'se_' + article_id;

//---------------------------------------------------------------------------------------------------------
//	Did the user click on another select edit while one was open for another article
//---------------------------------------------------------------------------------------------------------
	if ( se_id != last_se_id ) 
	{
		//	Another SE for article clicked?
		if ( se_list_obj.style.visibility == 'visible' )
		{
			//	Yep, open it
			se_list_obj.style.visibility = 'hidden';
		}

		//	Set the last se id clicked
		last_se_id = se_id;
	}

//---------------------------------------------------------------------------------------------------------
//	Let's setup the list for the select edit
//---------------------------------------------------------------------------------------------------------
	//	Is the pick list already displayed?
	if ( se_list_obj.style.visibility == 'hidden' )
	{

		//	Create the entries in the se list
		html		= '';
		cmds_string = se_obj.getAttribute('cmds');
		cmds_array	= cmds_string.split(',');
		vheight		= 0;

        // If edit is the only command available,
        // go ahead and run the action for it
        if(cmds_string === 'E')
        {
            selectEditAction(se_id, 'E');
            return;
        }

		//	Nope,  then show it
		//	Position the list below the button clicked
		se_list_obj.style.left = SE_getOffsetLeft( se_obj ) + 'px';
		se_list_obj.style.top  = ( SE_getOffsetTop( se_obj ) + se_obj.offsetHeight ) + 'px';
		se_list_obj.style.visibility = 'visible';

		for ( idx in cmds_array )
		{
			cmd		= cmds_array[idx];
			desc	= getCmdDesc( cmd );			
			html	+= '<li><a href="#" onclick="selectEditAction(\'' + se_id + '\',\'' + cmd + '\');return false;">' + desc + '</a></li>'
			vheight	+= 21;
		}
		
		//	Set the width and height
		se_list_obj.style.pixelWidth	= 100;
		se_list_obj.style.height		= vheight + 'px';

		se_list_obj.innerHTML		= html;

		//	If user doesn't select something in 5 seconds
		//	Then close the list
		if ( timeout_id )
		{
			clearTimeout( timeout_id );
			timeout_id = 0;
		}
		timeout_id = setTimeout("toggleSelectEdit(0)", 5000); 
	}
	else
	{
		if ( timeout_id )
		{
			clearTimeout( timeout_id );
			timeout_id = 0;
		}
		//	Already shown so hide it.
		se_list_obj.style.visibility = 'hidden';
	}
}

//*******************************************************************************************************
//	SE button positioning
//*******************************************************************************************************
function SE_getOffsetLeft(eElement)
{
//	eElement	= document.getElementById( eElement );

   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy

      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent is not a table or the body, then...
            nLeftPos += eParElement.clientLeft; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nLeftPos += 1;             // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nLeftPos += nParBorder;       // append the border width to counter
            }
         }
      }
      nLeftPos += eParElement.offsetLeft;    // append left offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nLeftPos;                          // return the number calculated
}

function SE_getOffsetTop(eElement)
{
//	eElement	= document.getElementById( eElement );
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nTopPos = eElement.offsetTop;         // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent a table cell, then...
            nTopPos += eParElement.clientTop; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nTopPos += 1;              // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nTopPos += nParBorder;        // append the border width to counter
            }
         }
      }

      nTopPos += eParElement.offsetTop;      // append top offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nTopPos;                           // return the number calculated
}


//*********************************************************************************************************
//	Function: getCmdDesc()
//
//*********************************************************************************************************
function getCmdDesc( cmd )
{
	switch( cmd )
	{
		case 'C':
			desc = 'Copy';
		break;

		case 'E':
			desc = 'Edit';
		break;

		case 'R':
			desc = 'Preview';
		break;

		case 'P':
			desc = 'Publish';
		break;

		case 'U':
			desc = 'Unpublish';
		break;

		case 'D':
			desc = 'Delete';
		break;

		case 'H':
			desc = 'History';
		break;

		default:
			desc = '';
	}

	return desc;
}

function viewpage( link )
{
	if (link)
	{
		window.open( link );
	}
}
