$(function() {
	$("table.tblInputForm .noticetip").easyTooltip();

	$.fn.clearForm = function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		  return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
		  this.value = '';
		else if (type == 'checkbox' || type == 'radio')
		  this.checked = false;
		else if (tag == 'select')
		  this.selectedIndex = 0;
	  });
	};
	
	// 新規登録画面 - 媒体種別選択
	if ($('#IpkUserMediaKind,#IpkUserMediaKindSub,#mediaKindSetJson').length == 3) {
		var mediaKindSet  = eval('(' + $('#mediaKindSetJson').text() + ')');
		var targetElement = $('#IpkUserMediaKindSub');
		var orgElement    = targetElement.clone();
		$('#IpkUserMediaKind').change(function(e){
			var orgClone = orgElement.clone();
		
			targetElement.replaceWith(orgClone);
			targetElement = orgClone;
			if (mediaKindSet[e.currentTarget.value]) {
				orgClone.children()
			            .not('option[value=' + mediaKindSet[e.currentTarget.value].join('],option[value=') + ']')
				        .remove();
			}
		}).change();
	}
	
	// Press Kit素材(一覧)用イベント - タブの色切り替え
	$('div.itemData > ul > li').click(function(e){
		$(e.currentTarget).siblings('li').toggleClass('active', false);
		$(e.currentTarget).toggleClass('active', true);
			
		return false;
	});
	
	// Press Kit素材(一覧)用イベント - タブのサブメニューの表示切替(動画のみ)
	var currentObj = null;
	$('div.itemData > ul > li').mouseover(function(e){
		if (!currentObj) {
			currentObj = e.currentTarget
		} else if (currentObj == e.currentTarget) {
			return;
		}
		$(e.currentTarget).find('.toggleTarget').toggleClass('enable', true);
	});
	$('div.itemData > ul > li').mouseout(function(e){
		try {
			// 不要なmouseoutイベントを抑制(IE7でcssの:hoverが動作しないための対策)
			$(e.relatedTarget).parents().each(function(){
				if (this == currentObj) {
					throw 1;
				}
			});
			
			// IEではli要素とabsoluteで表示しているその子要素の間に要素的な隙間があるらしく、
			// li要素->子要素のマウス移動時のmouseoutイベントを抑制するための対策
			$(e.currentTarget).parent().each(function(){
				if (this == e.relatedTarget) {
					throw 1;
				}
			});
		} catch (e) {
			return;
		}
		$(e.currentTarget).find('.toggleTarget').toggleClass('enable', false);
		currentObj = null
	});
	
	// Press Kit素材(一覧)用イベント - 素材情報の非表示切り替え
	$('div.itemData ul li a').click(function(e){
		var id = e.currentTarget.name;
		if (!id) {
			e.stopPropagation()
			return false;
		}
		
		$('#created-' + id).siblings('span[id^=created-]').css('display', 'none');
		$('#thumbnail-' + id).siblings('div[id^=thumbnail-]').css('display', 'none');
		$('#data-' + id).siblings('div[id^=data-]').css('display', 'none');
		$('#buttons-' + id).siblings('ol[id^=buttons-]').css('display', 'none');
	
		$('#created-' + id).css('display', 'inline');
		$('#thumbnail-' + id).css('display', 'block');
		$('#data-' + id).css('display', 'block');
		$('#buttons-' + id).css('display', 'block');
			
		return true;
	});
	
	// マスコミ試写用 - タブ切り替え
	$('#contentsArea div.previewList div.cinemaDescriptions').each(function(){
		$(this).find('ul > li:eq(0)').addClass('active');
		$(this).find('table:gt(0)').css('display', 'none');
		
		// 詳細ページ用
		$(this).parents('div.previewList ~ div.attention > div:gt(0)').css('display', 'none');
	});
	$('#contentsArea div.previewList div.cinemaDescriptions > ul').click(function(e){
		$(e.currentTarget).find('li > a').each(function(index){
			if (this == e.target) {
				$(e.currentTarget).parent().find('table').css('display', 'none');	
				$(e.currentTarget).parent().find('table:eq(' + index + ')').css('display', '');
				$(e.target).parent('li').siblings('li').removeClass('active');
				$(e.target).parent('li').addClass('active');
				
				// 詳細ページ用
				$(e.currentTarget).parents('div.previewList ~ div.attention > div:not(:eq(' + index + '))').css('display', 'none');
				$(e.currentTarget).parents('div.previewList ~ div.attention > div:eq(' + index + ')').css('display', '');
			}
		});
	});
	
	// 郵便番号
	$('#IpkUserPost2').keyup(function(){
		AjaxZip2.JSONDATA = '/ajaxzip2/data';
		AjaxZip2.zip2addr('data[IpkUser][post1]',
		                  'data[IpkUser][pref]',
						  'data[IpkUser][address]',
						  'data[IpkUser][post2]',
						  'data[IpkUser][address]');
	});
});

function do_submit(mode,mode_option,form) {

if(form == null){ form = 0; }

	document.forms[form].mode.value = mode;

	if (mode_option) {
		document.forms[form].mode_option.value = mode_option;
	}
    document.forms[form].submit();
}
