(function($) {$(function() {
		function Namespace(str) {
			var ns = str.split('.'), now = window;
			for(var i = 0, l = ns.length; i < l; i++) {
				if( typeof (now[ns[i]]) == 'undefined') {
					now[ns[i]] = {};
				} else {
					throw new Error('libs: Namespace conflicted.');
				}
				now = now[ns[i]];
			}
			return now;
		}

		var ns = new Namespace('jp.jrpg.brs');
		ns.popupParams = [];
		ns.popup = $('.popup').each(function(index, element) {
			var _e = $(element);
			var _p = {}, _ps = _e.attr('params').replace(/\s/, '').split(',');
			for(var i = 0, ilen = _ps.length; i < ilen; i++) {
				var _k = _ps[i].split('=');
				_p[_k[0]] = _k[1];
			}
			var __o = {
				target : _e,
				href : _e.attr('href') || './',
				x : _p['x'] || 'CENTER',
				y : _p['y'] || 'CENTER',
				width : parseInt(_p['width']) || 640,
				height : parseInt(_p['height']) || 480,
				duration : parseInt(_p['duration']) || 1000,
				onBg : _p['on_bg'] || false,
				bg_x : parseInt(_p['bg_x']) || 0,
				bg_y : parseInt(_p['bg_y']) || 0,
				bg_name_set : _p['bg_name_set'] || 'images/popup/frame',
				bg_img_pos_h : _p['bg_img_pos_h'] || 'CENTER',
				bg_img_pos_v : _p['bg_img_pos_v'] || 'CENTER',
				sc_tw_type : _p['sc_tw_type'] || 'horizontal',
				sc_tw_data_text : _p['sc_tw_data_text'] || null,
				sc_tw_name : _p['sc_tw_url'] || null,
				sc_tw_acount1 : _p['sc_tw_acount1'] || null,
				sc_tw_acount2 : _p['sc_tw_acount2'] || null
			}
			__o.bg_width = parseInt(_p['bg_width']) || __o.width;
			__o.bg_height = parseInt(_p['bg_height']) || __o.height;
			__o.type = getType(__o.href);
			ns.popupParams[index] = __o;
			_e.attr('href', 'javascript:void(0);');
			_e.click(function() {
				var _container = make(index);
				_e.bind('popupclose', function() { _container.trigger('popupclose');
				});
			});
		});
		ns.popupClose = function() { ns.popup.trigger('popupclose');
		}
		function make(index) {
			var _index = index, __p = ns.popupParams[_index];
			var _rootElement = $(versionCheck() == 'WEBKIT' ? 'body' : 'html');
			var _container = $('<div></div>').css({
				position: 'absolute',
				left: 0,
				top: 0,
				zIndex: 9999
			}).appendTo('body');
			var _background = $('<img src="images/black-70.png" />').css({ position: 'absolute'
			}).appendTo(_container);
			var _content = $('<div></div>').css({
				position: 'absolute',
				width: __p.onBg? Math.max(__p.bg_width, __p.width): __p.width,
				height: __p.onBg? Math.max(__p.bg_height, __p.height): __p.height
			}).appendTo(_container);
			_reDraw();
			switch(__p.type) {
				case 'SWF':
					setFlash(_content, __p.href, __p.width, __p.height);
					break;
				case 'WEB':
					setWeb(_content, __p.href, __p.width, __p.height, __p.onBg, __p.bg_x, __p.bg_y, __p.bg_width, __p.bg_height, __p.bg_name_set, __p.bg_img_pos_h, __p.bg_img_pos_v);
					break;
				default:
					throw new Error('libs:SWF,HTMLのファイルのみ対応。');
			}
			function _reDraw() {
				var __x = _rootElement.scrollLeft(), __y = _rootElement.scrollTop(), __w = $(document).width(), __h = $(document).height();
				_background.css({
					top : 0,
					left : 0,
					width : __w,
					height : __h
				});
				setAlign(_content, __p.x, __p.y);
			}

			function _close() { $(window).unbind('scroll resize', _reDraw);
				_content.children().remove();
				_content.remove();
				_background.remove();
				_container.remove();
				_rootElement = _reDraw = _close = _content = _background = _container = null;
			}

			$(window).bind('scroll resize', _reDraw);
			return _container.bind('popupclose', _close);
		}

		function setAlign(target, horizontal, vertical) {
			var h = $(versionCheck() == 'WEBKIT' ? 'body' : 'html');
			target.css({
				position : 'absolute'
			});
			if( typeof horizontal == 'string') {
				switch(horizontal) {
					case 'CENTER':
						target.css('left', ((($(window).width() / 2) - (parseInt(target.css('width')) / 2)) + h.scrollLeft() + 'px'));
						break;
					default:
						throw new Error('libs:align:コンテンツの水平方向の配置位置指定が不正です。');
						break;
				}
			} else if( typeof horizontal == 'number') {
				throw new Error('libs:align:コンテンツ配置位置は現在"CENTER"のみの実装です。');
			}
			if( typeof vertical == 'string') {
				switch(vertical) {
					case 'CENTER':
						//alert(((($(window).height() / 2) - (parseInt(target.css('height')) / 2)) + h.scrollTop() + 'px'));
						target.css('top', ((($(window).height() / 2) - (parseInt(target.css('height')) / 2)) + h.scrollTop() + 'px'));
						break;
					default:
						throw new Error('libs:align:コンテンツの垂直方向の配置位置指定が不正です。');
						break;
				}
			} else if( typeof vertical == 'number') {
				throw new Error('libs:align:コンテンツ配置位置は現在"CENTER"のみの実装です。');
			}
		}

		function getType(path) {
			if(path.match(/\.swf[?]*/) != null) {
				return 'SWF';
			} else if(path.match(/\.jpg|\.png|\.gif/) != null) {
				throw new Error('libs:getType: hrefに画像が指定されました。現在SWFのみの対応です。');
				return 'PIC';
			} else {
				return 'WEB';
			}
			return 'ERR';
		}

		function setWeb(target, path, width, height, onBg, bgX, bgY, bgW, bgH, bgNameSet, bgPosH, bgPosV) {
			var CORNER_SIZE = 15, CLOSE_SIZE = 77;
			var __t = target;
			var __p = [(Math.max(bgW, width) -  Math.min(bgW, width)) / 2, (Math.max(bgH, height) -  Math.min(bgH, height)) / 2];
			if(onBg == true || onBg == 'true') {
				var __bg = $('<div></div>').css({ position: 'absolute', left: bgX + 'px', top: bgY + 'px', width: bgW + 'px', height: bgH + 'px', backgroundImage: 'url(' + bgNameSet + '_bg.png )', backgroundRepeat: 'no-repeat', backgroundPosition: bgPosH + ' ' + bgPosV
				}).appendTo(__t);
				var __fm = $('<div></div>').css({ position: 'absolute', left: (__p[0] - CORNER_SIZE) + 'px', top: (__p[1] - CORNER_SIZE) + 'px', width: (width + CORNER_SIZE * 2) + 'px', height: (height + CORNER_SIZE * 2) + 'px'
				}).appendTo(__bg);
				var __para = [['absolute', 0, 0, CORNER_SIZE, CORNER_SIZE, 'url(' + bgNameSet + '_topLeft.png)', null], ['absolute', width + CORNER_SIZE, 0, CORNER_SIZE, CORNER_SIZE, 'url(' + bgNameSet + '_topRight.png)', null], ['absolute', 0, height + CORNER_SIZE, CORNER_SIZE, CORNER_SIZE, 'url(' + bgNameSet + '_bottomLeft.png)', null], ['absolute', width + CORNER_SIZE, height + CORNER_SIZE, CORNER_SIZE, CORNER_SIZE, 'url(' + bgNameSet + '_bottomRight.png)', null], ['absolute', CORNER_SIZE, 0, width, CORNER_SIZE, 'url(' + bgNameSet + '_top.png)', 'repeat-x'], ['absolute', CORNER_SIZE, height + CORNER_SIZE, width, CORNER_SIZE, 'url(' + bgNameSet + '_bottom.png)', 'repeat-x'], ['absolute', 0, CORNER_SIZE, CORNER_SIZE, height, 'url(' + bgNameSet + '_left.png)', 'repeat-y'], ['absolute', width + CORNER_SIZE, CORNER_SIZE, CORNER_SIZE, height, 'url(' + bgNameSet + '_right.png)', 'repeat-y']]
				for(var i = 0, ilen = __para.length; i < ilen; i++) { __fm.append($('<div></div>').css({
						position : __para[i][0],
						left : __para[i][1] + 'px',
						top : __para[i][2] + 'px',
						width : __para[i][3] + 'px',
						height : __para[i][4] + 'px',
						backgroundImage : __para[i][5],
						backgroundRepeat : __para[i][6] != null ? __para[i][6] : 'no-repeat'
					}));
				}
			}
			var __b;
			var __s = $('<iframe src="' + path + '" width="' + width + '" height = "' + height + '" ></iframe>').css({ position: 'absolute', left: __p[0] + 'px', top: __p[1] + 'px', backgroundColor: 'white', zIndex: 9998
			}).appendTo(__t).load(function(e) {
				__b = $('<div></div>').css({ position: 'absolute', left: (__p[0] + width - CLOSE_SIZE * 2) + 'px', top: (__p[1] + CLOSE_SIZE) + 'px', width: CLOSE_SIZE + 'px', height: CLOSE_SIZE + 'px', backgroundImage: 'url(' + bgNameSet + '_closeBtn.png)', backgroundRepeatr: 'no-repeat', zIndex: 9997
				}).appendTo(__t);
				__b.animate({
					left : (__p[0] + width - 35) + 'px',
					top : (__p[1] - 55) + 'px'
				}, {
					duration : "default",
					easing : "easeOutBack",
					complete : _complete
				});
			});
			function _complete() { $('<a href="javascript:void(0);"></a>').css({ display: 'block', position: 'absolute', left: __b.css('left'), top: __b.css('top'), width: __b.css('width'), height: __b.css('height'), zIndex: 9999
				}).click(_close).appendTo(__t);
			}

			function _close() { __b.remove();
				__b.unbind(_complete);
				ns.popupClose();
			}

		}

		function setFlash(target, path, width, height) {
			var __o1 = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' + width + '" height="' + height + '" id="' + path.replace(/.swf.*$/, '') + '" align="middle">';
			__o1 += '<param name="movie" value="' + path + '" />';
			__o1 += '<param name="quality" value="high" />';
			__o1 += '<param name="bgcolor" value="#ffffff" />';
			__o1 += '<param name="play" value="true" />';
			__o1 += '<param name="loop" value="true" />';
			__o1 += '<param name="wmode" value="transparent" />';
			__o1 += '<param name="scale" value="showall" />';
			__o1 += '<param name="menu" value="true" />';
			__o1 += '<param name="devicefont" value="false" />';
			__o1 += '<param name="salign" value="" />';
			__o1 += '<param name="allowScriptAccess" value="sameDomain" />';
			__o1 += '</object>';
			var __obj1 = $(__o1);
			var __o2 = '<object type="application/x-shockwave-flash" data="' + path + '" width="' + width + '" height="' + height + '">';
			__o2 += '<param name="movie" value="' + path + '" />';
			__o2 += '<param name="quality" value="high" />';
			__o2 += '<param name="bgcolor" value="#ffffff" />';
			__o2 += '<param name="play" value="true" />';
			__o2 += '<param name="loop" value="true" />';
			__o2 += '<param name="wmode" value="transparent" />';
			__o2 += '<param name="scale" value="showall" />';
			__o2 += '<param name="menu" value="true" />';
			__o2 += '<param name="devicefont" value="false" />';
			__o2 += '<param name="salign" value="" />';
			__o2 += '<param name="allowScriptAccess" value="sameDomain" />';
			__o2 += '</object>';
			var __obj2 = $(__o2);
			var __a = $('<a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Adobe Flash Player を取得" /></a>');
			__obj1.appendTo(target);
			if(versionCheck().match(/IE/) == null) { __obj2.appendTo(__obj1);
			} else {
			}
		}

		function setSoucial(target, twitterSet, facebookSet, evernoteSet, mixiSet, hatenaSet) {
			if(twitterSet != null) {
				var __tw = '<a href="http://twitter.com/share" class="twitter-share-button"';
				__tw += 'data-count="' + horizontal + '"';
				__tw += 'data-via="' + url + 'data-lang="ja">';
				__tw += 'Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
			}
		}

		function versionCheck() {
			//jQuery1.6.2 ver...(!1.6.1)
			if(jQuery.support.noCloneChecked) {
					if(!jQuery.support.checkOn) {
							//WebKit
							return 'WEBKIT';
							/*
							if(navigator.vendor.match(/apple/i)) {
									return 'SAFARI';
							}
							else if(navigator.vendor.match(/google/i)) {
									return 'CHROM';
							}*/
					}
					else if(jQuery.support.cors) {
							return 'FIREFOX';
					}
					else {
							return 'OPERA';
					}
			}
			else {
					//IE
					_isIE = true;
					if(jQuery.support.cssFloat) {
							return 'IE9';
					}
					else if(jQuery.support.tbody) {
							return 'IE8';
					}
					else if (typeof document.documentElement.style.maxHeight != "undefined") {
							return 'IE7';
					}
					else {
							return 'IE6';
					}
			}
			return 'OTHER';
		}

	});
})(jQuery);

