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

/*
 *  interval
 *  by Steven Wicklund <[EMAIL PROTECTED]>
 *  A setInterval wrapper for jQuery
 *  So this works for 1 timer interval, but the currentlyExecuting singleton will likely cause issues
 *  if there is more than one interval per page...
 */
/*
 * onTimerEvent courtesy Prototype PeriodicalExecuter()
 * (c) 2005 Sam Stephenson <[EMAIL PROTECTED]>
 * (MIT license see:  http://prototype.conio.net/)
 */
jQuery.extend( {
	interval:function(frequency, callback) {
		var interval = {
			executing: false,
			callback: callback,
			onTimerEvent: function() {
				if (!interval.executing) {
					try {
						interval.executing = true;
						interval.callback();
					} finally {
						interval.executing = false;
					}
				}
			},
			stop: function() {
				clearInterval(interval.timer)
			}
		};
		interval.timer = setInterval(interval.onTimerEvent, frequency * 1000);
		return interval;
	}
});
Featured = {
	index: 0,
	length: 0,
	pause: false,
	flip: function(next) {
		next = typeof next === "undefined" ? (Featured.index + 1 == Featured.length ? 0 : Featured.index + 1) : next;
		if (!Featured.pause && next != Featured.index) {
			$('.special_' + Featured.index).fadeOut("slow");
			$('.special_dot_' + Featured.index).removeClass("selected");
			$('.special_' + next).fadeIn("slow");
			$('.special_dot_' + next).addClass("selected");
			Featured.index = next;
		}
	}
};
RestfullDelete = function() {
	alert(this);
	var f = document.createElement('form');
	f.style.display = 'none';
	this.parentNode.appendChild(f);
	f.method = 'POST';
	f.action = this.href;
	var m = document.createElement('input');
	m.setAttribute('type', 'hidden');
	m.setAttribute('name', '_method');
	m.setAttribute('value', 'delete');
	f.appendChild(m);
	f.submit();
	return false;
};
(function($) {
	$.fn.sections = function() {
		this.each(function(element) {
			element = $(this);
			element.find('.section h3').click(function(e) {
				$('a', this).blur();
				$(this).parents('.section').toggleClass('section_active');
				e.preventDefault();
				return false;
			});
			element.find('.inline_dialog > a').click(function(e) {
				$(this).blur().toggle().parent().children('div').toggle().find(':text').val('').focus();
				e.preventDefault();
				return false;
			});
			element.find('.inline_dialog a.cancel').click(function(e) {
				$(this).blur().parents('.inline_dialog').children('a').click();
				e.preventDefault();
				return false;
			});
		});
	},
	$.fn.search_in = function(expr) {
	}
})(jQuery);

(function($) {
	$.fn.loan_calculator = function() {
		this.each(function(element) {
			element = $(this);
			element.find('input').keyup(function(e) {
				calculate();
			});
			element.find('input,select').change(function(e) {
				calculate();
			});
			calculate();
		});
		function calculate() {
			var price = to_safe_integer('#price');
			var rate  = to_safe_float('#rate') / 1200;
			var trade = to_safe_integer('#trade');
			var term  = $('#term').val();
			var payment = (price - trade) * rate / (1 - (Math.pow(1/(1 + rate), term)));
			if(isNaN(payment) || (payment <= 0)) {
				payment = 'N/A';
			} else {
				payment = '$' + Math.round(payment);
			}
			$('#payment').val(payment);
		}
		function to_safe_integer(element) {
			element = $(element);
			var value = parseInt(element.val().replace(/[^\d]/g, ''), 10);
			if(isNaN(value)) {
				value = 0;
			}
			element.val(value);
			return value;
		}
		function to_safe_float(element) {
			element = $(element);
			var value = parseFloat(element.val());
			if(isNaN(value)) {
				value = 0.0;
			}
			var str = value + "";
			if(str == '0') {
				str = ''
			} else if(element.val() == str + '.') {
				str = str + "."
			}
			element.val(str);
			return value;
		}
	}
})(jQuery);

