// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//http://www.ajay.ws/2006/12/14/tinymce-rails-rjs-tiny_mce-plugin-let-them-play-together-nicely

//////////////////////////////////////////////////////
// Lowpro function to allow for unobtrusive javascript
Event.addBehavior({
 'a.remote'    : Remote.Link,
 'form.remote' : Remote.Form,
 'input.remote': function() {Remote.Form.attach(this.up('form'));} 
});
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
// Better nested forms via http://railscasts.com/episodes/197-nested-model-form-part-2
function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".fields").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g")
  $(link).insert({
    bottom: content.replace(regexp, new_id)
  });
}
//////////////////////////////////////////////////////

function toggle_fuel(selector_div, fuel_div_title) { 
  if (selector_div.hasClassName('available')){
    selector_div.removeClassName('available');
    disable_elements(fuel_div_title);
    Effect.BlindUp(fuel_div_title, {duration: 0.5});
  } else {
    selector_div.addClassName('available');
    enable_elements(fuel_div_title);
    Effect.BlindDown(fuel_div_title, {duration: 0.5});
    $(fuel_div_title).style.height = '';
  }
}

function disable_elements(fuel_div_title) { 
  $(fuel_div_title).descendants().each(function(node){
    if (('SELECT INPUT').include(node.tagName)) node.disable();
  });
}

function enable_elements(fuel_div_title) { 
  $(fuel_div_title).descendants().each(function(node){
    if (('SELECT INPUT').include(node.tagName)) node.enable();
  });
}


var CommunityEngine = {	
	resize_image: function(img, options ) {
		this.options = options || {};

		var img_width = img.offsetWidth;
		var img_height = img.offsetHeight;
		var img_aspect_ratio = Math.round((img_width / img_height) * 100) / 100;

		var max_width = this.options['max_width'] || 120;
		var max_height = this.options['max_height'] || 90;
		var max_aspect_ratio = Math.round((max_width / max_height) * 100) / 100;

	//	alert("orig image size is " + img_width + "x" + img_height + "\n" + "aspect ratio is " + img_aspect_ratio + "\n\n" + "max image size is " + max_width + "x" + max_height + "\n" + "max aspect ratio is " + max_aspect_ratio);

		var new_img_width = 0;
		var new_img_height = 0;
		var new_aspect_ratio = 0;

		// if no resize needed
    if (img_width < 120 && img_height < 90) {
            new_img_width = img_width;
            new_img_height = img_height; 

		// if wider
		} else if (img_aspect_ratio > max_aspect_ratio) {
			new_img_width = max_width;
			new_img_height = Math.round(new_img_width / img_aspect_ratio);

		// if taller
		} else if (img_aspect_ratio < max_aspect_ratio) {
			new_img_height = max_height;
			new_img_width = Math.round(new_img_height * img_aspect_ratio);

		// equal
		} else {
			new_img_width = max_width;
			new_img_height = max_height;
		}

		img.style.width = new_img_width + "px";
		img.style.height = new_img_height + "px";
		new_aspect_ratio = Math.round((new_img_width / new_img_height) * 100) / 100;
	}
}