// JQUERY - JEFF'S_SIMPLE_CMS_LOGIN.JS

// Cookies Used	
$logged_in_cookie = "cms_logged_in_cookie";
$logged_in_username_cookie = "cms_username_cookie";

// What version are we running.
$cms_version = "0.30alpha";

// Is there a cms login toolbar - show/hide or not
var login_toolbarStatus = 0; 

$(document).ready(function() {		

	// The cms login toolbar
	$cms_login_toolbar_container = $('#cms_login_toolbar_container');
	$cms_login_toolbar_container_spacer = $('#cms_login_toolbar_container_spacer');
		
	// Wait till the entire window loads
	$(window).load(function(){
		
		// ***************************************************************************/
		// INSERT THE VERSION ********************************************************/
		// ***************************************************************************/
		
		$(".cms_version").text($cms_version);

		// ***************************************************************************/
		// SHOW/HIDE CMS LOGIN TOOLBAR ***********************************************/
		// ***************************************************************************/

		$(".cms_login").click(function(){ 		
			
			// OPEN THE INFO BAR IF IT ISN'T ALREADY
			if(!login_toolbarStatus) {
				open_login_toolbar();
			}
			
			// CLOSE THE INFO BAR
			else {
				close_login_toolbar();
			}
			
		});
						
		// ***************************************************************************/
		// OPEN CMS LOGIN TOOLBAR ****************************************************/
		// ***************************************************************************/

		// ON LOGIN SUBMIT - Verify user and password
		$("#cms_login_button").click(function() {
				
			// Get the username and password entered and pass to function
			// That will in turn pass to server via AJAX
			username = $("#cms_enter_username").val();
			password = $("#cms_enter_password").val();

			// Verify User and Password
			if (login_verify(username, password)) {
				// Validated - Good password
				// The page needs to reload with the new PHP/HTML
				// Remember, the session was set at the server
				//	a. logged_in
				//	b. username
	
				// SET A COOKIE TO REMEMBER IF YOU ARE LOGGED IN ACROSS ALL PAGES
				// The Session ID is also set at the server
				$.cookie($logged_in_cookie, "yes");
				$.cookie($logged_in_username_cookie, username);
				
				// The page will reload because submit was clicked.
				
			}
			else {
				// Error, Bad Username or password, or blank
				// DO NOT RELOAD THE PAGE - TRY AGAIN
				return false;
			}
		});
					
		// ***************************************************************************/
		// CLOSE CMS LOGIN TOOLBAR ***************************************************/
		// ***************************************************************************/
		
		$("#cms_login_toolbar_close").click(function(){  
			close_login_toolbar();  
			// DO NOT RELOAD THE PAGE
			return false;
		});  

		//Press Escape event!  
		$(document).keypress(function(e){  
			if(e.keyCode==27 && infobarStatus==1){  
				close_login_toolbar();  
			}  
		});  

	});
	
});

// ***********************************************************************/
// OPEN CMS LOGIN TOOLBAR ************************************************/
// ***********************************************************************/
function open_login_toolbar(){ 
	
	$cms_login_toolbar_container.slideToggle(500);
	$cms_login_toolbar_container_spacer.slideToggle(500);
	infobarStatus = 1;

}
 
// ***********************************************************************/
// CLOSE CMS LOGIN TOOLBAR ***********************************************/
// ***********************************************************************/
function close_login_toolbar(){ 

	$cms_login_toolbar_container.slideToggle(500);
	$cms_login_toolbar_container_spacer.slideToggle(500);
 	infobarStatus = 0;

}

// ***********************************************************************/
// LOGIN VERIFY  *********************************************************/
// ***********************************************************************/
function login_verify(username,password) {
	
	var returnVal = false;
	
	// Pass the username and password to the server
	$.ajax({
    	type: "POST",
    	url: "/jeffs_simple_cms/php_scripts/jeffs_simple_cms_verify.php",
    	data: 'username=' + username + '&password=' + password,
   		async: false, // THIS IS VERY IMPORTANT!!! - THE AJAX REQUEST MUST FINISH BEFORE JAVA CONTINUES
      	success: function(data) {
			
			// Get the response from the server, passed, empty, or tryagain, or error
			if (data=="passed") {
				// I think we alrady set up the session. should do it at the server. We need to reload.
				returnVal=true;
			}
			else if (data=="empty") {
				// Don't close the window..but do it again.
				returnVal = false;
				$("#cms_login_result").text("Please enter both, a username and password");
			}
			else if (data=="tryagain") {
				// Username or password was wrong
				returnVal = false;
				$("#cms_login_result").text("Username or password is incorrect, please try again");
			}
			else {
				// There was an error -  Can't log in
				returnVal = false;
				$("#cms_login_result").text("Error at the server, you will not be able to log in");
			}
		}
	});
	return returnVal;
}
			
// ****************************************************************************************/	
// COOKIE PLUGIN **************************************************************************/
//*****************************************************************************************/
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


