/*
 * Copyright 2002-2006 Jahia Ltd
 *
 * Licensed under the JAHIA SUSTAINABLE SOFTWARE LICENSE (JSSL), 
 * Version 1.0 (the "License"), or (at your option) any later version; you may 
 * not use this file except in compliance with the License. You should have 
 * received a copy of the License along with this program; if not, you may obtain 
 * a copy of the License at 
 *
 *  http://www.jahia.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
/**
* Constructor
*/
function TimeBasedPublishingStatus() {
}

TimeBasedPublishingStatus.prototype.run = 
  function (event, serverURL, objectKey, params, dialogTitle) {
    this._serverURL = serverURL;
    this._objectKey = objectKey;
    this._params = params;
    this._dialogTitle = dialogTitle;
    var theShell = DwtShell.getShell(window);
    if (theShell) {
        this._shell = theShell;
    } else {
        this._shell = new DwtShell("MainShell", true, null, userShell, true);
    }
    
    var obj = DwtUiEvent.getTarget(event);
    if (!obj) return false;
    
    var loc = Dwt.getLocation(obj);
    
    var jahiaCommand = new AsynchCommand();
    GlobalCallback[GlobalCallbackCount] = new AjxCallback(this, TimeBasedPublishingStatus.prototype._getTimeBasedPublishingStatusCallback,obj);
    jahiaCommand.addInvokeListener(GlobalCallback[GlobalCallbackCount]);
    GlobalCallbackCount++;

    var xmlDoc = AjxXmlDoc.createRoot("GetTimeBasedPublishingStatus");
    xmlDoc.set("objectKey", this._objectKey);
    xmlDoc.set("params", this._params);
    var myDate = new Date(); 
    xmlDoc.set("timeZoneOffset",myDate.getTimezoneOffset());
    xmlDoc.set("eventX", loc.x);
    xmlDoc.set("eventY", loc.y);
    xmlDoc.set("htmlElement", obj.id);
    jahiaCommand.invoke(xmlDoc, this._serverURL );
    
}

TimeBasedPublishingStatus.prototype._getTimeBasedPublishingStatusCallback 
  = function(args) {

    if ( !args[1] || args[1] instanceof AjxException ){
      alert("Exception during _getTimeBasedPublishingStatusCallback: " + args[1].dump());
    } else {  
      var ev = args[0];
    	var shell = DwtShell.getShell(window);
    	if ( shell ){
    	  var obj = DwtUiEvent.getTarget(ev);
    	  var xmlDoc = args[1].getDocXml();
        var textToDisplay = XmlUtils.getNodeValue(xmlDoc,'timeBasedPublishingDialog');
        textToDisplay = textToDisplay.replace("<![CDATA[","");
        textToDisplay = textToDisplay.replace("]]>","");
        var eventX = XmlUtils.getNodeValue(xmlDoc,'eventX');
        var eventY = XmlUtils.getNodeValue(xmlDoc,'eventY');
        var htmlElementId = XmlUtils.getNodeValue(xmlDoc,'htmlElement');
        var msgDialog = new DwtMessageDialog(shell, "TimeBasePublisningDwtMessageDialog");
        msgDialog.setMessage(textToDisplay,DwtMessageDialog.TIME_BASED_PUBLISHING_STYLE,this._dialogTitle);
        var el = document.getElementById(htmlElementId);        
        var x = getPageOffsetLeft(el);
        var y = getPageOffsetTop(el) + el.offsetHeight;
      
        // For IE, adjust position.
        if (AjxEnv.isIE) {
          x += el.offsetParent.clientLeft;
          y += el.offsetParent.clientTop;
        }
        var loc = new DwtPoint(x,y); 
        msgDialog.popup(loc);
        msgDialog.setLocation(x, y);
      } else {
        alert("Error: zimbra shell not found!");
      }
    }
}


