jQuery(document).ready(function(){

	var _win = jQuery(window);
	var _height = _win. height() - 45;
	var _width = _win. width() - 40;
	var _contents_obj = jQuery(".contents");

	_opening. _fuki_on (800, function(){
		_opening. _fuki_off (800);
	});

	_opening. _init (_win, _width, _height, _contents_obj);
	_action_graph. _Ajax_counter ("TOPPAGE");

});

var _opening = {

	_init : function (_win, _width, _height, _contents_obj) {

		jQuery(".menu_icon").css({
			"top"	: ((_height/2)-25)+"px"
			,"left"	: ((_width/2)-25)+"px"
			,"display" : "none"
		});

		_contents_obj.css({
			"marginTop"		: ((_height/2)-50)+"px",
			"width"			: "240px",
			"height"		: "200px",
			"marginLeft"	: "auto",
			"marginRight"	: "auto"
		});

		jQuery("#top_menu, #top_body, .contents, .contents_wrap").css({
			"height"		: "30px"
		});
		jQuery("#top_menu, #top_body").css({
			"height"		: "0px"
		});

		jQuery("#top_g_title").css({
			"cursor"	: "pointer"
		});

		jQuery("#top_g_title").click(function(){
			jQuery("#fuki_start").hide();
			var _height = _win. height() - 45;
			var _width = _win. width() - 40;
			_opening. _start (_win, _width, _height, _contents_obj);
		});
		jQuery("#top_g_title").mouseover(function(){
			_opening. _fuki_on (800);
		});
		jQuery("#top_g_title").mouseout(function(){
			_opening. _fuki_off (250);
		});

		_win.resize(function(){
			var _newHeight = _win. height();
			_contents_obj.css("margin-top",((_newHeight/2)-50)+"px");
		});

	},

	_fuki_on : function (_time, func) {

		jQuery("#fuki_start").fadeIn(_time,function(){
			if (func) { func(); }
		});
	},

	_fuki_off : function (_time) {

		jQuery("#fuki_start").fadeOut(_time);
	},

	_start : function (_win, _width, _height, _contents_obj) {

		jQuery("#top_body").css({
			"border"	: "1px solid #88f"
		});

		jQuery("#fuki_start").remove();
		jQuery("#top_g_title").unbind("mouseover");
		jQuery("#top_g_title").unbind("click");
		_contents_obj.animate({
			"width"			: _width + "px"
			,"marginTop"	: "0px"
		},500,function(){
			jQuery("#top_g_title").css({
				"position"	: "absolute"
			});
			jQuery("#top_g_title").animate({
				"top"		: "20px"
			});
		});

		_contents_obj.css({
			"height"	: _height+"px"
		});
		jQuery(".contents_wrap").css({
			"height"	: (_height-50)+"px"
		});
		jQuery("#top_body, #top_menu").animate({
			"height"	: (_height-45)+"px"
		},500);

		jQuery("#top_g_title").attr({
			"title"		: "Sub menu"
			,"target_id"	: "sub_menu"
		});

		jQuery("#top_footer").css({
			"display"	: "block"
		});

		jQuery("#top_g_title").click(function(){
			_target = jQuery("#" + jQuery(this).attr("target_id"));
			if (_target. css("display")!="none") {
				_commonLib. hide (_target, 200);
				jQuery(this).attr("title","開く");
			} else {
				_commonLib. show (_target, 200);
				jQuery(this).attr("title","閉じる");
			}
		});

		jQuery(".menu_icon").show();
		jQuery(".menu_icon").each(function(){
			var _menu_icon_obj = jQuery(this);
			if (_menu_icon_obj.attr("target_id")=="_go_dragdrop") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2-32  +20,
					"left"	: _win. width() /2-32,
					"top"	: _win. height() /2-65-(32*4) +40
				},1500);
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_dragdrop_off.gif"
					);
				});
			}
			if (_menu_icon_obj.attr("target_id")=="_go_menu") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2-100  -10-10,
//					"top"	: _win. height() /2-65-(32*4)+100  -20-10-10
					"left"	: _win. width() /2-100-32,
					"top"	: _win. height() /2-65-(32*4)+100
				},1700);
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_menu_off.gif"
					);
				});
			}
			if (_menu_icon_obj.attr("target_id")=="_go_gallery") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2+100-32  +20+10+10,
//					"top"	: _win. height() /2-65-(32*4)+100  -10+10
					"left"	: _win. width() /2+100-32,
					"top"	: _win. height() /2-65-(32*4)+100
				},1900);
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_gallery_off.gif"
					);
				});
			}
			if (_menu_icon_obj.attr("target_id")=="_go_php") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2-100-32  -20-10-10,
//					"top"	: _win. height() /2-65-(32*4)+200  +10-10
					"left"	: _win. width() /2-100-32,
					"top"	: _win. height() /2-65-(32*4)+200
				},2100);
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_php_off.gif"
					);
				});
			}
			if (_menu_icon_obj.attr("target_id")=="_go_ajax") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2+100-32  +10+10,
//					"top"	: _win. height() /2-65-(32*4)+200  +20+10+10
					"left"	: _win. width() /2+100-32,
					"top"	: _win. height() /2-65-(32*4)+200
				},2300);
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_ajax_off.gif"
					);
				});
			}
			if (_menu_icon_obj.attr("target_id")=="_help_dialog") {
				_menu_icon_obj. animate({
//					"left"	: _win. width() /2-32  -20,
//					"top"	: _win. height() /2-65-(32*4)+300
					"left"	: _win. width() /2-32,
					"top"	: _win. height() /2-65-(32*4)+300 -40
				},2500,function(){
				});
				_menu_icon_obj. find("img"). each(function(){
					jQuery(this).attr(
						"src","./image/_top_menu_btn_dialog_off.gif"
					);
				});
			}
		});

		_win.unbind("resize");
		_opening. _resize (_win, _contents_obj);
		_top_menu. _init ();
	},

	_resize : function (_win, _contents_obj) {

		_win.resize(function(){
			_contents_obj.css("width",_win.width()-40);
			_contents_obj.css("height",_win.height()-45);
			jQuery(".contents_wrap").css("height",_win.height()-45);
			jQuery("#top_body").css("height",_win.height()-95);
			jQuery("#top_menu").css("height",_win.height()-95);
			var _height = _win.height();
		});
	}

}

var _top_menu = {

	_init : function () {

		var _top_menu_icon = jQuery(".menu_icon");
		var _top_menu_area = jQuery("#top_menu");
		var _desc = '';
		_top_menu_icon.each(function(){
			var _top_menu_obj = jQuery(this);

			_top_menu_obj. draggable({
				zIndex		: 100,
				scroll		: true,
				helper		: "original",
				start		: function(ev,ui) {
				},
				drag		: function(ev,ui) {
					jQuery(this).attr("_oldTop", ui.position.top);
					jQuery(this).attr("_oldLeft", ui.position.left);
				},
				stop		: function(ev,ui) {
					var _obj = jQuery(this);
					var _new_node = _obj.clone().appendTo("#top_menu");
					_obj. remove();
//					_new_node = _obj;
					var offsetBoard = _top_menu_area. offset();
					var _top = ui.position.top - offsetBoard.top;
					var _left = ui.position.left - offsetBoard.left;
					var _newLeft = (_left < 15 ? 35 : (_left + _obj.width() + 94 > _top_menu_area. width() ? _top_menu_area. width()-_obj.width() + 10 - 32 -32 -15: ui.position.left ))+"px";
					var _newTop = (_top < 15 ? 58 : (_top + _obj.height() + 12 +80> _top_menu_area. height() ? _top_menu_area. height() - 48 : ui.position.top)) + "px";
					_new_node. css({
						"left"	: ui.position.left,
						"top"	: ui.position.top
					}). animate({
						"left"	: _newLeft,
						"top"	: _newTop
					}, function () {
					});
					_new_node. attr("_oldTop",_newTop);
					_new_node. attr("_oldLeft",_newLeft);
					_top_menu. _init ();

					_new_node. find("div").each(function(){
						if (jQuery(this).attr("class")=="desc") {
							_desc = jQuery(this);
						}
					});

					_top_menu. _desc_btn (_desc);


				}
			});

			_top_menu_obj.unbind("click");
			_top_menu_obj.click(function(){

				var _win = jQuery(window);
				var _obj_tmp = jQuery(this);
				var _obj = _obj_tmp.clone().appendTo("#top_menu");
				_obj_tmp. remove();

				var _offset = _obj.offset();
				var _target = _obj. attr("target_id");
				var _desc_target = _obj. attr("desc");
				_obj. find("div").each(function(){
					if (jQuery(this).attr("class")=="desc") {
						_desc = jQuery(this);
					}
				});
				_desc. find("div").each(function(){
					if (jQuery(this).attr("class")=="item") {
						jQuery(this).hide();
					}
				});

				jQuery("#"+_desc_target).slideToggle(250,function(){
					if (jQuery("#"+_desc_target).css("display")!="none") {
						var _click_obj = _obj.find("img");
						_click_obj. unbind("mouseover");
						_click_obj. unbind("mouseout");
						_click_obj.attr("src","./image/"+_top_menu. _menu_icon_on_array[_target]);
						_click_obj.animate({
							"marginTop"		: "0px",
							"marginLeft"	: "0px",
							"width"			: "80px",
							"height"		: "80px"
						});

						_obj.attr("_oldTop",_offset.top);
						_obj.attr("_oldLeft",_offset.left);
						var _newLeft = (_offset.left > _win.width()-695 ? _win.width()-695 : _offset.left);
						var _newTop = (_offset.top > _win.height()-380 ? _win.height()-380 : _offset.top);
						if (_newLeft < 35) _newLeft = 35;
						if (_newTop < 58) _newTop = 58;
						_obj.animate({
							"left" : _newLeft,
							"top" : _newTop
						},200);
					}
					if (jQuery("#"+_desc_target).css("display")=="none") {
						var _click_obj = _obj.find("img");
						_click_obj. unbind("mouseover");
						_click_obj.attr("src","./image/"+_top_menu. _menu_icon_off_array[_target]);
						_click_obj.animate({
							"marginTop"		: "20px",
							"marginLeft"	: "20px",
							"width"			: "40px",
							"height"		: "40px"
						});


						_obj.animate({
							"left" : _obj.attr("_oldLeft"),
							"top" : _obj.attr("_oldTop")
						},200);
					}
				});
// -
_action_graph. _Ajax_counter ("topmenu"+_target);
// -
				_top_menu. _desc_btn (_desc);
				_top_menu. _init ();
			});

		});

		_top_menu. _m_icon_animate_on (function(){
		});

	},

	_menu_icon_on_array : {
		"_go_dragdrop"	: "_top_menu_btn_dragdrop_on.gif",
		"_go_menu"		: "_top_menu_btn_menu_on.gif",
		"_go_gallery"	: "_top_menu_btn_gallery_on.gif",
		"_go_php"		: "_top_menu_btn_php_on.gif",
		"_go_ajax"		: "_top_menu_btn_ajax_on.gif",
		"_help_dialog"	: "_top_menu_btn_dialog_on.gif"
	},

	_menu_icon_off_array : {
		"_go_dragdrop"	: "_top_menu_btn_dragdrop_off.gif",
		"_go_menu"		: "_top_menu_btn_menu_off.gif",
		"_go_gallery"	: "_top_menu_btn_gallery_off.gif",
		"_go_php"		: "_top_menu_btn_php_off.gif",
		"_go_ajax"		: "_top_menu_btn_ajax_off.gif",
		"_help_dialog"	: "_top_menu_btn_dialog_off.gif"
	},

	_m_icon_animate_on : function (func) {
		jQuery(".m_icon").mouseover(function(){
			var _click_obj = jQuery(this);
			var _target = jQuery(this).parent().attr("target_id");
			_click_obj. unbind("mouseover");
			_click_obj. unbind("mouseout");
			_click_obj.attr("src","./image/"+_top_menu. _menu_icon_on_array[_target]);
			_click_obj.animate({
				"marginTop"		: "0px",
				"marginLeft"	: "0px",
				"width"			: "80px",
				"height"		: "80px"
			}, function(){
				if (func) {
					func ();
				}
			});
				_top_menu. _m_icon_animate_off ();
		});
	},

	_m_icon_animate_off : function (func) {
		jQuery(".m_icon").mouseout(function(){
			var _click_obj = jQuery(this);
			var _target = jQuery(this).parent().attr("target_id");
			_click_obj. unbind("mouseover");
			_click_obj. unbind("mouseout");
//			_click_obj.attr("src","./image/_top_menu_btn.gif");
			_click_obj.attr("src","./image/"+_top_menu. _menu_icon_off_array[_target]);
			_click_obj.animate({
				"marginTop"		: "20px",
				"marginLeft"	: "20px",
				"width"			: "40px",
				"height"		: "40px"
			}, function(){
				if (func) {
					func ();
				}
			});
				_top_menu. _m_icon_animate_on ();
		});
	},

	_desc_btn : function (_top_menu_desc_obj) {

		_top_menu_desc_obj.find("li").each(function(){
			var _obj = jQuery(this);
			if (_obj.parent().parent().attr("class")=="button_list") {

				_obj.unbind("click");
				_obj.click(function(){
					var _target = _obj.attr("target_id");
					if (_target == "_desc_close") {
						_top_menu_desc_obj.slideUp(250);
						jQuery("#top_menu").find("li").each(function(){
						});
					} else {
						var _dialog = jQuery("#_dialog_area");
						_g_navi. _switch(_target, _dialog);
					}
				});

				_obj.mouseover(function(){
					if (_obj.attr("target_id")=="_desc_close") {
						_obj.css("background","url(./image/_btn_tmd_close_on.gif) no-repeat");
					} else {
						_obj.css("background","url(./image/_btn_tmd_exec_on.gif) no-repeat");
					}
				});

				_obj.mouseout(function(){
					if (_obj.attr("target_id")=="_desc_close") {
						_obj.css("background","url(./image/_btn_tmd_close_off.gif) no-repeat");
					} else {
						_obj.css("background","url(./image/_btn_tmd_exec_off.gif) no-repeat");
					}
				});
			}
		});

	}

}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
var _g_navi = {

	_init : function () {
		jQuery("#header").find("li").each(function(){
			jQuery(this).click(function(){
				var _target = jQuery(this).attr("target_id");
				var _dialog = jQuery("#_dialog_area");
				_g_navi. _switch(_target, _dialog);
			});
		});
		jQuery("#header li").mouseover(function(){
			jQuery(this).css({
				"color"			: "#f00"
				,"background"	: "url(./image/_g_navi_icon_on_bg.gif)"
			});
		});
		jQuery("#header li").mouseout(function(){
			jQuery(this).css({
				"color"			: "#ecab00"
				,"background"	: "url(./image/_g_navi_icon_bg.gif)"
			});
		});
	},

	_switch : function (_target, _dialog) {
		switch (_target) {
			case "_help_dialog" :
// -
_action_graph. _Ajax_counter ("g_navi_help_dialog");
// -
				_g_navi. _Ajax_tpl_receive ("dragdrop", function(html) {
					_commonLib. _dialogs (_dialog, "このページについて", html, 480, 320);
				});
				break;
			case "_help_gallery" :
// -
_action_graph. _Ajax_counter ("g_navi_help_gallery");
// -
				_g_navi. _Ajax_tpl_receive ("gallery", function(html) {
					_commonLib. _dialogs (_dialog, "このページについて", html, 480, 320);
				});
				break;
			case "_go_toppage" :
				_g_navi. _change_toppage ();
				break;
			case "_go_dragdrop" :
				_g_navi. _change_dragdrop ();
				break;
			case "_go_menu" :
				_g_navi. _change_menu ();
				break;
			case "_go_gallery" :
				_g_navi. _change_gallery ();
				break;
			case "_go_php" :
				_g_navi. _change_php ();
				break;
			case "_go_ajax" :
				_g_navi. _change_ajax ();
				break;
			case "_go_dialog" :
				_g_navi. _change_dialog ();
				break;
			case "_mail_form_dialog" :
// -
_action_graph. _Ajax_counter ("g_navi_mail_form_dialog");
// -
				_g_navi. _Ajax_tpl_receive ("mailform", function(html) {
					_commonLib. _dialogs_form (_dialog, "お問い合わせ", html, 480, 320, function(){
						alert("ここで Ajax でメール送信");
					});
				});
				break;
			default :
				break;
		}
	},

	_change_action_graph : function () {
		jQuery(".rtop, .rbottom, #action_graph_area").show();
		jQuery("#main_board, #sche_body, #gallery_body, #top_menu_desc, #top_body").hide();
		jQuery("#header h1").html("ACTION グラフ表示");
	},

	_change_toppage : function () {
// -
_action_graph. _Ajax_counter ("to_toppage");
// -
		jQuery(".rtop, .rbottom, #top_body").show();
		jQuery("#action_graph_area, #main_board, #sche_body, #gallery_body, #top_menu_desc").hide();
		jQuery("#top_menu").find("li").each(function(){
			jQuery(this).css("margin-top","165px");
		});
		jQuery("#header h1").html("WEB 制作デモ");
	},

	_change_dragdrop : function () {
// -
//_action_graph. _Ajax_counter ("to_dragdrop");
// -
		location.href = _base_url+'action/sche/';
	},

	_change_menu : function () {
// -
//_action_graph. _Ajax_counter ("to_menu");
// -
		location.href = _base_url+'action/menu/';
	},

	_change_gallery : function () {
// -
//_action_graph. _Ajax_counter ("to_gallery");
// -
		location.href = _base_url+'action/gallery/';
	},

	_change_php : function () {
// -
//_action_graph. _Ajax_counter ("to_php");
// -
		location.href = _base_url+'action/php/';
	},

	_change_ajax : function () {
// -
//_action_graph. _Ajax_counter ("to_ajax");
// -
		location.href = _base_url+'action/ajax/';
	},

	_change_dialog : function () {
// -
//_action_graph. _Ajax_counter ("to_dialog");
// -
		location.href = _base_url+'action/dialog/';
	},

	_Ajax_tpl_receive : function (_template_name, _func) {
		jQuery.get(
			_base_url+'?a=controll&c=d_help',
			{
				"ctrl"	: 500,
				"file"	: _template_name
			},
			function(html) {
				if (_func) {
					_func (html);
				}
			},
			"html"
		);
	}

}
