﻿var messages = null;
Type.registerNamespace("cancercompass.controls");
cancercompass.controls.Messages = function(){
    this._userName = "";
    this._messageID = "";
    this._discussionID = "";
}
cancercompass.controls.Messages.prototype = {
    initialize : function(){
        // initialize AddThis functionality   
        var addthis_config = {
            ui_cobrand: "CancerCompass",
            data_track_linkbacks:true,
            services_compact: "blogger,delicious,digg,email,facebook,google,linkedin,newsvine,print,reddit,slashdot,stumbleupon,twitter,typepad,wordpress,yahoobkm",
            services_expanded:"blogger,delicious,digg,email,facebook,google,linkedin,newsvine,print,reddit,slashdot,stumbleupon,twitter,typepad,wordpress,yahoobkm"}
        $.getScript("http://s7.addthis.com/js/250/addthis_widget.js?pub=cancercompass");
        
        // initialize all the URLs needed on the page with facebox

        $(".a_addFriend").click(function(){
            if(!AuthService.get_isLoggedIn()){                                           
                jQuery.facebox({ ajax: "/login-lite.aspx?ReturnUrl=" + $.URLEncode($(this).attr("href")) + "&facebox=true&message=<p class='error'>In order to add a friend you need to log in to your CancerCompass account.</p>" });
            }else{
                jQuery.facebox({ ajax: $(this).attr("href") });
            }
            messages._userName = $(this).attr("title");
            return false;
        });
        $(".a_track").live("click",function(){
            if(!AuthService.get_isLoggedIn()){
                jQuery.facebox({ ajax: "/login-lite.aspx?div=" + $.URLEncode($(this).attr("href")) + "&facebox=true&message=<p class='error'>In order to track a discussion you need to log in to your CancerCompass account.</p>" });
            }else{
                jQuery.facebox({ div: $(this).attr("href") });
            }
            messages._discussionID = $(this).attr("title");
            return false;
        });
        $(".a_stopTrack").live("click",function(){
            jQuery.facebox({ div: $(this).attr("href") });
            messages._discussionID = $(this).attr("title");
            return false;
        });
        $(".a_report").click(function(){
            jQuery.facebox({ div: $(this).attr("href") });
            messages._messageID = $(this).parents(".mbpost").attr("id");
            messages._userName = $(this).attr("title");
            return false;
        });
        $(".a_trackDiscussion").live("click",function(){
            $(this).html("Tracking Discussion...");
            messages.trackDiscussion(messages._discussionID);
            return false;
        });
        $(".a_stopTracking").live("click",function(){
            $(this).html("Stopping...");
            messages.stopTracking(messages._discussionID);
            return false;
        });
    },
    isDiscussionTracked : function(discussionID){
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/common/webservices/social.asmx/IsDiscussionTracked",
            data: "{'discussionID':'" + discussionID + "'}",
            dataType: "json",
            success: function(data, textStatus){
                if(data.d){
                    $(".a_stopTrack").parent().removeClass("displayNone");
                    $(".a_track").parent().addClass("displayNone");
                }else{
                    $(".a_stopTrack").parent().addClass("displayNone");
                    $(".a_track").parent().removeClass("displayNone");
                }
            }
        });
    },
    stopTracking : function(discussionID){
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/common/webservices/social.asmx/StopTrackingDiscussion",
            data: "{'discussionID':'" + discussionID + "'}",
            dataType: "json",
            success: function(data, textStatus){
                $("#facebox .popUp .blueBorder").html("<h2>Discussion no longer tracked</h2><p>You will no longer receive notifications when another user posts a message to this discussion.</p>");
                $("#facebox .popUp .actions").css("display","none");
                $(".a_stopTrack").parent().addClass("displayNone");
                $(".a_track").parent().removeClass("displayNone");
            },
            error: function(xhr, textStatus, errorThrown){
                $("#facebox .popUp .blueBorder").html("<h2>Sorry, an error has occurred.</h2><p>We have been notified and are working to resolve it as soon as we can. Thank you for your patience.</p>");
                $("#facebox .popUp .actions").css("display","none");
            },
            complete: function(XMLHttpRequest, textStatus){
                setTimeout("jQuery(document).trigger('close.facebox')",4500);
            }
        });
    },
    trackDiscussion : function(discussionID){
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/common/webservices/social.asmx/TrackDiscussion",
            data: "{'discussionID':'" + discussionID + "'}",
            dataType: "json",
            success: function(data, textStatus){
                if(data.d){
                    $("#facebox .popUp .blueBorder").html("<h2>Discussion tracked</h2><p>You will now receive notifications when another user posts a message to this discussion.</p>");
                    $("#facebox .popUp .actions").css("display","none");              
                    $(".a_stopTrack").parent().removeClass("displayNone");
                    $(".a_track").parent().addClass("displayNone");
                }else{
                    $("#facebox .popUp .blueBorder").html("<h2>Already Tracked</h2><p>You are already tracking this discussion.</p>");
                    $("#facebox .popUp .actions").css("display","none");              
                }   
            },
            error: function(xhr, textStatus, errorThrown){
                $("#facebox .popUp .blueBorder").html("<h2>Sorry, an error has occurred.</h2><p>We have been notified and are working to resolve it as soon as we can. Thank you for your patience.</p>");
                $("#facebox .popUp .actions").css("display","none");
            },
            complete: function(XMLHttpRequest, textStatus){
                setTimeout("jQuery(document).trigger('close.facebox')",4500);
            }
        });
    }
}
$(document).ready(function(){
    messages = new cancercompass.controls.Messages;
    messages.initialize();
});