/*
    Calls the SavedAsset service to get Saved to My Active
*/

// ****************** Declarations  ****************************
var _constSavedAssetsUrl = "http://" + _RESTDomain + "/SavedAssets/SavedAssets.ashx";
var proxyUrl = "/NonAcm/Proxy.ashx";
var _isIE = false;
var _script;
var me;
var _xsl = "";
// ************************************************************

//Check the Browser Type
 if (navigator.appVersion.match(/\bMSIE\b/))
{
    _isIE = true;
}

function savedAssets()
{
    this.assetId = "";
    this.outputElmentId = "";
    this.getUsersWhoSaved = function()
                            {
                                //Get all of the users that have saved this asset to their My Saved Stuff
                                me = this;
                                isLoggedIn();
                                var postBodyValue = "";
                                postBodyValue = "<request>";
                                postBodyValue += "<assetId>" + this.assetId + "</assetId>";
                                postBodyValue += "<uId>" + _currentUserId + "</uId>";
                                postBodyValue += "<functionName>getUsers</functionName>";
                                postBodyValue += "<destinationUrl>" + _constSavedAssetsUrl + "</destinationUrl>";
                                postBodyValue += "<urlReferer>" + encodeURIComponent(location.href) + "</urlReferer>";
                                postBodyValue += "</request>";
                                
                                this.showWait(true);                  
                                var requestCall = new Ajax.Request(proxyUrl
                                                                    ,
                                                                        {
                                                                            method: "POST",
                                                                            contentType: "text/xml",
                                                                            postBody: postBodyValue,
                                                                            onSuccess: function(req)
                                                                                        {
                                                                                                document.getElementById(me.outputElmentId).innerHTML = req.responseText;
                                                                                                
                                                                                                
                                                                                 },
                                                                                 onFailure: function()
                                                                                        {
                                                                                            alert('Failed');
                                                                                        },
                                                                                 onComplete: function()
                                                                                        {
                                                                                            me.showWait(false);
                                                                                            
                                                                                        }       
                                                                     }
                                                         );
                            };
    this.Add = function()
                {
                    me=this;
                     this.showWait(true); 
                     isLoggedIn();
                    var postBodyValue = "";
                        postBodyValue = "<request>";
                        postBodyValue += "<assetId>" + this.assetId + "</assetId>";
                        postBodyValue += "<functionName>saveAsset</functionName>";
                        postBodyValue += "<uId>" + _currentUserId + "</uId>";
                        postBodyValue += "<destinationUrl>" + _constSavedAssetsUrl + "</destinationUrl>";
                        postBodyValue += "<urlReferer>" + encodeURIComponent(location.href)+ "</urlReferer>";
                        postBodyValue += "</request>";
                      
                        ShowWait(true);
                        var requestCall = new Ajax.Request(proxyUrl
                                                            ,
                                                                {
                                                                    method: "POST",
                                                                    contentType: "text/xml",
                                                                    postBody: postBodyValue,
                                                                    onSuccess: function(req)
                                                                                {
                                                                                    //Success
                                                                                    me.getUsersWhoSaved();
                                                                                    
                                                                                },
                                                                    onFailure: function(req)
                                                                                {
                                                                                    alert("We were unable to save this event at this time.\nPlease try again later.");
                                                                                },
                                                                    onComplete: function(req)
                                                                                {
                                                                                    //alert("Completed.");
                                                                                   me.showWait(false);
                                                                                }
                                                                 }
                                                            );
                };
    this.showWait = function(blnShow)
                    {
                        //Show the progress wait animation that is offset by the output element
                        var assetsList = $(me.outputElmentId);
                        var progressBar = $("divProgressBar");
                        
                        if (progressBar && assetsList)
                        {
                            // Position the progress bar relative to the list
                            // Left is the offset, plus half the width of the table, minus half the width of the image
                            var offset = assetsList.cumulativeOffset();
                            
                            if (blnShow) 
                            {
                                var left = assetsList.offsetLeft + 20;
                                 var top = assetsList.offsetTop; //IE
            
                                //Get the top position of the waiting animation
                                if (!_isIE)
                                {
                                    top = assetsList.top; //FF
                                }
                                
                                        
                                $(progressBar).setStyle
                                (
                                    {
                                        position: "relative", top: top + "px", left: left + "px"
                                    }
                                );
                                
                                progressBar.show();   
                                new Effect.Opacity(assetsList, { from: 1.0, to: 0.3, duration: 0 });
                            }
                            else 
                            {
                                progressBar.hide();    
                                new Effect.Opacity(assetsList, { from: .3, to: 1.0, duration: 0 });
                            }
                        }
    
                    };
}
function saveEvent(assetId, id)
{
    if (id == '')
    {
        id = "divPeopleContent";
    }
    
    var s = new savedAssets();
    s.assetId = assetId;
    s.outputElmentId = id;
    s.Add();
}

function setSavedIconCss(isSaved)
{
    //Set the css for the "Save to MyActive" Action link
    
    if (isSaved == 'True')
    {
        $("save").className = "saved " + $("save").className;
        $("saveLink").onClick = "";
        $("saveLink").innerHTML = "Saved";
        $("saveLink").title = "Saved";
    }
    
    
}