// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string img_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
        get_login_admin: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				widthPopup:			380
            }
                 
            var options =  $.extend(defaults, options);
			var o = options;
            
			$(".popup").remove();
			$(document.body).append("<div id=\"popup-login-admin\" class=\"popup\"></div>");
			
			var path = http_root+rp_cms+'form_login.php?pg_url=cms';
			//alert(path);
			
			// Get the current URL.
			var pathname = window.location.pathname;
			//alert(pathname);
			
			$('#popup-login-admin').load(path, {}, function(){
				
				// Attach plugins
				$.fn.get_login_admin.post_login_admin(pathname);
				$(this).set_center();
				$('.close',this).close_popup({target:'#popup-login-admin'});
				$('.reset-request').load_form_request_reset_account({popupWidth:600});
				$('form *[title]').inputHint();
				
				// Fade in this.
				$(this).fadeIn('fast');
				
			}).width(o.widthPopup).hide();
			
			// Submitting the form.
			$.fn.get_login_admin.post_login_admin = function(pathname){
			
				$("#form-login-admin").submit(function(){	
		
					var object_form = $(this);
					
					// Check each of the input field - if it has the same value as in its title - empty it.
					// Must place it here before serialising the form.
					$('input',object_form).each(function(){
						if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
					});
					
					var values_form = object_form.serialize();
					var path = object_form.attr('action');
					
					//alert(values_form);
					
					// Processing.
					$("#msgbox",object_form).removeClass().addClass('messagebox').text('Validating...').fadeIn(1000);
				
					// post the form.
					$.post(path,values_form,function(data){
						//alert(data);	
				
						// if the return data has 'granted'.
						//if correct login detail
						if(data =='granted')
						{ 
							$("#msgbox",object_form).fadeTo(200,0.1,function(){ 
								
								// processing.
								$(this).html('Logging in...').addClass('messageboxok').fadeTo(900,1,function(){  
								
								//redirect to secure page
								//document.location = http_root+pg_url+ask_id; 
								document.location = pathname; 
								//document.location = 'index.php';
								});			  
							});
						}

						// if the return data has 'not granted'.
						else if (data =='not granted') 
						{
							$("#msgbox",object_form).fadeTo(200,0.1,function(){ 
								$(this).html('You don\'t have the permission to edit this page.').addClass('messageboxerror').fadeTo(900,1);
								$('form *[title]').inputHint();
							});		
						}
						
						// if the return data has 'intruder'.
						else if (data =='intruder') 
						{
							$("#msgbox",object_form).fadeTo(200,0.1,function(){ 
								$(this).html('Invalid login!').addClass('messageboxerror').fadeTo(900,1); 
								$('form *[title]').inputHint();
							});		
						}
						
						// otherwise...
						else 
						{
							$("#msgbox",object_form).fadeTo(200,0.1,function(){ 
								$(this).html('Your login details are incorrect!').addClass('messageboxerror').fadeTo(900,1);
								$('form *[title]').inputHint();
							});	
						}
					
					});
			
					// do not post the form physically.
					return false; 
					
				});
		
			}
			
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
