$(function() {
  var template = $('#handlebars_resource_index').html();
  Resource.handlebars_resource_index_script = Handlebars.compile(template);

  $( "#languages" ).on('iconselectmenuchange', function() {
    var option = this.value;

    switch(option) {
      case 'lang-pl':
        Resource.handlebars_resource_index_script = Handlebars.compile(template);
        break;
      case 'lang-en':
        Resource.handlebars_resource_index_script = Handlebars.compile(template);
        break;
    }
  });

});

Resource.showModal = function(title, body) {
  $('#info_modal_title').html("" + title);
  $('#info_modal_body').html("" + body);

  return true;
}

Resource.prototype.showInfo = function() {
  if (document.body.className == 'pl') {
    Resource.showModal(this.infoData.description.pl.fullName, this.infoData.description.pl.description);
  } else {
    console.log(this.infoData.description.en);
    Resource.showModal(this.infoData.description.en.fullName, this.infoData.description.en.description);
  }
}

Resource.prototype.showIndexElement = function () {
  var _html;

  this.infoData.description.pl.idn = this.id;
  this.infoData.description.pl.url = this.infoData.url;
  this.infoData.description.pl.imgSrc = this.id + ".png";
  this.infoData.description.en.idn = this.id;
  this.infoData.description.en.url = this.infoData.url;
  this.infoData.description.en.imgSrc = this.id + ".png";
  _html = Resource.handlebars_resource_index_script(this.infoData.description);

  // var _html = Resource.handlebars_resource_index_script(this.infoData.description.pl);
  /*console.log("THIS INFODATA DESCRIPTION PL:::");
  console.log(this.infoData.description.pl);
  _html += "<div>Test</div>";*/
  el = this.tag.append(_html);
  el = el.children("div#lexpresource_" + this.id);
  $("[rel=tooltip]").tooltip({html:true});
  var _this = this,
      counter = 0;

  el.click(function (event) {
    var arrow = $(this).find('.arrow');
    var specificClass = 'arrow';
    var checkIfTargetHasSpecificClass = event.target.className.indexOf(specificClass) > -1;

    if(event.target.nodeName != "SPAN" || checkIfTargetHasSpecificClass) {
      if(_this.element.state == "hidden" || _this.element.state == "empty") {
        $(arrow[0]).removeClass("glyphicon-chevron-down");
        $(arrow[0]).addClass("glyphicon-chevron-up");
      } else {
        $(arrow[0]).removeClass("glyphicon-chevron-up");
        $(arrow[0]).addClass("glyphicon-chevron-down");
      }
      _this.element.toggle();
    }
  });
}

function Resource(id, infoData, tag, lexp) {
  this.tag = tag;
  this.id = id;
  this.infoData = infoData;
  this.lexp = lexp;
  this.showIndexElement();
  this.element = new Element($("#lexpelement_" + this.id), this.id, this.infoData, this.lexp);
}