var Keberox = {  
  cache: {},
  
  config: {
    ajax: false,
    debug: 1
  },
  
  /*
    Function: bootstrap
    Runs before anything else in all pages.
  */
  bootstrap: function () {
	this.inputFocus('input[type="text"]');
	this.inputFocus('textarea');
	this.formSubmit('form.form');
	this.preloadImages();
  }
};

$.extend(Keberox, {
  
	preloadImages : function () {
	  var images = ['img/swiss-form-b_01.png', 'img/swiss-form-b_02.png', 'img/swiss-form-b_02.png', 'img/form1_01a.png', 'img/form1_02a.png', 'img/form1_03.jpg', 'img/bg-textarea.jpg', 'img/bg-textarea-invalid.png', 'img/bg-input-invalid.png', 'img/bg-input.jpg'];
	  $.each(images, function(i, val){
		 var pic = new Image();
		 pic.src = server_path + val;
	  });
	},
	
	inputFocus: function(selector) {
	  $(selector).bind('focus', function(){
		  $(this).removeClass('error-input');
	  });
    },
    
   /*
   	Function: formVal});idation
   */
   formSubmit: function(selector) {
	  ($.test(selector)) ? this.log('done: added (submit) trigger to ' + selector) : null;
	  var _formid = '';
	  var _insertTo = $('#btn' + _formid);
	  $(selector).submit(function() {
		 var _target = $(selector).attr('action');
		 _formid = $(this).attr('id');
		 
		 _insertTo = $('#btn' + _formid);
		 _insertTo = (_insertTo) ? _insertTo : $('#' + _formid);
		 _insertTo.prepend('<p id="loading"><img src="img/ajax-loader.gif" /> &nbsp; &nbsp; <strong>Sending, please wait...</strong></p>');
		 $.post(_target, $(this).serialize(), flashMessage, 'html');
		 return false;
	  });
	  
	  function flashMessage (data, textStatus) {
		  
		  if (textStatus == 'success') {
			  var _data = $(data);
			  var _flashMsg = (_data.is('.flash')) ? _data : null;
			  var _prevFlashMsg = ($.test('.flash')) ? $('.flash') : null;
			  if (_prevFlashMsg) {
				  _prevFlashMsg.fadeOut('fast', function() {
					  $(this).remove();
				  });
			  }
			  var _prevShowMsg = ($.test('.show')) ? $('.show') : null;
			  if (_prevShowMsg) {
				  _prevShowMsg.fadeOut('fast', function() {
					  $(this).remove();
				  });
			  }
			  var msgvalue = (_data.is('.error')) ? true : false; 
			  if (msgvalue)
				var showmsg = '<div class="show error">Please complete the required fields</div>';
			  else
				var showmsg = '<div class="show success">Thank you, your message was sent.</div>';
			  
			  $('#loading').remove();
			  if (showmsg)
				_insertTo.prepend(showmsg).fadeIn();
			  else
				$(document.createElement('div')).hide().addClass('flash').prependTo(_insertTo);
			  
			  $('.error-input').each(function() {
				  $(this).removeClass('error-input');
			  });
			  
			  if (_flashMsg) {
				  $(_data.find('li')).each(function() {
					  $('#' + $(this).attr('title')).addClass('error-input');
				  });
			  }
		  }
	  }
  	},

  /*
  Function: log
  Logs output with a timestamp if debugging is enabled.

  Parameters:
    - str: variable to log
	*/
	log: function(str) {    
	  if (this.config.debug && window.console) {
	    if (!this.timestamp)
	      this.timestamp = $.now();
	      
	    console.log(($.now() - this.timestamp) + ': ' + str); // this.debug(str));
	  }
	  return str;
	} 
});
$(document).ready(function(){
	Keberox.bootstrap();
});


