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

function resize_attached_images() {
	var maxWidth = parseInt($('meta').getStyle('width'));
	var images = $('meta').getElementsByTagName('img')
	for (i in images) {
	  var width = images[i].width;
	  var height = images[i].height;
	  if (width > maxWidth) {
	    ratio = maxWidth / width;
	    images[i].width = maxWidth;
	    images[i].height = height * ratio;
	  }
	}
}

function select_text() {
	el = document.getElementById('permalink');
	if (el) {
		if (el.createTextRange) {
			var oRange = el.createTextRange();
	   		oRange.moveStart("character", 0);
			oRange.moveEnd("character", el.value.length);
			oRange.select();
		}
		else if (el.setSelectionRange) {
			el.setSelectionRange(0, el.value.length);
		}
		el.focus();
	}
}

function clear_default_text() {
	var active_color = '#000'; // Colour of user provided text
	var inactive_color = '#999'; // Colour of default text
    var default_values = new Array();
    $$("input.default-value").each( function (s) {
        $(s).setStyle({ color: inactive_color });
        $(s).observe( 'focus', function () {
            if (!default_values[s.id]) {
                default_values[s.id] = s.value;
            }
            if (s.value == default_values[s.id]) {
                s.value = '';
                $(s).setStyle({ color: active_color });
            }
            $(s).observe( 'blur', function () {
                if (s.value == '') {
                    $(s).setStyle({ color: inactive_color });
                    s.value = default_values[s.id];
                }
            });
        });
    });
}

Event.observe(window, 'load', resize_attached_images);
Event.observe(window, 'load', select_text);
Event.observe(window, 'load', clear_default_text);