/*
 * Copyright (C) 2008-2010 APN, LLC. All rights reserved
 * 
 * Ask.com Toolbar - Widgets JavaScript module. Author: Tanmay Shrivastava 
 * Description: Contains functions and logic required for notificaitons /toolbar messaging /OOBE.
 */
var ATB_Notifications=function(){this._init()};ATB_Notifications.NOTIFICATION_XUL_PATH="chrome://asktoolbar/content/notification-popup.xul";ATB_Notifications.NOTIFICATION_XUL_PATH_FF3="chrome://asktoolbar/content/notification-popup-ff3.xul";ATB_Notifications.prototype._winId=new Date().getTime();ATB_Notifications.prototype._notificationWebProgressListener=null;ATB_Notifications.prototype._init=function(){var a=this;setTimeout(function(){a.showNotificationOnFirstTBLaunch()},1000)};ATB_Notifications.prototype._getPosition=function(e,b,d){var a="";switch(e){case"toolbar":var c=document.getElementById("asktb-btn-service-provider")||document.getElementById("navigator-toolbox");a=",top="+(c.boxObject.screenY+c.boxObject.height)+",left="+(c.boxObject.screenX);break;case"centerscreen":a=",centerscreen";break;case"systray":a=",top="+(window.screen.availHeight-b)+",left="+(window.screen.availWidth-d);break;default:a=",centerscreen"}return a};ATB_Notifications.prototype.notificationWin=null;ATB_Notifications.prototype.showNotificationOnFirstTBLaunch=function(){if((!ATB.Prefs.getBoolPref("notification-shown")&&!(ATB.Update.overInstallEntry=="su"||ATB.Update.overInstallEntry=="upd"))||ATB.Prefs.getBoolPref("force-show-notification")){ATB.Logger.info("Displaying a notification popup since this is either a first install/ overinstall OR show-notification is set to true");if(ATB.Config&&typeof(ATB.Config.notification)!="undefined"){this.openNotificationWindow(ATB.Config.notification);ATB.Prefs.setBoolPref("notification-shown",true);ATB.Prefs.setBoolPref("force-show-notification",false)}}};ATB_Notifications.prototype.openNotificationWindow=function(e){ATB.Logger.debug("ATB.Notifications.openNotification() called to open widget");if(!e||!e.url){ATB.Logger.warn("Problems with arguments to ATB.Notifications.openNotificationWindow, the obj or obj.url is null. This is required.");return}this.closePopupIfOpen();var k=document.getElementById("asktb-btn-service-provider")||document.getElementById("navigator-toolbox");var l=e.height?(e.height):300;var b=e.width?(e.width):300;var j=e.top?e.top:(k.boxObject.screenY+k.boxObject.height);var c=e.left?e.left:(k.boxObject.screenX);var g=e.position?this._getPosition(e.position,l,b):(",top="+j+",left="+c);var f="chrome=yes,titlebar=no,dependent=yes,resizable=no,alwaysraised=no,height="+l+",width="+b+g;var d=(ATB.Utils.getBrowserVersion()>3);var i={url:ATB.Core.getCompleteUrl(e.url),userClose:e.userClose?e.userClose:null,fadeTime:e.userClose?null:(e.fadeTime?e.fadeTime:null),anchor:d&&(e.anchor=="tl"||e.anchor=="tr"||e.anchor=="bl"||e.anchor=="br")?e.anchor:null,toastDir:d&&(e.toastDir=="Left"||e.toastDir=="Right"||e.toastDir=="Top"||e.toastDir=="Bottom")?e.toastDir:null};ATB.Logger.debug("Opening a notification window:",this._winId);try{var a=(d?ATB_Notifications.NOTIFICATION_XUL_PATH:ATB_Notifications.NOTIFICATION_XUL_PATH_FF3);this.notificationWin=window.openDialog(a,this._winId,f,i)}catch(h){ATB.Logger.error("Error while opening notificationWindow, Error :"+h)}};ATB_Notifications.prototype.closePopupIfOpen=function(){try{if(this.notificationWin&&!this.notificationWin.closed){ATB.Logger.debug("Closing the notification popup:",this.notificationWin.name);this.notificationWin.close()}}catch(a){ATB.Logger.error("Could not close notification window -",this.notificationWin.name,", Error:",a)}};ATB_Notifications.prototype.addProgressListenerOnWindow=function(b){this._notificationWebProgressListener=new ATB_CustomCommandListener(true);this._notificationWebProgressListener.containerWindow=this.notificationWin;this._notificationWebProgressListener.onStateChange=function(e,c,d,f){ATB_CustomCommandListener.prototype.onStateChange.call(this,e,c,d,f);if((d&Components.interfaces.nsIWebProgressListener.STATE_STOP)&&this.containerWindow&&c instanceof Components.interfaces.nsIHttpChannel&&c.originalURI.spec==this.containerWindow.arguments[0].url&&((Math.floor(c.responseStatus/100)==4)||(Math.floor(c.responseStatus/100)==5))){ATB.Logger.info("The notification url:",this.containerWindow.arguments[0].url,", returned a server error. Closing this popup.");this.containerWindow.close()}};try{b.addProgressListener(this._notificationWebProgressListener,Components.interfaces.nsIWebProgress.NOTIFY_STATE_ALL)}catch(a){ATB.Logger.error("Error while adding progress listener, Error :",a)}};ATB_Notifications.prototype.removeProgressListenerOnWindow=function(b){try{b.removeProgressListener(this._notificationWebProgressListener)}catch(a){ATB.Logger.error("Error while removing progress listener, Error :",a)}};