/**
 * @name skybox.js
 * @created 2008-05-07
 * @modified 2008-05-20
 * @version 0.0.2
 */

var skybox = new Skybox();

$(function() {
	skybox.init('#body a.skybox');
});

function Skybox() {
	// コレクション
	this.collection = [];

	// IE7未満判定フラグ
	this.isLtIE7 = (navigator.userAgent.match('MSIE ([0-9]+\.[0-9])') && RegExp.$1 < 7) ? true : false;

	// Fx判定フラグ
	this.isFirefox = (navigator.userAgent.match('Firefox')) ? true : false;

	// 初期化
	this.init = function(selector) {
		var self = this;
		$(selector).each(function() {
			var index = self.collection.length;
			var href = this.href.split('?');
			var prop = self.getQuery(href[1]);
			prop.href = href[0];
			self.collection.push(prop);
			$(this).attr('href', 'javascript:void(0);').click(function() {
				self.show(index);
				return false;
			});
		});
	}

	// 表示
	this.show = function(index) {
		var self = this;
		$('body').append([
			'<div id="skybox">',
			'<div id="skybox_overlay"></div>',
			'<div id="skybox_stage">',
			'<iframe id="skybox_frame" name="skybox_frame_' + new Date().getTime() + '" frameborder="0"></iframe>',
			'</div>',
			'</div>'
		].join(''));
		var win = this.getWindowSize();
		var body = this.getElementSize(document.body);
		var prop = this.collection[index];
		var base = (win.width < body.width) ? win : body;
		var w = (prop.width > base.width - 40) ? base.width - 40 : prop.width;
		var h = (prop.height > win.height - 40) ? win.height - 40 : prop.height;
		var l = Math.floor((base.width - w) / 2);
		var t = Math.floor((win.height - h) / 2);
		if (this.isLtIE7) {
			var scr = this.getScrollLength();
			l += scr.x;
			t += scr.y;
			$('#skybox_overlay').css({ width: body.width + 'px', height: body.height + 'px' });
		}
		// Fxの場合はFlashを不可視化
		if (this.isFirefox) {
			$('embed').each(function() {
				$(this).css('visibility', 'hidden');
			});
		}
		$('#skybox_overlay').click(function() {
			self.hide();
		}).show();
		$('#skybox_frame').attr('src', prop.href).css({ width: w + 'px', height: h + 'px' });
		$('#skybox_stage').css({ left: l + 'px', top: t + 'px', width: w + 'px', height: h + 'px' }).show();
		$(window).bind('resize', function() {
			self.setStyle(index);
		});
		if (this.isLtIE7) {
			$(window).bind('scroll', function() {
				self.setPosition();
			});
		}
	}

	// 非表示
	this.hide = function() {
		var self = this;
		$('#skybox_stage').fadeOut(300);
		$('#skybox_overlay').fadeOut(300, function() {
			$(window).unbind('resize');
			if (self.isLtIE7) {
				$(window).unbind('scroll');
			}
			$('#skybox').remove();
			// Fxの場合は不可視となっていたFlashを再可視化
			if (self.isFirefox) {
				$('embed').each(function() {
					$(this).css('visibility', 'visible');
				});
			}
		});
	}

	// ステージのスタイルを設定
	this.setStyle = function(index) {
		var win = this.getWindowSize();
		var body = this.getElementSize(document.body);
		var prop = this.collection[index];
		var base = (win.width < body.width) ? win : body;
		var w = (prop.width > base.width - 40) ? base.width - 40 : prop.width;
		var h = (prop.height > win.height - 40) ? win.height - 40 : prop.height;
		var l = Math.floor((base.width - w) / 2);
		var t = Math.floor((win.height - h) / 2);
		if (this.isLtIE7) {
			var scr = this.getScrollLength();
			l += scr.x;
			t += scr.y;
			$('#skybox_overlay').css({ width: body.width + 'px', height: body.height + 'px' });
		}
		$('#skybox_frame').css({ width: w + 'px', height: h + 'px' });
		$('#skybox_stage').css({ left: l + 'px', top: t + 'px', width: w + 'px', height: h + 'px' });
	}

	// IE7未満のみスクロールに合わせて位置を調整
	this.setPosition = function() {
		var win = this.getWindowSize();
		var body = this.getElementSize(document.body);
		var size = this.getElementSize($('#skybox_stage').get(0));
		var scr = this.getScrollLength();
		var l = Math.floor((body.width - size.width) / 2) + scr.x;
		var t = Math.floor((win.height - size.height) / 2) + scr.y;
		$('#skybox_stage').css({ left: l + 'px', top: t + 'px' });
	}

	// サイトルートを取得
	this.getSiteRoot = function(key) {
		var scripts = document.getElementsByTagName('script');
		for (var i = 0, l = scripts.length; i < l; i++) {
			if (scripts[i].src.match(key + '([\?].*|$)')) return scripts[i].src.split(key)[0] + './';
		}
		return false;
	}

	// クエリを取得
	this.getQuery = function(str) {
		var prop = {}
		var temp = str.split('&');
		for (var i = 0, l = temp.length; i < l; i++) {
			temp[i] = temp[i].split('=');
			prop[temp[i][0]] = temp[i][1];
		}
		return prop;
	}

	// 要素のサイズを取得
	this.getElementSize = function(elm, mode) {
		var w = elm.offsetWidth || elm.clientWidth || 0;
		var h = elm.offsetHeight || elm.clientHeight || 0;
		return (mode == 'w') ? w : (mode == 'h') ? h : { width: w, height: h };
	}

	// ウィンドウのサイズを取得
	this.getWindowSize = function(mode) {
		var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
		var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
		return (mode == 'w') ? w : (mode == 'h') ? h : { width: w, height: h };
	}

	// スクロール量を取得
	this.getScrollLength = function(mode) {
		var x = window.scrollX || window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
		var y = window.scrollY || window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;	
		return (mode == 'x') ? x : (mode == 'y') ? y : { x: x, y: y };
	}
}








