/* zem@disybell.com */
(function($, undefined){
	var isRollover = false;
	//함수호출부분
	$.nirm = $.extend({
		apply : function(object, fn, args)
		{
			args = args ? ($.isArray(args) ? args : [args]) : [args];
			if($.isFunction(fn)) { fn.apply(object, args); }
		}
	});
		
	$.fn.extend({
		
		stopTime : function(name, complete)
		{
			return this.each(function()
			{
				var self = $(this);
				var onetime = self.data("onetime-" + name);
				if(onetime) clearTimeout(onetime);
				var everytime = self.data("everytime-" + name);
				if(everytime) clearInterval(everytime);
				$.nirm.apply(self, complete);
			});
		},
		oneTime : function(name, time, complete)
		{
			return this.each(function()
			{
				var self = $(this);
				var fn = complete;
				var timer = setTimeout(function() { $.nirm.apply(self, fn); }, time);
				self.data("onetime-" + name, timer);
			});
		},
		everyTime : function(name, time, complete)
		{
			return this.each(function()
			{
				var self = $(this);
				var fn = complete;
				var timer = setInterval(function() { $.nirm.apply(self, fn); }, time);
				self.data("everytime-" + name, timer);
			});
		},
		
		
		nImageHover : function(hover)
		{
			return $(this).each(function()
			{
				var self = $(this);
				var selected = self.data("selected");
				if(selected == "selected")
				{
					self.attr("src", self.data("hover"));
				}
				self.data("normal", self.attr("src")).mouseover(function()
				{
					self.attr("src", self.data("hover"));
				}).mouseleave(function()
				{
					if(selected != "selected")
					{
						self.attr("src", self.data("normal"));
					}
				});
			});
		}
	});
	$.widget("nirm.nPage",{
		options :
		{
			enable : true,
			selected : 0,
			resize : true,
			panel :
			{
				position : "relative",
				width : "auto",
				height : 100
			},
			next :
			{
				position : "absolute"
			},
			prev :
			{
				position : "absolute"
			},
			self : null,
			element : null
		},
		_create : function()
		{
			var o = this.options;
			o.self = this;
			o.element = o.self.element;
			$(window).load(function()
			{
				o.self.markup(o.element.css(o.panel));
				o.self.change(o.selected);
				o.enable = true;
			});
		},
		markup : function(element)
		{
			var o = this.options;
			var prev = element.children(".nPage-prev");
			var next = element.children(".nPage-next");
			var panels = element.children(".nPage-panel").children();
			var count = panels.size();
			prev.click(function()
			{
				if(o.enable)
				{
					var index = o.selected - 1;
					if(index < 0)
					{
						index = count - 1;
					}
					o.self.change(index);
				}
				return false;
			});
			next.click(function()
			{
				if(o.enable)
				{
					var index = o.selected + 1;
					if(index > count - 1)
					{
						index = 0;
					}
					o.self.change(index);
				}
				return false;
			});
			panels.each(function(i)
			{
				var index = i;
				var panel = $(this);
				panel.data("nPage-index", index);
				if(o.resize)
				{
					var img = panel.find("IMG");
					img.each(function()
					{
						var w = $(this).width();
						var h = $(this).height();
						if(w > h)
						{
							$(this).css(
							{
								"width" : "100%",
								"height" : "auto"
							});
							var p = $(this).parent().height();
							h = $(this).height();
							var t = (p - h) / 2;
							$(this).css(
							{
								"position" : "relative",
								"top" : t
							});
						}
						else
						{
							$(this).css(
							{
								"width" : "auto",
								"height" : "100%"
							});

						}
					});
					
				}
			}).hide();
		},
		change : function(i)
		{
			var o = this.options;
			if(o.enable)
			{
				o.enable = false;
				var index = i;
				var panels = o.element.children(".nPage-panel").children();
				panels.each(function(i)
				{
					if(o.selected == i)
					{
						$(this).fadeOut(function(){
							panels.eq(index).fadeIn(function(){
								o.enable = true;
							});
						});
					}
				});
				o.selected = index;
			}
		}
	});
	$.widget("nirm.nRoll",{
		options :
		{
			enable : true,
			selected : 0,
			panel :
			{
				position : "relative",
				width : "auto",
				height : 100
			},
			navi :
			{
				position : "absolute"
			},
			parent :
			{
				position : "relative"
			},
			self : null,
			element : null
		},
		_create : function()
		{
			var o = this.options;
			o.self = this;
			o.element = o.self.element;
			o.self.markup(o.element.css(o.parent));
			o.self.change(o.selected);
		},
		markup : function(element)
		{
			var o = this.options;
			var navi = element.children(".nRoll-navi").css(o.navi).children();
			var panels = element.children(".nRoll-panel").css(o.panel).children();
			navi.each(function(i)
			{
				var trigger = $(this);
				var index = i;
				trigger.data("nRoll-index", index).click(function()
				{
					o.self.change(index);
					return false;
				});
				var img = trigger.find("IMG");
				img.data("normal", img.attr("src")).mouseover(function()
				{
					if(o.selected != index)
					{
						img.attr("src", img.data("hover"));
					}
				}).mouseleave(function()
				{
					if(o.selected != index)
					{
						img.attr("src", img.data("normal"));
					}
				});
			});
			panels.each(function(i)
			{
				var index = i;
				var panel = $(this);
				panel.data("nRoll-index", index);
			}).hide();
		},
		change : function(i)
		{
			var o = this.options;
			if(o.enable)
			{
				o.enable = false;
				var navi = o.element.children(".nRoll-navi").children();
				var panels = o.element.children(".nRoll-panel").children();
				var index = i;
				panels.eq(o.selected).fadeOut(function()
				{
					panels.eq(index).fadeIn(function()
					{
						o.enable = true;
					});
				});			
				o.selected = index;
				navi.find("IMG").each(function(i)
				{
					if(i == index)
					{
						$(this).attr("src", $(this).data("selected"));
						$(this).addClass("navi-on");
					}
					else
					{
						$(this).attr("src", $(this).data("normal"));
						$(this).removeClass("navi-on");
					}
				});
			}
		}
	});	
	$.widget("nirm.nMenu",{
		options :
		{
			align : "none",
			adjustment : true,
			clearfix : false,
			selected : 0,
			subSelected : 0,
			accordion : true,
			self : null,
			element : null
		},
		_create : function()
		{
			var o = this.options;
			o.self = this;
			o.element = o.self.element;
			var element = $(o.element).children("LI");
			element.css({"visibility" : "hidden" });
			var index = o.selected - 1;
			if(o.selected > 0)
			{
				o.element.children("LI").eq(index).children("A").find("IMG").data("selected", "selected");
			}
			var sub = o.subSelected - 1;
			if(o.subSelected > 0)
			{
				o.element.children("LI").eq(index).children("UL").children("LI").eq(sub).children("A").find("IMG").data("selected", "selected");
			}

			$(window).load(function()
			{
				o.self.markup(element);
				if(index > 0)
				{
					o.self.open(element, index);
				}
				element.css({"visibility" : "visible" });
				//서브셀렉트
				
			});

		},
		markup : function(element)
		{
			var o = this.options;
			element.each(function(i)
			{
				var self = $(this);
				var target = self.children("UL");
				var index = i;

				//트리거를 A 테그로 : 수정
				self.find("A").hover(function()
				{
					isRollover = true;
					o.self.open(element, index);
				});
				self.find("A").mouseleave(function()
				{
					isRollover = false;
					setTimeout(function() {
						if (!isRollover) {
							//target.stop(true, true).fadeOut();
							target.stop(true, true).hide();
						}
					},500);
				});
				target.hover(function()
				{
					isRollover = true;
				});
				//포커스 잃었을경우 자동으로 닫기 : 추가
				target.mouseleave(function()
				{
					isRollover = false;
					setTimeout(function() {
						if (!isRollover) {
							//$(this).stop(true, true).fadeOut();
							$(this).stop(true, true).hide();
						}
					},500);
				});


				if(o.align == "center")
				{
					var width = target.outerWidth();
					var left = self.outerWidth() / 2 - width / 2;
					target.css({ "left" : left });
					target.show();
					var pos = target.offset();
					target.hide();
					var parent = o.element.offset().left + o.element.width();
					if(o.adjustment && pos)
					{
						var right = pos.left + width;
						if(right > parent)
						{
							adjustment = right - parent;
							
							target.css({ "left" : left - adjustment });
						}
					}
				}


				o.self.markup(target.children("LI"));
				if(o.clearfix)
				{
					target.addClass("layout-clear-fix");
				}

				target.hide();
			});
		},
		open : function(element, index)
		{
			var o = this.options;
			element.data("hover", "0");

			element.eq(index).data("hover", "1").children("UL").stop(true, true).show();//.fadeIn();
			if(o.accordion)
			{
				/*
				element.filter(function()
				{
					return $(this).data("hover") != "1";
				}).children("UL").stop(true, true).fadeOut();
				*/
				element.filter(function()
				{
					return $(this).data("hover") != "1";
				}).children("UL").stop(true, true).hide();

			}
		}
	});
	$.widget("nirm.nTab",{
		options :
		{
			enable : true,
			anchor : false,
			selected : 1,
			self : null,
			element : null,
			openable : true,
			markable : true
		},
		_create : function()
		{
			var o = this.options;
			o.self = this;
			o.element = this.element;
			o.self.markup($(o.element));
		},
		open : function(i)
		{
			isRollover = true;
			var o = this.options;
			var tabs = o.element.children(".nTabs-tabs")/*.addClass("clearfix")*/.children("LI");//.addClass("round-top");
			var panels = o.element.children(".nTabs-panel").addClass("clearfix").children("DIV");

			var tab = tabs.eq(i);

			tabs.removeClass("nTabs-selected");
			tab.addClass("nTabs-selected");
			var selected = tab.find("A").data("nTab-index");
				
			if (o.openable) {
				panels.each(function(i)
				{
					if(selected == i)
					{
						$(this).show();
						document.body.ondragstart = null;
						document.body.setAttribute("dummy", "dummy");
					}
					else
					{
						$(this).hide();
					}
				});
			}
		},
		markup : function(element)
		{
			var o = this.options;
			var tabs = element.children(".nTabs-tabs").addClass("clearfix").children("LI");//.addClass("round-top");
			var panels = element.children(".nTabs-panel").addClass("clearfix").children("DIV");
			panels.each(function(i)
			{
				var panel = $(this);
				panel.data("nTab-index", i.toString())
			});
			tabs.each(function(i)
			{
				var tab = $(this);
				var index = i;
				tab.click(function()
				{
					if(o.enable)
					{
						o.self.open(index);
					}
					return o.anchor;
				}).find("A").data("nTab-index", i.toString());
			})
			
			var anchor = self.location.hash;
			var start = o.selected - 1;
			if(anchor)
			{
				start = tabs.find("A[href=" + anchor + "]").data("nTab-index");
			}
			
			if (o.markable) {
				o.self.open(start);
			}
		}
	});


})(jQuery);
