/**
 * This source code is part of the Midnatt Content Management Framework.
 *
 * Copyright (c) Midnatt.org | http://www.midnatt.org | mail@midnatt.org
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Constructor.
 */
Midnatt_Controller = function() {
	var self = this;
	//this.key = window.Midnatt.config.api_key;
}

/**
 * Load a Midnatt Form via ajax.
 * 
 * Loads a form template into a given element.
 * 
 * @param string name Name of the form.
 * @param string id Id of the target element.
 * @param function cb Optional callback function, called after the target 
 * 	element was filled with the form html.
 */
Midnatt_Controller.prototype.loadForm = function(name, id, callback) {
	var self = this;

	//$.post('/ajax//form/'+name, function(data) { // TODO IE?
	$.get('/ajax/form/'+name, function(data) {
		$('#'+id).html(data);
		//window.Midnatt.app.loadLibraries();
		if ($.isFunction(callback)) callback();
	});
}

/**
 * Bind a Midnatt Form via ajax.
 * 
 * Binds the form with the given id to an ajax call.
 * Uses jquery.form from http://malsup.com/jquery/form/
 * 
 * @param string id If of the form to bind.
 * @param function cb Optional callback function, called on ajaxForm success.
 */
Midnatt_Controller.prototype.bindForm = function(id, cb) {
	var self = this;

	$('#'+id).ajaxForm({
		beforeSubmit: function(arr, $form, options) {
			window.Midnatt.app.setBusy(true);
		},
		success: function(responseText, statusText, xhr, $form) {
			if ($.isFunction(cb)) cb(responseText);
			window.Midnatt.app.setBusy(false);
		}
	});
}

/**
 * Method Description 
 * 
 * @param var type ?
 * @param var content_type ?
 * @param var id ?
 * @param var value ?
 * @param var callback ?
 */
Midnatt_Controller.prototype.request = function(type, content_type, id, value, callback) {
	var url = '/api/' + content_type + '/crud/' + type + '/';

	if (type != 'create') {
		if (content_type == 'options') {
			url += 'key/' + id;
		} else {
			url += 'id/' + id;
		}

		if (type == 'update') {
			url += '/value/' + value;
		}
	}

	$.getJSON(url, function(json) {
		if ($.isFunction(callback)) callback(json);
	});
}


// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery
jQuery.fn.extend({
insertAtCaret: function(myValue){
  return this.each(function(i) {
	if (document.selection) {
	  this.focus();
	  sel = document.selection.createRange();
	  sel.text = myValue;
	  this.focus();
	}
	else if (this.selectionStart || this.selectionStart == '0') {
	  var startPos = this.selectionStart;
	  var endPos = this.selectionEnd;
	  var scrollTop = this.scrollTop;
	  this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
	  this.focus();
	  this.selectionStart = startPos + myValue.length;
	  this.selectionEnd = startPos + myValue.length;
	  this.scrollTop = scrollTop;
	} else {
	  this.value += myValue;
	  this.focus();
	}
  })
}
});


