var Toggler = new Class({
  Implements: [Chain, Options],

  options: {
    buttons: {
      close:           "article-toolbox-close", // open-/close-Link
      comments:        "article-toolbox-comments-link",
      share:           "article-toolbox-share-link"
    },
    boxes: {
      container:       "article-toolbox-content", // Container, der auf-/zugeklappt wird
      comments:        "article-toolbox-comments",
      share:           "article-toolbox-share"
    },
    delays: {
      afterSlideOut:   400,
      beforeSlideIn:   200
    },
    text: {
      open:            "open",
      close:           "close"
    }
  },

  elements: {

    buttons: {
      close:           null,
      comments:        null,
      share:           null
    },
    boxes: {
      container:       null,
      comments:        null,
      share:           null
    }

  },

  target: null,

  initialize: function(options) {
    this.setOptions(options);

    this.elements.buttons.close     = $$("." + this.options.buttons.close);
    this.elements.buttons.comments  = $$("." + this.options.buttons.comments);
    this.elements.buttons.share     = $$("." + this.options.buttons.share);

    this.elements.boxes.container   = $$("." + this.options.boxes.container);
    this.elements.boxes.share       = $$("." + this.options.boxes.share);
    this.elements.boxes.comments    = $$("." + this.options.boxes.comments);

    this.elements.buttons.close.addEvent("click", this.toggle.bind(this));
    this.elements.buttons.comments.addEvent("click", this.displayComments.bind(this));
    this.elements.buttons.share.addEvent("click", this.displayShare.bind(this));

    // Startzustand herstellen:
	if (window.location.hash.match(/comment/)) {
		this.elements.buttons.comments.addClass("active");
		this.elements.boxes.share.setStyle("display", "none");
		this.elements.boxes.container.setStyle("display", "block");
  	} else {
		this.elements.buttons.close.each(function(el) { el.innerHTML = this.options.text.open; }.bind(this));

	  	  //this.elements.buttons.share.addClass("active");
	      this.elements.boxes.comments.setStyle("display", "none");
	}
    // Share aufgeklappt by default
	this.elements.boxes.container.setStyle("display", "block");
    this.elements.boxes.share.addClass("active");
    this.elements.buttons.close.set('text', this.options.text.close);
  },

  toggle: function(event) {
    this.target = event.target;

    if(this.target.innerHTML == this.options.text.close) {
      this.hide();
    } else {
      this.show();
    }
  },

  show: function() {
	$(this.options.buttons.close   + "-" + this.getBoxId()).innerHTML = this.options.text.close;
    $(this.options.boxes.container + "-" + this.getBoxId()).setStyle("display", "block").slide("in");

    this.callChain();
  },

  hide: function() {
    $(this.options.buttons.close   + "-" + this.getBoxId()).innerHTML = this.options.text.open;
    $(this.options.boxes.container + "-" + this.getBoxId()).slide("out");

    $(this.options.buttons.comments + "-" + this.getBoxId()).removeClass("active");
    $(this.options.buttons.share    + "-" + this.getBoxId()).removeClass("active");

    this.callChain.delay(this.options.delays.afterSlideOut, this);
  },

  displayComments: function(event) {
    this.target = event.target;

    if (!this.target.hasClass("active")) {
      this.chain(this.hide);
      this.chain(this.setStyleForComments);
      this.chain(this.show);
    }
    this.callChain();
    this.target.addClass("active");
  },

  displayShare: function(event) {
    this.target = event.target;

    if (!this.target.hasClass("active")) {
      this.chain(this.hide);
      this.chain(this.setStyleForShare);
      this.chain(this.show);
    }
    this.callChain();
    this.target.addClass("active");
  },

  setStyleForComments: function() {
    $(this.options.boxes.comments + "-" + this.getBoxId()).setStyle("display", "block");
    $(this.options.boxes.share    + "-" + this.getBoxId()).setStyle("display", "none");

    this.callChain.delay(this.options.delays.beforeSlideIn, this);
  },

  setStyleForShare: function() {
    $(this.options.boxes.comments + "-" + this.getBoxId()).setStyle("display", "none");
    $(this.options.boxes.share    + "-" + this.getBoxId()).setStyle("display", "block");

    this.callChain.delay(this.options.delays.beforeSlideIn, this);
  },

  getBoxId: function() {
    return this.target.id.replace(/^\D+/, '') || 0;
  }
});

window.addEvent("domready", function() {
  // toolbox-Funktionalitaet
  var toggler = new Toggler({});

  var fixIsNeeded = function() {
    return ((Browser.Engine.name == "trident" && Browser.Engine.version == 4) ||
            (Browser.Engine.name == "presto"  && Browser.Engine.version == 925));
  };

  // More-Link
  $$(".drop-btn div").addEvent("click", function(event) {
	  var target = $$("#post-" + event.target.getParent().id.replace(/^\D+/, '') + " .v-long")[0];
	  
	  if (fixIsNeeded()) {
		  target.setStyle("display", target.getStyle("display") == "none" ? "inline" : "none");
	  }
	  
	  else {
		  target.setStyle("display", "block");
		  target.slide("toggle").get("slide").addEvent("complete",
				  function(el) { el.retrieve("wrapper").setStyle("height", "auto");
		  });
	  }
	  event.target.innerHTML = event.target.innerHTML == "More" ? "Less" : "More";
  });

	$$(".drop-btn div").each(function(el) {
    var target = $$("#post-" + el.getParent().id.replace(/^\D+/, '') + " .v-long")[0];
		if (!fixIsNeeded()) {
		  target.slide("out");
		}
  });
	
});