
function add_comment(sub){
  var comment_form = $('comment_form');
  new Element('input',{'name':'sub','value':sub,'type':'hidden'}).inject(comment_form);
  comment_form.submit();  
  }

function $send( obj ){
  var obj = $(obj);
  if ( obj ){ 
    obj.submit();
    }
  }

function menu (obj){
  var li = $(obj).getParent();
  var ul = li.getElement('ul');  
  ul.toggleClass('hide');  
  if ( li.hasClass('active' ) )
    li.removeClass('active')
  else if ( !ul.hasClass('hide') ) 
    li.addClass('active');
  }
  
function swich_order_row ( obj ){
  var className = obj.get('name') ;
  if( className ){
    if ( obj.get('value') )
      $$('.'+className).removeClass('hide');
    else
      $$('.'+className).addClass('hide');
    }
  }   
  
function clear_input( input ){
  var input = $(input);
  if ( input ){
    input.addEvent('focus',function(){  if ( input.get('title') == input.get('value') ) input.set('value',''); });
    input.addEvent('blur',function(){ if ( input.get('value') == '' ) input.set('value', input.get('title') ); });
    }
  }
  
/******************************************************************************************************************************************/
  
window.addEvent('domready', function() {
  var mt = $('mt');
  if ( mt ){
    mt.getElement('a[href=Dodaj-do-ulubionych.html]').addEvent('click',function(e){
      new Event(e).stop();
      var url =window.location.href;
      var title = document.title;
      if (document.all)
        window.external.AddFavorite(url, title);
       else if (window.sidebar)
        window.sidebar.addPanel(title, url, "");      
      });
    }

  var mm = $('mm');
  if ( mm ){
    mm.getElements('td').each( function(el){
       el.addEvent('mouseover' , function(){
        this.addClass('hover');
        });
       el.addEvent('mouseout' , function(){
        this.removeClass('hover');
        });
      });
    }
  /*
  var mt = $('mt');  
  if ( mt ){
    mt.getElements('a').each(function(el){
      if ( el.get('href') == '#' || el.get('href') == '/#' ){
        el.addClass('dimed');
        el.addEvent(function(e){
          new Event(e).stop();
          });
        }
      });
    }
  */ 
  
 var clear_inputs  = $$('input.clear_input'); 
 if ( clear_inputs ){
  clear_inputs.each(function(el){
    clear_input(el);
    });
  } 
   
});

/******************************************************************************************************************************************/

function delete_post( id ){
  if ( confirm( 'Czy jesteś pewien że chcesz usunąć ten wpis?' ) ){    
    var myRequest = new Request({method:'get',url:'xml/forum.xml'});
    myRequest.send('delete_post='+id);
    $('post_'+id).dispose();
    if ( $('post2_'+id) ){
      $('post2_'+id).dispose();
      }
    }
  }
  
/******************************************************************************************************************************************/

function edit_post( id ){
    var post = $('post_'+id);
    var post2 = $('post2_'+id);
    
    var post_topic = post.getElement('.post_topic');
    var post_content = post.getElement('.post_content');
    if ( !post_content ){
      var post_content = post2.getElement('.post_content');
      }
  
    if ( post_topic ){
      post_topic.getParent().addEvent('click' , function(e){new Event(e).stop()} );
      var value = post_topic.get('text');
      post_topic.set('text','');
      new Element('input',{'type':'text','name':'post_topic','value':value,'class':'post_edit_field'}).inject( post_topic );
      }
    
    if ( post_content ){
      post_content.getParent().addEvent('click',function(e){new Event(e).stop()});
      var value = post_content.get('text');
      post_content.set('text','');
      new Element('textarea',{'type':'text','name':'post_topic','value':value,'class':'post_edit_field'}).set('text',value).inject(post_content);
      }
    
    post.getElement('.post_button').set('text','Zapisz').set('href','javascript:save_post('+id+')');
  }
  
/******************************************************************************************************************************************/

function save_post( id ){
    var post = $('post_'+id);
    var post2 = $('post2_'+id);
    
    var post_topic = post.getElement('.post_topic');
    var post_content = post.getElement('.post_content');
    if ( !post_content ){
      var post_content = post2.getElement('.post_content');
      }
  
    if ( post_topic ){
      post_topic.getParent().removeEvent('click');
      var topic = post_topic.getElement('input').get('value');
      post_topic.getElement('input').dispose();
      post_topic.set('text',topic);
      }
    
    if ( post_content ){
      post_content.getParent().removeEvent('click');
      var content = post_content.getElement('textarea').get('value');
      post_content.getElement('textarea').dispose();
      content = content.replace(/([^>]?)\n/g,'$1<br />\n');
      post_content.set('html',content);
      }
      
    post.getElement('.post_button').set('text','Edytuj').set('href','javascript:edit_post('+id+')');
    var myRequest = new Request({method:'post',url:'xml/forum.xml'});
    myRequest.send('update_post='+id+'&topic='+topic+'&content='+content );
  }

/******************************************************************************************************************************************/

function cday( year , month , day , banner_id , obj ){ 
  var obj = $(obj);
  var myRequest = new Request({method:'post',url:'xml/distributor-banner.xml',onComplete:function(responceText){
    var responceArray = responceText.split('|');
    $('banner_days_left').set('text',responceArray[3]);
    if ( responceArray[0] == 'ADD' )
      obj.addClass('selected');
    else if ( responceArray[0] == 'REMOVE' ) 
      obj.removeClass('selected');
    else if ( responceArray[0] == 'OVERWRITE' )
      alert('Przekroczyłeś ilość dostępnych dni');
    }});
  myRequest.send('year='+year+'&month='+month+'&day='+day+'&banner_id='+banner_id ); 
  }

