$(document).ready(function() {

	$("input[@id^='toggle_btn_']").css("cursor", "pointer");

        $("input[@id^='toggle_btn_']").click(function() {
                var box_name = $(this).attr("id").replace("toggle_btn_", "");
		var box_id = "boxbody_" + box_name;
		var box_display_val = $("#"+box_id).css("display");

		if($("#"+box_id).css("display")=="none")
		{
			$("#"+box_id).css("display", "block");
			$(this).attr("src", "/images/common/bt_black-col.png");
			box_display_val = "block";
		} else
		{
			$("#"+box_id).css("display", "none");
			$(this).attr("src", "/images/common/bt_black-exp.png");
			box_display_val = "none";
		}

		if($("#page_location").val()=="profile")
		{
			$.ajax({
				type: "POST",
				url: "/",
				data: {
					box_name: box_name,
					box_display: box_display_val,
					request_type: "save_box_display"
				}
			});
		}
        });

	if($("#sortable_list_m").length>0)
	{
		$("div[@id^='box_hd_']").css('cursor', 'move');

		$("#sortable_list_m").sortable({ 
			opacity: 0.8,
			handle: $("div[@id^='box_hd_']"),
			placeholder: 'sortable_helper_m',
			cursor: "move",
			update: save_box_position
		}); 
	}

	if($("#sortable_list_r").length>0)
	{
        	$("#sortable_list_r").sortable({
                	opacity: 0.8,
	                handle: $("div[@id^='box_hd_']"),
			placeholder: 'sortable_helper_r',
                	cursor: "move",
			update: save_box_position
        	});
	}

	/* member tags */
        $("#save_tag_btn").click(function() {
		save_member_tags();
        });

        $("#user_tags").keypress(function(e) {
                if(e.keyCode==13)
                {
                        save_member_tags();
                }
        });

	$("#update_tag_btn").click(function() {
		if($("#update_tag_container").css("display")=="none")
		{
			$("#profile_tag_container").hide();
			$("#update_tag_container").show();
		} else
		{
                        $("#update_tag_container").hide();
			$("#profile_tag_container").show();
		}
	});
	$("#save_update_tag_btn").click(function () {
		save_update_tags();
	});
	$("#cancel_update_tag_btn").click(function () {
		$("#update_tag_container").hide();
		$("#profile_tag_container").show();
	});
	/*** start profile status ***/
	$("#update_status_btn").css("cursor", "pointer").click(function() {
		$("#status_box").hide();
		$("#update_status_box").show();
	});

	$("#cancel_status_btn").css("cursor", "pointer").click(function() {
		$("#update_status_box").hide();
		$("#status_box").show();
	});

	$("#select_status_btn").css("cursor", "pointer").click(function() {
                $("#select_status_list").slideDown();
	});

	$("#select_status_list > div").mouseover(function() {
		$(this).css("background-color", "#FAAA54");
	}).mouseout(function() {
                $(this).css("background-color", "#FFFDDD");
        });

	$("#select_status_list > div").css("cursor", "pointer").click(function() {
		$("#profile_status").val($(this).text());
		$("#select_status_list").hide();
	});
	
	$(document.body).click(function() {
	        $("#select_status_list").click(function() {
			return false;
        	});

		if($("#select_status_list").css("display")=="block")
		{
			$("#select_status_list").hide();
		}
	});

	$("#profile_status").click(function() {
		if($(this).val()=="please write some message")
		{
			$(this).val("is ");
		}
	});

	$("#save_status_btn").css("cursor", "pointer").click(function() {
		save_profile_status();
	});

	$("#profile_status").keydown(function(e) {
		if(e.keyCode==13)
		{
			save_profile_status();
		}
	});

	$("#clear_status_btn").css("cursor", "pointer").click(function() {
                $.ajax({
                        type: "POST",
                        url: "/",
                        data: {
                                profile_status: "",
                                request_type: "save_profile_status"
                        },
                        beforeSend: function(){
                        },
                        success: function(xml){
                                $("#update_status_box").hide();
                                if(xml.length>0)
                                {
                                	$("#profile_status_box").html(xml);
                                }
                        	$("#status_box").show();
                	}
		});
	});
	/*** end profile status ***/

	/*** start last listen to ***/
        $("#update_last_listen_btn").toggle(
                function() {
			$("#last_listened_to_box").show();
                },
                function() {
			$("#last_listened_to_box").hide();
                }
        );
	$("#save_last_listen_btn").click(function() {
		save_last_listened_to();
	});
        $("#cancel_last_listen_btn").click(function() {
		$("#last_listened_to_box").hide();
        });
	/*** start last listen to ***/

	/*** start playlist ***/
	$("#member_playlist").change(function() {
		reload_playlist();
	});

	$("#add_playlist_btn").css("cursor", "pointer").click(function() {
		$("#playlist_name").val('');
		if($("#playlist_warning").length>0) $("#playlist_warning").remove();

		if($("#edit_playlist_id").length>0) $("#edit_playlist_id").remove();

		if ($("#clear_playlist_box").css("display")=="block")
			$("#clear_playlist_box").slideUp('normal', function() { $("#add_playlist_box").slideUp().slideDown('normal'); });
		else
			$("#add_playlist_box").slideUp().slideDown('normal');
	});

	$("#edit_playlist_btn").css("cursor", "pointer").click(function() {
		if($("#playlist_warning").length>0) $("#playlist_warning").remove();

		var select_playlist_name = $("#member_playlist option:selected").text();
		var select_playlist_id = $("#member_playlist option:selected").val();

		if (select_playlist_id > 0) {
			if($("#clear_playlist_box").css("display")=="block")
				$("#clear_playlist_box").slideUp('normal', function() { $("#add_playlist_box").slideUp().slideDown('normal', function() { $("#playlist_name").val(select_playlist_name); }); });
			else
				$("#add_playlist_box").slideUp().slideDown('normal', function() { $("#playlist_name").val(select_playlist_name); });

			$("#playlist_name").after('<input type="hidden" id="edit_playlist_id" name="edit_playlist_id" value="'+select_playlist_id+'" />');
		}
	});

	$("#playlist_name").focus(function() {
		if($(this).val()=="enter the new playlist name")
			$(this).val('');
	});

	$("#save_playlist_btn").css("cursor", "pointer").click(function() {
		var playlist_name = $("#playlist_name").val().replace(/^\s+|\s+$/g,"");
		var playlist_id;
		if($("#edit_playlist_id").length>0)
		{
			playlist_id = $("#edit_playlist_id").val();
		} else
		{
			playlist_id = 0;
		}
		
		if(playlist_name.length>0 && playlist_name!="enter the new playlist name")
		{
			save_playlist(playlist_name, playlist_id);
		} else
		{
			$("#playlist_name").val("enter the new playlist name");
		}
	});

	$("#cancel_save_btn").css("cursor", "pointer").click(function() {
		$("#add_playlist_box").slideUp();
		$("#playlist_name").val('');
		if($("#edit_playlist_id").length>0) $("#edit_playlist_id").remove();
	});

	$("#clear_playlist_btn").css("cursor", "pointer").click(function() {
		if($("#playlist_warning").length>0) $("#playlist_warning").remove();

		if ($("#member_playlist option:selected").val() > 0) {
			if($("#add_playlist_box").css("display")=="block") 
				$("#add_playlist_box").slideUp('normal', function() { $("#clear_playlist_box").slideDown();  });
			else
				$("#clear_playlist_box").slideDown();
		}
	});

	$("#confirm_clear_btn").css("cursor", "pointer").click(function() {
		var playlist_id = $("#member_playlist option:selected").val();

		clear_playlist(playlist_id);
	});

	$("#cancel_clear_btn").css("cursor", "pointer").click(function() {
		$("#clear_playlist_box").slideUp();
	});
	/*** end playlist ***/


	/*** start private message ***/
        if($("#list_recipient").length>0)
        {
		$("#list_recipient").click(function () {
			$("#recipients_error").html('');
		});
                var preset_recipients = new Array();

                $("input[@id^='recipient_']").each(function() {
                        recipient_id = $(this).attr("id").replace("recipient_", "");
                        preset_recipients[preset_recipients.length] = $(this).val() + "|" + recipient_id;
                });

                $("#list_recipient").autocomplete('/new-message/ajax/recipient/', properties = {
                        matchContains: true,
                        minChars: 0,
                        selectFirst: true,
                        intro_text: "type the name of a friend",
                        no_result: 'no user found',
                        result_field: 'recipients',
			preset_items: preset_recipients
                }).blur(function(){
                        //$("#result_list").hide();
                        if($("#result_list").html() == '<div class="default">type the name of a friend</div>')
                        $("#result_list").hide();
                });

                if($("input[@id^='recipient_']").length>0)
                {
                        $("#compose_message_body").focus();
                }

        }

	$("input[@id^='check_all_']").click(function() {
		if($(this).attr("checked") == true)
		{
			$("input[@id^='check_all_']").attr("checked", "checked");
			$("input[@id^='chk_message_']").attr("checked", "checked");
		} else
		{
                        $("input[@id^='check_all_']").attr("checked", "");
                        $("input[@id^='chk_message_']").attr("checked", "");
		}
	});

	$("img[@id^='del_msg_btn_']").css("cursor", "pointer").click(function() {
		var message_id = $(this).attr("id").replace('del_msg_btn_', '');
		if($("#selected_message_id").length>0)
			$("#selected_message_id").val(message_id);
		else
			$(document.body).append('<input type="hidden" id="selected_message_id" name="selected_message_id" value="'+message_id+'" />');

		if($("#selected_message_type").length>0)
			$("#selected_message_type").val('message');
		else
			$(document.body).append('<input type="hidden" id="selected_message_type" name="selected_message_type" value="message" />');

        	if($("#member_logged_in").val()=="yes")
        	{
                	show_delete_message_dialog();
        	} else
        	{
                	show_login_dialog("show_delete_message_dialog");
        	}

	});

        $("img[@id^='del_notification_btn_']").css("cursor", "pointer").click(function() {
                var message_id = $(this).attr("id").replace('del_notification_btn_', '');
		if($("#selected_message_id").length>0)
			$("#selected_message_id").val(message_id);
		else
                	$(document.body).append('<input type="hidden" id="selected_message_id" name="selected_message_id" value="'+message_id+'" />');

		if($("#selected_message_type").length>0)
			$("#selected_message_type").val('notification');
		else
                	$(document.body).append('<input type="hidden" id="selected_message_type" name="selected_message_type" value="notification" />');

                if($("#member_logged_in").val()=="yes")
                {
                        show_delete_message_dialog();
                } else
                {
                        show_login_dialog("show_delete_message_dialog");
                }

        });

	$("#compose_message_body").click(function() {
		$("#message_body_error").html('');
		if($(this).val()=="enter your message")
			$(this).val('');
	});
	$("#cancel_delete_btn").css("cursor", "pointer").click(function() {
		$("#confirm_delete_box").remove();
	});
	$("#send_message_btn").click(function() {
		var recipient_list = $("#recipients").val().split(",");
		var recipient_list_new = new Array();
		var j = 0;
		for(var i=0;i<recipient_list.length;i++)
		{
			if(recipient_list[i].replace(/^\s+|\s+$/g,"").length>0)
			{
				recipient_list_new[j] = recipient_list[i];
				j++;
			}
		}

		if(recipient_list_new.length<1)
		{
                        $("#recipients_error").html('<em class="input_error">please enter recipient(s)</em>');
                        return false;
		} else if($("#compose_message_body").val()=="" || $("#compose_message_body").val()=="enter your message")
		{
			$("#message_body_error").html('<em class="input_error">please enter message</em>');
			return false;
		} else
		{
			document.message_form.submit();
			return true;
		}
	});
	if($("img[name='unread_msg_icon']").length>0)
	{
		var inbox_count = parseInt($("#inbox_count_updated").val());
		var notif_count = parseInt($("#noti_count_updated").val());
		var total_count = inbox_count + notif_count;
		$("#inbox_count").html('inbox ('+inbox_count+')');
		$("#inbox_count_hd").html('('+total_count+')');
	}
	/*** end private message ***/

	/*** start connection ***/
	$("[name='connection_types']").click(function() {
		var type = $(this).val();
		reload_connection_list(type, 1);
	});
	/*** end connection ***/

	/*** invite_friend ***/
	$("#invite_friend_alt_btn").click(function() {
		var email_list = $("#invite_friend_alt_list").val().replace(/^\s+|\s+$/g,"");	
		if(email_list.length>0)
		{
			document.invite_friend_alt.submit();
		} else
		{
			$("#invite_friend_alt_msg").html('<div class="divbreak" style="width:300px;"><div class="alert_box" style="text-align:center;">please enter friends\' emails</div></div>');
		}
	});
	$("#search_contact_btn").css("cursor", "pointer").click(function() {
		return show_contact_list();
	});
        $("#search_contact_form").keypress(function(e) {
                if(e.which==13)
                {
			show_contact_list();
                }
        });

	$("#add_friends_btn").click(function() {
		checked_add_friends();
	});

	if(urldecode($("#request_uri").val())=="/find-friends/")
	{
		var default_query = "enter email, username, first name or last name";

		$("#search_member_btn").css('cursor', 'pointer').click(function() {
			return $("#search_member_form").submit();
		});

		$("#search_member_query").focus(function() {
			if ($(this).val() == default_query) $(this).val('');
		});

		$("#search_member_form").submit(function() {
			if ($("#search_member_query").val() == default_query || $("#search_member_query").val() == "")
				return false;

			member_search(1);
			window.location = "#result";
			return false;
		});


		var contact_count = parseInt($("#member_count").val());
		var email_domain = $("#email_domain").val();
		var email_domain_display = email_domain.replace('.com', '');
		var contact_label = "";
		if($("#member_count_label").length>0)
		{
			if(contact_count>0)
			{
				if(contact_count==1) contact_label = "contact";
				else contact_label = "contacts";
				$("#member_count_label").html('You have <b>'+contact_count+' '+email_domain_display+' '+contact_label+'</b> who are not yet on <b>Wayango</b>. Invite them now!');
                		if(contact_count<10)
		                {
                		        $("#contact_scroll").css("height", contact_count*30+"px");
		                }
			} else
			{
				$("#member_count_label").html('You have <b>no '+email_domain_display+' contacts</b> who are not yet on <b>Wayango</b>.');
			}
		}

	} else if(urldecode($("#request_uri").val())=="/invite-friends/")
	{
                var contact_count = parseInt($("#member_count").val());
                var email_domain = $("#email_domain").val();
                var email_domain_display = email_domain.replace('.com', '');
		var contact_label = "";
                if($("#member_count_label").length>0)
                {
                        if(contact_count>0)
                        {
                                if(contact_count==1) contact_label = "contact";
                                else contact_label = "contacts";
                                $("#member_count_label").html('You have <b>'+contact_count+' '+email_domain_display+' '+contact_label+'</b> already on <b>Wayango</b>! Add them	as friends now.');;
                        } else
                        {
                                $("#member_count_label").html('You have <b>no '+email_domain_display+'</b> contacts on	<b>Wayango</b>.');
				$("#member_contact_box").html('');
                        }
                }
	}
	/*** invite friend ***/

	/*** start blog ***/
	$("img[@id^='del_blog_btn_']").css("cursor", "pointer").click(function() {
		var blogpost_id = $(this).attr("id").replace('del_blog_btn_', '');
		delete_blogpost(blogpost_id);
	});
	/*** end blog ***/

	/*** start recommendation / wishlist / gift list ***/
	/*** end recommendation / wishlist / gift list ***/

	/*** start store recommendation ***/
	$("#member_profile_seed_form").submit(function() {
		var inp = $("#member_profile_seed_song");
		var res = $("#store_discover");

		if (inp.val() == '' || inp.val() == inp.attr('default')) return false;

		$.ajax({
			type: "POST",
			url: "/discover-store/",
			data: {
				query: inp.val()
			},
			beforeSend: function(){
				res.html('<div class="loading" style="padding:10px;">'+ajax_loading+'</div>');
			},
			success: function(xml){
				res.html(xml);
				setup_niftyplayer('#store_recommendation_list [id^=inline-player-]');
			}
		});

		return false;
	});
	$("#member_profile_seed_song").click(function() {
		if ($(this).val() == $(this).attr('default')) {
			$(this).val('');
			$(this).css('color', '#000000');
		}
	});
	$("#member_profile_seed_song").blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('default'));
			$(this).css('color', '#999999');
		}
	});
	/*** end recommendation ***/
});

function save_profile_status()
{
	var profile_status = $("#profile_status").val().replace(/^\s+|\s+$/g,"");
	if(profile_status.length==0 || profile_status=="is" || profile_status=="please write some message")
	{
		$("#profile_status").val("please write some message");
	} else
	{
		$.ajax({
			type: "POST",
			url: "/",
			data: {
				profile_status:	profile_status,
				request_type: "save_profile_status"
			},
			beforeSend: function(){
			},
			success: function(xml){
				$("#update_status_box").hide();
				if(xml.length>0)
				{
					$("#profile_status_box").html(xml);
				}
				$("#status_box").show();
				submit_fb_feed_status_msg();
			}
		});
	}
}

function save_member_tags()
{
	var request_uri	= $("#request_uri").val();
	if($("#user_tags").val())
	{
		if(request_uri.match(/tagged-as/) || request_uri.match(/my-tagged-content/) || request_uri.match(/tag/))
		{
			$.ajax({
				type: "POST",
				url: "/tagged-as/",
				data: {
					user_tags: $("#user_tags").val(),
					request_type: "save_tags"
				},
				beforeSend: function(){
					$("#save_tag_msg").html('<span class="loading">'+ajax_loading+'</span>');
				},
				success: function(xml){
					$("#user_tags").val("");
					$("#save_tag_msg").html('');
					$("#tag_list").html(xml);
				}
			});
		} else
		{
			$.ajax({
				type: "POST",
				url: "/",
				data: {
					user_tags: $("#user_tags").val(),
					request_type: "save_tags"
				},
				beforeSend: function(){
					$("#tag_list").html('<span class="loading">'+ajax_loading+'</span>');
				},
				success: function(xml){
					$("#user_tags").val("");
					$("#tag_list").html(xml);
				}
			});
		}
	}
}

function save_update_tags()
{
	var request_uri = $("#request_uri").val();

	if(request_uri.match(/tagged-as/))
	{
		$.ajax({
			type: "POST",
			url: "/",
			data: {
				tag_list: $("#update_tag_input").val(),
				page_uri: "tagged-as",
				request_type: "update_tags"
			},
			beforeSend: function(){
				$("#update_tag_msg").html('<span class="loading">'+ajax_loading+'</span>');
			},
			success: function(xml){
				$("#update_tag_msg").html('');
				$("#update_tag_container").hide();
				$("#profile_tag_container").html(xml).show();
			}
		});
	} else
	{
                $.ajax({
                        type: "POST",
                        url: "/",
                        data: {
                                tag_list: $("#update_tag_input").val(),
                                request_type: "update_tags"
                        },
                        beforeSend: function(){
				$("#update_tag_container").hide();
				$("#profile_tag_container").show();
				$("#tag_list").html('<span class="loading">'+ajax_loading+'</span>');
                        },
                        success: function(xml){
                                $("#tag_list").html(xml);
                        }
                });
	}
}

function save_box_position(type)
{
	$.ajax({
		type: "POST",
		url: "/",
		data: {
			maincolumn: $("#sortable_list_m").sortable('serialize'),
			rightcolumn: $("#sortable_list_r").sortable('serialize'),
			request_type: "save_box_position"
		}
	});	
}

function open_status_box()
{
        $("#status_box").hide();
        $("#update_status_box").show();
}

function reload_playlist()
{
	$("#add_playlist_box").hide();
	$("#clear_playlist_box").hide();

	var playlist_id	= $("#member_playlist option:selected").val();

	if (playlist_id > 0) {
		$.ajax({
			type: "POST",
			url: "/playlists/ajax/",
			data: {
				playlist_id: playlist_id,
				request_uri: $("#request_uri").val(),
				request_type: "load_playlist"
			},
			beforeSend: function(){
				$("#playlist_song_list").html('<img src="/images/common/loading.gif" />	loading...');
			},
			success: function(xml){
				$("#playlist_song_list").html(xml);
				setup_playlist();
				setup_niftyplayer('#playlist_tracks [id^=inline-player-]');
			}
		});

	} else {
		$("#playlist_song_list").html("");
	}
}

function save_playlist(playlist_name, playlist_id)
{
	$.ajax({
		type: "POST",
		url: "/playlists/ajax/",
		data: {
			playlist_name: playlist_name,
			playlist_id: playlist_id,
			request_type: "save_playlist"
		},
		beforeSend: function(){
			$("#add_playlist_box").hide().after('<div id="playlist_loading"><img src="/images/common/loading.gif" /> loading...</div>');
		},
		success: function(xml){
			if(xml.match(/^ERROR:/))
			{
				var error_msg = xml.replace('ERROR:', '');
				$("#playlist_loading").remove();
				$("#add_playlist_box").show().append('<div id="playlist_warning"></div>');
				$("#playlist_warning").html('<div class="alert_box" style="text-align:center;margin-top:10px;">'+error_msg+' <img src="/images/common/bt_ok.gif" align="absmiddle" style="cursor:pointer;" onclick="close_warning_box();" /></div>');
			} else
			{
				$("#playlist_name").val('');
				$("#member_playlist").html(xml);
				$("#playlist_loading").remove();
				$("#add_playlist_box").after('<div id="playlist_warning" class="alert_box" style="text-align:center;margin-top:10px;">playlist has been saved</div>');
				
				setTimeout(function() {
					$("#playlist_warning").fadeOut('slow', function() {
						$("#playlist_warning").remove();
					});
				}, 800);

				if(!playlist_id>0) reload_playlist();
			}
		}
	});
}

function clear_playlist(playlist_id)
{
	$.ajax({
		type: "POST",
		url: "/playlists/ajax/",
		data: {
			playlist_id: playlist_id,
			request_type: "clear_playlist"
		},
		beforeSend: function(){
			$("#add_playlist_box").hide().after('<div id="playlist_loading"><img src="/images/common/loading.gif" /> loading...</div>');
		},
		success: function(xml){
			$("#playlist_loading").remove();
			$("#add_playlist_box").after('<div id="playlist_warning" class="alert_box" style="text-align:center;margin-top:10px;">playlist deleted</div>');

			setTimeout(function() {
				$("#playlist_warning").fadeOut('slow', function() {
					$("#playlist_warning").remove();
				});
			}, 800);

			$("#member_playlist option:selected").remove();

			reload_playlist();
		}
	});
}

function close_warning_box()
{
	$("#playlist_warning").remove();
	$("#playlist_name").focus();
}

function remove_playlist_track(song_id, obj)
{
	var playlist_id = $("#member_playlist option:selected").val();

	$.ajax({
		type: "POST",
		url: "/playlists/ajax/",
		data: {
			song_id: song_id,
			playlist_id: playlist_id,
			request_type: "remove_playlist_track"
		},
		beforeSend: function(){
			obj.children("IMG").attr("src", "/images/common/loading.gif");
		},
		success: function(xml){
			obj.parents("THEAD").hide();
		},
		error: function() {
			obj.children("IMG").attr("src", "/images/common/st_remove.gif");
		}
	});
}

function show_delete_message_dialog()
{
	var message_id = $("#selected_message_id").val();
	var message_type = $("#selected_message_type").val();
        $.ajax({
                type: "POST",
                url: "/common/dialog/message/",
                data: {
                        message_id: message_id,
			message_type: message_type,
                        request_type: "show_delete_message_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });

	$("#selected_message_id").remove();
	$("#selected_message_type").remove();
}

function delete_selected_messages(type)
{
	$(document.body).append('<input type="hidden" id="selected_message_type" name="selected_message_type" value="'+type+'" />');
	if($("#member_logged_in").val()=="yes")
	{
		show_delete_messages_dialog();
	} else
	{
		show_login_dialog("show_delete_messages_dialog");
	}	
}

function show_delete_messages_dialog()
{
	var message_type = $("#selected_message_type").val();
	var message_count = $("input[@name^='checked_message']:checked").length;
        $.ajax({
                type: "POST",
                url: "/common/dialog/message/",
                data: {
			message_count: message_count,
			message_type: message_type,
                        request_type: "show_delete_messages_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });

}

function approve_friend(member_id, message_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/add-friend/",
                data: {
                        member_id: member_id,
			message_id: message_id,
                        request_type: "confirm_friend_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function ignore_friend(member_id, message_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/add-friend/",
                data: {
                        member_id: member_id,
                        message_id: message_id,
                        request_type: "ignore_friend_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function reload_connection_list(type, page)
{
        $.ajax({
                type: "POST",
                url: "/connections/",
                data: {
                        type: type,
			page: page,
                        request_type: "reload_connection_list"
                },
                beforeSend: function(){
			$("#connection_list").html('<img src="/images/common/loading.gif" /> Loading');
                },
                success: function(xml){
                        $("#connection_list").html(xml);
                }
        });
}

function reload_connection_view(view)
{
	var type = $("[name='connection_types']:checked").val();

        $.ajax({
                type: "POST",
                url: "/connections/",
                data: {
                        view: view,
			type: type,
                        request_type: "reload_connection_view"
                },
                beforeSend: function(){
                        $("#connection_list").html('<img src="/images/common/loading.gif" /> Loading');
                },
                success: function(xml){
                        $("#connection_list").html(xml);
			if(view=='list')
				toggle_box_html = '<strong>&raquo; list view</strong> | <a href="javascript:reload_connection_view(\'grid\');">&raquo; grid view</a>';
			else
				toggle_box_html = '<a href="javascript:reload_connection_view(\'list\');">&raquo; list view</a> | <strong>&raquo; grid view</strong>'

			$("#toggle_view_box").html(toggle_box_html)
                }
        });
}

function remove_friend(member_id)
{
	remove_connection_dialog(member_id, "friend");
}

function remove_star(member_id)
{
	remove_connection_dialog(member_id, "star");
}

function remove_connection_dialog(member_id, type)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/remove-connection/",
                data: {
                        member_id: member_id,
                        type: type,
                        request_type: "remove_connection_dialog"
                },
                beforeSend: function(){
			show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function show_contact_list()
{
	var request_uri = urldecode($("#request_uri").val());

	var email_account = $("#email_account").val().replace(/^\s+|\s+$/g,"");
	var email_domain = $("#email_domain").val();
	var email_password = $("#email_password").val().replace(/^\s+|\s+$/g,"");

	var email_domain_display = email_domain.replace('.com', '');

	if($("#search_member_result").length>0) $("#search_member_result").hide();

	if(email_account.length>0 && email_password.length>0)
	{
        	$.ajax({
                	type: "POST",
	                url: request_uri,
        	        data: {
                	        email_account: email_account,
                        	email_domain: email_domain,
				email_password: email_password,
	                        request_type: "show_contact_list"
        	        },
                	beforeSend: function(){
                        	$("#search_contact_msg").html('<img src="/images/common/loading.gif" /> Loading');
	                },
        	        success: function(xml){
				$("#search_contact_msg").html('');
				$("#contact_list").html(xml).slideDown('slow');
				var contact_count = parseInt($("#contact_count").val());
				if(contact_count<10)
				{
					$("#contact_scroll").css("height", contact_count*30+"px");
				}

				var contact_label = "";
				if(contact_count==1) contact_label = "contact";
				else contact_label = "contacts";
				if(request_uri=="/find-friends/")
				{
					if(contact_count>0)
					{
						$("#contact_count_label").html('You have <b>'+contact_count+' '+email_domain_display+' '+contact_label+'</b> already on <b>Wayango</b>! Add them as friends now.');
					} else
					{
						$("#contact_count_label").html('You have <b>no '+email_domain_display+'</b> contacts on <b>Wayango</b>. Try again by signing in to another email service above.');
						$("#email_contact_box").html('');
					}
				} else if(request_uri=="/invite-friends/")
				{
                                        if(contact_count>0)
                                        {
                                                $("#contact_count_label").html('You have <b>'+contact_count+' '+email_domain_display+' '+contact_label+'</b> who are not yet on <b>Wayango</b>. Invite them now!');
                                        } else
                                        {
                                                $("#contact_count_label").html('You have <b>no '+email_domain_display+' contacts</b> who are not yet on <b>Wayango</b>.');;
                                                $("#email_contact_box").html('');
                                        }

				}
        	        }
	        });

	} else
	{
		$("#search_contact_msg").html('<div class="divbreak"><div class="alert_box">please specify email address and password</div></div>');
	}

	return false;
}

function toggle_contact_selection()
{
	if($("#contact_select_all").attr("checked")==true)
	{
		$("input[@name^='email_list']").each(function() {
			$(this).attr("checked", true);
		});
	} else
	{
                $("input[@name^='email_list']").each(function() {
                        $(this).attr("checked", false);
                });
	}

	if($("#invite_friend_box").length>0) check_contact_selection();
}

function check_contact_selection()
{
	if($("input[@name^='email_list']:checked").length>0)
	{
		$("#invite_friend_box").slideDown('slow');
		$("#invite_friends_btn").attr("disabled", false);
	} else
	{
		$("#invite_friend_box").slideUp('slow');
		$("#invite_friends_btn").attr("disabled", true);
	}
}

function checked_add_friends()
{
        if($("input[@name^='email_list']:checked").length>0)
        {
                return true;
        } else
        {
                $("#add_friends_msg").html('<div class="divbreak"><div class="alert_box">please select the contacts you want to add as friends.</div></div>');
                return false;
        }
}

function delete_blogpost(blogpost_id)
{
        if($("#member_logged_in").val()=="yes" && ($("#is_owner").val()=="yes" || $("#is_admin").val()=="yes"))
        {
                show_delete_blogpost_dialog(blogpost_id);
        } else
        {
		return false;
        }

}

function show_delete_blogpost_dialog(blogpost_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/delete-blogpost/",
                data: {
                        blogpost_id: blogpost_id,
                        request_type: "load_delete_blogpost_dialog"
                },
                beforeSend: function(){
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function member_search(page)
{
	if($("#contact_list").length>0) $("#contact_list").hide();
	$.ajax({
		type: "POST",
		url: "/find-friends/ajax/",
		data: {
			search_query: $("#search_member_query").val(),
			page: page
		},
		beforeSend: function(){
			$("#search_member_result_body").html('<img src="/images/common/loading.gif" /> Loading');
		},
		success: function(xml){
			$("#search_member_result_body").html(xml);
			$("[@id^='add_friend_btn_']").css("cursor", "pointer").click(function() {
				var member_id = $(this).attr("id").replace("add_friend_btn_", "");
				add_friend(member_id);
			});
		},
		error: function(xml){
			$("#search_member_result_body").html('Not found!');
		}
	});

	$("#search_member_query_t").html($("#search_member_query").val());
	$("#search_member_result").show();
}

function remove_shout_message(message_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/shoutbox/",
                data: {
                        message_id: message_id,
                        request_type: "show_remove_shout_message_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function switch_list(selected_box_name)
{
	var box_name;
	var box_id;
	var box_label;
	$("span[@id^='switch_btn_']").each(function() {
		box_name = $(this).attr("id").replace("switch_btn_", "");
		box_id = "switch_btn_"+box_name;

		if(selected_box_name==box_name)
		{
			box_label = $("#"+box_id+" > a").html();
			$("#"+box_id).html("<strong>"+box_label+"</strong>");
			//$("#recommendation_box_hd").html(box_label);
			$("#"+box_name+"_container").show();
		} else
		{
			if(!$("#"+box_id+" > a").length)
			{
				box_label = $("#"+box_id+" > strong").html();
				$("#"+box_id).html("<a href=\"javascript:switch_list('"+box_name+"');\">"+box_label+"</a>");			
				$("#"+box_name+"_container").hide();
			}
		}
	});
}

function remove_wishlist_item(song_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/wishlist/",
                data: {
                        song_id: song_id,
                        request_type: "show_remove_wishlist_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });	
}

function remove_feed_item(feed_id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/feed/",
                data: {
                        feed_id: feed_id,
                        request_type: "show_remove_feed_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
                        $("#dialog_box").html(xml).show();
                }
        });
}

function save_last_listened_to()
{
        var last_listened_to = $("#last_listened_to_input").val().replace(/^\s+|\s+$/g,"");
        if(last_listened_to.length==0)
        {
		return false;
        } else
        {
                $.ajax({
                        type: "POST",
                        url: "/",
                        data: {
                                last_listened_to: last_listened_to,
                                request_type: "save_last_listened_to"
                        },
                        beforeSend: function(){
				$("#last_listened_to").html(ajax_loading);
                        },
                        success: function(xml){
                                $("#last_listened_to_box").hide();
                                if(xml.length>0)
                                {
                                        $("#last_listened_to").html(xml);
					$("#update_last_listen_btn").trigger('click');
                                }
                        }
                });
        }
}
