/**
 * works
 * [rana]
 *
 * @copyright   2010 RaNa design associates, inc.
 * @link        http://kaelab.ranadesign.com/
 * @version     1.0
 * @date        2011-08-01
 */
(function($) {

	// add CSS rule to hide default contents
	var mysheet = document.styleSheets[0];
	var totalrules = mysheet.cssRules ? mysheet.cssRules.length : mysheet.rules.length;
	if (mysheet.insertRule) {
		mysheet.insertRule("#cover {visibility: hidden}", totalrules);
		mysheet.insertRule("#pageTop {visibility: hidden}", totalrules);
	} else if (mysheet.addRule) { //for msie
		mysheet.addRule("#cover", "visibility: hidden");
		mysheet.addRule("#pageTop", "visibility: hidden");
	}

	var PHONE = /(iPhone)|(iPad)|(iPod)|(Android)/i.test(navigator.userAgent),
		IE6 = $.browser.msie && $.browser.version < 7 ? true : false;

	$(function() {
		// fallback=1パラメータが付いていたらコンテンツを入れ替えない
		// フォールバック対応：IE6, スマートフォン
		var fallback = /fallback=1/.test(location.search) || PHONE || IE6;
		if (fallback) {
			$(document.body).children().css('visibility', 'visible');
			// footer調整
			initFooter();

			// 動画プレーヤー
			if ($("body")[0].id === "works") {
				// topからの呼び出しの場合は、icon.jsのloadメソッドのコールバック内で実行する。
				$.setPlayer();
			}
			return;
		}
		// 代替コンテンツをまず入れ替える
		initContents().done(function () {
			// footer調整
			initFooter();

			// 動画プレーヤー
			if ($("body")[0].id === "works") {
				// topからの呼び出しの場合は、icon.jsのloadメソッドのコールバック内で実行する。
				$.setPlayer();
			}
		});
	});
	//
	// 代替コンテンツ入れ替え
	//
	function initContents() {
		return $.ajax('/works/', {
			dataType: 'html',
			context: document.body,
			success: function (data) {
				// change css
				var sheets = $(document).find('link');
				sheets.remove();
				var css = document.createElement('link');
				css.rel = 'stylesheet';
				css.href = '/css/index.css';
				$('head')[0].appendChild(css);
				// change html
				var html = data.slice(data.search(/<body/), data.search(/<\/body/));
				html = html.replace(/<body[^>]*?>/, '');
				$(document.body).empty().html(html).css('visibility', 'hidden');
				// append new script 
				var script = document.createElement('script');
				script.src = '/js/rana-works-icon.js';
				$('head')[0].appendChild(script);
			}
		});
	}
	
	//
	// footer調整
	//
	function initFooter() {
		var contentWidth = 960 + 100,
			minHeight = 560;

		// absolute position 対応
		$("#wrapper").height(Math.max(
			$(window).height() - $(".area-footer").height() - 48, 
			$(".area-container").height() + $(".area-footer").height() - 48
		));  // 48: area-button.height

		// position:fixed非対応処理
		if (IE6 || PHONE) {
			$("#area-container").css("position", "relative");
			$(".area-footer, #mod-searchbox").css("position", "absolute");
			return false;
		} else {
			// 狭小画面対応
			$(window).resize(function() {
				$(".area-footer, #mod-searchbox").css({
					position: ($(window).width() < contentWidth || $(window).height() < minHeight) ? "absolute" : "fixed"
				});
			}).resize();
		}
	}
	
})(jQuery);


/**
 * プラグイン
 */
(function($) {

	// 動画プレーヤー or スライドビュワー
	// 呼び出し用
	$.setPlayer = function() {
		var that = $("#area-capture");

		that.flvPlayer({
			// 動画パス
			file: that.data("movie"),
			// Flash有効時
			success: function(obj) {

			},
			// Flash無効時
			error: function() {
				$(".area-view").show().slideViewer({
					transfers: $(".area-view .area-viewThumb").outerWidth(),
					count: $(".area-view .area-viewThumb").length
				});
			}
		});
	};

	/**
	 * 動画プレーヤー
	 *
	 * 呼び出し元はIDがある前提。
	 * @file: flvパス
	 * @thumg: サムネイル画像(jpg)パス
	 */
	$.fn.flvPlayer = function(options) {
		var self = $(this),
			config = $.extend({
				url: "/swf/movieplayer.swf",
				id: this.attr("id"),
				width: 480,
				height: 320 + 29,
				ver: "10",
				success: function() {},
				error: function() {}
			}, options),
			flashvars = {
				file: config.file,  // flv
				thumb: config.thumb  // jpg
			},
			params = {
				menu: "false",
				allowScriptAccess: "always",
				allowFullScreen: "true",
				wmode: "transparent"
			},
			attributes = {},
			callback = function(obj) {
				if (obj.success) {
					config.success(obj);
				} else {
					config.error(obj);
				}
			};

		// ファイル名エラー
		if (!config.file) {
			config.error();
			return false;
		}
		// サムネイル設定
		// flvファイルと同名で拡張子のみjpg
		flashvars.thumb = config.file.slice(0, -3) + "jpg";
		

		// embed swf
		swfobject.embedSWF(
			config.url, config.id, config.width, config.height, config.ver, config.install,
			flashvars,
			params,
			attributes,
			callback
		);
	};

	/**
	 * スライドビュワー
	 */
	$.fn.slideViewer = function(options) {
		var self = $(this),
			config = $.extend({
				easing: "swing",
				speed: 600,
				wait: 5000,
				transfers: 0,
				count: 1
			}, options),
			_slide = function() {
				self.stop(true).animate({
					marginLeft: config.count > 1 ? "-=" + config.transfers : 0
				}, {
					duration: config.speed,
					easing: config.easing,
					complete: function() {
						config.count = config.count > 1 ? config.count - 1 : options.count;
						setTimeout(_slide, config.wait);
					}
				});
			};
			
		setTimeout(_slide, config.wait);
	};

})(jQuery);

