<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Account extends Controller_TemplateDefault {
	public function action_login() {
		# If user already signed-in
		if (Auth::instance()->logged_in()!= 0) {
			# Redirect to the user account
			Request::instance()->redirect('welcome/index');
		}

		Block::add(array(
			'title'=>_('Login to server'),
			'body'=>View::factory('blogin'),
			'style'=>array('css/blogin.css'=>'screen'),
		));

		# @todo this is not being updated - want to make this automatic
		$this->template->control = sprintf('%s -&gt; %s',
			HTML::anchor(URL::site(),'Home'),
			HTML::anchor($this->request->uri(),'Login',array('id'=>'ajxbody'))
		);

		$this->template->content = Block::factory();

		# If there is a post and $_POST is not empty
		if ($_POST) {
			# Instantiate a new user
			$user = ORM::factory('account');

			# Check Auth
			$status = $user->login($_POST);

			# If the post data validates using the rules setup in the user model
			if ($status) {
				# Redirect to the user account
				Request::instance()->redirect('welcome/index');

			} else {
				# @todo Get errors for display in view
				#$this->template->content = $_POST->errors('signin');
			}
		}

		$js = '';
		$js .= '<script type="text/javascript">
$(document).ready(function() {
	$("#ajxbody").click(function() {alert("hello login - body!"); $("#ajBODY").load("/"); return false;});
	$("#ajRIGHT").ajaxSend(function() {$(this).text("Triggered ajaxSend handler.");});
});
</script>';
		$this->template->content .= $js;
	}

	public function action_logout() {
		# If user already signed-in
		if (Auth::instance()->logged_in()!= 0) {
			Auth::instance()->logout();

			Request::instance()->redirect('account/login');
		}

		Request::instance()->redirect('welcome/index');
	}
} // End Welcome
