var IW_ContactForm = Class.create({

	LOADING_INFO: 'Loading form... Please wait',
	
	SENDING_INFO1: '<p class="blue">Sending information...</p>',
	SENDING_INFO2: '<p class="blue">Wysyłanie...</p>',

	m_objForm: null,
	m_objSubmit: null,
	m_objInfoBox: null,
	m_strURL: null,
	m_objRequest: null,
	
	onLoad: null,

	initialize: function(p_strFormId, p_strSubmitId, p_strInfoId, p_strActionURL){
		this.m_objForm = $(p_strFormId);
		this.m_objSubmit = $(p_strSubmitId);
		this.m_objInfoBox = $(p_strInfoId);
		this.m_objForm.observe('keydown', this._onEnter.bindAsEventListener(this));
		this.m_strURL = p_strActionURL;

		this.m_objSubmit.observe('click', this._onClick.bindAsEventListener(this));
		
		//this.m_objInfoBox.update(this.LOADING_INFO);
		
	},
	
	_onEnter: function(event){
		if(event.keyCode == Event.KEY_RETURN){
			//Event.stop(event);
		//	this._send();
		}
	},
	
	_onClick: function(event){
		var objButton = Event.element(event);
    if(objButton.id = this.m_objSubmit.id){
			Event.stop(event);
			this._send();
		}
	},
	
	_send: function(){	  
  	  var lang = (window.location.toString().indexOf("/pl/") > -1)? 'pl' : 'en';
  	  if(lang == 'en'){
  		  this.m_objInfoBox.update(this.SENDING_INFO1);
      } else{
  		  this.m_objInfoBox.update(this.SENDING_INFO2);
      }
      this.m_objRequest = new Ajax.Request(this.m_strURL, {
  		  method: 'post',
  		  parameters: this.m_objForm.serialize(),
  		  onSuccess: this._update.bind(this)
  		});
	},
	
	_update: function(p_objResponse){
		this.m_objInfoBox.update(p_objResponse.responseText);
		if(this.onLoad){
			this.onLoad(p_objResponse.responseText);
		}
	}
});


function IW_open_contact_form(p_strSubject){

  var lang = (window.location.toString().indexOf("/pl/") > -1)? 'pl-PL' : 'en-GB';
  jQuery.fancybox({
    'href': '/en/modules/mod_contactform/form.php?subject=' + encodeURIComponent(p_strSubject) + '&lang=' + lang, //<-- in form.php put the HTML form implementation and set subject hidden value
    'width': 700,
    'height': 600,
    'scrolling': 'no',
    'autoDimensions': 'true',
    'onComplete': function(event){
       var objForm = new IW_ContactForm('contact_form', 'submit_button', 'info_box', '/en/modules/mod_contactform/send.php?lang=' + lang);//<-- send.php implement sending data
    }
  });
  
}

