// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function showInPlaceForm(in_place_form_id, button_id) {
  Effect.SlideDown(in_place_form_id, { duration: 0.25 });

  if (button_id) {
    $(button_id).hide();
  }
};

function hideInPlaceForm(in_place_form_id, button_id) {
  Effect.SlideUp(in_place_form_id, { duration: 0.25 });

  if (button_id)
    $(button_id).show();
};

function showInPlacePanelWithSelector(el, direction, css_rule) {
  var e = $(el)[direction](css_rule || ".in_place_panel");
  
  if (e) {
    Effect.SlideDown(e, { duration: 0.25 });
  }
};

function hideInPlacePanelWithSelector(el, direction, css_rule) {
  var e = $(el)[direction](css_rule || ".in_place_panel");
  
  if (e) {
    Effect.SlideUp(e, { duration: 0.1 });
  }
};



// Pop-Up Helpers
//
function openMarkdownSyntaxHelpPopup() {

  var help_popup = window.open("/markdown_syntax_help.html", "_blank",
  "location=0,menubar=0,scrollbars=0,status=0,toolbar=0,resizable=1,width=610,height=610");

  return help_popup;
};



// Form Helpers
//

function resetForm(el) {
  var e = $(el);

  if (e.tagName != "form") {
    e = e.up("form") || e.down("form");
  }
  
  if (e) {
    e.reset();
    
    var _error = e.down(".errorExplanation");
    if (_error) _error.remove();
  }
};

function submitForm(el) {
  var e = $(el);
  
  if (e.tagName != "form") {
    e = e.up("form") || e.down("form");
  }
  
  if (e)
    e.submit();
}


// Drag Helper
//

function toggleDragMode(el) {
  var list = $(el);
  
  list.toggleClassName("under_reordering");

  list.childElements().each(function(e){
    e.childElements().each(function(n){

      if (n.hasClassName("disable_with_drag")) {
        n.disabled = !n.disabled
      }
      else if (n.hasClassName("toggle_with_drag")) {
        n.toggle();
      }

    });
  });
};

function doToggleButton(el) {
  var e = $(el);
  
  var button = e.next(".toggle_button") || e.previous(".toggle_button");
  
  if (button) {
    e.toggle();
    button.toggle();
  }
};



// Translate Helper
//
function translateElement_small(source_id, lang_code) {
  var target_id = source_id + "_translated";
  var source = $(source_id);

  if (lang_code == "") {
    var container = $(target_id);
    container.innerHTML = "";
  }
  else {
    google.language.translate(source.innerHTML, "", lang_code, function(result) {
      if (!result.error) {
        var container = $(target_id);
        container.innerHTML = result.translation;
        new Effect.Highlight(target_id);
      }
      else {
        // show error message
        var container = $(target_id);
        container.innerHTML = "<div class='translate_error_message'>error: " + result.error.message + "</div>";
        new Effect.Highlight(target_id);
      }
    }.bind(this));
  }
};


function translateElement(source_id, lang_code) {
  var target_id = source_id + "_translated";
  var source = $(source_id);

  if (lang_code == "") {
    var container = $(target_id);
    container.innerHTML = "";
  }
  else {
    
    _translateIncrementally(source_id, 0, lang_code);
    
  }
};

function _translateIncrementally(source_id, position, lang_code) {
  
  var chunk_size = 256;
  
  var source = $(source_id);
  var target_id = source_id + "_translated";

  var source_text = source.innerHTML;
  var source_length = source_text.length;
  var next_position = position + chunk_size;

  if (position > source_length) return; // The End of Translation!


  // translate the next chunk
  var chunk = source_text.substring(position, next_position-1);
  
  google.language.translate(chunk, "", lang_code, function(result) {
    if (!result.error) {

      var container = $(target_id);
      
      if (position == 0) {
        container.innerHTML = result.translation;
        new Effect.Highlight(target_id);
      } else {
        container.innerHTML = container.innerHTML + result.translation;
      }

      // calling recursively
      _translateIncrementally(source_id, next_position, lang_code);
      
    }
    else {
      // show error message
      var container = $(target_id);
      container.innerHTML = container.innerHTML + "<div class='translate_error_message'>error: " + result.error.message + "</div>";
      new Effect.Highlight(target_id);
    }
  }.bind(this));

};





// assets
var __checkAsset = function(cb) {
	cb.checked = true;
	var parentLi = $(cb).up("li.asset")
	Element.addClassName(parentLi, "selected");
};

var __uncheckAsset = function(cb) {
	cb.checked = false;
	var parentLi = $(cb).up("li.asset")
	Element.removeClassName(parentLi, "selected");
};


var selectAllAsset = function(_type) {
	$$('input.asset_select_helper').each(function(cb) {
    if(_type) {
      if(new RegExp(_type, "i").test(cb.className)) __checkAsset(cb);
      else __uncheckAsset(cb);
    } else {
			__checkAsset(cb)
    }
	});
};

var deselectAllAsset = function() {
	$$('input.asset_select_helper').each(function(cb) {
		__uncheckAsset(cb);
	});
};

var reverseAllSelection = function() {
	$$('input.asset_select_helper').each(function(cb) {
		if(cb.checked) __uncheckAsset(cb);
		else __checkAsset(cb);
	});
};

var collectSelectedAssets = function() {
  var selected = [];
	$$('input.asset_select_helper').each(function(cb) {
    if(cb.checked) selected.push(/_([0-9]+)_/.exec(cb.id).last());
  });

	return selected;
};



// Message Notification
var selectAllNotification = function() {
	$$('input.notification_select_helper').each(function(cb) {
	  cb.checked = true;
	});
};

var deselectAllNotification = function() {
	$$('input.notification_select_helper').each(function(cb) {
	  cb.checked = false;
	});
};

var reverseAllNotification = function() {
	$$('input.notification_select_helper').each(function(cb) {
	  if (cb.checked) cb.checked = false;
	  else cb.checked = true;
	});
};




// Asset Import Widget
//
var MAX_ASSET_WIDTH = 640;

function importAssetAsImage(asset_url, asset_name, asset_width, cb_fn) {
  var tpl = new Template('\n![#{title}](#{url}#{option})\n');
	var asset_option = (Number(asset_width) > MAX_ASSET_WIDTH) ? "|width=" + MAX_ASSET_WIDTH : "";
  var asset_data = {title: asset_name, url: asset_url, option: asset_option };

  var result = tpl.evaluate(asset_data);
	cb_fn(result);
};

function importAssetAsLink(asset_url, asset_name, asset_width, cb_fn) {
  var tpl = new Template('\n[#{title}](#{url})\n');
	var asset_option = (Number(asset_width) > MAX_ASSET_WIDTH) ? "|width=" + MAX_ASSET_WIDTH : "";
  var asset_data = {title: asset_name, url: asset_url, option: asset_option };

  var result = tpl.evaluate(asset_data);
	cb_fn(result);
};

function toggleNewAssetForm(target_id, form_id) {
  var target_element = $(target_id);
  var form_element = $(form_id);
  
  if (form_element.visible()) {
		Effect.Fade(form_element, { duration: 0.1 });
	}
	else {
    var offset = target_element.cumulativeOffset();
    
    form_element.absolutize();
		form_element.style.left = offset[0] + "px";
		form_element.style.top = offset[1] + "px";
		
		Effect.Appear(form_element, { duration: 0.1 });
	}
}





