// JavaScript Document
/*////////////////////////////////////////////////////////////

KICKCREATIVE - jq.js

Copyright (C) KICKCREATIVE Co., All Rights Reserved.
------------------------------------------------------------

[hoverToggle]
[findPrev]
[jqSwfAddress]
[fontScale]
////////////////////////////////////////////////////////////*/


/*============================================================
hoverToggle
--------------------------------------------------------------
1. 
==============================================================*/

;(function($) {
	$.fn.hoverToggle = function(settings) {
		// set value
		var _defaultSettings = {
			active: "active",
			hover: "hover",
			list: "li",
			bind_1: "mouseover",
			bind_2: "mouseout",
			bind_3: "click",
			callback_1: function(e) {
				var obj = $(e.currentTarget);
				if (!obj.hasClass(_this.active)) obj.addClass(_this.hover);
			},
			callback_2: function(e) {
				var obj = $(e.currentTarget);
				if (!obj.hasClass(_this.active)) obj.removeClass(_this.hover);
			},
			callback_3: function(e) {}
		};
		var _this = $.extend(_defaultSettings, settings);
		
		// set init
		return this.each(function() {
			var obj = $(this).find(_this.list);
			obj.bind(_this.bind_1, _this.callback_1);
			obj.bind(_this.bind_2, _this.callback_2);
			obj.bind(_this.bind_3, _this.callback_3);
		});
	};
})(jQuery);


/*============================================================
findPrev
--------------------------------------------------------------
1. 
==============================================================*/

;(function($) {
	$.fn.findPrev = function(selector) {
		return this.parentsUntil().find(selector + ":first-child");
	};
})(jQuery);


/*============================================================
jqSwfAddress
--------------------------------------------------------------
1. 
==============================================================*/

;(function($) {
	$.fn.jqSwfAddress = function(settings) {
		// set value
		var _defaultSettings = {
			keyFunc: new Object(),
			dotoFunc: function (e) {
				for (var k in _this.keyFunc) {
					var ikey = "/" + k;
					if (ikey == e.path) {
						_this.keyFunc[k]();
					} else if (e.path == "/") {
						return;
					}
				}
			}
		};
		var _this = $.extend(_defaultSettings, settings);
		
		// set init
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, _this.dotoFunc);
	};
})(jQuery);


/*============================================================
fontScale
--------------------------------------------------------------
1. 
==============================================================*/

;(function($) {
	$.fn.fontScale = function(settings) {
		// set value
		var _defaultSettings = {
			step: "step",
			hover: "hover",
			targetThis: new Object(),
			target: "input",
			targetCon: ".fontScale",
			pre_obj: null,
			time: 800,
			cookieName: "setSize",
			size_1: "90%",
			size_2: "100%",
			size_3: "140%",
			size_4: "180%",
			bind_1: "click",
			callback_1: function(e) {
				// get self
				var obj = $(e.currentTarget);
				// get self eq position
				var self_eq = $(_this.targetThis).find(_this.target).index(this);
				// get font size
				var resizeValue = _this.getSize(self_eq);
				_this.animateSize(resizeValue);
				// save step
				if (_this.pre_obj != null) _this.pre_obj.removeClass(_this.step);
				obj.addClass(_this.step);
				_this.pre_obj = obj;
				// save cookie
				_this.setCookie(_this.cookieName, resizeValue, 999);
			},
			bind_2: "mouseover",
			callback_2: function(e) {
				// get self
				var obj = $(e.currentTarget);
				obj.addClass(_this.hover);
			},
			bind_3: "mouseout",
			callback_3: function(e) {
				// get self
				var obj = $(e.currentTarget);
				obj.removeClass(_this.hover);
			},
			animateSize: function(resizeValue){
				$(_this.targetCon).animate({
					"fontSize": resizeValue
				}, _this.time);
			},
			cssSize: function(resizeValue){
				$(_this.targetCon).css({
					"fontSize": resizeValue
				});
			},
			getSize: function(eq){
				// get font size
				var resize = "100%";
				switch (eq) {
					case 0:
						resize = _this.size_1;
						break;
					case 1:
						resize = _this.size_2;
						break;
					case 2:
						resize = _this.size_3;
						break;
					case 3:
						resize = _this.size_4;
						break;
				}
				return resize;
			},
			getEq: function(resize){
				// get elm eq
				var eq = 0;
				switch (resize) {
					case _this.size_1:
						eq = 0;
						break;
					case _this.size_2:
						eq = 1;
						break;
					case _this.size_3:
						eq = 2;
						break;
					case _this.size_4:
						eq = 3;
						break;
				}
				return eq;
			},
			setCookie: function(name, value, lifedays){
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+lifedays);
				document.cookie=name+ "=" +escape(value)+((lifedays==null) ? "" : ";expires="+exdate.toUTCString());
			},
			getCookie: function(name){
				if (document.cookie.length>0) {
					var start = document.cookie.indexOf(name + "=");
					if (start != -1) {
						start = start + name.length + 1;
						var end=document.cookie.indexOf(";", start);
						if (end == -1) end = document.cookie.length;
						return unescape(document.cookie.substring(start, end));
					}
				}
				return false;
			},
			initSet: function(initSize){
				_this.cssSize(initSize);
				var obj = $(_this.targetCon).find(_this.target).eq(_this.getEq(initSize));
				if (_this.pre_obj != null) _this.pre_obj.removeClass(_this.step);
				obj.addClass(_this.step);
				_this.pre_obj = obj;
			}
		};
		var _this = $.extend(_defaultSettings, settings);
		
		// set init
		return this.each(function() {
			_this.targetThis = this;
			var obj = $(this).find(_this.target);
			obj.bind(_this.bind_1, _this.callback_1);
			obj.bind(_this.bind_2, _this.callback_2);
			obj.bind(_this.bind_3, _this.callback_3);
			// init run setting size, if cookie have
			var initSize = _this.getCookie(_this.cookieName);
			if (initSize) _this.initSet(initSize);
		});
	};
})(jQuery);
