//$.fn.delay = function(time, func) {
//  return $(this).each(function() {
//    setTimeout($(func), time);
//  });
//};

jQuery.fn.flashit = function(color, duration) {
  color = color || '250,250,140';
  duration = duration || 1500

  var current = this.css('background-color');
  this.animate({ backgroundColor: 'rgb(' + color + ')' }, duration / 2);
  this.animate({ backgroundColor: current }, duration / 2);
} 

function confirm_delete(text) {
  text = text || "削除しますか?"
  return confirm(text)
}

function delete_model(button, url) {
  if (!confirm_delete()) return
  
  form = $(button).parents('form')
  $(form).attr('action', url)
  $(form).submit()
}

function trim(an_array){
	var tmp = new Array();
	for(j=0; j < an_array.length; j++) {
	  an_array[j] = an_array[j] || ''
		if(an_array[j] != '')
			tmp[tmp.length] = an_array[j];
	}
	
	an_array.length = tmp.length;
	for(j=0; j < tmp.length; j++)
		an_array[j] = tmp[j];
	return an_array;
}

function disable_anchor(anchor) {
  $(anchor).attr('href', 'javascript:void(0)')
  $(anchor).attr('onclick', '')
  $(anchor).addClass('disabled')
}

function open_window(url, width, height) {
  var top = (screen.height - height) / 2
  var left = (screen.width - width) / 2
  var features = 'menubar=0, resizable=0, scrollbars=1, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left
  window.open(url, "_blank", features, false)
}

Object.size = function(obj) {
  var size = 0, key;    
  for (key in obj) {        
    if (obj.hasOwnProperty(key)) size++;    
  }    
  return size;
};

$.fn.show_ajax_response = function(options) {
  var settings = {
    text: '',
    error: false,
    hide_after_show: false
  }
  //extending options
  options = options || {};
	$.extend(settings, options);
        	
  $(this).find('.notify').remove()
  notify_box = $('<div />').addClass('notify hidden').html(options.text)
  if (options.error)
    $(notify_box).addClass('error')
  $(this).append($(notify_box))

  $(notify_box).fadeIn('normal', function() {
    if (options.hide_after_show)
      $(this).fadeOut(3000)
  })
}

$.fn.flash_new_item = function(new_item, input_fields) {
  $(this).append($(new_item))
  new_item_id = $(new_item).attr('id')

  $(this).find('#' + new_item_id)
    .css('opacity', 0)
      .animate({ backgroundColor: 'khaki', opacity: 1.0 }, 800)
      .animate({ backgroundColor: '#ffffff' }, 350, function() { this.style.removeAttribute('filter'); });

  if (input_fields != null && $(input_fields).length > 0) {
    $(input_fields).each(function(iloop, input) {
      $(input).val('')
    })
  } // end if
}

$.fn.reorder = function(url) {
  $(this).tableDnD({
    onDragClass: "on-drag",
    onDrop: function(table, row) {
      var ids = new Array()
      var rows = table.tBodies[0].rows;
      for (var iloop=0; iloop < rows.length; iloop++)
        ids[iloop] = rows[iloop].id

      $.post(url, {ids : ids.join(',')}, function(response) {
        if (!response.success)
          alert("順位変えられませんでした！")
      }, 'json')
    }
  })
}

jQuery.preload_images = function(images) {
  image_obj = new Array();
  for (var i = 0; i < images.length; i++) {
    image_obj.src = images[i];
  }
}

$('span.radio').click(function(event) {
  if (event.target.tagName.toLowerCase() == 'span')
    $(this).find('input[type=radio]').click()
})