/***********************************************************
   AOL IM Javascript Tool v2.0.2005-2006
   Juan F. Sarria (JuanFerManNY@aol.com)
   Associate Software Engineer
   AOL Web Services and Publishing
   Documentation: http://www.aolexd.com/AIMThis/AIMThis.htm
************************************************************/

    function ShareThis() {
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        //        DEFAULT VALUES
        //
        this.setDefault = function () {
            //    Could be modified by the user
            this.sendTo  = ""
            this.message = "A Friend Wants You to See This";    // Default Message
            this.subject = "Hey, check this out from AOL";      // Default Subject.
            this.linkUrl = document.URL;                        // Default URL is the current page.
            this.linkTxt = "";                                  // Default Empty makes the link text the same link Url
            this.confirm = true;                                // Opens a pop-up window for non-AOL browsers asking for AIM confirmation
            this.thisPageName = "";

            //    Used internally
            this.msgBody     = "";
            this.linkTo      = "";
            this.pageTo      = ""
            this.thisPageUrl = document.URL.split("?")[0];
        }
        this.setDefault();

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        //        CONSTANTS
        //
        var uAgt    =    navigator.userAgent.toLowerCase();
        var isIE    =    uAgt.indexOf("msie")    != -1;
        var isAOL   =    uAgt.indexOf(" aol ")    != -1;
        var isSAFARI=    uAgt.indexOf("safari")    != -1;

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        //        FEATURE FUNCTIONS
        //
        //        For non-AOL browsers the message has to be encode
        this.encode    = function (sTxt) {
            if ( !isAOL ) {
                sTxt = sTxt.replace(/\</g, "%3C");
                sTxt = sTxt.replace(/\>/g, "%3E");
                sTxt = sTxt.replace(/\//g, "%2F");
                sTxt = sTxt.replace(/\"/g, "%22");
                sTxt = sTxt.replace(/ /g, "+");
            }
            return sTxt;
        }

        this.decodeEntities    = function (sTxt) {
            if ( typeof(sTxt) != "undefined" ) { 
                sTxt = sTxt.replace(/&apos;/g, "\'");
                sTxt = sTxt.replace(/&quot;/g, "\"");
                sTxt = sTxt.replace(/&amp;/g, "\&");
                sTxt = sTxt.replace(/&lt;/g, "\<");
                sTxt = sTxt.replace(/&gt;/g, "\>");
            }
            return sTxt;
        }

        //        Replace defaults with custom values
        this.setValues = function ( sMessage, sSubject, sLinkUrl, sLinkTxt, bConfirm, sThisPageName, sSendTo ) {
            this.setDefault();

            sLinkTxt = this.decodeEntities(sLinkTxt);
            sSubject = this.decodeEntities(sSubject);

            if ( typeof(sSendTo)  != "undefined" && sSendTo  != "" ) this.sendTo  = sSendTo;
            if ( typeof(sMessage) != "undefined" && sMessage != "" ) this.message = sMessage;
            if ( typeof(sLinkTxt) != "undefined" && sLinkTxt != "" ) this.linkTxt = sLinkTxt;
            if ( typeof(sLinkUrl) != "undefined" && sLinkUrl != "" ) this.linkUrl = sLinkUrl;
            if ( typeof(sSubject) != "undefined" && sSubject != "" ) this.subject = sSubject;
            if ( typeof(bConfirm) == "undefined" || bConfirm == "" ) this.confirm = true;
            if ( typeof(sThisPageName) != "undefined" && sThisPageName != "" ) this.thisPageName = sThisPageName;
            if ( this.linkTxt == "" ) this.linkTxt = this.linkUrl;    //    If the text for the link is empty then use the link url as text
        }

        //        Launch IM
        this.byAIM = function ( sMessage, sLinkUrl, sLinkTxt, bConfirm, sThisPageName, sSendTo ) {

            this.setValues( sMessage, '', sLinkUrl, sLinkTxt, bConfirm, sThisPageName, sSendTo );

            //    If the token LINK is not included in the message then add it at the end of it.
            if ( this.message.indexOf("-LINK-") == -1 ) this.message += " -LINK-";

            //    For non-AOL browsers the Link Url has to be escaped and the message encoded
            if (!isAOL ) {
                this.linkUrl = escape(this.linkUrl.replace(/\&/g, "%26"));
                this.linkTxt = this.linkTxt.replace(/ /g, "+").replace(/\&/g, "%2526");
                this.message = this.message.replace(/ /g, "+");
                this.thisPageName = this.thisPageName.replace(/ /g, "+").replace(/\&/g, "%2526");
            }

            //    Build the complete link
            if ( isSAFARI ) {
                this.linkTo = this.linkUrl;
                this.pageTo = this.thisPageUrl;
            } else {
                this.linkTo = this.encode('<a href="') + this.linkUrl + this.encode('">') + this.linkTxt + this.encode("</a>");
                this.pageTo = this.encode('<a href="') + this.thisPageUrl + this.encode('">') + this.thisPageName + this.encode("</a>");
            }
            
            //    Build the body of the message, which is; message plus link
            this.msgBody = this.message.replace(/\-LINK\-/g, this.linkTo);
            this.msgBody = this.msgBody.replace(/\-THISPAGE\-/g, this.pageTo);

            if ( isAOL ) {
                // Launch IM from AOL browser
                document.location = 'aol://9293:' + this.sendTo + ':' + this.msgBody;
            } else {
                // Launch IM from non-AOL browser
                if ( bConfirm && !isAOL )    {
                    myWin = this.OpenPopUp("http://cdn.digitalcity.com/_media/channels/jfs_msgr_aimthisconfwin20.html?message=" + this.msgBody + '&screenname=' + this.sendTo, "confirmWindow", 350, 100);
                } else {
                    this.OpenPopUp('aim:GoIm?message=' + this.msgBody + '&screenname=' + this.sendTo, "IM", 1,1).close();
                }
            }
        }

        //        Launch Email
        this.byEMAIL = function ( sMessage, sSubject, sLinkUrl, sThisPageName, sSendTo ) {
            this.setValues( sMessage, sSubject, sLinkUrl, '', false, sThisPageName, sSendTo );
            this.msgBody = this.message + " " + escape(this.linkUrl);
            this.msgBody = this.msgBody.replace(/\-THISPAGE\-/g, this.thisPageUrl);
            document.location = "mailto:" + this.sendTo + "?subject=" + this.subject + "&body=" + this.msgBody;
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        //        POP-UP FUNCTIONS
        //
        this.OpenPopUp = function (url, name, w, h) {
            //    Opens a centered window
            LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
            TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
            settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,resizable=no,menubar=no,toolbar=no,status=no,location=no';
            //    Assign the new window to an object and then return it
            var popWin = window.open(url,name,settings);
            popWin.focus();
            return popWin;
        }
    }


//************************************************************/

//---------------------------------------------------------------------------------------------------
//  MESSANGER ACTION FUNCTIONS
//---------------------------------------------------------------------------------------------------
//
var FlashMsgr = new ShareThis();

FlashMsgr.sendIM = function( sMessage, sLinkUrl, sLinkTxt, bConfirm, sThisPageName, sSendTo ) {
    this.byAIM( sMessage, sLinkUrl, sLinkTxt, bConfirm, sThisPageName, sSendTo );
}

FlashMsgr.sendEmail = function( sMessage, sSubject, sLinkUrl, sThisPageName, sSendTo ) {
    this.byEMAIL( sMessage, sSubject, sLinkUrl, sThisPageName, sSendTo );
}


