$(document).ready(function() {
  
  $('textarea.tinymce').tinymce({
    script_url : '/javascripts/tiny_mce/tiny_mce.js',
    theme : "advanced",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    plugins: "safari,autoresize,inlinepopups,advimage",
    file_browser_callback: 'file_browser',
    relative_urls: false
  });
  
});

win_width = $(window).width();
win_height = $(window).height();

function file_browser (field_name, url, type, win) {
  tinyMCE.activeEditor.windowManager.open({
      file : '/hornet/media?tinymce=true',
      title : 'Hornet Media Library',
      width : win_width*.95,  // Your dimensions may differ - toy around with them!
      height : win_height*.85,
      resizable : "yes",
      inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
      close_previous : "no",
      popup_css : false
  }, {
      window : win,
      input : field_name
  });
  return false;
}

$('.add-attachment').concrete({
  onclick: function() {
    $(this).parent().siblings('.attachment:hidden:first').show()
    return false
  }
})

$('.select-attachment').concrete({
  onclick: function() {
    hornet_media($(this).siblings('select').val(), $(this).parent('.attachment'));
    return false;
  }
})

$('.remove-attachment').concrete({
  onclick: function() {
    $(this).siblings('input._delete').val('1')
    $(this).parent('.attachment').hide()
    return false;
  }
})

$('.attachment select').concrete({
  onmatch: function() {
    $(this).hide_select()
  },
  onchange: function() {
    $(this).hide_select()
  },
  hide_select: function() {
    if ($(this).val() == 'subscribe-form' || $$(this).val() == 'contact-form') {
      $(this).siblings('.select-attachment').hide();
    }
  }
})