/*
	（´・ω・｀）やあ、参考にしに来たのかい？
*/


(function($){
	var type = 0;
	var check = 0;
	var atdlen = 0;
	var ids = [];
	var atds = [];
	var nums = 50;
	var names = ["お気に入り", "お気に入り２", "お気に入り３", "お気に入り４"];
	var status = [0, 0, 0];
	$.cookie = function(name, value, options){
		if(typeof value != 'undefined'){
			options = options || {};
			if(value === null){
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)){
				var date;
				if(typeof options.expires == 'number'){
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				}else{
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
			}
			var path = options.path ? '; path=' + (options.path) : '';
			var domain = options.domain ? '; domain=' + (options.domain) : '';
			var secure = options.secure ? '; secure' : '';
			document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		}else{
			var cookieValue = null;
			if(document.cookie && document.cookie != ''){
					var cookies = document.cookie.split(';');
				for (var i = 0; i < cookies.length; i++){
					var cookie = jQuery.trim(cookies[i]);
					if(cookie.substring(0, name.length + 1) == (name + '=')){
						cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
						break;
					}
				}
			}
			return cookieValue;
		}
	};

	$.load = function(){
		ids = [];
		nums = 50;
		check = 0;
		names = ["お気に入り", "お気に入り２", "お気に入り３", "お気に入り４"];
		var SET = decodeURI($.cookie("m"+type)).split("<>");
		if(typeof(SET[3]) !== "undefined"){
			nums = SET[0];
			check = Number(SET[4]);
			names[type] = SET[1];
			if(SET[3].length >= 3){
				for(var i=0; i<SET[3].length; i+=3){
					var ID = SET[3].substr(i, 3);
					ids[ID] = true;
					ID = parseInt(ID, 36);
					$("#ID"+ID).find("i.CHU").addClass("ON2");
				}
			}
		}
		$("#CHECK").attr("checked", check);
		$("#NAME").val(names[type]);
		$("#NUMS").val(nums);
	}

	$.save = function(){
		var IDS = "";
		for(var i in ids){
			if(ids[i] === true) IDS += i;
		}
		var NAME = (names[type])? names[type].replace(/[<>]+/i, ""): "お気に入り";
		$.cookie("m"+type, encodeURI(nums + "<>" + NAME + "<>1<>" + IDS + "<>" + check), {"expires":1000, "path":"/"});
	}

	$.atdload = function(){
		var SET = $.cookie("atd");
		if(SET != null){
			if(SET.length >= 3){
				for(var i=0; i<SET.length; i+=6){
					var ID = SET.substr(i, 6);
					atds[ID] = true;
					atdlen++;
					ID = parseInt(ID, 36);
					$("#ID"+ID).find(".ATD").addClass("ON2");
				}
			}
		}
	}

	$.atdsave = function(){
		var ATDS = "";
		for(var i in atds){
			if(atds[i] === true) ATDS += i;
		}
		$.cookie("atd", ATDS, {"expires":1000, "path":"/"});
	}

	var over = function(){$(this).css("background-color", "#CDF");}
	var out = function(){$(this).css("background-color", "");}

	$(function(){
		type = $("input:radio").change(
			function(){
				type = Number($(this).val());
				$("th.ON1").removeClass("ON1");
				$("td.ON2").removeClass("ON2");
				$.load();
			}
		).filter(":checked").val();

		$("#NUMS").change(
			function(){
				nums = $(this).val();
				$.save();
			}
		);

		$("#SUBMIT").click(
			function(){
				names[type] = $("#NAME").val();
				$.save();
			}
		);

		$("#CHECK").click(
			function(){
				check = $(this).is(":checked")? 1: 0;
				$.save();
			}
		);

		$("#DELETE").click(
			function(){
				if(window.confirm("それを消すなんてとんでもない！")){
					$.cookie("m"+type, null);
					$("th.ON1").removeClass("ON1");
					$("td.ON2").removeClass("ON2");
					$.load();
				}
			}
		);

		$.load();
		$.atdload();
		var set_cookie = $.cookie("set");
		if(set_cookie !== null) set_cookie = set_cookie.split(""); else set_cookie = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'];
		switch(set_cookie[0]){//HTN1 TWT2 VEW4
			case "1":
				status = [1, 0, 0];
				break;
			case "2":
				status = [0, 2, 0];
				break;
			case "3":
				status = [1, 2, 0];
				break;
			case "4":
				status = [0, 0, 4];
				break;
			case "5":
				status = [1, 0, 4];
				break;
			case "6":
				status = [0, 2, 4];
				break;
			case "7":
				status = [1, 2, 4];
				break;
			default:
		}
		if(status[0] === 1){
			$("#HTN").text("");
			$("a.HTN").hide();
		}
		if(status[1] === 2){
			$("#TWT").text("");
			$("a.TWT").hide();
		}
		if(status[2] === 4){
			$("#VEW > span").text("ジャンプ");
		}
		if(set_cookie[2] == 1){
			$("tbody").find("tr[id]:odd").find("td:eq(2)").css("background-color", "#F7F7FE")
		}

		$("th.ALL").live("click",
			function(){
				var THIS = $(this);
				if(THIS.is(".ON1") === false){
					THIS.addClass("ON1").parent().nextAll("tr").find("td > .SRE").hide().next(".RES").show();
				}else{
					THIS.removeClass("ON1").parent().nextAll("tr").find("td > .SRE").show().next(".RES").hide();
				}
			}
		).hover(over, out);

		$("a.OPN").live("click",
			function(){
				if(status[2] === 4) return true;
				$(this).parent().hide().next().show();
				return false;
			}
		);

		$("img.CLS").live("click",
			function(){
				$(this).parent().hide().prev().show();
			}
		);

		$("td.ATD").live("click",
			function(){
				var THIS = $(this);
				var ID = THIS.css("background-color", "").parent().attr("id").replace(/^ID/i, "");
				if(ID > 0){
					ID = Number(ID).toString(36).split("");
					while(ID.length < 6) ID.unshift("0");
					ID = ID.join("");
					if(atds[ID] === true){
						atdlen--;
						atds[ID] = false;
						THIS.removeClass("ON2");
					}else{
						if(atdlen >= 650){
							alert("もうcookieが限界だ！\nあとで読むとか言ってないで今読め！");
							return false;
						}
						atdlen++;
						atds[ID] = true;
						THIS.addClass("ON2");
					}
					$.atdsave();
				}
  			}
		).hover(over, out);

		$("#BGC").live("click",
			function(){
				if(set_cookie[2] == 0){
					$("tbody").find("tr[id]:odd").find("td:eq(2)").css("background-color", "#F7F7FE")
					set_cookie[2] = 1;
				}else{
					$("tbody").find("tr[id]:odd").find("td").css("background-color", "")
					set_cookie[2] = 0;
				}
				while(set_cookie.length < 10) set_cookie.push("0");
				var view = set_cookie.join("");
				$.cookie("set", view, {"expires":1000, "path":"/"})
			}
		).hover(over, out);

		$("#VEW").live("click",
			function(){
				if(status[2] === 0){
					$(this).find("span").text("ジャンプ");
					status[2] = 4;
				}else{
					$(this).find("span").text("プレビュー");
					status[2] = 0;
				}
				set_cookie[0] = status[0]+status[1]+status[2];
				while(set_cookie.length < 10) set_cookie.push("0");
				var view = set_cookie.join("");
				$.cookie("set", view, {"expires":1000, "path":"/"})
			}
		).hover(over, out);

		$("#HTN").live("click",
			function(){
				if(status[0] === 0){
					$(this).text("");
					$("a.HTN").hide();
					status[0] = 1;
				}else{
					$(this).text("hateB");
					$("a.HTN").show();
					status[0] = 0;
				}
				set_cookie[0] = status[0]+status[1]+status[2];
				while(set_cookie.length < 10) set_cookie.push("0");
				var view = set_cookie.join("");
				$.cookie("set", status[0]+status[1]+status[2], {"expires":365, "path":"/"})
			}
		).hover(over, out);

		$("#TWT").live("click",
			function(){
				if(status[1] === 0){
					$(this).text("");
					$("a.TWT").hide();
					status[1] = 2;
				}else{
					$(this).text("Tweet");
					$("a.TWT").show();
					status[1] = 0;
				}
				set_cookie[0] = status[0]+status[1]+status[2];
				while(set_cookie.length < 10) set_cookie.push("0");
				var view = set_cookie.join("");
				$.cookie("set", status[0]+status[1]+status[2], {"expires":365, "path":"/"})
			}
		).hover(over, out);

		$("i.CHU").live("click",
			function(){
				var THIS = $(this);
				var ID = THIS.css("background-color", "").parent().attr("id").replace(/^ID/i, "");
				if(ID > 0){
					ID = Number(ID).toString(36).split("");
					while(ID.length < 3) ID.unshift("0");
					ID = ID.join("");
					if(ids[ID] === true){
						ids[ID] = false;
						THIS.removeClass("ON2");
					}else{
						ids[ID] = true;
						THIS.addClass("ON2");
					}
					$.save();
				}
			}
		).hover(over, out);

		$("i.SUB").live("click",
			function(){
				var THIS = $(this);
				var FLAG = false;
				if(THIS.is(".ON1") === false){
					THIS.addClass("ON1").parent().nextAll("li").each(
						function(){
							var ID = $(this).attr("id").replace(/^ID/i, "");
							if(!ID){
								FLAG = true;
								return false;
							}
							ID = Number(ID).toString(36).split("");
							while(ID.length < 3) ID.unshift("0");
							ID = ID.join("");
							ids[ID] = true;
							$(this).find("i.CHU").addClass("ON2");
						}
					);
					if(FLAG){
						return;
					}

					THIS.parent().parent().nextAll("ul").each(
						function(){
							$(this).find("li").each(
								function(){
									var ID = $(this).attr("id").replace(/^ID/i, "");
									if(!ID){
										FLAG = true;
										return false;
									}
									ID = Number(ID).toString(36).split("");
									while(ID.length < 3) ID.unshift("0");
									ID = ID.join("");
									ids[ID] = true;
									$(this).find("i.CHU").addClass("ON2");
								}
							);
							if(FLAG){
								return false;
							}
						}
					);
				}else{
					THIS.removeClass("ON1").parent().nextAll("li").each(
						function(){
							var ID = $(this).attr("id").replace(/^ID/i, "");
							if(!ID){
								FLAG = true;
								return false;
							}
							ID = Number(ID).toString(36).split("");
							while(ID.length < 3) ID.unshift("0");
							ID = ID.join("");
							ids[ID] = false;
							$(this).find("i.CHU").removeClass("ON2")
						}
					);
					if(FLAG){
						return;
					}

					THIS.parent().parent().nextAll("ul").each(
						function(){
							$(this).find("li").each(
								function(){
									var ID = $(this).attr("id").replace(/^ID/i, "");
									if(!ID){
										FLAG = true;
										return false;
									}
									ID = Number(ID).toString(36).split("");
									while(ID.length < 3) ID.unshift("0");
									ID = ID.join("");
									ids[ID] = false;
									$(this).find("i.CHU").removeClass("ON2")
								}
							);
							if(FLAG){
								return false;
							}
						}
					);
				}
				$.save();
			}
		).hover(over, out);
/*
					THIS.addClass("ON1").parent().nextAll("li").each(
						function(){
							var ID = $(this).attr("id").replace(/^ID/i, "");
							if(!ID) return false;
							ID = Number(ID).toString(36).split("");
							while(ID.length < 3) ID.unshift("0");
							ID = ID.join("");
							ids[ID] = true;
							$(this).find("i.CHU").addClass("ON2");
						}
					);



		$("a.CNT").live("mousedown",
			function(){
				var THIS = $(this);
				if(THIS.is(".OPN") === true && status[2] === 0){
					return;
				}
				var ID = THIS.parents("tr").attr("id").replace(/^ID/i, "");
				$.post("/click.php", {"id":ID}, function(){return true;});
			}
		);
*/
	});
})(jQuery);


