/**
 * --------------------------------------------------------------------------------
 * @desc NameSpace is the root javascript object for this library, and serves primarily as a namespace.
 * 
 * usage: if you define a new section/namespace, include it here and put the full implementation details later in the file.
 *  
 * --------------------------------------------------------------------------------
 */
library = {
    version: 1,
    constant: {},       // store universal constants here, including html, text, numerics, etc.
    state: {},          // store state variables.
    fn: {},             // global shortcuts to hi-value functions.
    util: {},           // general purpose utility functions:
    widget: {},         // self-contained widgets: eg, colour picker, etc.
    module: {           // self-contained modules, these correspond to modules/components in the application
        library:{}       // All admin module code should go here, can also have sub name spaces.
    }
    
};

/** 
 * --------------------------------------------------------------------------------
 * @desc Constants
 * --------------------------------------------------------------------------------
 */
library.constant.submitError = '<error>';
library.state.formAjaxRequestInProgress = 0;

/** 
 * --------------------------------------------------------------------------------
 * @desc Global functions
 * --------------------------------------------------------------------------------
 */
library.fn.prepareform = function(formObject){
	var beforeSubmit = formObject.attr('onBeforeSubmit');
	var success = formObject.attr('onSuccess');
	var options = {};
	
	if (beforeSubmit != null){
		options.beforeSubmit = function (){
		   var returnValue = eval(beforeSubmit + '(formObject);');
		   if (returnValue){
		       library.state.formAjaxRequestInProgress = 1;
		   }
		   
		   return returnValue;
		};
	}
	if (success != null){
		
		options.success =  function(responseText, statusText){            
            eval (success + '(responseText, formObject, statusText);library.state.formAjaxRequestInProgress = 0;');
        }
        
	}
	
	options.timeout = 1800000;

	formObject.ajaxForm(options);
}

/**
 * @desc Returns a get param from the url request.
 * @return An empty string or the value
 */
library.fn.getParam = function (name) {
    var returnValue = '';
    var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results != null ){
      
        returnValue = results[1];
    }
    
    return returnValue;
  };
  
/**
 * @desc Uses the jquery tools validator to validate a form.
 * @return boolean
 */
library.fn.validateForm = function(){
    if (!jQuery().validator){
        alert('Validator not loaded. Please let the site administrator know about this error.');
        return false;
    }
    var inputs = $(".library_validate :input").validator();  
    

    return inputs.data("validator").checkValidity();

};


/**
* @desc Generates a growl from the supplied message.
*
*       Two options are 'sticky' and 'duration'.
* 
*       sticky is boolean that makes the growl stay on screen
*       until the user clicks it.
*
*       duration is the number of milliseconds that the growl
*       will stay on the screen.
*
*       sticky overrides duration.
*
*
* @param Boolean sticky
* @param Integer duration
*/
library.fn.growl = function (msg, sticky, duration) {
   var options = {life: 3500};
   
   if (typeof msg != 'string') {
       return false;
   }

   if (sticky) {
       options.sticky = true;
   }

   if (!isNaN(parseInt(duration, 10))) {
       options.life = parseInt(duration, 10);
   }

   $.jGrowl( msg.replace("\n",'<br>', 'g'), options );
   msg;
};

library.fn.checkFormRequiredFields = function (formObj){
	  var returnValue = true;
	  
	  formObj.find('.library_form_required').each(function(){
	      
	      if ($(this).attr('type') == 'hidden'){
	              return;
	      }
	
	      if ($(this).attr('type') == 'checkbox'){
	              returnValueCheckBoxGroup = false;
	
	              $('[library_form_checkbox_group="' +  $(this).attr('library_form_checkbox_group') + '"]').each(function(){
	
	                      if ($(this).is(':checked')){
	                              returnValueCheckBoxGroup = true;
	                      }
	              });
	
	              if (!returnValueCheckBoxGroup){
	                      returnValue = false;
	                      $(this).addClass('library_form_required_no_value');
	                      
	              }
	              else {
	                      $(this).removeClass('library_form_required_no_value');
	              }
	      }
	      else {
	              if ($(this).val() == '') {
	                  returnValue = false;
	                  $(this).addClass('library_form_required_no_value');
	          }
	          else {
	        	  $(this).removeClass('library_form_required_no_value');
	          }
	      }
	  });
	  
	  return returnValue;
}

library.util.ajaxSyncCall = function(link){
	
	return $.ajax({
		async: false,
        url: link
        
    }).responseText;

}
