// -- DYNAMYC TEXTAREAS ------------------------------------------------------------------
jQuery(document).ready(function() {
  var __fix_textarea = function() {
    jQuery("textarea").each(function() {
      if (this.scrollHeight > this.offsetHeight) {
        if (this.offsetHeight < 400) {
          if (this.scrollHeight < 400) {
            this.style.height = this.scrollHeight+"px";
          } else {
            this.style.height = "400px";
          }
        }
      }  
    });
    window.setTimeout(__fix_textarea, 100);
  }
  window.setTimeout(__fix_textarea, 100);
});
//*/

// -- HEIGHT LAYOUT HACK -----------------------------------------------------------------
/*
jQuery(document).ready(function() {
  var j_reforma = jQuery(".home #reforma");
  var j_jump = jQuery(".home #jump");
  var j_paradise = jQuery(".home #paradise");
  var j_trainers = jQuery(".home #trainers");
  var j_content = jQuery(".home .content.index_height");
  var j_height = jQuery(".home .index_height");

  function __set_height() {
    var height = j_reforma.height();
    var height1 = j_jump.height();
    var height2 = j_paradise.height();
    var height3 = j_content.height();
    var height4 = j_height.height();

    if (height1 > height) { height = height1 };
    if (height2 > height) { height = height2 };
    if (height3 > height) { height = height3 };
    if (height4 > height) { height = height4 };

    j_height.css("height", height);
    
    window.setTimeout(__set_height, 1000);
  }
  window.setTimeout(__set_height, 1000);
});
*/
//*/

// -- MENU -------------------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery(".menu .item")
    .hover(
      function() { jQuery(this).find(".drop_menu").show(); },
      function() { jQuery(this).find(".drop_menu").hide(); }
    );
});
//*/

// -- FAQ --------------------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery("div#faq .item a.block")
    .click(function() {
      jQuery(this).toggleClass("act").next().toggleClass("act");
      return false;
    });
});
//*/

// -- ONLINE CONSULT ---------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery("div.content_page div.online_block div.contents2").each(function() {
    var j_online = jQuery(this).height();
    jQuery("a.abs", this).css("height", j_online);
  });
});
//*/

// -- AJAX -------------------------------------------------------------------------------
var ui_wrap = null;

function ui_init() {
  if (ui_wrap) {
    return true;
  } else {
    ui_wrap = ReForma.Controls.Scripts;
    if (ui_wrap) {
      return true;
    } else {
      alert("ui_init(): ajax UI not avaiable");
    }
  }
}
function ui_status(method, data, status) {
  if (status=="success") {
    if (data) {
      if (data.Succeed) {
        return true;
      } else {
        alert(method+"(): " + data.Exception.Message);
        return false;
      }
    } else {
      alert(method+"(): AJAX data have not received");
      return false;
    }
  } else {
    alert(method+"(): AJAX status '" + status + "' have received");
    return false;
  }
}
//*/

// -- POLLS ------------------------------------------------------------------------------
function ui_poll_load() {
  if (ui_init()) {
    function __callback(data, status) {
      if (ui_status("ui_poll_load", data, status)) {
        jQuery("#div_poll").html(data.Value);
      } else {
        jQuery("#div_poll").html("");
      }
    }
    ui_wrap.AjaxLoadPoll(__callback);
  }
}
function ui_poll_vote(poll_id) {
  if (ui_init()) {
    function __callback(data, status) {
      if (ui_status("ui_poll_load", data, status)) {
        jQuery("#div_poll").html(data.Value);
      } else {
        jQuery("#div_poll").html("");
      }
    }
    
    var j_input = jQuery("#div_poll input[@name='VOTE_ANSWER']:checked");
    if (j_input.length > 0) {    
      var answer_id = parseInt(j_input.attr("value"));
      ui_wrap.AjaxVotePoll(poll_id, answer_id, __callback);
    } else {
      alert("Пожалуйста, выберите вариант ответа");
    }
  }
  return false;
}
function ui_poll_skip(poll_id) {
  if (ui_init()) {
    function __callback(data, status) {
      if (ui_status("ui_poll_load", data, status)) {
        jQuery("#div_poll").html(data.Value);
      } else {
        jQuery("#div_poll").html("");
      }
    }
    ui_wrap.AjaxSkipPoll(poll_id, __callback);
  }
  return false;
}
//*/

// -- POPUP LINKS ------------------------------------------------------------------------
function ui_popup(url) {
  var window_url = url;
  var window_id = "POPUP";
  window.open(window_url, window_id, "menubar=0,resizable=0,width=930,height=720,scrollbars=yes"); 
}
jQuery(document).ready(function() {
  var base = jQuery("base").attr("href");
  jQuery("a.ct_popup").each(function() {
    var j_a = jQuery(this);
    j_a.attr("href", "javascript: ui_popup('" + base + "" + j_a.attr("href") + "');");
  });
});
//*/

// -- MESSAGES ---------------------------------------------------------------------------
function ui_message_delete(message_id) {
  if (ui_init() && confirm("Вы действительно хотите удалить это сообщение?")) {
    function __callback(data, status) {
      if (ui_status("ui_message_delete", data, status)) {
        jQuery("#div_message_"+message_id).remove();
      }
    }
    ui_wrap.AjaxMessageDelete(message_id, __callback);
  }
  return void(0);
}
jQuery(document).ready(function() {
  jQuery("div.members_list table.members_item tr").each(function() {
    var j_ml_h = jQuery(this).height() - 15;
    jQuery(this).find(".item").css("height", j_ml_h);
  })
});
//*/