var rlpost;
var PostForm = Class.create({
  initialize: function()
  {

    Event.observe($$('div#post-form input.cancel').first(), 'click', function(event)
    {
      Event.stop(event);

      Element.hide($('overlayer'));
      Element.hide($('post-form'));
    });



    this.form = $('newPost');
    this.formHelp = $('parametrsTopic');
    Event.observe(this.form, 'submit', this.post.bindAsEventListener(this));

    if($$('.setResolvedPost')[0]){
		$A($$('.setResolvedPost')).each(function(el)
		  {
		  	if(Cookie.get('TT_NAME') == $('topic_owner_name').value){
		  		el.show();
		  		Event.observe(el, 'click', function(idobj, event){
					var id_post = idobj.substr(13);
				    Event.stop(event);
				    new Ajax.Request('/forum/resolvedpost', {
				      parameters: {resolved: id_post},
				      method: 'POST',
				      onSuccess: function(transport)
				      {
				        if (transport.responseText == 'true')
				        {
								$(idobj).hide();
								$('icon_resolved_'+id_post).show();
								$('post_'+id_post).addClassName('blue');
				        }

				      }.bind(this)
				    });
		  		}.bind(this,el.id));
			}
		  });
	}
  },

  setElement: function(element)
  {
    this.root = element;
  },
  post: function(event)
  {
  	$('postTinyContent').value = tinyMCE.get('post_content').getContent();
    Event.stop(event);
	Element.hide($('newPost-error'));
	Element.hide($('newPost-good'));
    new Ajax.Request('/forum/newpost', {
      parameters: this.form.serialize() +'&'+ this.formHelp.serialize(),
      method: 'POST',
      onSuccess: function(transport)
      {
        if (transport.responseText != 'false'){
			//window.location.href='';
			//window.location.reload();
			url = $F('urlLastPage');
			url = url.sub('#','/uniq/'+Math.ceil(Math.random()*1000)+'#');
			document.location = url;
			return false;
		}
      }.bind(this)
    });
    return true;
  }
});


var CreatePost = Class.create({
  initialize: function(element)
  {
    this.root = element;

    Event.observe(this.root, 'click', function(event)
    {
      Event.stop(event);

      isLogged = new LoginMsg(this.root);
      if(!isLogged.check()) return false;

	  tinyMCE.get('post_content').setContent('');

	  var numbers = document.viewport.getScrollOffsets();
      var post = $('post-form');

       heightWind = document.viewport.getHeight();
       heightPopup = $('post-form').getHeight();

      	if(heightWind > heightPopup){
      		halfHeigtWind = heightWind/2;
			halfHeightPopup =  heightPopup/2;
			correctHeight = halfHeigtWind - halfHeightPopup;

      	}else{
      		correctHeight  = 10;
      	}


	  if(this.root.id.substring(0,8) =='answerid'){

	  	id = this.root.id.substring(9);
	  	cont = $('contentposthidden_'+id).value;
	  	cont2 = '[cytat_'+id+']'+cont+'[/cytat_'+id+']';
		tinyMCE.get('post_content').setContent(cont2);

      }

      this.viewport();

     if ((numbers[1]) > 0)
      {
        Element.setStyle(post, {
          top: (numbers[1] + parseInt(correctHeight)) + 'px'
        });
      }
      else
      {
        Element.setStyle(post, {
          top: (parseInt(correctHeight)) + 'px'
        });
      }

      var overlay = $('overlayer');

 	  Element.hide($('newPost-error'));
      Element.setStyle(overlay, {
        height: this.pageY + 'px'
      });
      Element.show(overlay);
      Element.show(post);

      rlpost.setElement(this.root);
    }.bind(this));
  },
  viewport: function()
  {
    this.pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
    this.pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
  }
});



document.observe("dom:loaded", function(event)
{
  if($$('table#thread tr').first()){
	  $$('table#thread tr').each(function(objon)
	  {
		  Event.observe(objon, 'mouseover', function(event)
		   {
		    	//alert(objon.id.substr(5));
		    	idpst = objon.id.substr(5);
		    	if($$('#post_'+idpst+' td.main div.top-info span.right').first()){
			    	$$('#post_'+idpst+' td.main div.top-info span.right').first().show();
			    	Element.setStyle($('answerid_'+idpst), {
			          visibility: 'visible'
			        });
		        }
		      	return false;
		  });

		  Event.observe(objon, 'mouseout', function(event)
		   {
		    	idpst2 = objon.id.substr(5);
		    	if($$('#post_'+idpst2+' td.main div.top-info span.right').first()){
			    	$$('#post_'+idpst2+' td.main div.top-info span.right').first().hide();
			    	Element.setStyle($('answerid_'+idpst2), {
			          visibility: 'hidden'
		    	    });
		    	 }
		      	return false;
		  });
	   });
  }


  //if($$('.login').length == 0){
	  if($$('a.createPost')[0]){
		  $A($$('a.createPost')).each(function(a)
		  {
		   new CreatePost(a);
		  });
	  }
  //}

  if($('post-form')){
	  rlpost = new PostForm();
  }

  if($$('a.showUrl')[0]){
	  $A($$('a.showUrl')).each(function(el)
	  {
	  	el.onclick = function(){
	  		this.up('td').down('div.popup-cover').style.visibility = 'visible';
	  		return false;
	  	}
	  });
  }

  if($$('div.popup-cover a')[0]){
	  $A($$('div.popup-cover a')).each(function(obj)
	  {
	  	obj.onclick = function(){
	  		this.up('div.popup-cover').style.visibility = 'hidden';
	  		return false;
	  	}
	  });
  }

  if($('search-filter')){
	 Event.observe($('search-filter'), 'click', function(event){
	 	$('search-filter').submit();
	 }.bind(this));
  }


  $$('div#overlayer , a.closeA').each(function(e)
  {
    Event.observe(e, 'click', function(event)
    {
      Event.stop(event);
      $$('div.box-overlayer').invoke('hide');
      Element.hide($('overlayer'));
      return false;
    });
  });
});

