/**
 * toilet labo
 *
 * @copyright	2011 RaNa design associates, inc.
 * @link		http://http://www.ranadesign.com/
 * @version		0.3
 * @since		Mar 01, 2011
 * @update		Mar 28, 2011
 */
(function($) {

	var panel = {
			init: function() {
				panel.initReset();
				panel.initDraggable();
				panel.changeHand();
			},
			initReset: function() {
				$(window).scroll(function() {
					$("#area-button").stop(true).animate({
						top: $(window).scrollTop() + 175
					}, {
						duration: 500
					});
				}).scroll();
				$(".reset").click(function() {
					panel._reset();
					return false;
				});
				$(".draggable").each(function(i) {
					$(this).data("x", $(this).position().left);
					$(this).data("y", $(this).position().top);
				});
			},
			_reset: function() {
				$(".draggable").each(function(i) {
					$(this).animate({
						left: $(this).data("x"),
						top: $(this).data("y")
					}, {
						duration: 1000,
						easing: "easeOutElastic"
					});
				});
			},
			initDraggable: function() {
				$(".draggable").mousedown(function(event) {
					event.preventDefault();
				});
				// jQuery UI
				$(".draggable").draggable({
					cancel: ".nohandle",	// drag handle
					stack: ".draggable",	// visual feedback
					containment: ".area-container"	// constain movement
				});
			},
			changeHand: function() {
				$(".nohandle").mousedown(function(event) {
					event.stopPropagation();
				}).mouseup(function(event) {
					event.stopPropagation();
				});
				$(".draggable").mousedown(function() {
					$(this).css("cursor", "url(/img/top/close.png), url(/img/top/close.cur), move");
				}).mouseup(function() {
					$(this).css("cursor", "url(/img/top/open.png), url(/img/top/open.cur), move");
				});
			}
		},
		// news slider
		news = {
			init: function() {
				var url = "/xml/banner.xml",
					frame = $(".slideFrame"),
					guide = $("<ul>", { "class": "slideGuide" }),
					ctrl = $(".slideCtrl");
				frame.append(guide);
				$.ajax(url, {
					dataType: "xml",
					success: function(data) {
						var timerId;
						guide.width($("banner", data).length * 235);
						$("banner", data).each(function() {
							var li = $("<li>", { "class": "slideCell" }),
								a = $("<a>", { href: $("link", this).text(), target: $("link", this).attr("target") }),
								img = new Image();
							img.src = $("img", this).text();
							a.append(img).appendTo(li);
							guide.append(li);
						});
						timerId = setTimeout(news._slideLeft, 2800);
						ctrl.click(function() {
							clearTimeout(timerId);
							if ($(this).hasClass("right")) {
								news._slideLeft({ speed: 3 });
							} else {
								news._slideRight({ speed: 3 });
							}
						});
/*
						$(".left").hover(function() {
							clearTimeout(timerId);
							news._slideRightHover({ speed: 3, wait: 400 });
						}, function() {
							news._slideLeft();
						});
*/
					}
				});
				// 14日以内のニュースにアイコンをつける。
				news.addNew(14);
			},
			timerIdSlide: 0,
			// default
			options: function() {
				return {
					time: 1600,
					wait: 3200,
					speed: 1
				}
			},
			_slideLeft: function(params) {
				var frame = $(".slideFrame"),
					guide = $(".slideGuide"),
					cell = $(".slideCell"),
					options = news.options();
				$.extend(options, params);
				clearTimeout(news.timerIdSlide);
				cell.eq(0).stop(true).animate({
					marginLeft: -235
				}, {
					duration: (1 + cell.eq(0).css("marginLeft").slice(0, -2) / cell.eq(0).width()) * options.time / options.speed,
					complete: function() {
						$(this).css("marginLeft", 0).appendTo(guide);
						news.timerIdSlide = setTimeout(news._slideLeft, options.wait);
					}
				});
			},
			_slideRight: function(params) {
				var frame = $(".slideFrame"),
					guide = $(".slideGuide"),
					cell = $(".slideCell"),
					options = news.options();
				$.extend(options, params);
				clearTimeout(news.timerIdSlide);
				cell.eq(0).stop(true);
				if (cell.eq(0).css("marginLeft").slice(0, -2) < 0) {
					cell.eq(0).animate({
						marginLeft: 0
					}, {
						duration: Math.abs(cell.eq(0).css("marginLeft").slice(0, -2) / cell.eq(0).width()) * options.time / options.speed,
						complete: function() {
							cell.eq(-1).css("marginLeft", -235).prependTo(guide);
							news.timerIdSlide = setTimeout(news._slideLeft, options.wait);
						}
					});
				} else {
					cell.eq(-1).css("marginLeft", -235).prependTo(guide);
					news._slideRight();
				}
			},
/*
			_slideRightHover: function(params) {
				var frame = $(".slideFrame"),
					guide = $(".slideGuide"),
					cell = $(".slideCell"),
					options = news.options();
				$.extend(options, params);
				clearTimeout(news.timerIdSlide);
				cell.eq(0).stop(true);
				if (cell.eq(0).css("marginLeft").slice(0, -2) < 0) {
					cell.eq(0).animate({
						marginLeft: 0
					}, {
						duration: Math.abs(cell.eq(0).css("marginLeft").slice(0, -2) / cell.eq(0).width()) * options.time / options.speed,
						complete: function() {
							cell.eq(-1).css("marginLeft", -235).prependTo(guide);
							news.timerIdSlide = setTimeout(function() {
								news._slideRightHover(options);
							}, options.wait);
						}
					});
				} else {
					cell.eq(-1).css("marginLeft", -235).prependTo(guide);
					news._slideRightHover(options);
				}
			},
*/
			addNew: function(day) {
				$(".news h3").each(function() {
					var date = Date.parse($(this).attr("date")),
						def = Math.floor((new Date().getTime() - date) / 1000 / 60 / 60 / 24);

					if (def < day) {
						$(this).addClass("new");
					}
				});
			}
		},

		// あそびかた
		userGuide = {
			init: function() {
				if ($.browser.msie && $.browser.version < 7) {
					return false;
				}
				userGuide.set();
				$(window).resize(function() {
					var mod = $(".mod-userGuide");
					if ($(this).height() < 430) {
						mod.css("position", "absolute");
					} else {
						mod.css("position", "fixed");
					}
				}).resize();
				if (userGuide.checkCookie() === false) {
					userGuide.fader();
				}
				$(".userguide").click(function(event) {
					$(".draggable").css("zIndex", 0);  // reset z-Index
					userGuide.fader();
					event.preventDefault();
				});
			},
			set: function() {
				var area = $("<div>", { id: "area-userGuide" }),
					mod = $("<div>", { "class": "mod-userGuide" }),
					img = new Image(),
					button = $("<input>", { type: "image", src: "/img/top/a-close.png" });
				img.src = "/img/top/userguide.png";
				button.click(function() {
					if ($.support.opacity) {
						area.fadeOut();
					} else {
						area.hide();
					}
				});
				mod.append(img, button).appendTo(area);
				area.appendTo("body");
			},
			checkCookie: function() {
				var hasCookie;
				if ($.cookie("toilet")) {
					hasCookie = true;
				} else {
					hasCookie = false;
				}
				$.cookie("toilet", "true", { expires: 1 });
				return hasCookie;
			},
			fader: function() {
				if ($.support.opacity) {
					$("#area-userGuide").fadeIn();
				} else {
					$("#area-userGuide").show();
				}
			}
		},
		
		// 背景
		bg = {
			path: "/img/top/bg/",
			ext: ".png",
			n: 8,
			init: function() {
				if ($.browser.msie && $.browser.version < 7) {
					return false;
				}
				$(".changebg").click(function(event) {
					var i = Math.floor(Math.random() * bg.n);
					$("#container").css("background", "url(" + bg.path + i + bg.ext + ") no-repeat transparent");
					event.preventDefault();
				}).click();
			}
		};
	
	$(function() {
		$("html").addClass("js");
		panel.init();
		news.init();
		userGuide.init();
		bg.init();
	});

})(jQuery);


