/**
 * @name skybox.js
 * @version 0.1.6
 * @date 2011-06-17
 * @revision 32
 */

var skybox = {
	// 設定
	conf: {
		bgBackground: '#000',
		bgOpacity: 0.75,
		boxBackground: '#fff',
		contentBackground: '#fff',
		fadeInDuration: 300,
		fadeOutDuration: 300,
		easing: 'linear',
		boxMarginTop: 0,
		boxMarginRight: 20,
		boxMarginBottom: 20,
		boxMarginLeft: 20,
		stageBorderTop: '0 none',
		stageBorderRight: '0 none',
		stageBorderBottom: '0 none',
		stageBorderLeft: '0 none',
		stagePaddingTop: 0,
		stagePaddingRight: 0,
		stagePaddingBottom: 0,
		stagePaddingLeft: 0,
		stageMinWidth: 100,
		stageMinHeight: 100,
		closeButton: false
	},

	// 初期化
	init: function(selector, opts, callback) {
		var self = this;
		var conf = this.conf;
		for (var key in opts) conf[key] = opts[key];
		conf.boxMarginWidth = conf.boxMarginLeft + conf.boxMarginRight;
		conf.boxMarginHeight = conf.boxMarginTop + conf.boxMarginBottom;
		conf.stagePaddingWidth = conf.stagePaddingLeft + conf.stagePaddingRight;
		conf.stagePaddingHeight = conf.stagePaddingTop + conf.stagePaddingBottom;
		conf.boxMinWidth = conf.stageMinWidth + conf.stagePaddingWidth;
		conf.boxMinHeight = conf.stageMinHeight + conf.stagePaddingHeight;
		this.conf = conf;
		$('body').append([
			'<div id="skybox">',
			'<div id="skybox_bg" />',
			'<div id="skybox_box">',
			((conf.closeButton) ? '<a href="javascript:void(0);" id="skybox_close">Close</a>' : ''),
			'<div id="skybox_stage">',
			'<div id="skybox_content" />',
			'</div>',
			'</div>',
			'</div>'
		].join(''));
		var container = this.container = $('#skybox');
		var bg = this.bg = $('#skybox_bg');
		var box = this.box = $('#skybox_box');
		var stage = this.stage = $('#skybox_stage');
		var content = this.content = $('#skybox_content');
		if (conf.closeButton) {
			$('#skybox_close').click(function() {
				self.hide();
			});
		};
		bg.css({
			background: conf.bgBackground,
			opacity: 0
		}).click(function() {
			self.hide();
		});
		box.css({
			background: conf.boxBackground,
			opacity: 0
		});
		if (this.isLteIE6) {
			container.css({
				position: 'absolute',
				left: 0,
				top: 0,
				overflow: 'hidden'
			});
			bg.css('position', 'absolute');
			box.css('position', 'absolute');
		};
		stage.css({
			borderTop: conf.stageBorderTop,
			borderRight: conf.stageBorderRight,
			borderBottom: conf.stageBorderBottom,
			borderLeft: conf.stageBorderLeft,
			paddingTop: conf.stagePaddingTop,
			paddingRight: conf.stagePaddingRight,
			paddingBottom: conf.stagePaddingBottom,
			paddingLeft: conf.stagePaddingLeft
		});
		conf.stageBorderTopWidth = parseInt(stage.css('border-top-width'));
		conf.stageBorderRightWidth = parseInt(stage.css('border-right-width'));
		conf.stageBorderBottomWidth = parseInt(stage.css('border-bottom-width'));
		conf.stageBorderLeftWidth = parseInt(stage.css('border-left-width'));
		content.css('background', conf.contentBackground);
		if (selector) {
			$(selector).each(function() {
				var elm = $(this);
				var href = elm.attr('href').split('?');
				var params = self.getQuery(href[1]);
				params.src = elm.attr('href');
				elm.click(function(e) {
					e.preventDefault();
					this.blur();
					self.show(params);
				});
			});
		};
		if (callback) callback();
	},

	// 表示
	show: function(params, callback) {
		var self = this;
		this.current = params;
		var src = params.src;
		var win = $(window);
		var duration = this.conf.fadeInDuration;
		var easing = this.conf.easing;
		var content = this.content;
		if (/\.swf$/.test(src)) {
			if (swfobject.hasFlashPlayerVersion(params.base.version)) {
				var base = params.base;
				content.append('<div id="skybox_flashcontent" />');
				var swf = function() {
					swfobject.embedSWF(src, 'skybox_flashcontent', base.width, base.height, base.version, base.expressInstall, params.flashvars, params.params, params.attributes, params.callback);
				};
			} else {
				var alt = [
					'<div id="skybox_flashcontent_alt">',
					'<p id="skybox_flashcontent_alt_lead">このコンテンツをご覧いただくには、以下の設定が必要となります。</p>',
					'<ul id="skybox_flashcontent_alt_list">',
					'<li>JavaScriptの設定を有効にしてください</li>',
					'<li>最新のAdobe Flash Playerをインストールしてください</li>',
					'</ul>',
					'<p id="skybox_flashcontent_alt_ref"><a href="https://www.adobe.com/go/getflashplayer" target="_blank">&raquo; Adobe Flash Playerのダウンロード</a></p>',
					'</div>'
				].join('');
				content.append((params.alt || alt));
			};
		} else if (/^#/.test(src)) {
			$(src).contents().appendTo(content);
		} else {
			content.append('<iframe src="' + src + '" id="skybox_frame" name="skybox_frame_' + new Date().getTime() + '" frameborder="0" />');
		};
		if (this.setStyle(params)) {
			this.container.css('display', 'block');
			this.bg.stop().animate({
				opacity: this.conf.bgOpacity
			}, {
				duration: duration,
				easing: easing
			});
			this.box.stop().animate({
				opacity: 1
			}, {
				duration: duration,
				easing: easing,
				complete: function() {
					if (self.isIE) this.style.removeAttribute('filter');
					if (swf) swf();
					if (callback) callback();
				}
			});
		};
		win.bind('resize', function() {
			if (self.isLteIE6) {
				setTimeout(function() {
					self.setStyle(params);
				}, 50);
			} else {
				self.setStyle(params);
			};
		});
		if (this.isLteIE6) {
			win.bind('scroll', function() {
				self.setBoxPosition();
			});
		};
	},

	// 非表示
	hide: function() {
		var self = this;
		var src = this.current.src;
		var duration = this.conf.fadeOutDuration;
		var easing = this.conf.easing;
		var content = this.content;
		this.bg.stop().animate({
			opacity: 0
		}, {
			duration: duration,
			easing: easing
		});
		this.box.stop().animate({
			opacity: 0
		}, {
			duration: duration,
			easing: easing,
			complete: function() {
				self.container.css('display', 'none');
				var win = $(window);
				win.unbind('resize');
				if (self.isLteIE6) win.unbind('scroll');
				if (self.isIE) this.style.removeAttribute('filter');
				if (/\.swf$/.test(src)) {
					content.empty();
				} else if (/^#/.test(src)) {
					content.contents().appendTo(src);
				} else {
					content.empty();
				};
			}
		});
	},

	// スタイルを設定
	setStyle: function(params) {
		var conf = this.conf;
		var d = $(document);
		var w = params.width;
		var h = params.height;
		var win = $(window);
		var winWidth = win.width();
		var winHeight = win.height();
		var boxMaxWidth = winWidth - conf.boxMarginWidth;
		var boxMaxHeight = winHeight - conf.boxMarginHeight;
		if (/%$/.test(w)) w = Math.floor(winWidth * parseInt(w) / 100);
		if (/%$/.test(h)) h = Math.floor(winHeight * parseInt(h) / 100);
		if (w > boxMaxWidth) w = boxMaxWidth;
		if (h > boxMaxHeight) h = boxMaxHeight;
		if (w < conf.boxMinWidth) w = conf.boxMinWidth;
		if (h < conf.boxMinHeight) h = conf.boxMinHeight;
		var l = Math.floor((winWidth - w) / 2);
		var t = Math.floor((winHeight - h) / 2);
		if (l < conf.boxMarginLeft) l = conf.boxMarginLeft;
		if (t < conf.boxMarginTop) t = conf.boxMarginTop;
		if (this.isLteIE6) {
			l += win.scrollLeft();
			t += win.scrollTop();
			this.container.css({
				width: $('body').width() + 'px',
				height: d.height() + 'px'
			});
		};
		if (this.isIE8) {
			this.bg.css({
				width: $('body').width() + 'px',
				height: d.height() + 'px'
			});
		};
		this.box.css({
			left: l + 'px',
			top: t + 'px',
			width: w + 'px',
			height: h + 'px'
		});
		w -= conf.stagePaddingWidth + conf.stageBorderRightWidth + conf.stageBorderLeftWidth;
		h -= conf.stagePaddingHeight + conf.stageBorderTopWidth + conf.stageBorderBottomWidth;
		$('#skybox_stage').css({
			width: w + 'px',
			height: h + 'px'
		});
		if (this.isLteIE6) {
			this.content.css({
				width: w + 'px',
				height: h + 'px'
			});
		};
		return true;
	},

	// IE6以下のみスクロールに合わせて位置を調整
	setBoxPosition: function() {
		var conf = this.conf;
		var win = $(window);
		var box = this.box;
		var l = Math.floor((win.width() - box.width()) / 2);
		var t = Math.floor((win.height() - box.height()) / 2);
		if (l < conf.boxMarginLeft) l = conf.boxMarginLeft;
		if (t < conf.boxMarginTop) t = conf.boxMarginTop;
		box.css({
			left: (l + win.scrollLeft()) + 'px',
			top: (t + win.scrollTop()) + 'px'
		});
	},

	// クエリを取得
	getQuery: function(str) {
		var query = {}
		var temp = decodeURIComponent(str).split(/&/);
		for (var i = 0, l = temp.length; i < l; i++) {
			var param = temp[i].split(/=/);
			query[param[0]] = param[1];
		};
		return query;
	},

	// IE判定フラグ
	isIE: /msie ([0-9]+)/.test(navigator.userAgent.toLowerCase()),

	// IE6以下判定フラグ
	isLteIE6: /msie ([0-9]+)/.test(navigator.userAgent.toLowerCase()) && RegExp.$1 <= 6,

	// IE8判定フラグ
	isIE8: /msie ([0-9]+)/.test(navigator.userAgent.toLowerCase()) && RegExp.$1 == 8,

	// Fx2以下判定フラグ
	isLteFx2: /firefox\/([0-9]+)/.test(navigator.userAgent.toLowerCase()) && RegExp.$1 <= 2
}








