
if ( typeof jQuery == 'function' ) var $j = jQuery.noConflict() ;

(function($) {
	$.fn.addSelected = function( target, delimiter ){
		if ( typeof target == 'undefined' ) return ;
		if ( typeof delimiter == 'undefined' ) var delimiter = '|' ;
		return this.each( function(){
			var targetVal = $( target ).val() ;
			if ( this.value != '' ) {
				var str = ( targetVal == '' ) ? this.value : targetVal + delimiter + this.value ;
				$( target ).val( str ) ;
			}
			return ;
		}) ;
	};
})(jQuery);

function search_style( word ){
	switch( word ) {
		case '' :
			return { 'color':'#999' } ;
			break;
		case 'キーワードを入力してください' :
			return { 'color':'#999' } ;
			break;
		default :
			return { 'color':'#000' } ;
	}
}

function col_check_on_off( checkbox, target ){
	var check_val = ( checkbox.checked ) ? true : false ;
	$j.each( $j( '#[ id *= "' + target + '" ]' ), function(){
		this.checked = check_val ;
	});
}

function link_list_DnD( target, tr , position ){
	var id ;
	$j( target ).tableDnD({
		onDrop: function( table, row ) {
			 $j.each( $j( tr ), function( i, value ){
				id = ( this.id ) ? this.id.split( '_' ) : null ;
				if ( id ) $j( position + parseInt( id[ id.length - 1 ] ) ).val( i ) ;
			 });
		},
		dragHandle: 'dragHandle'
	 });
	 $j( 'td:first', tr ).hover(function() {
		$j( this ).addClass('showDragHandle') ;
	 }, function() {
		$j( this ).removeClass('showDragHandle') ;
	 });
}

function getReview( ArticleId, offset ){
	var parameter = 'id=' + ArticleId + '&offset=' + offset ;
	var url = base_url + 'get_review.php' ;
	var successCallback = function ( request, status ) {
		if ( status != 'error' && request != '' ){
			var el = $j( '#Review' ) ;
			el
				.hide()
				.html( request )
				.fadeIn( 1500 ) ;
			var t = $j.support.boxModel ? navigator.appName.match(/Opera/) ? 'html' : 'html,body' : 'body' ;
			$j( t ).animate({ scrollTop: el.offset().top - 50 }, { duration: 1500 }) ;
		}
	} ;

    $j.ajax({ type: 'GET', url: url , data: parameter, async : true, dataType : 'json', success : successCallback }) ;
	return false;
}

$j( function($){
	if ( mode == '' || mode == 'edit' ) {
		var ArticleId = $.getArticleId() ;
		$( '#moshimo_sozai' ).getSozai( ArticleId ) ;
		var searchid = $( '#word' ) ;
		var default_word = 	'キーワードを入力してください' ;
		var word = ( searchid.val() == '' ) ? default_word : searchid.val() ;
		searchid
			.val( word )
			.css( search_style( word ) )
			.bind({
				focus : function(e){
					if ( this.value == default_word ) $( this ).val('') ;
					$( this )
						.css( { 'color':'#000' } )
						.select() ;
				},
				blur : function(e){
					if ( this.value == '' ) $( this ).val( default_word ) ;
					$( this ).css( search_style( this.value ) ) ;
				},
				keydown : function(e){
					if ( e.keyCode == 13 && searchid.val() == '' ) $( this ).val('') ;
				}
			}) ;
		$( '#btn' ).click(function(){
			if ( searchid.val() == default_word ) searchid.val('') ;
		}) ;
		$( '#category, #sortorder, #field' ).jQselectable({
			set: 'fadeIn',
			out: 'fadeOut',
			inDuration: 'slow',
			outDuration: 'slow',
			opacity: .9

		});
		$( '#alldel, #[ id ^= "del_mylink_" ], #del_item, #[ id ^= "del_pickup_" ]' ).click(function() {
			return confirm('削除していいですか?');
		});
		$( '#[ id ^= "review-prev" ], #[ id ^= "review-next" ]' ).live( 'click', function(){
			var offset = parseInt( this.id.slice( 11 ) ) ;
			getReview( ArticleId, offset );
			return false;
		});
	}
	if ( mode == 'edit' ) {
		if( $( '#ItemImage_list tr' ).size() > 1 ) link_list_DnD(  '#ItemImage_list' ,'#ItemImage_list tr' ,'#position_' );
		$( '#tags_selector_val' ).live( 'change', function(){
			$( this ).addSelected( '#tags' ) ;
		}) ;
	}
	if ( mode == 'config' ) {
		$( '#col_action' ).click(function() {
			col_check_on_off( this, 'col_action_' );
		});
		if( $( '#link_list tr' ).size() > 1 ) link_list_DnD( '#link_list' ,'#link_list tr' ,'#position_' );
		$( '#delete, #delete_tags' ).click(function() {
			return confirm('削除していいですか?');
		});
		$( '#request_password' ).click( function(){
			if ( confirm('「はい」をクリックすると、メールでパスワード再発行します。再発行が不要な場合はキャンセルしてくだい。') ) location.href= $( '#request_password_url' ).val() ;
		});
		$( '#select_db' ).change(function() {
			switch ( this.value ) {
				case 'sqlite2' :
				case 'sqlite3' :
					$( 'tr, #db_define' ).not( '#select_table, #db_prefix, #export' ).hide() ;
					$( '#select_table, #db_prefix, #export' ).fadeIn( 1500 ) ;
					break ;
				default:
					$( 'tr, #db_define' ).fadeIn( 1500 ) ;
			}
			if ( this.value == '0' ) $( '#db_define' ).hide() ;
			else $( '#db_define' ).fadeIn( 1500 ) ;
		});
		$( '#export' ).click( function(){
			return confirm('「はい」をクリックすると、エクスポート先のデータをすべて削除し、エクスポートを実行します。データが多い場合は、タイムアウトでエラーになってしまう場合があります。その場合は、エクスポートするテーブルの数を減らして試してください。');
		});
	}
});


