{% load i18n %}
<script type="text/javascript">

$(document).ready(function(){
	try {
		$("a.colorbox-form").colorboxify({
			close_popup_and_media_slide: function() {
      $.colorbox.close();
      $("#coopbar_medialibrary").click();
			}
		})
	} catch (err) {
		//console.log("err: "+err);	
	}
	
	if ($(".coop-fragment-type").length) {
		var icon = $(".coop-bar .icon.if-fragments").show();
		icon.next(".separator").show();
	} else {
		var icon = $(".coop-bar .icon.if-fragments").hide();
		icon.next(".separator").hide();
	}

{% if edit_mode %}
	$(".coop-bar a.slide").pageSlide({width:'350px', direction:'right'});
	
	$("form#cms_form input[type='submit']").hide();
    $('#coopbar_save').click(function(event) {
        $("form#cms_form").submit();    
        event.preventDefault();
    });
	
	var toggle_save = function() {
	    if (!$(".show-dirty").is(":visible")) {
			$(".show-clean").hide();
			$(".show-dirty").show();
			
			$("a.alert_on_click").each(function(idx, elt) {
				if (!$(elt).hasClass('toogle-save-active')) {
					$(elt).addClass('toogle-save-active');
					$(elt).bind('click', function(event) {
						return confirm("{% trans 'Your modifications are not saved and will be lost. Continue?' %}");
					});
				}
			})
		}
		{% if draft %}
		$(".publish").hide();
		{% endif %}
	};
	$(".show-dirty").hide();
  {% if html_editor == 'aloha' %}
  Aloha.bind('aloha-editable-deactivated', function(event, eventProperties){
        toggle_save();
	});
  {% endif %}
	$(".inline-editable").keypress(function() {
		toggle_save();
	});
	
	$("a.update-logo img").change(toggle_save);
	$(".article select").change(toggle_save);
	$(".article input").change(toggle_save);
	
	var resize_thumbnail = function (the_obj) {

    $(".inline-editable img.library-thumbnail").each(function(idx, elt) {
      $(elt).removeClass("library-thumbnail");
      $(elt).attr("src", $(this).attr("rel"));
      $(elt).removeAttr('rel');
    });

    $(".inline-editable img.library-document").each(function(idx, elt) {
      var doc_url = $(elt).attr('rel');
      var doc_title = $(elt).attr('title');
      var icon_url = $(elt).attr('src');
      var doc_ext = icon_url.replace(/\\/g,'/').replace( /.*\//, '').replace('.png', '');
      var link = '<a href="'+doc_url+'" target="_blank" class="doc-link" rel="'+doc_ext+'">'+'<img src="'+icon_url+'" /><span class="doc-title">'+doc_title+'</span></a>';
      $(link).insertAfter($(elt));
      $(elt).remove();
    });

    //force the focus in order to make sure that the editable is activated
    //this will cause the deactivated event to be triggered, and the content to be saved
    the_obj.focus();
  };

	{% if html_editor == 'aloha' %}
  //resize image when dropped in the editor
  //GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
  Aloha.bind('aloha-editable-created', function(event, editable){
      var the_obj = editable.obj;
      jQuery(editable.obj).bind('drop', function(event){
        setTimeout(function() {
          resize_thumbnail(the_obj);
        }, 100);
      });
  });
  {% endif %}
{% endif %}
});
</script>
