// ====================================================
// Monopad Wishlist Javascript
// ====================================================

if (typeof Monopad == "undefined")
	Monopad = {};
if (typeof Monopad.Wishlist == "undefined")
	Monopad.Wishlist = {};

Monopad.Wishlist = {
	// options for reloading
	options : {},
	id : null,
	username : null,
	listtype : null,
	inited : false,
	page : null,

	// init Wishlist javascript class
	init : function(id, username, page) {
		this.id = id;
		this.username = username;
		this.inited = true;
		this.page = page;
	},

	// switch list type
	switchListtype : function(listtype) {
		var types = {
			list : "viewList",
			thumbnail : "viewThumb",
			blog : "viewBlog"
		};
		for (var type in types) {
			var id = types[type];
			if (type == listtype) {
				$("#" + id).addClass("active");
			} else {
				$("#" + id).removeClass("active");
			}
		}

		var limit = new Date();
		limit.setTime(limit.getTime() + (86400 * 14));
		document.cookie = "CakeCookie[listtype]=" + listtype + "; expires="
				+ limit.toGMTString();
		this.listtype = listtype;

		if (this.inited && this.id != "top") {
			if (document.all) {
				location.reload();
			} else {
				this.reload();
			}
		} else {
			if (this.id == "top") {
				location.reload();
			}
		}
	},

	// add
	add : function(item_id, type, obj_id) {
		//obj_id is to use object identification
		if(!obj_id) {
			obj_id = item_id;
		}
		
		Monopad.Wishlist.setEvent(type, item_id, false, false);

		$.ajax({
					type : "GET",
					url : "/item/add/" + type,
					data : {
						item_id : item_id
					},
					success : function(id) {
						if (id) {
							if (type == 1) {
								var obj = $('#fav_' + obj_id);
								obj.removeClass('favAdd');
								obj.addClass('favDel');
								obj.attr('id', 'fav_' + id);
								obj.css('display', 'none');
								obj.css('display', 'block');
							} else if (type == 2) {
								var obj = $('#have_' + obj_id);
								obj.removeClass('haveAdd');
								obj.addClass('haveDel');
								obj.attr('id', 'have_' + id);
								obj.css('display', 'none');
								obj.css('display', 'block');
							}
							Monopad.Wishlist.setEvent(type, id, false,true);
							soundManager.play('add', '/common/mp3/button.mp3');
						} else {
							Monopad.Wishlist.setEvent(type, id, true, true);
						}
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						Monopad.Wishlist.setEvent(type, obj_id, true, true);
					}
				});
	},

	// delete
	del : function(item_id, type) {
		Monopad.Wishlist.setEvent(type, item_id, false, false);

		$.ajax({
					type : "GET",
					url : "/item/del/" + type,
					data : {
						item_id : item_id
					},
					success : function(msg) {
						if (msg) {
							Monopad.Wishlist
									.setEvent(type, item_id, true, true);
							if (type == 1) {
								var obj = $('#fav_' + item_id);
								obj.removeClass('favDel');
								obj.addClass('favAdd');
								obj.css('display', 'none');
								obj.css('display', 'block');
							} else if (type == 2) {
								var obj = $('#have_' + item_id);
								obj.removeClass('haveDel');
								obj.addClass('haveAdd');
								obj.css('display', 'none');
								obj.css('display', 'block');
							}
							soundManager.play('add', '/common/mp3/button.mp3');
						} else {
							Monopad.Wishlist.setEvent(type, item_id, false,
									true);
						}
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						Monopad.Wishlist.setEvent(type, item_id, false, true);
						alert("Connection Failed!");
					}
				});
	},

	setShoppingType : function(type) {
		this.options["shoppingtype"] = type;
	},

	setEvent : function(type, id, is_add, is_event) {
		if (type == 1) {
			var obj = $('#fav_' + id);
			var obj2 = $('a', obj);
			obj2.removeAttr('onclick');// delete existing event on html
			obj2.unbind('click');
			if (is_event) {
				if (is_add) {
					obj2.bind('click', function() {
								Monopad.Wishlist.add(id, 1);
								return false;
							});
				} else {
					obj2.bind('click', function() {
								Monopad.Wishlist.del(id, 1);
								return false;
							});
				}
			} else {
				obj2.bind('click', function() {
							return false;
						});
			}
		} else if (type == 2) {
			var obj = $('#have_' + id);
			var obj2 = $('a', obj);
			obj2.removeAttr('onclick');// delete existing event on html
			obj2.unbind('click');
			if (is_event) {
				if (is_add) {
					obj2.bind('click', function() {
								Monopad.Wishlist.add(id, 2);
								return false;
							});
				} else {
					obj2.bind('click', function() {
								Monopad.Wishlist.del(id, 2);
								return false;
							});
				}
			} else {
				obj2.bind('click', function() {
							return false;
						});
			}
		}
	},

	// trash
	trash : function(id) {
		this.options['trash'] = id;
		this.reload();
	},

	// restore
	restore : function(id) {
		this.options['restore'] = id;
		this.reload();
	},

	// reload list by ajax request
	reload : function() {
		$('#content').load(this.createURL());

		// reset Parameters
		this.options = {};
	},

	createURL : function() {
		var url = "";
		if (this.id == "itemsearch") {
			url = "/item/search";
			if (this.listtype) {
				url += "/" + this.listtype;
			}
			url += "?q=" + this.username;
			if (this.page) {
				url += "&page=" + this.page;
			}
		} else if (this.id == "shoppingsearch") {
			url = "/shopping/search/" + this.options['shoppingtype'];
			if (this.listtype) {
				url += "/" + this.listtype;
			}
			url += "?q=" + this.username;
			if (this.page) {
				url += "&page=" + this.page;
			}
		} else {
			url = "/entry/category/" + this.id + "/" + this.username;
			if (this.listtype) {
				url += "/" + this.listtype;
			}
			if (this.page) {
				url += "?page=" + this.page;
			}
		}

		if (url.search(/\?/)) {
			url += "&" + this.createParameters();
		} else {
			url += "?" + this.createParameters();
		}

		return url;
	},

	createParameters : function() {
		var pars = [];
		for (var key in this.options) {
			pars.push(key + "=" + this.options[key]);
		}
		return pars.join("&");
	},

	reportError : function() {
		alert("Connection Failed");
	},

	addFavorite : function(user_id) {
		$('#favoriteTool').load("/users/favorite_add/" + user_id);
	},

	delFavorite : function(user_id) {
		$('#favoriteTool').load("/users/favorite_del/" + user_id);
	},

	//dummy function for avoiding [,] error of IE
	dummy : function() {
	}
}