var server = {
	host: location.hostname.replace(/^[^k][^\.]+/i, '').replace(/^\./, ''),
	uri: function() {
		var args = arguments;
		var sub = args[0];
		var route = [];
		for (var i = 1; i < args.length; i++) {
			route.push(args[i]);
		}
		// http:, https:
		var protocol = window.location.protocol.str('/');
		var domain = [sub, this.host].join('.');
		return [protocol, domain, route].join('/');
	}
};

// virtual submit form
var form = function() {
	var element = document.createElement('form');

	this.param = function(name, value) {
		if (name) {
			var node = document.createElement('input');
			node.type = 'hidden';
			node.name = name;
			node.value = value;
			element.appendChild(node);
		}
	};
	this.send = function(action, method, target) {
		document.body.appendChild(element);
		if (action) element.action = action;
		if (target) element.target = target;
		if (method) element.method = method;
		element.submit();
	}
};

var loc = {
	action: function(uri, target) {
		var fm = new form();
		var split = uri.split(/\?/);

		var route = typeof(split[0]) != 'undefined' ? split[0] : '';
		var param = typeof(split[1]) != 'undefined' ? split[1] : '';
		param = param.split(/&/);
		for (var idx in param) {
			var paramSplit = param[idx].split(/=/);
			var name = typeof(paramSplit[0]) != 'undefined' ? paramSplit[0] : '';
			var value = typeof(paramSplit[1]) != 'undefined' ? paramSplit[1] : '';
			fm.param(name, decodeURIComponent(value));
		}
		fm.send(route, 'get', target);
		return false;
	},
	del: function(uri, target, deleteMsg) {
		if (!deleteMsg) {
			deleteMsg = 'Do you want to delete? Deleted data recovery is impossible.';
		}
		if (confirm(deleteMsg)) {
			this.action(uri, target);
		}
		return false;
	}
};

var flash = {
	echo: function(obj) {
		var node   = obj.node;
		var src    = obj.src;
		var width  = obj.width;
		var height = obj.height;
		var wmode   = 'transparent';
		var code    = 'https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0';
		var classId = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
		var type    = 'application/x-shockwave-flash';
		var plug    = 'http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash';

		var html = "";
		html += "<object classid='" +classId+ "' codebase='" +code+ "' width='" +width+ "' height='" +height+ "'>";
		html += "<param name='wmode' value='" +wmode+ "' />";
		html += "<param name='movie' value='" +src+ "' />";
		html += "<param name='quality' value='high' />";
		html += "<embed src='" +src+ "' quality='high' wmode='" +wmode+ "' type='" +type+ "' pluginspage='" +plug+ "' width='" +width+ "' height='" +height+ "' />";
		html += "</object>";
		if (node) {
			if (typeof(node) == 'object') {
				node.innerHTML = html;
			}
			else document.getElementById(node).innerHTML = html;
		}
		return html;
	},

	link: function(param) {
		var url = this.url(param);
		if (url) {
			if (typeof(url) == 'string') {
				if (url.match(/^alert:/i)) {
					alert(url.replace(/^alert:/i, ''));
				}
				else loc.action(url);
			}
			else loc.action(url[0], url[1]);
		}
		else alert('undefined parameter: ' + param);
	},

	url: function(param) {
		switch (param) {
			case 'code01': return '/about/about_01.html';
			case 'code02': return '/game/game_01.html';
			case 'code03': return '/fish_hook/hook_01.html';
			case 'code04': return '/movie/movie_01.html';
			case 'code05': return '/area/area_01.html';
			case 'code06': return '/helper/helper_01.html';
			case 'code07': return '/community/notice_list.html';
			case 'code08': return '/bbs/list.html?code=sea_kpfa';

			case 'code10': return '/about/about_01.html';
			case 'code11': return '/about/about_02.html';
			case 'code12': return '/about/about_10.html';
			case 'code13': return '/about/about_03.html';
			case 'code14': return '/about/about_04.html';
			case 'code15': return '/about/about_05.html';
			case 'code16': return '/about/about_06.html';
			case 'code17': return '/about/about_07.html';
			case 'code18': return '/about/about_08.html';
			case 'code19': return '/about/about_09.html';

			case 'code20': return '/game/game_01.html';
			case 'code21': return '/game/game_02.html';
			case 'code22': return '/game/game_03.html';

			case 'code23': return '/fish_hook/hook_01.html';
			case 'code24': return '/fish_hook/hook_02.html';
			case 'code25': return '/fish_hook/hook_03.html';
			case 'code26': return '/fish_hook/hook_04.html';
			case 'code27': return '/fish_hook/hook_05.html';

			case 'code28': return '/movie/movie_01.html';
			case 'code29': return '/movie/movie_02.html';
			case 'code30': return '/movie/movie_03.html';
			case 'code31': return '/movie/movie_04.html';

			case 'code33': return '/area/area_01.html';
			case 'code34': return '/area/area_02.html';
			case 'code35': return '/area/area_03.html';

			case 'code36': return '/helper/helper_01.html';
			case 'code37': return ['http://www.nori.go.kr/info/tide_forcast.asp', '_blank'];
			case 'code38': return ['http://www.kma.go.kr/mini/marine/main.jsp', '_blank'];
			case 'code39': return '/helper/helper_04.html';

			case 'code40': return '/community/notice_list.html';
			case 'code41': return '/community/free_list.html';
			case 'code42': return '/community/praise_list.html';
			case 'code43': return '/community/feeling_list.html';
			case 'code44': return '/community/proposal_list.html';
			case 'code45': return '/community/qna_list.html';
			case 'code46': return '/bbs/list.html?code=sea_mention';
			case 'code47': return '/community/life_list.html';

			case 'code49': return '/bbs/list.html?code=sea_kpfa';
			case 'code51': return '/bbs/list.html?code=sea_account';
			case 'code52': return '/bbs/list.html?code=sea_account';

			case 'code53': return '/bbs/list.html?code=sea_preowned';
			case 'code54': return ['http://www.kma.go.kr/mini/marine/main.jsp', '_blank'];
			case 'code55': return ['http://www.nori.go.kr/info/tide_forcast.asp', '_blank'];
		}
	}
};

var cookie = {
	get: function(name) {
		var cookies = document.cookie.split(';');
		for (var idx in cookies) {
			var cook = cookies[idx].split('=');
			var cookName  = cook.shift().replace(/^\s+|\s+$/, '');
			var cookValue = cook.shift();
			if (cookName == name) {
				return cookValue ? unescape(decodeURIComponent(cookValue)) : '';
			}
		}
	},

	set: function(name, value, time) {
		var date = new Date();
		var domainSet = '.' + server.host;
		date.setTime(date.getTime() + time * 1000);
		var cookTime = time ? "expires=" +date.toGMTString()+ ";" : '';
		var cookValue = value ? encodeURIComponent(escape(value)) : '';
		document.cookie = name + '=' +cookValue+ "; domain=" +domainSet+ "; path=/; " +cookTime;
	}
};

var checkLogin = function(obj) {
	var opp = obj.opp;
	var id  = obj.member_id;
	var pw  = obj.member_pass;
	if (opp.value) return false;

	if(id.value.length < 3) {
		alert('¾ÆÀÌµð¸¦ 3ÀÚ¸® ÀÌ»ó ÀÔ·ÂÇÏ¼¼¿ä.');
		id.focus();
	}
	else if(pw.value.length < 4) {
		alert('ºñ¹Ð¹øÈ£¸¦ 4ÀÚ¸® ÀÌ»ó ÀÔ·ÂÇÏ¼¼¿ä.');
		pw.focus();
	}
	else return true;
	return false;
};

var outloginSetting = function() {
	var element = document.getElementById('outlogin');
	var inputs = element.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		var input = inputs.item(i);
		if (input.type.match(/text|password/)) {
			input.keepClass = input.className;
			input.onfocus = function() { this.className = 'input' };
			input.onblur = function() { if (!this.value) this.className = this.keepClass };
		}
		else if (input.type.match(/checkbox/i)) {
			input.onclick = function() {
				if (this.checked) {
					var message = [
						'ÀÚµ¿·Î±×ÀÎÀ» »ç¿ëÇÏ½Ã¸é ´ÙÀ½ºÎÅÍ È¸¿ø¾ÆÀÌµð¿Í ÆÐ½º¿öµå¸¦ ÀÔ·ÂÇÏ½Ç ÇÊ¿ä°¡ ¾ø½À´Ï´Ù.',
						'°ø°øÀå¼Ò¿¡¼­´Â °³ÀÎÁ¤º¸°¡ À¯ÃâµÉ ¼ö ÀÖÀ¸´Ï »ç¿ëÀ» ÀÚÁ¦ÇÏ¿© ÁÖ½Ê½Ã¿À.',
						'ÀÚµ¿·Î±×ÀÎÀ» »ç¿ëÇÏ½Ã°Ú½À´Ï±î?'
					];
					if (confirm(message.join('\n\n'))) {
						this.checked = true;
					}
					else this.checked = false;
				}
			};
		}
	}
};

