﻿
// Add to My W

function AddToMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID) {

    var wuser = GetWNetworkUserCookie();
    var wsUrl = WebServiceUrl + '/AddToMyW';
    var wsData = "{'ContentType':'" + ContentType + "','ContentID':" + ContentID + "}";

    // Make a different call when adding blog posts to favourites. It needs to store additional
    // information in the database.
    if (ContentType.toLowerCase() == 'blog') {
        var metadata = $(sender).attr('data');
        wsUrl = WebServiceUrl + '/AddBlogToMyW';
        wsData = "{'ContentType':'" + ContentType + "','ContentID':" + ContentID + ",'MetaData':'" + metadata + "'}";
    }
    
    if (wuser != null && wuser != '') {
        $.ajax({
            type: "POST",
            url: wsUrl,
            data: wsData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(result) {
                if (result.d != undefined && result.d != null) {
                    ChangeStatus(sender, true, AddImageUrl, RemoveImageUrl);
                }
            },
            complete: function(XMLHttpRequest, textStatus) {
                //alert(XMLHttpRequest.status + ' ' + textStatus);
                //alert(XMLHttpRequest.responseText);
            }
        });
    }
    else {
        ShowLoginPopup(function() {
            var wuser2 = GetWNetworkUserCookie();
            if (wuser2 != null && wuser2 != '') {
                AddToMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID);
            }
        });
    }
}

function ChangeStatus(sender, added, AddImageUrl, RemoveImageUrl) {
    if (added) {
        $(sender).attr('src', RemoveImageUrl);
        $(sender).removeClass('AddToMyW');
        $(sender).addClass('RemoveFromMyW');
    }
    else {
        $(sender).attr('src', AddImageUrl);
        $(sender).removeClass('RemoveFromMyW');
        $(sender).addClass('AddToMyW');
    }
}

function CheckMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID, CSID) {

    $.ajax({
        type: "POST",
        url: WebServiceUrl + '/CheckMyW',
        data: "{'ContentType':'" + ContentType + "','ContentID':" + ContentID + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.d != undefined && result.d != null) {
                ChangeStatus(sender, result.d, AddImageUrl, RemoveImageUrl);
                $(sender).click(function(event) {
                    if ($(this).is('.AddToMyW')) {
                        AddToMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID);
                    }
                    else {
                        RemoveFromMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID);
                    }
                });
            }
        },
        complete: function(XMLHttpRequest, textStatus) {
            //alert(XMLHttpRequest.status + ' ' + textStatus);
            //alert(XMLHttpRequest.responseText);
        }
    });
};

function RemoveFromMyW(sender, WebServiceUrl, AddImageUrl, RemoveImageUrl, ContentType, ContentID) {
    $.ajax({
        type: "POST",
        url: WebServiceUrl + '/RemoveFromMyW',
        data: "{'ContentType':'" + ContentType + "','ContentID':" + ContentID + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.d != undefined && result.d != null) {
                ChangeStatus(sender, false, AddImageUrl, RemoveImageUrl);
            }
        },
        complete: function(XMLHttpRequest, textStatus) {
            //alert(XMLHttpRequest.status + ' ' + textStatus);
            //alert(XMLHttpRequest.responseText);
        }
    });
}

function RemoveFromMyWBackground(WebServiceUrl, ContentType, ContentID) {
   
    $.ajax({
        type: "POST",
        url: WebServiceUrl + '/RemoveFromMyW',
        data: "{'ContentType':'" + ContentType + "','ContentID':" + ContentID + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            
        },
        complete: function(XMLHttpRequest, textStatus) {
           // alert(XMLHttpRequest.status + ' ' + textStatus);
           // alert(XMLHttpRequest.responseText);
        }
    });
}



