Controller = {
	last: '',
	current: '',
	init: function(){},
	load: function()
	{
		$('#content').fadeOutIE('normal');
		if(Controller[Pages.current] == undefined)
				$.getScript(box3document.baseURL + 'assets/site/js/controllers/controller.'+Pages.current+'.js', Controller.scriptLoaded);
		else
		{
			if(Controller[Pages.current].html != '')
			{
				Controller.loadSuccess(Controller[Pages.current].html);
				Controller.loadComplete();
			}
			else
			{
				$.ajax({
					url: 'ajax/load/',
					data: {
						section: Pages.current
					},
					type: 'post',
					cache: false,
					dataType: 'html',
					beforeSend: function(){},
					success: Controller.loadSuccess,
					complete: Controller.loadComplete
				});
			}
		}
	//Controller[c].load();
	},
	scriptLoaded: function(data, textStatus)
	{
		Controller.load();
	},
	loadSuccess: function(ret)
	{
		// atribui o html carregado a uma variavel dentro do controller
		Controller[Pages.current].html = ret;
		//
		$section = $(ret).first();
		$section.hide();
		//
		Controller[Pages.current].mainElement = $section;
		//
		$('#content').children().remove().end();
		//
		var $css = $('head').find('link').last();
		$css.attr('href', box3document.baseURL + 'assets/site/css/' + Pages.current + '.css');
		//
		$('#content').append($section);
		//
		Controller[Pages.current].load.success();
		//
		if(Controller[Pages.current].customButtons != undefined)
				CustomButtons.init();
		//
		if(Controller[Pages.current].hasForm != undefined)
		{
			$('form').find('.bt-enviar, .bt-concluir').bind('click', Controller.formSend);
			$('form').bind('submit', Controller.formSubmit).data('sending', false);
		}
	},
	loadComplete: function()
	{
		$('body').attr('class', 'page-'+Pages.current);
		Controller[Pages.current].load.complete();
		$('input[type=text], textarea').placehold();
		box3document.fixMasks();
		//
		$('.flash').embedSWF();
		//
		$('#content').fadeInIE('normal');
	},
	close: function()
	{
		if(Pages.last != '' && Controller[Pages.last] != undefined)
			Controller[Pages.last].close();
	},
	formSend: function(e)
	{
		e.preventDefault();
		e.stopImmediatePropagation();
		//
		var $self = $(this);
		var $form = $self.closest('form');
		if(box3form.validate($form))
			$form.submit();
	},
	formSubmit: function(e)
	{
		e.preventDefault();
		e.stopImmediatePropagation();
		$form = $(this);
		$message = $form.find('.error-message');
		if($form.data('sending') === false)
		{
			$.ajax({
				type: $form.attr('method'),
				url: box3document.baseURL + 'site/' + $form.attr('action'),
				data: $form.serialize(),
				dataType: 'json',
				beforeSend: function(){
					$form.data('sending', true);
					$message.text('Enviando dados...');
				},
				success: function(ret){
					$form.data('sending', false);
					$message.text(ret.message);
					//
					if(typeof(Controller[Pages.current].clearForm) == 'function')
					Controller[Pages.current].clearForm($form);
				}
			});
		}
	}
}

