function update_component(component_name) {
    var update_component_url = '/member/ajax/get_component/' + escape(component_name) + '/';
    //$('#' + component_name).html('<span style="color:grey">Updating...</span>');
    $.get(update_component_url,[],function(data) {
       $('#' + component_name).replaceWith(data);
    });
    
}
$(document).bind("update_ajax_goodies",function(e,status){
    $('.update_me').each(function(){
        update_component($(this).attr('id'));
    })
});

function toggle_woggle_ajax(element, url, callback) {
    element.html('<span style="color:grey">Updating...</span>');
    $.post(url, null, function(data) {
        if (data != 'success') {
            // XXX: checking response string isn't that great because this code will stop working if the response string is changed
            if (data == 'You must be logged in to save this property.') {
                url = '/member/popups/login_plz/?action=' + escape('to save a favorite property');
                $('#modal_div').jqm({
                    ajax: url
                });
                $('#modal_div').jqmShow();
            }
            else {
                alert(data);
            }
            callback(false);
            return;
        }
        $(document).trigger('update_ajax_goodies');
        callback(true);
    });
}

function toggle_woggle(element,url,show) {
    var element_old_html;
    element_old_html = element.html();

    toggle_woggle_ajax(element, url, function(success) {
        element.html(element_old_html);
        if (success) {
            show.show();
            element.hide();
        }
    });
}

function map_toggle_woggle(element, url, callback) {
    toggle_woggle_ajax(element, url, callback);
}

function save_property(id,element,callback) {
    var url = '/member/ajax/save_property/' + id + '/';
    if (typeof callback === 'function') {
        map_toggle_woggle(element, url, callback);
    } else {
        toggle_woggle(element, url, $(".delete_favorite_button_" + id));
    }
}

function unsave_property(id,element,callback) {
    var url = '/member/ajax/unsave_property/' + id + '/',
        show = $(".add_favorite_button_" + id);
    if (typeof callback === 'function') {
        map_toggle_woggle(element, url, callback);
    } else {
        toggle_woggle(element,url,show);
    }
}

function unsave_search(id) {
    var url = '/member/ajax/unsave_search/' + id + '/';
    $.post(url,null,function(data) {
        if (data != 'success') {
            alert(data);
            return;
        }
        $('#saved_search_row_'+id).hide();
        $(document).trigger('update_ajax_goodies');
    });
}

function ajax_login(recipient) {
    var url = '/member/ajax/login/';
    $.post(url,recipient.serialize(),function(data) {
        var i, mark_as_error;
        if (data != 'success') {
            mark_as_error = [$('#email').parent(),$('#password').parent()];
            for (i in mark_as_error) {
                mark_as_error[i].addClass('error');
            }
            $('#id_error').html(data);
            $('#id_error').slideDown();
        }
        else {
            $(document).trigger('update_ajax_goodies');
        }
    });
}

function change_frequency(search_id,frequency) {
    var url = "/member/ajax/update_saved_search/" + search_id + '/';
	$('#frequency_spinner_' + search_id).show();
	$.post(url, { 'frequency':frequency }, function(data) {
		if (data == 'success') {
    		$('#frequency_updated_' + search_id).show();
    	} else {
    	    alert(data);
    	}
    	$('#frequency_spinner_' + search_id).hide();
	});
}

function rename_saved_search(search_id, callback) {
    var url = '/member/ajax/get_search_name/' + search_id + '/';
    $.ajax({
        type: "GET",
        url: url,
        dataType: 'text',
        success: function(data) {
    		var elem = $('#saved_search_row_' + search_id + ' .search_name');
    		elem.text(data);
    		if (typeof callback === 'function') callback();
    	},
    	error: function (xhr, textStatus, errorThrown) {
            alert(xhr.responseText);
            if (typeof callback === 'function') callback();
        }
    });
}

$(document).ready(function() {
    $("[class*='add_favorite_button']").live("click", function(e){
        var recipient, id;
        e.preventDefault();
        recipient = $(e.target).parent();
        id = recipient.attr('class').slice(35);
        recipients = $('.add_favorite_button_' + id);
        save_property(id,recipients);
        if (typeof favMgr !== 'undefined') favMgr.favs.push(id);
    });
    $("[class*='delete_favorite_button']").live("click", function(e){
        var recipient, id;
        e.preventDefault();
        recipient = $(e.target).parent();
        id = recipient.attr('class').slice(38);
        recipients = $('.delete_favorite_button_' + id);
        unsave_property(id,recipients);
        if (favMgr) favMgr.favs.remove(id);
    });
    $("[id*='delete_search_button']").bind("click", function(e){
        var recipient, id;
        e.preventDefault();
        recipient = $(e.target);
        id = recipient.attr('id').slice(21);
        unsave_search(id);
    });
    
    $('#ajax_login').bind('submit',function(e) {
        e.preventDefault();
        recipient = $(e.target);
        ajax_login(recipient);
    });
});
