
jQuery.noConflict();


jQuery( document ).ready( function () {
	var businessContactHeight = 400;
	var tabs = [
		{
			_name: "#gm-profile",
			_isOpen: false,
			top: {
			// must be tha same value with "top" value defined in header.css.
				open: 0,
				close: 	 -610
			}
		}, {
			_name: "#gm-business-contact",
			_isOpen: false,
			top: {
			// must be tha same value with "top" value defined in header.css.
				open: 0,
				close: - businessContactHeight
			}
		}
	];

	var businessContainerSubAnim = {
		open: {
			height: 29 + businessContactHeight
		},
		close: {
			height: 29
		}
	};

	var prefixes = ["open", "close"];
	for ( var i = 0; i < tabs.length; i++ ) {
		( function (tab) {
			var name = tab._name;
			for ( var j = 0; j < prefixes.length; j++ ) {
				( function (prefix) {
					var id = name + "-" + prefix + "trigger";
					jQuery( id ).click(function(){

						var param = {};
						
					tab._isOpen = !tab._isOpen;
						var action = (tab._isOpen) ? "open" : "close";
						
						for ( var n in tab ) {
							if ( n.match( /^_/ )  )
								continue;
							param[n] = tab[n][action];
						}

						jQuery( name ).animate( param, 280, "swing" );

						if ( name == "#gm-business-contact" ) {
							jQuery( name + "-container" ).animate( 
							businessContainerSubAnim[action] , 280, "swing" );
						}
					} );
				} )(prefixes[j]);
			}
		} )( tabs[i] );
	}

	// mouseover evet handling.
	var Hover = function (trigger, target, name) {
		var e = document.getElementById(trigger);

		var path = "/img/garretmenu/";
		var suffix = "_onmouse";
		target = document.getElementById(target);

		var onmouse_image = path + name + suffix + ".gif";

		e.onmouseover = function () {
			target.style.backgroundImage = "url(" + onmouse_image + ")";
		};

		var revert = function () {
			var img = path + name + ".gif";
			target.style.backgroundImage = "url(" + img + ")";
		};
		e.onmouseout = revert;
		e.onclick =  revert;
		// Traditional preload.
		(new Image()).src = onmouse_image;
	}
	new Hover("gm-profile-opentrigger", "gm-profile", 'profile-bg');
	new Hover("gm-business-contact-opentrigger", "gm-business-contact-container", 'b_contact-bg');
	


	var messageStore = {
		"": function () {
			alert( "ご用件を選択してください");
			return 1;
		},
		"Request-Manga": function () {
			alert("申し訳ありませんが、新規の漫画執筆は只今休止中です。")
			return 1;
		},
		"Request-others": function () {
			return 0;
		},
		"Fan_mail": function () {
			alert("ファンメールはコーラスのHPからお願いします！");
			var u = "http://chorus.shueisha.co.jp/mail/";
			window.open(u,"_blank");
			return 1;
		}
	};

	document.forms["gm-business-contact-form"].selectID.onchange = function (e) {
		var opt = this.options[ this.selectedIndex ];
		var f = messageStore[opt.value];

		this.form.mail_post.disabled = f.call(this);
	};

	var id2jaName = {
		mail_text: "本文",
		mail_author: "お名前",
		mail_email: "メールアドレス"
	};

	var mailForm = document.forms["gm-business-contact-form"];
	mailForm.onsubmit = function (e) {

		if ( !mailForm.mail_email.value.match( /^(.+?)\@([\w\-]+\.)+([a-z]{2,6})$/i ) ) {
			alert("メールアドレスはすべて半角英数字で正しく入力してください。");
			return false;
		}

		var b = true;
		  for ( var name in id2jaName ) {
			  if ( mailForm[name].value.match(/^\s*$/)  ) {
					alert( id2jaName[name] + "を入力してください。");
					b = false;
			   }
		  }

        if ( mailForm.selectID.selectedIndex == 0 ) {
            alert('ご用件を選んでください。');
            b = false;
        }

		return b;
	};




} );
