(function ($) {
	$.fn.khjPageLink = function () {
		var _this = $(this);
		
		_this.bind("mousedown", function () {
			$(this).addClass("pageLink_push");
		});		
		
		_this.bind("mouseup", function () {
			$(this).removeClass("pageLink_push");
		});	
		
		_this.bind("mouseleave", function () {
			$(this).removeClass("pageLink_push");
		});						
	};
	
	$.fn.khjAccordion = function () {
		var _this = $(this);
		
		_this.bind("click", function () {
			$(this).parent().parent().find(".body").toggle();
		});
		
		_this.khjToggleBtn();
	};

	$.fn.khjToggleBtn = function () {
		var _this = $(this),
			src = null,
			value = null;
		
		_this.each(function () {
			value = {};
			src = "";
			
			value.obj = $(this);
			value.mode = 0;
			
			src = value.obj.attr("src");
			
			value.nSrc = [src, src.replace("_N", "_SN")];			
			value.oSrc = [src.replace("_N", "_O"), src.replace("_N", "_SO")];
			value.pSrc = [src.replace("_N", "_P"), src.replace("_N", "_SP")];
			value.dSrc = [src.replace("_N", "_D"), src.replace("_N", "_SD")];
			
			value.obj.data("value", value);
		});
		
		_this.bind("mouseover", function () {
			var value = $(this).data("value");			
			value.obj.attr("src", value.oSrc[value.mode]);			
		});				
		
		_this.bind("mousedown", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.pSrc[value.mode]);			
		});			

		_this.bind("mouseup", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.oSrc[value.mode]);			
		});			

		_this.bind("mouseleave", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.nSrc[value.mode]);			
		});			
		
		_this.bind("click", function () {
			var value = $(this).data("value");
			value.mode = value.mode?0:1;
			value.obj.attr("src", value.oSrc[value.mode]);	
		});			
	};	
	
	$.fn.khjImgButton = function () {
		var _this = $(this),
			src = null,
			value = null;
		
		_this.each(function () {
			value = {};
			src = "";
			
			value.obj = $(this);
			
			src = value.obj.attr("src");
			
			value.nSrc = src;			
			value.oSrc = src.replace("_N", "_O");
			value.pSrc = src.replace("_N", "_P");
			value.dSrc = src.replace("_N", "_D");
			
			value.obj.data("value", value);
		});
		
		_this.bind("mouseover", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.oSrc);
		});				
		
		_this.bind("mousedown", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.pSrc);			
		});

		_this.bind("mouseup", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.oSrc);			
		});			

		_this.bind("mouseleave", function () {
			var value = $(this).data("value");
			value.obj.attr("src", value.nSrc);			
		});					
	};		

	$.fn.khjSelectBox = function () {
		var _this = $(this),
			value = null;
		
		_this.each(function () {
			value = {};
			
			value.obj = $(this);
			value.text = value.obj.find(".selectTxt");
			value.btn = value.obj.find(".selectBtn");
			value.wrap = value.obj.find(".optionWrap");
			value.items = value.wrap.find("li");	
						
			value.obj.data("value", value);
			
			if (value.items.size() > 5) value.wrap.css("height", "150px");
			
			value.btn.bind("click", function () {
				var value = $(this).parent().data("value");
				value.wrap.toggle();			
			});	
			
			value.obj.bind("mouseleave", function () {
				var value = $(this).data("value");
				value.wrap.hide();			
			});									

			value.items.bind("click", function () {
				var _this = $(this),
					value = _this.parent().parent().parent().data("value");
				
				value.text.html(_this.html());	
				value.wrap.hide();			
			});	
			
			value.items.bind("mouseover", function () {
				$(this).addClass("on");
			});

			value.items.bind("mouseleave", function () {
				$(this).removeClass("on");
			});
			
			value.btn.khjImgButton();
		});
	};
	
	$.fn.khjSlider = function (value) {
		var _this = $(this),
			flag = false,
			txt = _this.find(".txt"),
			wrap = _this.find(".slider"),
			bg = _this.find(".sliderFull"),
			ball = _this.find(".ball"),
			width = wrap.width() - 20,
			old = 0,
			margin = 0,
			left = 0,
			per = width/100;
		
		if (value == undefined) value = txt.html();
		
		ball.khjImgButton();
		
		left = Math.ceil(per*value);
		ball.css("left", left);
		bg.css("width", left);
		txt.html(value);
		
		ball.get(0).ondragstart = function() { return false; };
			
		ball.bind("mousedown", function (e) {
			flag = true;	
			old = e.pageX;
        	e.stopPropagation(); 
		});
		
		$(document).bind("mousemove", function (e) {
			if (flag) {
				margin = e.pageX - old;
				left += margin;
				
				if (left >= width) left = width;
				if (left <= 0) left = 0;
				
				ball.css("left", left);	
				bg.css("width", left);
				txt.html(Math.ceil(left/per));
				if(txt.prop("id") == "viewSizeSlider"){
    				setviewsize(txt.html());
				}
				else if(txt.prop("id") == "bufferingSlider"){
    				display_buff_change(txt.html());
				}
				old = e.pageX;			
			}
		});
		
		$(document).bind("mouseup", function (e) {
			flag = false;
		});		

		$(document).bind("mouseleave", function (e) {
			flag = false;
		});		
	};
	
	$.fn.khjMenu = function () {
		var _this = $(this),
			src = null,
			value = null,
			on = _this.parent().filter(".on");

		_this.khjTxtToggleBtn();
		
		_this.bind("click", function () {
			$(this).parent().toggleClass("on");
		});
		
		on.removeClass("on");		
		on.find(".menu img").trigger("click");
	};	
	
	$.fn.khjTxtButton = function () {
		var _this = $(this),
			src = null,
			value = null;
		
		_this.each(function () {
			value = {};
			src = "";
			
			value.obj = $(this);
			value.img = value.obj.find("img");
			value.span = value.obj.find("span");
			
			src = value.img.attr("src");
			value.nSrc = src;			
			value.oSrc = src.replace("_N", "_O");
			value.pSrc = src.replace("_N", "_P");
			value.dSrc = src.replace("_N", "_D");
			
			value.obj.data("value", value);
		});	

		_this.bind("mouseover", function () {
			var value = $(this).data("value");			
			value.img.attr("src", value.oSrc);			
		});				
					
		_this.bind("mousedown", function () {
			var value = $(this).data("value");
			value.span.addClass("p");			
			value.img.attr("src", value.pSrc);			
		});			

		_this.bind("mouseup", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.oSrc);			
		});			

		_this.bind("mouseleave", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.nSrc);			
		});				
	};

	$.fn.khjSubMenuButton = function () {
		var _this = $(this),
			src = null,
			value = null;
		
		_this.each(function () {
			value = {};
			src = "";
			
			value.obj = $(this);
			value.img = value.obj.find("img");
			value.span = value.obj.find("span");
			
			src = value.img.attr("src");
			value.nSrc = src;			
			value.oSrc = src.replace("_N", "_O");
			value.pSrc = src.replace("_N", "_P");
			value.dSrc = src.replace("_N", "_D");
			
			value.obj.data("value", value);
		});	

		_this.bind("mouseover", function () {
			var value = $(this).data("value");			
			value.img.attr("src", value.oSrc);			
		});				
					
		_this.bind("mousedown", function () {
			var value = $(this).data("value");
			value.span.addClass("p");			
			value.img.attr("src", value.pSrc);			
		});			

		_this.bind("mouseup", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.oSrc);			
		});			

		_this.bind("mouseleave", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.nSrc);			
		});				

		_this.bind("click", function () {
			var id = $(this).attr("id").slice(1);
			T_SelectMenu(id);
		});				
	};
	
	$.fn.khjTxtToggleBtn = function () {
		var _this = $(this),
			src = null,
			value = null;
		
		_this.each(function () {
			value = {};
			src = "";
			
			value.obj = $(this);
			value.img = value.obj.find("img");
			value.span = value.obj.find("span");
			value.mode = 0;
						
			src = value.img.attr("src");

			value.nSrc = [src, src.replace("_N", "_SN")];			
			value.oSrc = [src.replace("_N", "_O"), src.replace("_N", "_SO")];
			value.pSrc = [src.replace("_N", "_P"), src.replace("_N", "_SP")];
			value.dSrc = [src.replace("_N", "_D"), src.replace("_N", "_SD")];
						
			value.obj.data("value", value);
		});	

		_this.bind("mouseover", function () {
			var value = $(this).data("value");			
			value.img.attr("src", value.oSrc[value.mode]);			
		});				
					
		_this.bind("mousedown", function () {
			var value = $(this).data("value");
			value.span.addClass("p");			
			value.img.attr("src", value.pSrc[value.mode]);			
		});			

		_this.bind("mouseup", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.oSrc[value.mode]);			
		});			

		_this.bind("mouseleave", function () {
			var value = $(this).data("value");
			value.span.removeClass("p");			
			value.img.attr("src", value.nSrc[value.mode]);			
		});	
		
		_this.bind("click", function () {
			var value = $(this).data("value");
			value.mode = value.mode?0:1;
			value.img.attr("src", value.oSrc[value.mode]);	
		});						
	};	
	
	$.extend({
		changeWidth : function (margin) {
//			$(".wrapWidth").css("width", "+=" + margin);
//			$(".viewWidth").css("width", "+=" + margin);
			$(".wrapWidth").css("width", margin+201);
			$(".viewWidth").css("width", margin);
		},
        changeSetupWidth : function  (margin) {
//			$(".wrapWidth").css("width", "+=" + margin);
//			$(".viewWidth").css("width", "+=" + margin);
			$(".wrapWidth").css("width", margin+301);
			$(".viewWidth").css("width", margin+61);
		},
		khjDisable : function (id, num) {
			var _this = $("#" + id),
				value = _this.data("value"),
				dSrc = null,
				btn = null;

			if (!value) {
				value = _this.find(".ball").data("value");
			}
			
			dSrc = value.dSrc;
				
			if (num != undefined) dSrc = value.dSrc[num];
			
			if (dSrc) {
				if (value.img != undefined) {
					value.img.attr("src", dSrc);
					value.span.addClass("d");				
				} else {
					value.obj.attr("src", dSrc);				
				}
			} else {
				btn = value.btn.data("value");
			
				dSrc = btn.dSrc;
				value.btn.attr("src", dSrc);
				value.btn.unbind("mouseover mouseleave mouseup mousedown click");
			}
						
			value.obj.unbind("mouseover mouseleave mouseup mousedown click");				
		}
	});
})(jQuery);
