﻿/*
** 用户收藏JS类
** 用户不需要登陆后才可以将商品加入到收藏
** 在用户将商品增加到收藏的时候要做登陆验证
*/
//这是程序的入口，用于检查页面所有的加入收藏的链接并绑定事件
$(document).ready(function() {
	$(".addfavor").bind("click", function() {
		var mySKUDiv = $(this).parents("div[styleId]");
		var myFavorite = new Favorite(mySKUDiv.attr("styleId"));
		myFavorite.AddFavorite();
		return false;
	});
})
// 用户收藏的类
var Favorite = function(styleId) {
	return Favorite.func.init(styleId);
}
Favorite.func = Favorite.prototype = {
	// 程序初始化
	init: function(styleId) {
		this.styleId = styleId;
	},
	//增加到购物车
	AddFavorite: function() {
		var myObject = this;
		if (this.styleId) {
			MiniLogin.Action(myObject, "", "");
		}
	},
	//提示信息显示 5000秒钟以后自动关闭
	ShowForm: function(msg) {
		var myDiv = $("<div class=\"detail-pop\" style=\"display:none;\" id=\"favorite_pop\"><div class=\"title\">收藏备选</div><p>" + msg + "</p><div class=\"btn-favcon clearfix\"><a href=\"http://www.m18.com/Member/MemberFavourite.aspx\" class=\"btn-favlist\">查看备选</a><a href=\"#\" class=\"btn-buylist\"  name=\"fClose\">继续购物</a></div><div class=\"close-favlist\"><a href=\"#\" name=\"fClose\">关闭</a></div></div>");
		$("body").append(myDiv);

		$("#favorite_pop").fadeIn("slow");
		var timeOut = setTimeout(function() {
			$("#favorite_pop").fadeOut("slow", function() {
				$(this).remove();
			});
		}, 4000);

		myDiv.find("a[name='fClose']").bind("click", function() {
			clearTimeout(timeOut);
			$("#favorite_pop").remove();
			return false;
		});
	},
	//方法的回调，用于在检查完了用户是否登陆以后，才执行的操作
	Callback: function() {
		var sc = this.ShowForm;
		if (this.styleId) {
			$.AjaxLoader(ajaxServiceAddress.AddFavorite, "{styleId:'" + this.styleId + "'}",
             function(data) {
             	var result = eval('(' + data + ')');
             	var jsonObj = eval(result.d);
             	//增加到收藏！
             	setTimeout(function() {
             		$("#Points_pop_wait").remove();
             		sc(jsonObj[0].Msg);
             	}, 1500);
             },
            function() {
            	alert("服务器联接超时！");
            },
            function() {
            	//$("#Points_pop_wait").remove();
            },
            function() {
            	var myDiv2 = $("<div id=\"Points_pop_wait\" style=\"display:none;\" class=\"detail-pop\"><div class=\"title\">收藏备选</div><p><img src=\"http://img.m18.com/web/i/comm/load.gif\"/></p><div class=\"close-favlist\"><a href=\"#\" name=\"fClose\">关闭</a></div></div>");
            	$("body").append(myDiv2);
            	$("#Points_pop_wait").fadeIn("slow");
            }
        );
		}
	}
};

//----------------------------------Static Method-----------------------
$.AjaxLoader = function(url, data, callback, error, wait, start) {
	$.ajax({
		url: url,
		data: data,
		type: "POST",
		contentType: "application/json; charset=utf-8",
		success: callback,
		error: error,
		complete: wait,
		beforeSend: start
	});
};