var ForumphotoObject;
var Forumphoto = Class.create(
{
  initialize: function()
  {
  	if($('post-form')){
  		this.version="2";
		this.root = $('forumIframeupload2');
	}
	else{
		this.version="";
		this.root = $('forumIframeupload');
	}
    this.mainDiv = Element.up(this.root, 'div');
    this.photosCount = 0;
    this.pic = "";
    this.file = "";
    this.hash = "";
    this.maxCount = 6;
  },

  createBox: function(pic, hash, fileInfo)
  {
    this.pic = pic;
    this.fileInfo = fileInfo;
    this.hash = hash;
    this.photosCount += 1;
	this.number= this.photosCount;

    if (this.photosCount > this.maxCount)
    {
      Element.hide(Event.element(event));
      return false;
    }
    var name = 'file_' + this.photosCount;
    var hidden = document.createElement('input');
    hidden.type = 'hidden';
    hidden.name = 'file_' + this.photosCount;
    hidden.id = 'file_' + this.photosCount;

    var hiddenSerialize = document.createElement('input');
    hiddenSerialize.type = 'hidden';
    hiddenSerialize.name = 'fileinfo_' + this.photosCount;
    hiddenSerialize.id = 'fileinfo_' + this.photosCount;
	this.mainDiv.appendChild(hidden);
    this.mainDiv.appendChild(hiddenSerialize);

    this.elem =  document.getElementById('file_' + this.number);
  	this.elem2 = document.getElementById('fileinfo_' + this.number);

  	this.elem.value = this.hash;
  	this.elem2.value = this.fileInfo;

	cont =  '<li id="forumboxforimage_'+this.number+'" style="float: left; width: 90px;">'
			+ '<div class="item boxoneimg">'
			+ '<img src="'+this.pic+'" alt="obrazek" class="wpic2" style="clear: both;" />'
			+ '<a href="#" onclick="ForumphotoObject.deleteBox('+this.number+'); return false;" style="display: block; color: black; text-align: center;" class="action-del">usuń</a>'
			+'</div>'
			+'</li>';

	$('boximages'+this.version).show();
	Element.insert($('forumforimagesBox'+this.version), cont);

  },

  deleteBox: function(id){
	$('file_' + id).value = '';
	$('fileinfo_' + id).value = '';
	Element.remove('forumboxforimage_'+ id);
  	return false;
  }
});

var TopicForm = Class.create({
  initialize: function()
  {

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

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

    this.form = $('newTopic');
    Event.observe(this.form, 'submit', this.topic.bindAsEventListener(this));
  },

  setElement: function(element)
  {
    this.root = element;
  },


  topic: function(event)
  {

    Event.stop(event);
	Element.hide($('newTopic-error'));
	Element.hide($('newTopic-good'));
	$('topicTinyContent').value = tinyMCE.get('textposttopic').getContent();
    new Ajax.Request('/forum/newtopic', {
      parameters: this.form.serialize(),
      method: 'POST',
      onSuccess: function(transport)
      {
        if (transport.responseText != 'false'){
			document.location = document.location.href;
		}

      }.bind(this)
    });
  }
});

var CreateTopic = 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('textposttopic').setContent('');

      tinyMCE.get('textposttopic').onChange.add(function(ed) {

		$('topicTinyContent').value = tinyMCE.get('textposttopic').getContent();
		//$('topicTinyContent').focus();
	});

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

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

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

      	}else{
      		correctHeight  = 10;
      	}

      this.viewport();

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

      var overlay = $('overlayer');

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

      rl2.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($('forumIframeupload'))
  {
  		ForumphotoObject = new Forumphoto();
  }

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

  if($('topic-form')){
  	rl2 = new TopicForm();
  }

  $$('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;
    });
  });
});
