$(document).ready(function() {
	$('#btnFav').click(function(){
	    $.ajax({
	        url:baseUrl+'ajax.aspx?NoCopyRight=1&type=GetMemberFavCat',
	        cache:false,
	        success:function(data){
	            if(data == 'NOLOGIN'){
	                if(confirm('请先登录再收藏,是否现在就登录?')){
	                    window.location.href= baseUrl + 'login.aspx?ReturnUrl='+window.location.href;
	                };
	            }else{
	                $('#ddlFavCat').html(data);
	                $('#ShowFav').popup({
	                    width:300,
	                    height:200,
	                    isFixed:true,
	                    btnClose:'closepop'
	                });
	            };
	        },
	        error:function(){alert('error');}
	    });
	    return false;
	});
	
	$('#btnAddFav').click(function(){
	    $.ajax({
	        url:baseUrl+'ajax.aspx?NoCopyRight=1&type=AddToFav&ProductID='+productID+'&FCID='+$('#ddlFavCat').val(),
	        cache:false,
	        success:function(data){
	            switch(data){
	                case 'NOLOGIN':
	                    alert('请先登录');
	                    break;
	                case 'HAD':
	                    alert('您的收藏夹已经有了这个物品');
	                    break;
	                case 'OK':
	                    alert('收藏成功');
	                    break;
	            }
	            disablePopup($('#ShowFav'));
	        },
	        error:function(){alert('error');}
	    });
	});//end click
	
	//添加新分类
	$('#NewFC').toggle(function(){
	    $(this).html('取消添加');
	    $('#newFCContent').show();
	    $('#txtNewFCName').val('');
	    $('#btnAddFC').unbind().click(function(){
	        if($.trim($('#txtNewFCName').val()) == ''){
	            alert('请输入分类名称');
	            return false;
	        };
	        $.ajax({
	            url:baseUrl + 'ajax.aspx?NoCopyRight=1&type=AddNewFavCat',
	            type:'post',
	            data:'txtNewFCName='+escape($.trim($('#txtNewFCName').val())),
	            cache:false,
	            success:function(data){
	                switch(data){
	                    case 'NOLOGIN':
	                        alert('请先登录');
	                        break;
	                    case 'ERROR':
	                        alert('添加失败');
	                        break;
	                    default:
	                        $('#ddlFavCat').html(data);
	                        $('#ddlFavCat option:last-child').attr('selected','selected');
	                        $('#NewFC').click();
	                        break;    
	                }
	            },
	            error:function(){alert('error');}
	        });
	    });
	    return false;
	},function(){
	    $(this).html('添加分类');
	    $('#newFCContent').hide();
	});//end $('#NewFC').click
	
	//修改分类名
	$('#EditFC').toggle(function(){
	    if($('#ddlFavCat').val() == '0'){
	        alert('请选择要修改的分类');
	        $(this).click();
	        return false;
	    };
	    $('#ddlFavCat').attr('disabled','disabled');
	    $(this).html('取消修改');
	    $('#newFCContent').show();
	    $('#txtNewFCName').val($('#ddlFavCat option[@selected]').text());
	    $('#btnAddFC').unbind().click(function(){
	        if($.trim($('#txtNewFCName').val()) == ''){
	            alert('请输入分类名称');
	            return false;
	        };
	        if($.trim($('#txtNewFCName').val()) == $.trim($('#ddlFavCat option[@selected]').text())){
	            alert('请输入新的分类名称');
	            return false;
	        };
	        $.ajax({
	            url:baseUrl + 'ajax.aspx?NoCopyRight=1&type=EditFavCat',
	            type:'post',
	            data:'txtNewFCName='+escape($.trim($('#txtNewFCName').val())) + '&FCID='+$('#ddlFavCat').val(),
	            cache:false,
	            success:function(data){
	                switch(data){
	                    case 'NOLOGIN':
	                        alert('请先登录');
	                        break;
	                    case 'ERROR':
	                        alert('修改失败');
	                        break;
	                    case 'OK':
	                        $('#ddlFavCat option[@selected]').text($.trim($('#txtNewFCName').val()));
	                        $('#EditFC').click();
	                        break;    
	                }
	            },
	            error:function(){alert('edit fc error');}
	        });
	    });
	},function(){
	    $(this).html('修改分类');
	    $('#newFCContent').hide();
	    $('#ddlFavCat').removeAttr('disabled');
	});
	
	//删除分类
	$('#DelFC').click(function(){
	    if($('#ddlFavCat').val() == '0'){
	        alert('请选择要删除的分类');
	        return false;
	    }else if(confirm('确定要删除此收藏夹分类吗?')){
	        var fcID = $('#ddlFavCat').val();
	        $.ajax({
	            url:baseUrl + 'ajax.aspx?NoCopyRight=1&type=DelFavCat',
	            type:'post',
	            data:'&FCID='+fcID,
	            cache:false,
	            success:function(data){
	                switch(data){
	                    case 'NOLOGIN':
	                        alert('请先登录');
	                        break;
	                    case 'ERROR':
	                        alert('删除失败');
	                        break;
	                    case 'OK':
	                        $('#ddlFavCat option[@value='+fcID+']').remove();
	                        break;    
	                }
	            },
	            error:function(){alert('edit fc error');}
	        });
	    };
	    return false;
	});
	

});
