// JavaScript Document

var popForm = new Class({
	initialize: function(ico,itm){
		this.parent = $('articles');
		
		this.toggler = ico;
		this.tooltip = itm;
		this.visible = false;
		
		this.tooltipSet();
		this.tooltipEvents();
	},
	tooltipPosition: function(){
		var position = this.toggler.getCoordinates(window);
		return position;
	},
	icoReplace: function(stan){
		var ico = '_images/polec.gif';
		var ico2 = '_images/polec2.gif';
		if(stan==0){
			this.toggler.getElement('img').set('src',ico);
			this.toggler.getElement('img').setStyle('margin-left',11);
		}else{
			this.toggler.getElement('img').set('src',ico2);
			this.toggler.getElement('img').setStyle('margin-left',7);
		}
	},
	tooltipSet: function(){
		
		var itm = this.tooltip;
		var offset = this.tooltipPosition();
		itm.setStyle('top',offset + offset.height);
		if(offset.left<=330){
			itm.setStyle('left',offset.left - offset.width);
			itm.setStyle('top',offset.height + 288);
		}else{
			itm.setStyle('left',(offset.left + offset.width)-210);
			itm.setStyle('top',offset.height + 288);
		}
	},
	tooltipHide: function(e){
		this.icoReplace(0);
		
		this.tooltip.removeClass('shown');
		this.tooltip.addClass('hiden');
		var myFx = new Fx.Morph(this.tooltip,{duration:200});
		myFx.start({
				    'height':[254,0],
					'opacity':[1,0]
					});
		
	},
	tooltipShow: function(e){
		this.icoReplace(1);
		
		this.tooltip.removeClass('hiden');
		this.tooltip.addClass('shown');
		var myFx = new Fx.Morph(this.tooltip,{duration:200});
		myFx.start({
				   'height':[0,254],
				   'opacity':[0.5,1]
					});
		
	},
	tooltipEvents: function(){
		this.toggler.addEvent('click', function(e){
					e.stop();
					var visible = this.tooltip.get('class');							
							 if (this.tooltip.hasClass('hiden')){ 
								this.tooltipShow()
							 }else{
							 	this.tooltipHide();
							 }
		}.bind(this));
	
	/* AJAX  */
	 this.tooltip.addEvent('submit', function(event) {  
            //prevent the page from changing 
            event.stop();
    
            //make the ajax call  
            var req = new Request({  
              method: 'post',  
              url: 'ajax.php?mod=polec&lang='+$('form_lang').value, 
              data: { 'mod' : 'polec',
                      'lang': $('form_lang').value,
                      'email': $('mail').value,
                      'name' : $('name').value,
                      'description' : $('tekst').value,
                      'url' : $('url').value
                      },  
               onRequest: function() { this.preloader(); }.bind(this), 
               onComplete: function(response) { this.showMessage(response);  }.bind(this)  
           }).send();  
       }.bind(this));
    },
    
  preloader: function(){
   $('polec_przeslona').setStyle('z-index','2');
   $('polec_przeslona').fade('.9');
   $('polec_img').setStyles({	visibility:'visible',opacity: 1	});
  },
  
  showMessage: function(str){
    var code = str.split(";",1); 
    var txt = str.substr(2);
 
    $('polec_img').fade('out'); 
    $('polec_msg').innerHTML = txt;
    (function(){$('polec_msg').fade('in');}).delay(400); 
    (function(){    
      $('polec_przeslona').fade('out');
      (function(){
          $('polec_przeslona').setStyle('z-index','-1');
          $('polec_msg').setStyles({	visibility:'hidden',opacity: 0}); 
      }).delay(500);     
    }).delay(3500);    
    
    if(code=='1'){
      //ok
      (function(){
        this.tooltipHide();
        $('mail').value = $('name').value = $('tekst').value = '';
      }).delay(3500,this);     
    }else{
      //error
    }
 }

});// /class

// wywolanie
$(window).addEvent('domready', function(){
	if($('polecToggle1')){
      var popform = new popForm($('polecToggle1'),$('toolTip1'))
  };     
});