﻿// JScript File
function KGPublishingChoicesControl_UpdateMouseOver(_id)
{
  document.getElementById(_id).style.color = "#000e98";
}  

function KGPublishingChoicesControl_UpdateMouseOut(_id)
{
  document.getElementById(_id).style.color = "#535353";
} 

function KGPublishingChoicesControl_SelectionClick()
{
  //Call through to the existing publishing here with the appropriate variables.
  
  /*  The current control calls
   * KGPublishOptionControl_PublishClick(_object, _action, 
                                                  _originalKgId, 
                                                  _publishedKgId, 
                                                  _locationId,
                                                  e)
   */
}

function KGPublishingChoicesControl_CallbackTrigger_Wrapped(_action, 
                                                        _originalKgId, 
                                                        _publishedKgId, 
                                                        _locationId,
                                                        e,
                                                        _callbackScript)
{
  // debugger;
  var args;

  // Construct the callback arguments
  if(_action.length > 0)
    args = 'action=' + _action;
  
  if(_originalKgId > 0)
    args += '|originalKgId=' + _originalKgId;

  if(_publishedKgId != null && _publishedKgId.length > 0)
    args += '|publishedKgId=' + _publishedKgId;

  if(_locationId != null && (_locationId > 0 || _locationId == -1))
    args += '|locationId=' + _locationId;
    

  // Test if we are dealing with Remove or Publish action
  // YES: use the coords saved from gen-menu (1st callback), 
  //      coz we don't want to move the menu to the new event location
  //  NO: we are dealing with gen-menu action, so used the event location
  //      also we need to save the coords for subsequent actions (2nd callback)
  if(_action == 'generate')
  {
    if(e != null)
    {
      g_originalKgId = _originalKgId;
      g_locationId = _locationId;
      //if(_isOnCommunityTab.toLowerCase() == "true")
      //{
      //  g_eventX = e.clientX;
      //  g_eventY = e.clientY;
      //}
      //else
      {
        g_eventX = e.x || e.pageX;
        g_eventY = e.y || e.pageY;
      }
    }
  }
  
  args += '|eventX=' + g_eventX;
  args += '|eventY=' + g_eventY;
  
  if(args.length > 0)
  {      
    CancelBubble(e);
    eval(_callbackScript);
  }
}

function KGPublishingChoicesControl_CallbackDone_Wrapped(_args, _context, _isOnCommunityTab)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  // for firefox
  // set the current scroll position
//  if(Splitter1 != null && Splitter1.childNodes != null)
//  {
//    g_splitter1DivScrollTop = Splitter1.childNodes[0].scrollTop;
//    g_splitter1DivScrollLeft = Splitter1.childNodes[0].scrollLeft;
//  }


  var tokens = _args.split('|');
  if(tokens.length == 5)
  {
    var holder = document.getElementById(tokens[0]);
    //g_publishHolderId = tokens[0];
    var action = tokens[1];
    var eventX = tokens[2];
    var eventY = tokens[3];
    var content = tokens[4];
   
    if(holder != null)
    {
      holder.innerHTML = content;
      // eval javascripts
      var cdatas = _args.split('<![CDATA[');
      var z = 0;
      for(z = 1; z < cdatas.length; z++)
      {
	      var posStart = cdatas[z].indexOf('window');      
	      var posEnd = cdatas[z].lastIndexOf('}') + 1;
	      if(posStart > -1 && posEnd > -1)
	      {
	  	    if (posEnd < posStart)
	  	    {
	          posEnd = cdatas[z].lastIndexOf(']') + 1;
	          var jscript = cdatas[z].substring(posStart, posEnd);
		        if(jscript.length != 0)
		        {
		          eval(jscript);
		        }
	        }
	        else
	        {
		        var jscript = cdatas[z].substring(posStart, posEnd);
		        if(jscript.length != 0)
		        {
			        eval(jscript);
			        var posNameStart = jscript.indexOf('window');
			        var posNameEnd = jscript.indexOf('=');
			        if(posNameStart > -1 && posNameEnd > -1)
			        {
			          var functionName = jscript.substring(posNameStart, posNameEnd);
			          eval(functionName+'()');
			        }
		        }
		      }
	      }
      }
      
//      // for firefox
//     if(Splitter1 != null && Splitter1.childNodes != null && Splitter1.childNodes.length > 0)
//     {
//        if(Splitter1_pane_0.scrollTop != g_splitter1DivScrollTop)
//          Splitter1_pane_0.scrollTop = g_splitter1DivScrollTop;
//        
//        if(Splitter1_pane_0.scrollLeft != g_splitter1DivScrollLeft)
//          Splitter1_pane_0.scrollLeft = g_splitter1DivScrollLeft;
//     }


     ShowKGPublishingChoicesControl();
     
     //Bug 5997: position in middle of screen instead of alongside menu
     var control = document.getElementById('KGPublishingChoicesControl_DIV_ID') 
     eventY = ((screen.height - control.offsetHeight) / 2) + document.body.scrollTop;    //Display in middle of visible screen.  JPC. 
     eventX = (screen.width / 2) - (control.offsetWidth / 2);
           
      // Need to test for which page we are on to decided what co-ordinates to use:
      // MapTabbedView.aspx has an extra spliter pane which will mess up the normal event.x and event.y
      // so we have to use event.clientX and clientY to correctly position the popup relative to the 
      // extra spliter pane.
      // Options.aspx does not have the extra spliter pane so it can use the normal event.x and event.y
      // however, if we apply clientX and clientY the positioning is wrong for that page.
      // Hence, we need to detect where we are and apply the correct co-ordinates [PL05Mar2008]
      if(_isOnCommunityTab.toLowerCase() == "true")
      {
        //if(action == 'publish'|| action =='remove' || action == 'republishall' || action == 'unpublishall')
        //  window.setTimeout("KGPublishOptionControl_ShowAndReposition('absolute', " + parseInt(g_eventX) + 2 + "," + (parseInt(g_eventY) - 5) + ",'" + tokens[0] + "')", 1000);
        //else
          UpdateKGPublishingChoicesControlPos('absolute', parseInt(eventX) + 2, parseInt(eventY) - 5);
      }
      else 
      { 
        //if(action == 'publish'|| action =='remove' || action == 'republishall' || action == 'unpublishall')
        //  window.setTimeout("KGPublishOptionControl_ShowAndReposition('absolute', " + parseInt(g_eventX) + "," + (parseInt(g_eventY) + 5) + ",'" + tokens[0] + "')", 1000);
        //else
          UpdateKGPublishingChoicesControlPos('absolute', parseInt(eventX), parseInt(eventY) + 5);
      }

    }
    else
      KGPublishingChoicesControl_CallbackError('Content Div element is missing.');
  }
}


