/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: Sets up an extremely easy to adjust MVC with angular.js
@ngmodel: an object that is visible to the angular app on the page,
	changing ngmodel will affect the html template in real-time if it is
	under the control of the angular rootScope, thanks to watch.js.
@htmlmodel: reference/alias of ngmodel
@ng: reference/alias of ngmodel
*******************************************************************************/

var ngmodule = angular.module('html', ['ngAnimate']);
			
var ngmodel = {};

if (0) watch(ngmodel, function(){
		ngModel(ngmodel, true); 
	}, 50, true);
	//}, Number.MAX_VALUE, true);
	
var htmlmodel = ngmodel;
var ng = ngmodel;

setInterval(function(){
	ngModel(ngmodel);
}, 80);

function ngModel(object, repeat){
	if (repeat) setTimeout(function(){ngModel(ngmodel, false);}, 50);
	if (object !== undefined && angular.element(document.body).scope()){
		angular.element(document.body).scope().$root.$apply(function(scope){
			for (prop in object){
				if (object.hasOwnProperty(prop)) scope[prop] = object[prop];
			}
		});
		return angular.element(document.body).scope().$root;
	}
	else {return false;}
}

function ngInject(html, element){
	angular.element(document).injector().invoke(function($http, $compile){
		var scope = angular.element(document).scope();
		var $injected = $(html);
		$(element).html($injected);
		$compile($injected)(scope);
		ngModel(ngmodel);
	});
}

function ngImport(file, element){
	$.get(file, function(result){
		angular.element(document).injector().invoke(function($http, $compile){
			var scope = angular.element(document).scope();
			var $injected = $(result);
			$(element).html($injected);
			$compile($injected)(scope);
			ngModel(ngmodel);
		});
	});
}

function ngBind(prop, controllername){
	ngmodule.controller(controllername, ['$scope', function($scope){
		$scope[prop] = function(newValue) {
			if (!undef(newValue)){
				ngmodel[prop] = newValue;
			}
			return newValue;
		}
    }]);
}
