/*
 * cocomite クラス
 *
 * このクラスを使用する場合は、先に jQuery.js（ver 1.2.X 以上） を読み込む必要があります。
 *
 * jQuery：     http://jquery.com/
 *
 * @author      Hiroaki Wakamatsu
 * @copyright  ZYYX Inc. <http://www.zyyx.jp/>
 * @version     1.0
 *
 */

/**
 * Cocomite class のコンストラクタ
 */
var Cocomite = {};

(function() {

	Cocomite.openId = null;
	Cocomite.popupWin = null;
	Cocomite.swfUrl = Setting.rootPath + "/common/media/cocomite.swf";
	/*
	 * Cocomite class ： load メソッド
	 * 
	 * 冊子イメージを書き出す
	 */
	Cocomite.load = function(id) {
		if (parseInt(Flash.getVersion(), 10) < Setting.flashVersion) {
			$(".flashError").css("visibility", "visible");
		} else {
			$(".flashError").css("display", "none");

			var flashvars = {};
			var params = {
				allowScriptAccess: 'sameDomain',
				allowFullScreen: 'false',
				wmode: 'transparent',
				menu: 'false'
			};
			var attributes = {};

			swfobject.embedSWF(Cocomite.swfUrl, "cocomiteFlashBook", "560", "375", Setting.flashVersion + ".0.0", null, flashvars, params, attributes);
		}
	}

	/*
	 * Cocomite class ： openCategory メソッド
	 * 
	 * クリックされた中分類名にフォーカスを当て、所属する小分類を表示する
	 */
	Cocomite.openCategory = function(id) {
		if (isIE6) {
			$("#footer").css("position", "static");
		}

		if (Cocomite.openId) {
			var targetId = Cocomite.openId;
			if (isIE6) {
				$("#" + targetId).parent().removeClass("forcus");
			} else {
				$("#" + targetId).next().animate(
					{"height": "0", "opacity": "0"},
					{"duration": "slow", "easing": "quart", "complete": function() {
						$(this).css({
							"visibility": "hidden",
							"position": "absolute",
							"height": "auto"
						});
						$("#" + targetId).parent().removeClass("forcus");
					}}
				);
			}
		}
		if (Cocomite.openId == id) {
			Cocomite.openId = null;
		} else {
			Cocomite.openId = id;

			var obList = $("#" + id).next();

			if (isIE6) {
				$("#" + Cocomite.openId).parent().addClass("forcus");
			} else {
				var categryHeight = obList.height();
				obList.css({"visibility": "visible", "position": "static", "height": "0", "opacity": "0"});

				$("#" + id).parent().addClass("forcus");

				obList.animate(
					{"height": categryHeight + "px", "opacity": "1.0"},
					{"duration": "slow", "easing": "quart", "complete": function() {
						$(this).css({
							"height": "auto"
						});
					}}
				);
			}
		}

		if (isIE6) {
			$("#footer").css("position", "absolute");
		}
	};

	/*
	 * Cocomite class ： openCocomite メソッド
	 * 
	 * クリックされた小分類に関連するCOCOMITEのページを別ウインドウで開き、同時に小分類別商品一覧ページに遷移する
	 */
	Cocomite.openCocomite = function(key, no) {

		// ブラウザがWindows版 IEの時のみ有効
		if (navigator.platform.indexOf("Win") != -1 && navigator.userAgent.indexOf("MSIE") != -1) {

			if (!navigator.javaEnabled()) {
				// 警告メッセージをオーバーレイで表示する
				Cocomite.openOverlay(Setting.rootPath + "/common/partsHTML/cocomiteNonJava.html");
				//Cocomite.openPopup("cocomite/setting.html", 600, 500);
				return false;
			} else {
				var url = "/cocomite/cocomite.html";
				if (no) {
					url += "?page=" + no;
				}
				Cocomite.openPopup(url, 1014, 700);
			}
			if (key) {
				location.href = "/small_categories/index/" + key;
				//location.href = "UP-1-3.html?key" + key;
			} else {
				return false;
			}
		} else {
			// 警告メッセージをオーバーレイで表示する
			Cocomite.openOverlay(Setting.rootPath + "/common/partsHTML/cocomiteInfo.html");
			return false;
		}
	};


	/*
	 * Cocomite class ： openOverlay メソッド
	 * 
	 */
	Cocomite.openOverlay = function(readHTML) {
			// オーバーレイを開く → Overlay クラスの内容をそのまま引用
			$("body").append(Overlay.baseHTML);
			$("#overlayScreen").css("display", "block").click(function() {
				Overlay.close();
			});

			// body 要素のマージン値を取得する
			bodyOffset = $("body").offset();

			if (isIE6) {
				// SELECTがある場合は、表示を隠す（IEのバグ対応）
				$("select:visible").hide();

				$("#overlayScreen").css({
					"left": "-" + bodyOffset.left + "px",
					"top": "-" + bodyOffset.top + "px"
				});
			}

			var objFrame = $("#overlayFrame");
			var objContent = $("#overlayContent");

			objContent.load(readHTML, function() {

				// 表示要素を読み込み、各種処理を実行
				var objThis = $(this);
				var width = objThis.width();
				var height = objThis.height();

				var sabunW = width - objFrame.width();
				var sabunH = height - objFrame.height();

				objFrame.animate(
					{"width": "+=" + sabunW + "px", "left" : "-=" + Math.floor(sabunW / 2) + "px", "height": "+=" + sabunH + "px", "top" : "-=" + Math.floor(sabunH / 2) + "px"},
					{"duration": "first", "easing": "quart", "complete": function() {
						objContent.css({"position": "static", "visibility": "visible"});
						$("h2 > span", objContent).click(function() {
							Overlay.close();
						});
					}}
				);
			});

			Overlay.setCenter();
			objFrame.css("visibility", "visible");
	};

	/*
	 * Cocomite class ： openPopup メソッド
	 * 
	 */
	Cocomite.openPopup = function(url, width, height) {

		if (Cocomite.popupWin) {
			Cocomite.popupWin.close();
		}

		//ウインドウの表示サイズを取得する
		var sWidth = screen.width;
		var sHeight = screen.height;

		//画面の中央に表示するため、XY座標を算出する
		var left = (sWidth - parseInt(width, 10)) / 2;
		var top = (sHeight - parseInt(height, 10)) / 2;

		Cocomite.popupWin = window.open(url, "_blank", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,screenX=" + left + ",screenY=" + top + ",left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
		Cocomite.popupWin.focus();
	};

})();

/**
 * ページ読み込み時の処理
 */
$(function() {
	Cocomite.load();
});
