RhapsodyShareManager = {
  twitterHeight: 436,
  twitterWidth: 774,
  facebookHeight: 436,
  facebookWidth: 626,
  overlayWidth: 395,
  overlayHeight: 380,
  iframeWidth: 395,
  iframeHeight: 340,
  _shareMask: null,
  _windowVariant: null,
  genericJsonMgr: new Object(),
  instanceName: "RhapsodyShareManager",
  init: function(){
    cThis = this; /* the actual instance of "this" */
    cThis.genericJsonMgr = new genericXmlManager();
  },
  _utilGetWindowTop: function(height){
    var top = ((screen.height/2)-(height/2));
    return top;
  },
  _utilGetWindowLeft: function(width){
    var left = ((screen.width/2)-(width/2));
    return left;
  },
  _utilGetMediaTypeFlag: function(sharetype){
    /* needs to return an expected type. if it's not, fix and return. */
    switch(sharetype){
      case "video":
        return "musicvideo";
        break;
      default: /* is an expected type */
        return sharetype;
    }
  },
  _utilGetTwitterMessage: function( passedTitle, subtitle, sharetype ){
    var message = "";
    if(sharetype == "video" || sharetype == "musicvideo" || sharetype == "musicvideoplaylist"){
      message = "Watching "+unescape(passedTitle);
    }else{
      message = "Listening to "+unescape(passedTitle);
    }
    if(subtitle && subtitle != undefined && subtitle != " "){
      message+=" by "+unescape(subtitle);
    }
    return message;
  },
  _utilCreateShareableUrl: function( rcids , sharetype ){
    var shareableUrl = rhapsodyStreamURL + "/goto?rcid=" + rcids;
    var cleanShareType = this._utilSanitizeShareType(sharetype);
    
    if(cleanShareType == "album" || cleanShareType == "albums" || cleanShareType == "tracks" || cleanShareType == "track" || cleanShareType == "playlist" || cleanShareType == "radio"){
      shareableUrl += "&variant=play";
    }
    /* User playlists are different (playlist central playlists) */
    if( cleanShareType == "userplaylist"){
      shareableUrl = rhapsodyPlayURL + "/playlistcentral/playlistdetail?tracks=" + rcids;
    }
    /* and member playlists are different yet again (myrhapsody and member pages) */
    if(cleanShareType == "myrhapsodyplaylist"){
      var sharetype = sharetype.split("_");
      shareableUrl = rhapsodyStreamURL + "/_playlist?id=" + rcids;
      if(sharetype[1] && sharetype[1] != "undefined" && sharetype[1] != undefined){
        shareableUrl += "&u=" + sharetype[1];
      }
    }
    return shareableUrl;
  },
  _utilSanitizeShareType: function(sharetype){
    if(sharetype.indexOf("myrhapsodyplaylist") >= 0){
      var sharetype = sharetype.split("_");
      return sharetype[0];
    }else{
      return sharetype;
    }
  },
  shareItem: function( rcids, passedTitle, subtitle, shareto, sharetype ){
    switch(shareto){
      case "twitter":
        this._buildShareForTwitter( rcids, passedTitle, subtitle, sharetype );
        break;
      case "facebook":
        this._buildShareForFacebook( rcids, sharetype );
        break;
      default: /* rhapcom */
        this._buildShareForRhapcom( rcids, passedTitle, subtitle, sharetype );
    }
  },
  _buildShareForTwitter: function( rcids, passedTitle, subtitle, sharetype ){
    var msg = this._utilGetTwitterMessage( passedTitle, subtitle, sharetype ); 
    var longurl = this._utilCreateShareableUrl( rcids, sharetype );
    this._shareToTwitter( msg , longurl );
  },
  _buildShareForFacebook: function( rcids, sharetype ){
    this._shareToFacebook( this._utilCreateShareableUrl( rcids, sharetype ) );
  },
  _buildShareForRhapcom: function( rcids, passedTitle, subtitle, sharetype ){
    this._shareFromRhapcom( rcids, passedTitle, subtitle, sharetype );
  },
  _shareToTwitter: function( msg, longurl ){
    var passedUrl = longurl;
    var listeningToMsg = msg;
    var top = this._utilGetWindowTop(this.twitterHeight);
    var left = this._utilGetWindowLeft(this.twitterWidth);
    window.open(rhapsodyStreamURL +  "/share/twitter.html?msg="+encodeURIComponent(msg)+"&longurl="+encodeURIComponent(longurl), 'twittersharer', 'toolbar=0,status=0,width=774,height=436,top=' + top + ',left=' + left );
    RhapsodyShareManager._sendReportingEvent( 'Twitter' );
    return false;
  },
  _shareToFacebook: function( passedUrl ){
    var top = this._utilGetWindowTop( this.facebookHeight) ;
    var left = this._utilGetWindowLeft( this.facebookWidth );
    window.open( 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent( passedUrl ) + '&t=' + encodeURIComponent( document.title ), 'sharer', 'toolbar=0,status=0,width=626,height=436,top=' + top + ',left=' + left );
    RhapsodyShareManager._sendReportingEvent( 'Facebook' );
    return false;
  },
  _shareFromRhapcom: function( rcids, pTitle, subtitle, sharetype ){
    var passedTitle = unescape( pTitle );
    if( subtitle  && subtitle != " " && ( typeof subtitle != 'undefined' ) ){
      passedTitle += " - " + unescape( subtitle) ;
    }
    var mediaTypeFlag = this._utilGetMediaTypeFlag(sharetype);
    var passedUrl = this._utilCreateShareableUrl( rcids , sharetype );
    this._showShareOverlay( rcids, mediaTypeFlag, passedTitle, passedUrl );
    RhapsodyShareManager._sendReportingEvent( 'Rhapcom' );
  },
  _sendReportingEvent: function( serviceName ){
    RhapsodyTracking.sendLinkEvent( {
      eVar17: 'Nav: Click: Share: ' + serviceName,
      prop18: 'E: Nav: Click: Share: ' + serviceName
    },
    {
      type: 'custom',
      linkName: 'Navigation',
      events: 'event8'
    } );
  },
  renderButtons: function( rcids, passedTitle, subtitle, sharetype, size, disabled ){
    document.write( this._getButtonHtml( rcids , passedTitle, subtitle, sharetype, size, disabled ) );
  },
  renderButtonsJS: function( rcids, passedTitle, subtitle, sharetype, size, disabled ){
    return this._getButtonHtml( rcids , passedTitle, subtitle, sharetype, size, disabled );
  },
  _getButtonHtml: function( rcids, passedTitle, subtitle, sharetype, size, disabled ){
    if(passedTitle.indexOf("&#039;") > 0){
      /* there are some instances where this still gets through, catch and fix here */
      passedTitle = passedTitle.replace(/&#039;/g, "'");  
    }
    var imgDir = staticPath + "images/shareicons/";
    var sizeStateString = "";
    var theHtml="";
    var twitterIcon="";
    var facebookIcon="";
    var rhapcomIcon="";
    if( size == "" || ( typeof size == 'undefined' ) ){
      sizeStateString += "_med";
    } else {
      sizeStateString += "_" + size;
    }
    if( disabled ){
      sizeStateString+="_disabled";
      twitterIcon = imgDir + "icon_twitter" + sizeStateString + ".jpg";
      facebookIcon = imgDir + "icon_facebook" + sizeStateString + ".jpg";
      rhapcomIcon = imgDir + "icon_email" + sizeStateString + ".jpg";
      theHtml = 
        '<img src="' + facebookIcon + '" class="shareFacebook' + sizeStateString + '"/>' +
        '<img src="' + twitterIcon + '" class="shareTwitter' + sizeStateString + '"/>' + 
        '<img src="' + rhapcomIcon + '" class="shareRhapcom' + sizeStateString + '"/>';
    } else {
      twitterIcon = imgDir+"icon_twitter"+sizeStateString+".jpg";
      facebookIcon = imgDir+"icon_facebook"+sizeStateString+".jpg";
      rhapcomIcon = imgDir+"icon_email"+sizeStateString+".jpg";
      theHtml = 
        '<a href="#" onclick="RhapsodyShareManager.shareItem(\'' + rcids + '\' , \'' + escape(passedTitle) + '\', \'' + escape(subtitle) + '\', \'facebook\' , \'' + sharetype + '\'); return false;" title="Post to Facebook"><img title="Post to Facebook" src="' + facebookIcon + '" class="shareFacebook' + sizeStateString + '" \/><\/a>' +
        '<a href="#" onclick="RhapsodyShareManager.shareItem(\'' + rcids + '\' , \'' + escape(passedTitle) + '\', \'' + escape(subtitle) + '\', \'twitter\' , \'' + sharetype + '\'); return false;" title="Post to Twitter"><img title="Post to Twitter" src="' + twitterIcon + '" class="shareTwitter' + sizeStateString + '" \/><\/a>' +
        '<a href="#" onclick="RhapsodyShareManager.shareItem(\'' + rcids + '\' , \'' + escape(passedTitle) + '\', \'' + escape(subtitle) + '\', \'rhapcom\' , \'' + sharetype + '\'); return false;" title="Share by link or HTML"><img title="Share by link or HTML" src="' + rhapcomIcon + '" class="shareRhapcom' + sizeStateString + '"\/><\/a>';
    }
    return theHtml;
  },
  _showShareOverlay: function( rcids, mediaTypeFlag, passedTitle, passedUrl ){
    var overlayHtmlContent = '';
    var closeXSpan = '<div class="shareOverlayClose"><a href="#" onclick="RhapsodyShareManager.ModalDialogBoxManager.destroy(); return false;">Close <span>X<\/span><\/a><\/div>';
    var mShareOverlayIFrame = rhapsodyStreamURL + '/share/shareoverlay.html?prepop=overlay&mediaTypeFlag=' + mediaTypeFlag + '&mediaId=' +  rcids  + '&title=' + encodeURIComponent(passedTitle) + '&passedurl=' + encodeURIComponent(passedUrl);
    if( this.getWindowVariant() ){
      mShareOverlayIFrame += '&variant=' + this.getWindowVariant();
    }
    var closeXSpanBottom = '<div class="shareOverlayCloseBottom"><a href="#" onclick="RhapsodyShareManager.ModalDialogBoxManager.destroy(); return false;">Close<\/a><\/div>';
    overlayHtmlContent = closeXSpan;
    overlayHtmlContent += '<iframe id="shareOverlayIFrame" src="' + mShareOverlayIFrame + '" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="' + this.iframeWidth + '" height="' + this.iframeHeight + '" style="overflow: hidden; background-color: #0099CC;">';
      overlayHtmlContent += 'Your browser does not appear to support iframes.';
    overlayHtmlContent += '<\/iframe>';
    RhapsodyShareManager.ModalDialogBoxManager.init(overlayHtmlContent);
  },
  getWindowVariant: function(){
    return this._windowVariant;
  },
  setWindowVariant: function( wv ){
    this._windowVariant = wv;
  },
  ModalDialogBoxManager: {
    modalDialogBoxInstance: null,
    _dialogBoxOverrides: null,
    setDialogBoxOverrides: function( dbOverrideObj ){
      this._dialogBoxOverrides = dbOverrideObj;
    },
    init: function(overlayHtmlContent){
      if( overlayHtmlContent != null && overlayHtmlContent != 'undefined' && overlayHtmlContent != '' ){
        var dbWidth = ( this._dialogBoxOverrides && this._dialogBoxOverrides.dbWidth )? this._dialogBoxOverrides.dbWidth: RhapsodyShareManager.overlayWidth;
        var dbClassName = ( this._dialogBoxOverrides && this._dialogBoxOverrides.dbClassName )? this._dialogBoxOverrides.dbClassName: null;
        RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance = new DialogBox(
          {
            width: dbWidth + 'px',
            height: RhapsodyShareManager.overlayHeight+'px',
            maskOpacityPercent: 60,
            holdingElementBgColor: '#363636',
            zIndex: '999999999',
            maskBgColor: '#000',
            maskZIndex: '10000',
            htmlContent: overlayHtmlContent,
            elementClassName: dbClassName
          }
        );
        RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.centerInPage( .90 );
        RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.toggleMeddlesomeElements();
        RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.setMask( true );
        RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.setShadow( true );
        
        Event.observe( window, 'scroll', function(e){
          RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.centerInPage( .90 );
          RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.setMask( true );
        } );
        Event.observe( window, 'resize', function(e){
          RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.centerInPage( .90 );
          RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.setMask( true );
        } );
      }
    },
    destroy: function(){
      Event.unloadCache();
      RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.toggleMeddlesomeElements();
      RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance.destroy();
      RhapsodyShareManager.ModalDialogBoxManager.modalDialogBoxInstance = null;
    }
  }
}
RhapsodyShareManager.init();
/* for the flash player */
function ShareItem( rcids, passedTitle, subtitle, shareto, sharetype ){
  RhapsodyShareManager.shareItem( rcids, passedTitle, subtitle, shareto, sharetype );
}





