$(function() {

	// 最大文字数セット
	$( 'input.year' ).each( function(){
		$( this ).attr( 'maxlength', 4 );
	});
	$( 'input.month, input.day' ).each( function(){
		$( this ).attr( 'maxlength', 2 );
	});
	$( 'input.zip-u' ).each( function(){
		$( this ).attr( 'maxlength', 3 );
	});
	$( 'input.zip-l' ).each( function(){
		$( this ).attr( 'maxlength', 4 );
	});
	$( 'input.phone' ).each( function(){
		$( this ).attr( 'maxlength', 4 );
	});
	$( 'input.magid' ).each( function(){
		$( this ).attr( 'maxlength', 10 );
	});
	$( 'input.userid, input.userpass' ).each( function(){
		$( this ).attr( 'maxlength', 32 );
	});
	$( 'var' ).each( function(){
		$( 'input', $( this ).parent().parent() ).attr( 'maxlength', $( this ).text() );
	});

// input button
	$( '#lnavi li:not([class~="sel"]), :submit, :button' ).hover( function() {
		$( this ).toggleClass( 'hover' );
	}, function(){
		$( this ).removeClass( 'hover' );
	});

// ローカルナビサポート
	$( '#lnavi li:not([class~="sel"]), .get' ).hover( function() {
		$( this ).toggleClass( 'hover' );
	}, function(){
		$( this ).removeClass( 'hover' );
	});


//矢印処理
	$( '#search-keyword #wrapper #sidebar #search label' ).each( function(){
		$( this ).append( '<div class="pointer"><img src="../img/search-arrow.gif" /></div>' );
	});

//矢印処理
var offsetX = 0;
var offsetY = 0;

	$( '#search input.research' ).each( function( idx, obj ) {
		var adNo = 'no-' + idx;
		$( this ).attr( 'id', adNo );
		var adX = $( this ).position().left;
		var a2 = $( this )[0].offsetWidth;
		adX += a2;
		var adY = $( this ).position().top + offsetY;
		var ad = '<div class="re-pointer"><img src="../img/re-pointer.gif" /></div>';
		$( ad ).appendTo( '#search' ).css({ top: adY + 'px', left: adX + 'px' }).addClass( adNo );
	});

// 角丸処理
	$( '.mag-reg, #result, #charge-ossm' ).each( function(){
		$( this ).append( '<div class="box-foot-l"></div><div class="box-foot-r"></div>' );
	});

	$( '#ranking, #prsite, #other-cat, #cat-list, .cat-group' ).each( function(){
		$( this ).append( '<div class="box-head-l"></div><div class="box-head-r"></div><div class="box-foot-l"></div><div class="box-foot-r"></div>' );
	});

	var nowYear = new Date().getFullYear();
	$( 'li:first', '.mag-rule ul' ).addClass( 'first-child' );
	$( 'li:first', '.mag-unit .mag-cat' ).addClass( 'first-child' );
	$( 'dd:last', '.mag-unit .mag-bn' ).addClass( 'last-child' );
	$( '.page-navi:last' ).addClass( 'last-child' );
	$( '#ranking dd:last' ).addClass( 'last-child' );
	$( '#footer address span' ).append( nowYear );
	$( 'li:first', '#footer ul' ).addClass( 'first-child' );
	$( 'img:last', '.mag-unit .mag-title' ).addClass( 'last-child' );
	$( '.meta-author a' ).click( function(){
		$( this ).toggleClass( 'open' );
		$( '.meta-detail' , $( this ).parent().parent().parent() ).toggle();
		return false;
	});
	$( '.meta-close' ).click( function(){
		$( '.meta-author a', $( this ).parent().parent() ).toggleClass( 'open' );
		$( this ).parent().toggle();
		return false;
	});
	
//	$( '.cat1 li ul' ).hide();
//	$( '> li > a', '.cat1, .cat2' ).click( function() {
//		$( '+ ul', this ).slideToggle( 'fast' );
//		$( this ).toggleClass( 'open' );
//		return false;
//	});
	

	var searchComment = "(キーワードを入力)";
	var searchMessage = "キーワードを入力してください。";

	$( "#keyword" ).attr( "value", searchComment );
	$( "#keyword" ).addClass( 'passive' );
	$( "#keyword" ).focus( function() {
		$( this ).removeClass( 'passive' );
		var keyword = this.value;
		if( keyword == searchComment ) {
			this.value = "";
		}
	});
	$( "#keyword" ).blur( function() {
		var keyword = this.value;
		if( keyword == "" ) {
			this.value = searchComment;
			$( this ).addClass( 'passive' );
		}
	});
	$( "#search input[@type='submit']" ).click( function() {
		var keyword = $( "#keyword" )[0].value;
		if( keyword == "" || keyword == searchComment ) {
			alert( searchMessage );
			$( "#keyword" ).focus();
			return false;
		}
	});

	var mailFormat = /^[\+\-_a-zA-Z0-9][a-zA-Z0-9\?\+\-\._\/]*\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/;
	var regComment = '(メールアドレスを入力)';

	$( '.reg input.rdemail, .del input.rdemail' ).val( regComment );
	$( 'input.rdemail' ).addClass( 'passive' );
	$( 'input.rdemail' ).focus( function() {
		$( this ).removeClass( 'passive' );
		var rdemail = $( this ).val();
		if( rdemail == regComment ) {
			$( this ).val( '' );
		}
	});
	$( '.reg input.rdemail, .del input.rdemail' ).blur( function() {
		var rdemail = $( this ).val();
		if ( rdemail == '' ) {
			$( this ).val( regComment ).addClass( 'passive' );
		}
	});
	$( 'form.reg, form.del' ).submit( function(){
		var rdemailObj = $( '.rdemail', this );
		var rdemail = rdemailObj.val();
		if ( rdemail == '' || rdemail == regComment ) {
			alert( 'メールアドレスを入力してください。' );
			rdemailObj.focus();
			return false;
		} else if ( rdemail.search( mailFormat ) ){
			alert( 'メールアドレスの書式が不正です。' );
			rdemailObj.focus();
			return false;
		}
	});
	$("ul.cat li:last-child, ul.cat-set li:last-child").addClass( 'end' );

	// class セット
	$( 'li:first', '#topic-path, #footer ul, #footer .genre' ).addClass( 'first-child' );
	$( 'li:last', 'fieldset' ).addClass( 'last-child' );
	$( '.block:first', '#content .split' ).addClass( 'first-child' );
	$( '.block:last', '#content .split' ).addClass( 'last-child' );
	$( 'input' ).each( function() {
		$( this ).addClass( $( this ).attr( 'type' ) );
	});

	$( 'div.search-more' ).hide();
	$( 'a.search-more' ).toggle( function(){
		$( 'div.search-more' ).slideDown();
		$( 'span', this ).text( '▲' );
	}, function() {
		$( 'div.search-more' ).slideUp();
		$( 'span', this ).text( '▼' );
	});

// カレンダー操作
	calControl();

// MAGPAY読み込み
	var nowDate = new Date();
	nowDate = nowDate.getHours();
	$( '#magazine' ).each( function() {
		$( '#magpay-data' ).load( '../data/magpay.txt?' + nowDate );
	});

});

function calControl() {
	$( 'thead :button', 'table.cal-day' ).toggle( function(){
		var obj = '.' + $( this ).parent().attr( 'class' );
		$( ':checkbox', $( obj ) ).attr( 'checked', 'checked' );
	}, function(){
		var obj = '.' + $( this ).parent().attr( 'class' );
		$( ':checkbox', $( obj ) ).attr( 'checked', '' );
	});

	$( 'tbody :button', 'table.cal-day' ).toggle( function(){
		var obj = '.' + $( this ).parent().parent().attr( 'class' );
		$( ':checkbox', $( obj ) ).attr( 'checked', 'checked' );
	}, function(){
		var obj = '.' + $( this ).parent().parent().attr( 'class' );
		$( ':checkbox', $( obj ) ).attr( 'checked', '' );
	});

	$( ':button', 'table.cal-num' ).toggle( function(){
		$( ':checkbox', 'table.cal-num' ).attr( 'checked', 'checked' );
	}, function(){
		$( ':checkbox', 'table.cal-num' ).attr( 'checked', '' );
	});
}

