if (document.images) {
    var preload_image = new Array();
    var imageArray = new Array();    
        imageArray[0] = "http://media.intherooms.com/images/general/ajax-loader.gif";
        imageArray[1] = "http://media.intherooms.com/images/icons/icon-maximize.gif";
        imageArray[2] = "http://media.intherooms.com/images/icons/icon-maximize-hover.gif";
        imageArray[3] = "http://media.intherooms.com/images/icons/icon-new-messages.gif";
        imageArray[4] = "http://media.intherooms.com/images/general/file-uploader.gif";
        imageArray[5] = "http://media.intherooms.com/images/general/loading-tree.gif";
        imageArray[6] = "http://media.intherooms.com/images/general/share-grey.gif";
    
    for (var i = 0; i < imageArray.length; i++) {
        preload_image[i] = new Image(); 
        preload_image[i].src = imageArray[i];    
    }
}

$(document).ready(function(){

	$('#home-group-list li[class!=selected]').css('opacity','0.7');
	
    $('#home-group-list li').hover(
		function () {
			if ($(this).attr('class') != 'selected')  
				$(this).css('opacity','1.0');
		}, 
		function () {
			if ($(this).attr('class') != 'selected')  
				$(this).css('opacity','0.7');
		}
	);
	
	$('#home-group-list li').bind("click",function(){
		if ($(this).attr('class') != 'selected') {
			$(this).addClass('selected');
			$(this).css('opacity','1.0');
			
			addRemoveUserList('add', $(this).attr('id'));
		}
		else {
			$(this).removeClass('selected');
			$(this).css('opacity','0.7');
			
			addRemoveUserList('remove', $(this).attr('id'));
		}
	});
    
    var labelText;
    
    // tabs on member.home
    var tabindex = typeof(_defaultList) != 'undefined' ? _defaultList : 0;
    $("#feed-filters-div").tabs({
        select: function(event, ui) {
            var url = $(ui.tab).hasClass('nofollow');

            if (!$(ui.tab).hasClass('static')) {
                var currentTab = $(ui.tab);
                labelText = currentTab.html();
                
                currentTab.html('<span>Loading...</span>');
            }
            
            if( url ) {
                location.href = $.data(ui.tab, 'load.tabs');
                return false;
            }
            return true;
        },
        load: function(event, ui) {
             $(ui.tab).html(labelText);
        }
    }).tabs('select', tabindex);

    timedUpdateNotification();

    $(".closed-thread").bind("click",function(){
        var thread = this;
        $.ajax({
            type: "POST",
            url: "/messaging/thread",
            data: "thread=" + $(thread).attr('id'),
            success: function(data, text) {
				$(thread).after(data);
				$(thread).css('display','none');
			}
        });
        
        updateNotificationCount();
        return false;
    });

    $(".closed-group").bind("click",function(){
        var thread = this;
        $.ajax({
            type: "POST",
            url: "/messaging/thread",
            data: "group_msg=" + $(thread).attr('id'),
            success: function(data, text) {
				$(thread).after(data);
				$(thread).css('display','none');
                $(".thread-close").bind("click",function(){
                    var thread = $(this).parent().parent().parent();
                    $(thread).prev().css('display','block');
                    $(thread).remove();
                });
			}
        });
        return false;
    });
    
    
	$("#select-all-messages").bind("click", function(e) {
		var checked_status = this.checked;
		$(".message-checkbox").each(function() {
			this.checked = checked_status;
		});
	});
	
	
	$("#delete-selected").bind("click", function(e) {
	
        if(confirm("Delete Selected Messages?")) {
			$(".message-checkbox:checked").each(function() {
				var mtype = $(this).attr("rel");
	            
	            if (mtype == "thread") {
		            var thid = $(this).attr("name");
		            var ele = this;
		            
		            $('#thread_id-'+thid).remove();
		            		            
		            $.ajax({
		                type: "POST",
		                url: "/messaging/delete",
		                data: "thid=" + thid,
		                dataType: "json",
		                //beforeSend: showLoadGraphic(this),
		                success: function(data, text) {
		                  callbackFadeDiv(ele, thid, "");
		                }
		            });
	            } else if (mtype == "group") {
		            var msid = $(this).attr("name");
		            var ele = this;
		            
		            $('#announcement_id-'+msid).remove();
		            
		            $.ajax({
		                type: "POST",
		                url: "/messaging/delete",
		                data: "msid=" + msid,
		                dataType: "json",
		                //beforeSend: showLoadGraphic(this),
		                success: function(data, text) {
		                  callbackFadeDiv(ele, msid, "");
		                }
		            });
	            }
	            
	            $(this).remove();
			});
		}
		
	});
	

    $("a.dlt-thread").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Delete Message Thread?")) {
            var thid = $(this).attr("name");
            var ele = this;
            
            $.ajax({
                type: "POST",
                url: "/messaging/delete",
                data: "thid=" + thid,
                dataType: "json",
                //beforeSend: showLoadGraphic(this),
                success: function(data, text) {
                  callbackFadeDiv(ele, thid, "");
                }
            });
        }
        return false;
    });

    $("a.dlt-group-msg").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Delete Announcement?")) {
            var msid = $(this).attr("name");
            var ele = this;
            
            $.ajax({
                type: "POST",
                url: "/messaging/delete",
                data: "msid=" + msid,
                dataType: "json",
                //beforeSend: showLoadGraphic(this),
                success: function(data, text) {
                  callbackFadeDiv(ele, msid, "");
                }
            });
        }
        return false;
    });

    $("#add-college").bind("click",function(){
        var Count = $('p[id="college-input"]').length;
        if (Count < 5) {
            var prevPart = $(this).parent().prev().html();
            $(this).parent().prev().after('<p id="college-input">' + prevPart + '</p>');
            var newElement = $(this).parent().prev();
            $(newElement).find('input[type="text"]').val('');
            $(newElement).find('input[type="checkbox"]').removeAttr('checked').val(Count);
            $(newElement).find('select').attr('selectedIndex',0);
        }
        return false;
    });

    $("#add-high-school").bind("click",function(){
        var Count = $('p[id="hsInput"]').length;
        if (Count < 3) {
            var prevPart = $(this).parent().prev().html();
            $(this).parent().prev().after('<p id="hs-input">' + prevPart + '</p>');
            var newElement = $(this).parent().prev();
            $(newElement).find('input[type="text"]').val('');
            $(newElement).find('input[type="checkbox"]').removeAttr('checked').val(Count);
            $(newElement).find('select').attr('selectedIndex',0);
        }
        return false;
    });

    $(".applet h1 .toggle").toggle(function(){
        $(this).addClass("max");
        $(this).parent().next("div").slideUp(600);
    },function(){
       $(this).removeClass("max");
        $(this).parent().next("div").slideDown(600);
    });

    $("#add-friend").bind("click", function(e) {
        e.preventDefault();
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/friend/add",
            data: "fid=" + $(this).attr("name"),
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
				callbackReplaceLink(data, text, ele);
			}
        });  // end of ajax call    
    });
    
	$(".add-friend").live("click", function(e) {
        e.preventDefault();
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/friend/add",
            data: "fid=" + $(this).attr("name"),
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
				callbackReplaceLink(data, text, ele);
			}
        });  // end of ajax call    
    });
    
    $("a.approve-friend").bind("click", function(e) {
        e.preventDefault();
        var id = $(this).attr("name");
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/friend/approve",
            data: "fid=" + id,
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, id, "friend-div");
              updateNotificationCount();
            }
        });  // end of ajax call
    });

    $("a.deny-friend").bind("click", function(e) {
        e.preventDefault();

        var id = $(this).attr("name");
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/friend/deny",
            data: "fid=" + id,
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, id, "friend-div");
              updateNotificationCount();
            }
        });  // end of ajax call
    });

    $("a.invite-group").bind("click", function(e) {
        e.preventDefault();
        
        var str = $(this).attr("name");
        
        var id = str.split(".");
        var mid = id[0];
        var gid = id[1];
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/group/ajax/invite", 
            data: "gid=" + gid + "&mid=" + mid,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function (data, text) {
                var msg = data.message;
                callbackReplaceLink(msg, text, ele);
			} 
        });  // end of ajax call    
    });

    $("a.approve-group").bind("click", function(e) {
        e.preventDefault();
        
        var gid = $(this).attr("name");
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/group/ajax/approve",
            data: "gid=" + gid,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, gid, "group-div");
              updateNotificationCount();
            }
        });  // end of ajax call    
    });

    $("a.deny-group").bind("click", function(e) {
        e.preventDefault();
        
        var gid = $(this).attr("name");
        var ele = this;
        $.ajax({
            type: "POST",
            url: "/group/ajax/deny",
            data: "gid=" + gid,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, gid, "group-div");
              updateNotificationCount();
            }
        });  // end of ajax call    
    });

    $("a.post-comment").live("click", function(e) {
        e.preventDefault();

        var id = $(this).attr("name");
        var postUrl = $("#comments input[name='post']").val();

        var comments = $("#comments #msg-content-area");
        var messageBox = $("#comments #comment-text");
        var message = escape(messageBox.val());
        var link = this;

        if (message) {
            $.ajax({
                type: "POST",
                url: postUrl,
                data: "id=" + id + "&comment=" + message,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function(data, text) {
                    messageBox.val('');
                    callbackPostComment(data, text, link, comments);
                }
            });  // end of ajax call 
        }
    });

    $("a.join-group").bind("click", function(e) {
        e.preventDefault();

        var gid = $(this).attr("name");
        var ele = this;
        
        // if trying to join group 1160 (MusiCares) then need to prompt form before join request
        if (gid == 1160) {
            tb_show("Join Information", '#TB_inline?width=400&height=275&inlineId=additional-join-group-info');
        } else {
            $.ajax({
                type: "POST",
                url: "/group/ajax/join",
                data: "gid=" + gid,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (data, text) {
                    //callbackAlert(data, text, ele);
                    callbackReplaceLink(data.message, text, ele);
                } 
            });  // end of ajax call
        }
    });


    $('#additional-join-group-info input:button').bind('click', function(e) {
        var name, experience = false;
        if ($("input[name='full_name']").val()) {
            name = true;
        }
        
        if ($("textarea[name='experience']").val()) {
            experience = true;
        }
        
        if (name && experience) {
            $.ajax({
                type: "POST",
                url: "/group/ajax/join",
                data: $('#additional-info-form').serialize(),
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (json, text) {
                    if (json.success) {
                        alert('Thank you!  Your request has been sent.');
                    }
                    
                    tb_remove();
                } 
            });  // end of ajax call
        } else {
            alert('Please fill in both fields to continue');
        }
        
    });

    $("a.leave-group").live("click", function(e) {
        e.preventDefault();

        if (confirm(" Leave Group?")) {
            var gid = $(this).attr("name");

            $.ajax({
                type: "POST",
                url: "/group/ajax/leave",
                data: "gid=" + gid,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (data, txt, url) {
                    callbackAlertRD(data, txt, '/profile/view');
                }
            });  // end of ajax call
        }

    });

    $("a.disable-group").bind("click", function(e) {
        e.preventDefault();

        if (confirm("Disable Group?")) {
            var gid = $(this).attr("name");

            $.ajax({
                type: "POST",
                url: "/group/ajax/disable",
                data: "gid=" + gid,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (data, txt, url) {
                    callbackAlertRD(data, txt, '/profile/view');
                }
            });  // end of ajax call    
        }
    });

    $("a.approve-join-request").bind("click", function(e) {
        e.preventDefault();

        var str = $(this).attr("name");

        var id = str.split('-');
        var mid = id[0];
        var gid = id[1];

        var ele = this;
        $.ajax({
            type: "POST",
            url: "/group/ajax/approve",
            data: "gid=" + gid + "&mid=" + mid,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, str, "join-div");
              updateNotificationCount();
            }    
        });    
    });

    $("a.deny-join-request").bind("click", function(e) {
        e.preventDefault();

        var str = $(this).attr("name");

        var id = str.split('-');
        var mid = id[0];
        var gid = id[1];

        var ele = this;
        $.ajax({
            type: "POST",
            url: "/group/ajax/deny",
            data: "gid=" + gid + "&mid=" + mid,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function(data, text) {
              callbackFadeDiv(ele, str, "join-div");
              updateNotificationCount();
            }
        });    
    });

    $("a#remove-meditation").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Remove meditation from profile?")) {
            var medId = 0;
            var ele = this;

            $.ajax({
                type: "POST",
                url: "/profile/ajax/meditation",
                data: "med_id=" + medId,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function(data, text) {
                    if (data.success) {
                        callbackFadeDiv(ele, '', "profile-meditation");
                    } else {
                        hideLoadGraphic(ele);
                    }
                }
            });
        }
    });

    $("a#dlt-photo, a.dlt-photo").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Delete Photo?")) {

            var str = $(this).attr("name"); 
            var ele = this;

            var id = str.split('.');
            var mediaId = id[0];
            var albumId = id[1];
            var mod = id[2];
            var gid = id[3];

            var redirect = (mod == 'group') ? '/group/photos/view_album?gid=' + gid : '/profile/photos/view_album?aid=' + albumId;

            $.ajax({
                type: "POST",
                url: "/" + mod + "/ajax/delete_photo",
                data: "media_id=" + mediaId + "&album_id=" + albumId,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (data, txt, url) {
                    if ($(ele).hasClass("fade")) {
                        callbackFadeDiv(ele, mediaId, "media-");
                    } else {
                        callbackRD(data, txt, redirect);
                    }
                }
            });
        }
    });

    $("a.dlt-photo-album").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Delete Album?\n(This will delete all photos in this album!) ")) {
            var albumId = $(this).attr("name"); 
            var ele = this;

            $.ajax({
                type: "POST",
                url: "/profile/ajax/delete_album",
                data: "album_id=" + albumId,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function (data, txt, url) {
                    if (data.success) {
                        callbackFadeDiv(ele, albumId, "album-");
                    } else {
                        alert(data.message);
                        hideLoadGraphic(ele);
                    }
                }
            });
        }
    });


    $("a#set-as-cover").bind("click", function(e) {
        e.preventDefault();

        var str = $(this).attr("name");
        var id = str.split('.');
        var mediaId = id[0];
        var albumId = id[1];

        $.ajax({
            type: "POST",
            url: "/profile/ajax/cover",
            data: "media_id=" + mediaId + "&album_id=" + albumId,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function (data, txt, url) {
                callbackAlertRD(data, txt, '/profile/photos/view_album?aid=' + albumId);
            }
        });
    });

    $("a#make-profile-pic").bind("click", function(e) {
        e.preventDefault();

        var mediaId = $(this).attr("name");

        $.ajax({
            type: "POST",
            url: "/profile/ajax/set_profile_pic",
            data: "media_id=" + mediaId,
            dataType: "json",
            beforeSend: showLoadGraphic(this),
            success: function (data, txt, url) {
                callbackAlertRD(data, txt, '/profile/view');
            }
        });
    });

    $("a#deactivate-account").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Deactivate Account?")) {
        	window.location.href = "/profile/edit/deactivate";
        }
    });

    $("a.reactivate-group").bind("click", function(e) {
        e.preventDefault();

        if(confirm("Reactivate Group?")) {
        	var gid = $(this).attr("name");

	        $.ajax({
	            type: "POST",
	            url: "/group/ajax/reactivate",
	            data: "gid=" + gid,
	            dataType: "json",
	            beforeSend: showLoadGraphic(this),
	            success: function (data, txt) {
	                callbackAlertRD(data, txt, '/group/view?gid=' + gid);
	            }
       	 	});
        }
    });

    $("a.add-book-to-profile").bind("click", function(e) {
        e.preventDefault();

    	var setId = $(this).attr("name");
    	var ele = this;

        $.ajax({
            type: "POST",
            url: "/meditation/ajax/add", 
            data: "set_id=" + setId,
            dataType: 'json',
            beforeSend: showLoadGraphic(this),
            success: function (data, text) {
				callbackReplaceLink(data.message, text, ele);
			} 
   	 	});
    });

    $("a.add-med-to-profile").bind("click", function(e) {
        e.preventDefault();

    	var medId = $(this).attr("name");
    	var ele = this;

        $.ajax({
            type: "POST",
            url: "/meditation/ajax/add", 
            data: "med_id=" + medId,
            dataType: 'json',
            beforeSend: showLoadGraphic(this),
            success: function (data, text) {
				callbackReplaceLink(data.message, text, ele);
			} 
   	 	});
    });


    $(".button, .button-lg").bind("click", function(e) {
        var formName = $(this).attr("id");        
        $("form[name='" + formName + "']").submit();
    });

    $(".send-message").bind("click", function(e) {
        e.preventDefault();

    	var id = $(this).attr("name");
        var ele = this;

        $.ajax({
            type: "POST",
            url: "/profile/ajax/message",
            data: "id=" + id + "&label=Message",
            dataType: 'html',
            success: callbackGetMessageBox

   	 	});
    });

    $(".remove-member").bind("click", function(e) {
        e.preventDefault();

        var str = $(this).attr("name");
        var id = str.split('.');
        var mid = id[0]; 
        var gid = id[1];

    	var ele = this;

    	if (confirm("Remove member from group?")) {
            $.ajax({
                type: "POST",
                url: "/group/ajax/remove_member", 
                data: "mid=" + mid + "&gid=" + gid,
                dataType: 'json',      
                beforeSend: showLoadGraphic(this),
                success: function (data, text) {
                    if (data.success) {
                        callbackFadeDiv(ele, mid, "group-member");
                    } else {
                      callbackReplaceLink(data.message, text, ele);
                    }
    			} 
       	 	});
   	 	}
    });

    $(".remove-friend").bind("click", function(e) {
        e.preventDefault();

        var mid = $(this).attr("name");
    	var ele = this;

    	if (confirm("Remove Friend?")) {
            $.ajax({
                type: "POST",
                url: "/profile/ajax/remove_friend", 
                data: "mid=" + mid,
                dataType: 'json',      
                beforeSend: showLoadGraphic(this),
                success: function (data, text) {
                    if (data.success) {
                        callbackFadeDiv(ele, mid, "friend_");
                    } else {
                      callbackReplaceLink(data.message, text, ele);
                    }
    			} 
       	 	});
   	 	}
    });
    
    $(".remove-friend-profile").bind("click", function(e) {
        e.preventDefault();

        var mid = $(this).attr("name");
    	var ele = this;

    	if (confirm("Remove Friend?")) {
            $.ajax({
                type: "POST",
                url: "/profile/ajax/remove_friend", 
                data: "mid=" + mid,
                dataType: 'json',      
                beforeSend: showLoadGraphic(this),
                success: function (data, text) {
                    if (data.success) {
                        alert('This person has been removed from your friends list.');
                        location.reload(true);
                    } else {
                      callbackReplaceLink(data.message, text, ele);
                    }
    			} 
       	 	});
   	 	}
    });

    $(".play-tape").bind("click", function(e) {
        e.preventDefault();

        var tapeId = $(this).attr("name");
    	window.open ("/library/speaker/player?tape_id=" + tapeId, "player","location=0,status=0,scrollbars=0,width=200,height=70");    	
    });

    $(".tag-tape").bind("click", function(e) {
        e.preventDefault();

    	var id = $(this).attr("name");
        var ele = this;

        $.ajax({
            type: "POST",
            url: "/profile/ajax/message",
            data: "id=" + id + "&label=Tags&type=tag",
            dataType: 'html',
            success: function(xhtml, msg) {
                callbackGetMessageBox(xhtml, msg, ele, 180);
            }
   	 	});
    });
    
    $(".add-tape").bind("click", function(e) {
        e.preventDefault();

    	var id = $(this).attr("name");
        var ele = this;

        $.ajax({
            type: "POST",
            url: "/profile/ajax/add_tape",
            data: "id=" + id,
            dataType: 'json',
            success: function(json, msg) {
                callbackAlertRD(json, msg, '/profile/view');
            }
   	 	});
    });

    $(".discussion-tab").bind("click", function(e) {
        e.preventDefault();
    	var method;
        var ele = this;

        if ($(ele).hasClass('selected')) {
            $(ele).removeClass('selected');
            method = 'new_discussions';
        } else {
            $(ele).addClass('selected');
            method = 'hot_discussions';
        }

        $.ajax({
            type: "POST",
            url: "/member/home/" + method,
            dataType: 'html',
            beforeSend: function() {
                $("#discussion-content").html('<br class="clear-fix" /><div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div><br class="clear-fix" />');
            },
            success: function(xhtml, msg) {
                $("#discussion-content").html(xhtml);
            }
   	 	});
    });

    $("#attach-photo").bind('click', function(e) {
        $(this).hide();
        $("#hidden-photo-div").show();
    });
    
    $("#status-text").bind('focus', function(e) {
        if ($(this).val() == 'What are you doing now?') {
            $(this).empty().css('color', '#444');
        }
    });
    
    $("#user-status-form").bind('submit', function(e) {
        var statusObj = $("#status-text");
        var options = { 
            beforeSubmit:  function() { 
                $("#update-user-status").hide();
                $("#status-ajax-loader").show();

                if (statusObj.val() == 'What are you doing now?') {
                    statusObj.empty();
                }
            },
            success:       function(html, status) { 
                $("#update-user-status").show();
                $("#status-ajax-loader").hide();

                if (html) {
                    $("#feed-filters").tabs('select', 0);
                    $('#profile-status-link').click().parent().click();
    
                    $("#status-list").prepend(html).find("#new-newsfeed-li").slideDown();
                }
            }, 
            url:       "/member/home/update_status",
            type:      'post',
            dataType:  'html',
            clearForm: true,
            resetForm: true 

        }; 

        if ($("#user-status-form :file").val().length || (statusObj.val() != 'What are you doing now?' && statusObj.val() != '')) {
            $("#user-status-form").ajaxSubmit(options); 
        }

        
        return false; 
    });
    
    $(".single-comment").bind("mouseover", function(e) {
        $(this).children("a.flag-comment:first").css("visibility" , "visible");
    });

    $(".single-comment").bind("mouseleave", function(e) {
        $(this).children("a.flag-comment:first").css("visibility" , "hidden");
    });

    $("textarea[maxlength], input.maxlength").keyup(function(){
        if( ($(this).attr('maxlength') > 0) && ($(this).val().length >= $(this).attr('maxlength')) ) {
            $(this).val($(this).val().substr(0, $(this).attr('maxlength')));
            $(this).parent().find('.maxlength-reached').css({color: "red", display: "block"}).html('Sorry, ' + $(this).attr('maxlength') +' characters max.');
        }
    });

    $("#photo-form, #group_photo_form, #speaker-form").bind("click", function(e) {
        var formName = $(this).attr("id");
        $("form[name='" + formName + "']").submit(); 
        document.getElementById('myimage').parentNode.style.display = 'block';
    });

    $(".block-user").bind("click", function(e) {
        e.preventDefault();

        var mid = $(this).attr("name");
    	var ele = this;

    	if (confirm("Block/Un-Block User?")) {
            $.ajax({
                type: "POST",
                url: "/profile/ajax/block_user", 
                data: "mid=" + mid,
                dataType: 'json',      
                beforeSend: showLoadGraphic(this),
                success: function (json, text) {
                    
                    if (json.success) {
                        linkHTML = json.newLinkText;
                    }
                    hideLoadGraphic(ele);
    			} 
       	 	});
   	 	}
    });

    $("#welcome-view .newsfeed .search-paging a").live("click", function(e) {
        //e.preventDefault();
        paginateNews(this);
    });

    $("#profile-content .search-paging a").live("click", function(e) {
        //e.preventDefault();
        paginateProfileStatus(this);
    });

    $(".status-list-class li:not(.has-comments)").live("mouseover", function(e) {
        $(this).find("span").css('visibility', 'visible'); 
    });
    
    $(".status-list-class li:not(.has-comments)").live("mouseout", function(e) {
        $(this).find("span").css('visibility', 'hidden'); 
    });

	$('.status-list-class li small span.comment-button a').live('click', function() {
		$(this).children().css('visibility','hidden');
		if ( $(this).parent().parent().next().next().css('display') == 'none' ) {
			$(this).addClass('open');
			$(this).parent().parent().next().next().slideDown();
			$(this).parent().parent().parent().unbind("mouseout");
		}
		else {
			$(this).removeClass('open');
			$(this).parent().parent().next().next().slideUp();
			$(this).children().css('visibility','visible');
			$(this).parent().parent().parent(':not(.has-comments)').bind("mouseout", function(e) {
      			$(this).find("span").css('visibility', 'hidden'); 
			});
    	}
	});

	$(".leave-a-comment a").live('click', function(e){
		e.preventDefault();
		if ($(this).prev().val() != "Leave Comment...") {
			addFeedComment(e, this);
		}
	});
	
	$(".lc").live('keyup', function(e) {
		e.preventDefault();
		var key = e.charCode || e.keyCode || 0;
        var ele = $(this).next();

        if (key == 13) {
            addFeedComment(e, ele);   
   	 	}
	});
	
	$(".expand-all-comments").live('click', function(e){
		if ($(this).attr('checked')) {
			$('.status-list-class li.has-comments small span a').each(function() {
				$(this).children().css('visibility','hidden');
				$(this).addClass('open');
				$(this).parent().parent().next().next().slideDown();
				$(this).parent().parent().parent().unbind("mouseout");
			});
		} else {
			$('.status-list-class li.has-comments small span a').each(function() {
				$(this).removeClass('open');
				$(this).parent().parent().next().next().slideUp();
				$(this).children().css('visibility','visible');
				$(this).parent().parent().parent(':not(.has-comments)').bind("mouseout", function(e) {
	   			$(this).find("span").css('visibility', 'hidden'); 
				});
			});
		}
	});

    $(".remove-announcement").live('click', function(e){
         $.ajax({
            type: "POST",
            url: "/member/ajax/remove_announcement",
            dataType: 'json',
            success: function(data, text) {
				$(".itr-announcement").fadeOut();
			}
        });
    });


    $(".remove-profile-percentage").live('click', function(e){
         $.ajax({
            type: "POST",
            url: "/member/ajax/remove_profile_percentage",
            dataType: 'json',
            success: function(data, text) {
				$("#profile-meter").fadeOut();
			}
        });
    });

 
 
 /* new profile stuff */
    $('#detailed-profile-info').toggle(function() {
        $('#detailed-info').slideDown();
        $('#detailed-profile-info').html('Less...');
    }, function() {
        $('#detailed-info').slideUp();
        $('#detailed-profile-info').html('More...');
        var offset = $('#about-me').offset();
        $('html, body').animate({scrollTop:(offset.top-5)}, 'slow');
        return false;
    });

    $('#more-meditation').toggle(function() {
        $('#meditation-continued').slideDown();
        $('#more-meditation').html('Less...');
    }, function() {
        $('#meditation-continued').slideUp();
        $('#more-meditation').html('More...');
        var offset = $('#my-meditation').offset();
        $('html, body').animate({scrollTop:(offset.top-5)}, 'slow');
        return false;
    });
    
    $('#profile-filters li').click(function(){
        $('#profile-filters li').removeClass('selected');
        $(this).addClass('selected');				
    });
    
    $("#profile-filters li a").bind('click', function(e){
        var tmp = $(this).attr('name').split('.');
        var action = tmp[0];
        var mid = tmp[1];
        
        $.ajax({
            type: "POST",
            url: "/profile/tab/" + action, 
            data: "mid=" + mid,
            //dataType: 'json',      
            beforeSend: function() {
                $("#profile-content").html('<div style="text-align: center; margin-top:30px;"><img src="http://media.intherooms.com/images/general/loading-tree.gif" alt="loading..."></div>');
            },
            success: function (html, text) {
                $("#profile-content").hide().html(html).fadeIn('fast');
            } 
        });
    
    });

    $('.more-meditation').toggle(function() {
        $(this).parent().parent().parent().css('height','auto');
        $(this).html('Less...');
    }, function() {
        $(this).parent().parent().parent().css('height','180px');
        $(this).html('More...');
        var offset = $(this).parent().parent().parent().offset();
        $('html, body').animate({scrollTop:(offset.top-5)}, 'slow');
        return false;
    });


    $(".parsed-link").live('click', function(e) {
        e.preventDefault();
        var oembedOptions = {
            maxWidth : 385,
            embedMethod: "custom"
        };
        $(this).oembed(null, oembedOptions);
    
    });

    $('.delete-feed-update').live('click', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = ele.attr('name');

        if (id && confirm('Delete status update?')) {
             $.ajax({
                type: "POST",
                url: "/member/ajax/delete_feed_update",
                dataType: 'json',
                data: 'feed_id=' + id,
                success: function(json, text) {
                    if (json.success) {
                        ele.parents('li').fadeOut();
                    } else if (json.message){
                        alert(json.message);
                    }
                }
            });
        }
    });

    $('.delete-feed-comment').live('click', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = ele.attr('name');

        if (id && confirm('Delete comment?')) {
             $.ajax({
                type: "POST",
                url: "/member/ajax/delete_feed_comment",
                dataType: 'json',
                data: 'comment_id=' + id,
                success: function(json, text) {
                    if (json.success) {
                        ele.parent().fadeOut();
                    } else if (json.message){
                        alert(json.message);
                    }
                }
            });
        }
    });

    $('.delete-profile-comment').live('click', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = ele.attr('name');

        if (id && confirm('Delete comment?')) {
             $.ajax({
                type: "POST",
                url: "/member/ajax/delete_profile_comment",
                dataType: 'json',
                data: 'comment_id=' + id,
                success: function(json, text) {
                    if (json.success) {
                        ele.parent().parent().fadeOut();
                    } else if (json.message){
                        alert(json.message);
                    }
                }
            });
        }
    });

    $('.delete-pc-comment').live('click', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = ele.attr('name');

        if (id && confirm('Delete comment?')) {
             $.ajax({
                type: "POST",
                url: "/member/ajax/delete_pc_comment",
                dataType: 'json',
                data: 'comment_id=' + id,
                success: function(json, text) {
                    if (json.success) {
                        ele.parent().fadeOut();
                    } else if (json.message){
                        alert(json.message);
                    }
                }
            });
        }
    });

    
    $('.newsfeed-status-list .like a, .profile-status-list .like a').live('click', function(e) {
         var ele = $(this);
         var objectId = ele.attr('name');
         
         var objectType = 'STATUS_UPDATE';
         
         if ($("#like-object-type").length) {
            objectType = $("#like-object-type").val();
         }

         $.ajax({
            type: "POST",
            url: "/member/ajax/like",
            dataType: 'json',
            data: 'object_id=' + objectId + '&object_type=' + objectType,
            beforeSend: function() {
                ele.hide();
                ele.parent().append('<img src="http://media.intherooms.com/images/general/ajax-loader.gif" alt="loading" style="border-width:0;">');
            },
            success: function(json, text) {
                ele.parent().children('img').remove();
                ele.show();

                if (json.success) {
                    if (json.like) {
                        ele.parent().addClass('on');
                        ele.parent().parent().parent().addClass('has-comments');
                        
                        if (json.count) {
                            var peopleText = (json.count - 1) > 1 ? ' others' : ' other';
                            var label = json.count == 1 ? 'You like this.' : 'You and ' + (json.count - 1) + peopleText + ' like this.';

                            if (ele.parent().parent().children('span.like-count').length) {
                                ele.parent().parent().children('span.like-count').html(label).css('display', 'block').addClass('on');
                            } else {
                                ele.parent().parent().append('<span class="like-count on">' + label + '</span>').addClass('on');
                            }
                        }
                    } else {
                        ele.parent().removeClass('on');

                        if (json.count) {
                            var peopleText = json.count > 1 ? ' People like' : ' Person likes';

                            ele.parent().parent().children('span.like-count').html(json.count + peopleText + ' this.').removeClass('on');
                        } else if (json.count == 0) {
                            ele.parent().parent().children('span.like-count').fadeOut();
                        }
                    }
                } else if (json.message){

                }
            }
        });
    });

    $('#burning-desire-button').live('click', function(e) {
    	if (!$(this).hasClass('on')) {
			var burningAnswer = confirm("Clicking this button notifies the community that you are either in danger of relapse, want to hurt yourself or someone else.\n\nInTheRooms will put a red border around your icon across the site. If you feel you you truly need help, click 'OK'. Otherwise please click 'Cancel'.");
			if (burningAnswer) {
		    	$(this).addClass('on');
	    		$('.burning-message').addClass('on');

                 $.ajax({
                    type: "POST",
                    url: "/profile/ajax/burning_desire",
                    dataType: 'json',
                    data: 'flag=on',
                    success: function(json, text) {
                        window.location.href = '/help/burningdesire';
                    }
                });
	    	}
    	} else {
    		$(this).removeClass('on');
    		$('.burning-message').removeClass('on');
    		
              $.ajax({
                type: "POST",
                url: "/profile/ajax/burning_desire",
                dataType: 'json',
                data: 'flag=off',
                success: function(json, text) {

                }
            });

    	}    			
    });


    $('#burning-form').submit(function() { return false; }).validate({
        submitHandler   :   function(form) { 
            var tcPartner = $("#tc-partner").val()
    
            $.ajax({
                type: "POST",
                url: "/member/ajax/lead_form",
                data: $("#burning-form").serialize(),
                dataType: "json",
                success: function(json, text) {
                    if (json.success) {
                        if (true) {
                            $('body').append('<iframe width="1" height="1" style="background:none" src="http://intherooms.com/twilio/makecall.php?from=8887557227&to=' + $("#phone").val() + '"></iframe>');
                        } else {
                            $('body').append('<iframe width="1" height="1" style="background:none" src="http://intherooms.com/twilio/makecall.php?from=8887557227&to=' + $("#phone").val() + '"></iframe>');
                        }
                        
                        $("#burning-form").html('<h2>Thank you for contacting us. You are now being connected to a counselor who can assist you.</h2>');
                    }
                }
            });
            return false;
        }
    });


    $("a#post-profile-comment").live("click", function(e) {
        e.preventDefault();

        var id = $(this).attr("name");
        var postUrl = $("#comments input[name='post']").val();

        var comments = $("#comments #msg-content-area");
        var messageBox = $("#comments #comment-text");
        var message = escape(messageBox.val());
        var link = this;

        if (message) {
            $.ajax({
                type: "POST",
                url: postUrl,
                data: "id=" + id + "&comment=" + message,
                dataType: "json",
                beforeSend: showLoadGraphic(this),
                success: function(json, text) {
                    hideLoadGraphic(link);
                    messageBox.val('');
                    //callbackPostComment(json, text, link, comments);

                    if(json.success) { 
                        var deleteUrl = (json.deleteFlag) ? '<a href="#" class="right dlt-comment" name="' + json.comment_id + '">Delete</a>' : '';
                        
                        var comment = '<li class="cf "><a href="/' + json.displayName + '"><img width="45" alt="winner" src="' + json.profileIcon + '"></a>'
                                      +'<p><b><a href="/' + json.displayName + '">' + json.displayName + '</a></b> ' + json.comment + '</p>'
                                      +'<small>Updated  seconds ago  <span class="comment-button" style="visibility: hidden;"><a href="javascript:;"><div></div></a></span>'
                                      +'<span class="like " style="visibility: hidden;"><a name="" href="javascript:;" title="like this"></a></span>'
                                      +'<br class="clear-fix"></small><br class="clear-fix">'
                                      +'<ul class="sc-list">'
                                      +'<li class="cf"><div class="pc_leave-a-comment"><p class="maxlength-reached red"></p>'
                                      +'<input type="text" onfocus="if (this.value == \'Leave Comment...\') this.value = \'\';" value="Leave Comment..." name="leave-comment" class="maxlength pc" maxlength="1000">'
                                      +'<a name="' + json.comment_id + '" href="#"><input type="image" src="http://media.intherooms.com/images/general/post.gif"></a>'
                                      +'</div></li></ul></li> ';
                                            
                        $("ul#status-list").prepend(comment);
    
                    } else {
                        alert("Failed.");
                    }
                    
                    $(".maxlength-reached").html('');
                    $("a.dlt-comment").bind("click", function(e){ 
                        e.preventDefault();
                        deleteComment(e, this);
                    });
                    
                }
            });  // end of ajax call 
        }
    });


	$(".pc_leave-a-comment a").live('click', function(e){
		e.preventDefault();
		if ($(this).prev().val() != "Leave Comment...") {
			addPcCommnet(e, this);
		}
	});
	
	$(".pc").live('keyup', function(e) {
		e.preventDefault();
		var key = e.charCode || e.keyCode || 0;
        var ele = $(this).next();

        if (key == 13) {
            addPcCommnet(e, ele);   
   	 	}
	});
	
	
	$('.report-spam').live('click', function(){
	
		var spam_id		 	 = encodeURI($(this).attr('rel'));
		var spam_username    = encodeURI($('#name-'+spam_id).html());
		var spam_user_id     = encodeURI($('#name-'+spam_id).attr('rel'));
		var spam_message     = encodeURI($('#msg-'+spam_id).html());
		var spam_reported_by = encodeURI($(this).attr('role'));
		
		// alert(spam_id+"|"+spam_username+"|"+spam_user_id+"|"+spam_message);
			
	    var answer = confirm("Are you sure you want to report "+spam_username+" for spam?");
		
	    if (answer) {
	        $.ajax({
	            type: "POST",
	            url: "/messaging/ajax/spam",
	            data: "spam_username=" + spam_username + "&spam_user_id=" + spam_user_id + "&spam_message=" + spam_message + "&spam_reported_by=" + spam_reported_by,
	            dataType: 'json',
	            success: function(data,response){alert(data.message)}
	     	});
	 	} 		
	
	});
	
	$("#email-unsub-cb input:checkbox").bind('click', function(e) {
	   if ($(this).attr('checked')) {
	       $(".email_cb input:checkbox").attr('disabled', 'disabled');
	   } else {
	       $(".email_cb input:checkbox").removeAttr('disabled');
	   }
	});
	
	


 }); // end $(document).ready



/************************************************************************** begin support and callback functions ************************************************************************/



var linkHTML = '';
var photoCount = 1;


function showLoadGraphic(e) {
    linkHTML = $(e).html();
    $(e).prepend('<img src="http://media.intherooms.com/images/general/ajax-loader.gif" style="border:0;" alt="loading" />');
}

function hideLoadGraphic(e) {
    var hideLoad = function() {
        $(e).html(linkHTML);
    };
    setTimeout(hideLoad,'500');
}

function callbackReplaceLink(data, text, e) {
	$(e).replaceWith(data);
}

function callbackAlert(json, msg, e) {
    hideLoadGraphic(e);
    alert(json.message);
}

function callbackAlertRD(json, msg, url) {
    alert(json.message);
    window.location.href = url;
}

function callbackRD(json, msg, url) {
    window.location.href = url;
}

function callbackFadeDiv(e, divId, type) {
    hideLoadGraphic(e);
    $("#" + type + divId).fadeOut(1000);
}

function callbackPostComment(json, msg, e, comments) {
    hideLoadGraphic(e);
    
    var deleteUrl = (json.deleteFlag) ? '<a href="#" class="right dlt-comment" name="' + json.comment_id + '">Delete</a>' : '';

    if(json.success) {
        var comment = '<div id="comment-' + json.comment_id + '" class="cf">'
                    + deleteUrl
                    + '<a href="/' + json.displayName + '"><img src="' + json.profileIcon + '" /></a>'
                    + '<strong><a href="/' + json.displayName + '">' + json.displayName + '</a> - ' + json.date + '</strong>'
                    + '<p>' + json.comment + '</p>'
                    + '</div>';

        $(comments).prepend(comment).children("p").remove();
        var count = $(comments).find('div').length;

        if (count%2) {
            $(comments).find('div:first').addClass("hl");
        }

    } else {
        alert("Failed.");
    }

    $(".maxlength-reached").html('');
    $("a.dlt-comment").bind("click", function(e){ 
        e.preventDefault();
        deleteComment(e, this);
    });

}

function callbackJoinRequest(json, msg) {
    hideLoadGraphic();
}

function openTos() {
    var url = "/site/tos?popup=true";
    window.open(url, "tos", "width=700, height=500, scrollbars=1");
}

function sendMessage(ele){
    var mid = $(".message-send").attr("id");
    var msg = encodeURI($("#message-body").val());

    if (msg) {
        $.ajax({
            type: "POST",
            url: "/messaging/ajax/send",
            data: "mid=" + mid + "&message=" + msg,
            dataType: 'json',
            success: callbackSendMessage
     	});
 	} else {
        $(ele).parent().find('p:first').css('color', 'red').html('<b>Please enter a message to send!</b>');
    }
}

function addTag(ele){
    var id = $(".message-send").attr("id");
    var msg = encodeURI($("#message-body").val());

    if (msg) {
        $.ajax({
            type: "POST",
            url: "/library/ajax/tag",
            data: "id=" + id + "&tags=" + msg,
            dataType: 'json',
            success: callbackSendMessage
     	});
    } else {
        $(ele).parent().find('p:first').css('color', 'red').html('<b>Please enter some tags to send!</b>');
    }
}


function callbackGetMessageBox(xhtml, msg, ele, xAdjust) {
    var offset, top, left;

    if (xAdjust) {
        offset = $(ele).offset();
        top = offset.top + "px";
        left = (offset.left - xAdjust) + "px";
    } else {
        offset = $(".send-message").offset();
        top = offset.top + "px";
        left = (offset.left - 30) + "px";
    }

    if (!document.getElementById('msg-box')) {
        $('<div id="msg-box"></div>').appendTo("body");
    }

    $('#msg-box').html(xhtml);
    //$('#msg-box').css({position: "absolute", zIndex: 100000, display: "block", backgroundColor: "transparent", top: top, left: left});
    $("#msg-box").dialog({
        autoOpen: true,
        modal: true,
        bgiframe: true, 
        width:350,
		position: ['center','top']
    }).dialog('open').css({"position":"absolute", "top":"200px"});
}

function callbackSendMessage(json, msg) {
    if (json.success) { 
        $("#msg-content").html('<br /><br/><h2>' + json.message + '</h2><br /><div class="button right" onclick="closeMessageBox();"><div>Close</div></div>');
    } else {
        $("#msg-content").html('<br /><br/><div class="error-pane"><h2>' + json.message + '</h2></div>');
    }
    setTimeout(closeMessageBox, 1500);
}

function closeMessageBox() {
    //$('#msg-box').fadeOut(500);
    $('#msg-box').dialog('close');
}

function deleteComment(e, ele) {
    var id = $(ele).attr("name");
    var deleteUrl = $("#comments input[name='delete']").val();

    if (confirm('Delete Comment?')) {
        $.ajax({
            type: "POST",
            url: deleteUrl,
            data: "id=" + id,
            dataType: "json",
            beforeSend: showLoadGraphic(ele),
            success: function(data, text) {
                if (data.success) {
                    callbackFadeDiv(ele, id, "comment-");
                } else {
                    hideLoadGraphic(ele);
                }
            }
        });  // end of ajax call 
    }

    return false;
}

function paginateComments(ele, id) {
    id = id.split('.');
    var page = $(ele).attr("name");

    $.ajax({
        type: "POST",
        url: "/" + id[0] + "/paginate_comment",
        data: "id=" + id[1] + "&page=" + page,
        dataType: 'html',
        beforeSend: function() {
            $("#comments-paged-content").html('<br class="clear-fix" /><div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div><br class="clear-fix" />');
        },
        success: callbackPageComments
    });
}

function callbackPageComments(html, msg) {
    $("#comments-paged-content").html(html);
}

function paginateNews(ele) {
    var page = (typeof ele == 'object') ? $(ele).attr("name") : ele;
    var contentId = $(ele).parent().parent().attr('id');
    var method = (contentId == 'newsfeed-content') ? 'paginate_news' : 'filter_news';
    var sendData = '';
    
    if (contentId == 'newsfeed-content-tabs-home') {
        sendData = '&filter=home_group';
    } else if (contentId == 'newsfeed-content-tabs-dates') {
        sendData = '&filter=dates';
    } else if (contentId == 'newsfeed-content-tabs-blog') {
        sendData = '&filter=blog';
    }

    $.ajax({
        type: "POST",
        url: "/member/home/" + method,
        data: "page=" + page + sendData,
        dataType: 'html',
        beforeSend: function() {
            $("#" + contentId).html('<br class="clear-fix" /><div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div><br class="clear-fix" />');
        },
        success: function(html, msg) {
            callbackPageNews(html, msg, contentId);
        }
    });
}

function paginateProfileStatus(ele) {
    var page = (typeof ele == 'object') ? $(ele).attr("name") : ele;
    var contentId = $(ele).parent().parent().attr('id');
    var mid = $('#profile-status-mid').val();

    $.ajax({
        type: "POST",
        url: "/profile/tab/paginate_status",
        data: "page=" + page + "&mid=" + mid,
        dataType: 'html',
        beforeSend: function() {
            $("#" + contentId).html('<br class="clear-fix" /><div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div><br class="clear-fix" />');
        },
        success: function(html, msg) {
            callbackPageNews(html, msg, contentId);
        }
    });
}


function callbackPageNews(html, msg, id) {
    $("#" + id).html(html);
}

function paginateAnniversaries(ele) {
    var page = $(ele).attr("name");

    $.ajax({
        type: "POST",
        url: "/news/view/paginate_anniversaries",
        data: "page=" + page,
        dataType: 'html',
        beforeSend: function() {
            $("#anniversaries-today").html('<br class="clear-fix" /><div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div><br class="clear-fix" />');
        },
        success: callbackPageAnniversaries
    });
}

function callbackPageAnniversaries(html, msg) {
    $("#anniversaries-today").html(html);
}


function paginateThread(ele, id) {
    var page = $(ele).attr("name");
    var threadId = id;

    $.ajax({
        type: "POST",
        url: "/messaging/thread",
        data: "thread=" + threadId + "&page=" + page + "&refresh=true",
        dataType: 'html',
        /*
        beforeSend: function() {
            $("#comments-paged-content").html('<br class="clear-fix" /><img src="http://media.intherooms.com/images/general/ajax-loader.gif" style="border:0;" alt="loading" /><br class="clear-fix" />');
        },
        */
        success: function(html, msg) {
            callbackPageThread(html, msg, threadId);
        }
    });
}

function callbackPageThread(html, msg, threadId) {
    $("#thread_id-" + threadId).html(html);
}


function sendFriendInvite(ele) {
    var email = $('input[name="email"]').val();
    var msg = $('#message-body').val();

    $.ajax({
        type: "POST",
        url: '/friend/invite',
        data: "to=" + email + "&message=" + msg,
        dataType: "json",
        beforeSend: showLoadGraphic(ele),
        success:callbackSendMessage
    });  // end of ajax call 
}

/*
function updateUserStatus(statusField) {
    var status = statusField.val();  

    $.ajax({
        type: "POST",
        url: "/member/ajax/update_status",
        data: "status=" + encodeURIComponent(status),
        dataType: 'json',
        beforeSend: function() {
            $("#status-submit-btn").attr("src", "http://media.intherooms.com/images/general/ajax-loader.gif");
        },
        success: function(json, msg) {
            if (json.success) {
                $("#user-status-message").html(json.message);
                $("#status-header h1").effect('highlight', {}, 2000);
                statusField.val("is ");
                $("#status-cover").css("visibility", "visible");
                $("#status-submit-btn").attr("src", "http://media.intherooms.com/images/welcome/submit-status.gif");               
            }
        }
    });
}
*/

function updateUserStatus(statusField) {
    var status = statusField.val();  

    $.ajax({
        type: "POST",
        url: "/member/home/update_status",
        data: "status=" + encodeURIComponent(status),
        dataType: 'html',
        beforeSend: function() {
            $("#status-submit-btn").attr("src", "http://media.intherooms.com/images/general/ajax-loader.gif");
        },
        success: function(html, msg) {
            if (html) {
                $("#tabs-home").tabs('select', 0);
                $('#profile-status-link').click().parent().click();

				$("#status-list").prepend(html).find("#new-newsfeed-li").slideDown();
                
				$("#user-status-message").html(status).effect('highlight', {}, 2000);
                statusField.val("is ");
                $("#status-cover").css("visibility", "visible");
                $("#status-submit-btn").attr("src", "http://media.intherooms.com/images/welcome/submit-status.gif");
            }
        }
    });
}


function updateNotificationCount() {
    var date = new Date();
    var plural;
    $.ajax({
        type: "POST",
        url: '/profile/ajax/get_notification_count?time=' + date.getTime(),
        dataType: "json",
        success: function (json, msg) {
            if (json.success) {
                $("#new-message-popup").html('<a style="float: right;" href="#" title="Click to hide" onclick="closeNotificationPopup();">X</a>');
                if (json.friend_requests > 0) {
                    plural = (json.friend_requests > 1) ? 's' : '';
                    $("#new-message-popup").css('visibility', 'visible').append('<a href="/profile/requests">You have ' + json.friend_requests + ' new friend request' + plural + '!</a>');
                    $("#menu_friend_req_total").html(json.friend_requests);
                } else {
                    $("#notification_friend").html('');
                }

                if (json.group_requests > 0) {
                    plural = (json.group_requests > 1) ? 's' : '';
                    $("#new-message-popup").css('visibility', 'visible').append('<a href="/profile/requests">You have ' + json.group_requests + ' new group request' + plural + '!</a>');
                    $("#menu_group_req_total").html(json.group_requests);
                } else {
                    $("#notification_group").html('');
                }

                if (json.messages > 0) {
                    $("#notification_message").html('<a href="/messaging/inbox"><div class="blinking-message">&nbsp;</div> Messages (' + json.messages + ')</a>');
                    plural = (json.messages > 1) ? 's' : '';
                    $("#new-message-popup").css('visibility', 'visible').append('<a href="/messaging/inbox">You have ' + json.messages + ' new message' + plural + '!</a>');
					$("#menu_messages_total").html(json.messages);
                } else {
                    $("#notification_message").html('<a href="/messaging/inbox"> Messages (0)</a>');
                }
                
                if (json.notifications > 0) {
                    if ($("#notification_total").html()) {
                        $("#notification_total").html(json.notifications);
	                    $("#menu_notification_total").html(json.notifications);
                    } else {
                        $("#new-nav li.notification a").css('padding-right', '23px').append('<span id="notification_total">' + json.notifications + '</span>');
                    }
                }
            }

            if ((json.friend_requests == 0 && json.group_requests == 0 && json.messages == 0) || !json.popup ) {
                $("#new-message-popup").html('').css('visibility', 'hidden');
            }
        }
    });  // end of ajax call 

}

var timer;

function timedUpdateNotification() {
    updateNotificationCount();    
    timer = setTimeout("timedUpdateNotification()", 30000);
}

function flagComment(e, ele) {
    e.preventDefault();

    var id = $(ele).attr("name");
    var flagUrl = $("#comments input[name='flag']").val();
    var ele = ele;

    if (confirm('Flag Comment as offensive/inappropriate?')) {
        $.ajax({
            type: "POST",
            url: flagUrl,
            data: "id=" + id,
            dataType: "json",
            beforeSend: showLoadGraphic(ele),
            success: function(data, text) {
                hideLoadGraphic(ele);
                alert(data.message); 
            }
        });  // end of ajax call 
    }
}

function getServerTime() {
    $.ajax({
		    type: "POST",
		    url: "/livemeetings/ajax/servertime", 
		    dataType: 'html',      
		    success: function (html) {
				$('#servertime').html(html);
			} 
	});
}


function replyToMessage(form) {
    var payload = "";
    var textarea = $("form[name='" + form + "']").find("textarea");
    var form_dom = document.forms[form];
    var quicky = $("form[name='" + form + "']").find("select");

	for (var i = 0; i < form_dom.elements.length; i++) {
        if (payload != "") {
        	payload += "&";
        }

		if (form_dom.elements[i].value) {
            payload += encodeURIComponent(form_dom.elements[i].name) + "=" + encodeURIComponent(form_dom.elements[i].value);
		}
	}

    if (textarea.val() || quicky.val() != 0) {
    	$.ajax({
            type: "POST",
            url: "/messaging/ajax/send",
            data: payload,
            dataType: "html",
            success: function(data, text) {
                updateThread(data, form);
            }
        });  // end of ajax call
    }
}

function updateThread(html, form ) {
    $("form[name='" + form + "']").parent().parent().html(html);
}

function closeNotificationPopup(ele) {
    $.ajax({
        type: "POST",
        url: "/profile/ajax/close_popup",
        dataType: 'json',
        success: function(json, msg) {
            $('#new-message-popup').css('visibility', 'hidden');
        }
    });
}

function openChat(channel) {
	var chaturl = channel ? '/chat?channel=' + channel : '/chat';
    window.open(chaturl, 'chatwindow', 'location=0, scrollbars=1,width=985, height=750, status=0'); 
}

function addFeedComment(e, ele){
	e.preventDefault();
	var commentField = $(ele).prev();
	var comment = escape(commentField.val());
	
	if (comment) {
		$.ajax({
		    type: "POST",
		    url: "/member/home/add_comment", 
		    data: "feed_id=" + $(ele).attr('name') + "&comment=" + comment,
		    dataType: 'html',      
		    beforeSend: function(){
				$(ele).parent().prepend('<img src="http://media.intherooms.com/images/general/ajax-loader.gif" style="border:0; float:right; " alt="loading" />');
			},
		    success: function (html, text) {
		        //$(ele).parent().parent().parent().prepend(html).find('.new-comment').slideDown();
		        $(ele).parent().parent().parent().find("li:last-child").before(html).parent().find('.new-comment').slideDown();
		        $(ele).parent().find("img").remove();

		        commentField.val('');
			} 
		});
	}
}


function addPcCommnet(e, ele){
	e.preventDefault();
	var commentField = $(ele).prev();
	var comment = escape(commentField.val());
	
	if (comment) {
		$.ajax({
		    type: "POST",
		    url: "/profile/view/add_comment", 
		    data: "pc_comment_id=" + $(ele).attr('name') + "&comment=" + comment,
		    dataType: 'html',      
		    beforeSend: function(){
				$(ele).parent().prepend('<img src="http://media.intherooms.com/images/general/ajax-loader.gif" style="border:0; float:right; " alt="loading" />');
			},
		    success: function (html, text) {
		        //$(ele).parent().parent().parent().prepend(html).find('.new-comment').slideDown();
		        $(ele).parent().parent().parent().find("li:last-child").before(html).parent().find('.new-comment').slideDown();
		        $(ele).parent().find("img").remove();

		        commentField.val('');
			} 
		});
	}
}


function searchDiscussions(id, keywords, page) {
	$.ajax({
        type: "POST",
        url: "/group/discussion/search_discussions",
        data: "gid=" + id + "&keywords=" + keywords + "&page=" + page,
        dataType: 'html',
        beforeSend: function() {
			$("#discussion-results").html('<div class="paginate-loader">Loading...<br /><img src="http://media.intherooms.com/images/general/file-uploader.gif" style="border:0;" alt="loading" /></div>');
		},
		success: function(html, msg) {
			    /*
			    var html = '';			    
			    jQuery.each(json.results, function() {
                    html += '<div><a href="/group/discussion/view?gid=' + this.group_id + '&did=' + this.discussion_id + '">' + this.subject + '</a><p>' + this.post_count + ' post(s) by ' + this.user_count + '</p></div>';
                }); 
			    */
				$("#discussion-results").html(html);
        }
    });
    
}

function addRemoveUserList(action, id) {
    $.ajax({
		    type: "POST",
		    url: "/user/ajax/user_list", 
		    data: "user_id=" + id + "&action=" + action,
		    dataType: 'json',      
		    success: function (json, text) {

			} 
		});
}

function showHiddenComments(ele) {
    $(ele).parent().parent().children().each( function(){
        $(this).show();
    });
    $(ele).parent().hide();
}

function closeFlashAd() {
    document.body.style.height = '80px';
    $("#google-ad-parent").css({'height' : '100px'});
    $('#flash-ad-1').css({'height' : '80px'});
}


function openFlashAd() {
    $("#google-ad-parent").css({'height' : '278px'});
    $('#flash-ad-1').css({'height' : '278px'});
}

function getFlashCookie(c_name)
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}


function setFlashCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + '; path=/';
}


function checkFlashCookie()
{
    var cookieVal = getFlashCookie('flash_ad_seen');
    if (cookieVal!=null && cookieVal!="") {
        return false;
    }
    else {
        setFlashCookie('flash_ad_seen',true,30);
        return true;
    }
}

function logAdClick() {
    var page = $("#_current-page").val();
    $.ajax({
        type: "POST",
        url: "/member/ajax/log_ad_click?ad_source=" + page,
        dataType: 'json'
    });
}

