/**
 * Webfolio CMS
 *
 * Copyright (C) 2009 Nikola Posa (http://www.nikolaposa.in.rs)
 *
 * This file is part of Webfolio CMS.
 *
 * Webfolio CMS is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Webfolio CMS is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with Webfolio CMS. If not, see <http://www.gnu.org/licenses/>.
 */
 
/**
 * Script for handling login box requests, which is provided 
 * by admin module.
 *
 * @author Nikola Posa <posa.nikola@gmail.com>
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License
 */
 
$(document).ready(function() {
	if ($('#login_form').length > 0) {
		if ($('#login_form').css('visibility') == 'visible') {
			$('#login_form').hide();
		}
	   
		$('#login_link span').click(function () { 
			$('#login_form').toggle('normal');	
			$('#username').val(''); 
			$('#password').val(''); 
			$('#login_message').html(''); 
			setTimeout(function() { $("#username").focus(); }, 500); //IE hack.
		});
		
		$('#login_form').submit(function() {
            $(this).ajaxSubmit(
			{
				type: 'POST',
				url: $('#base_url').val() + '/admin/login',
				success: function(response, status, xhr) {
					if (response.indexOf('#ok#') != -1) {
						window.location = response.substr(4);
					}
					else {
						$('#login_message').html(response);
					}
				}
			});

            return false;
		});
	}
});
