﻿// -- AJAX -------------------------------------------------------------------------------
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;
  }
}
var ui_wrap = null;
function ui_init() {
  if (ui_wrap) {
    return true;
  } else {
    ui_wrap = RainGroup.Controls.Scripts;
    if (ui_wrap) {
      return true;
    } else {
      alert("ui_init(): ajax UI not avaiable");
    }
  }
}
//*/

// -- SLIDER -----------------------------------------------------------------------------
jQuery.fn.applySlider  = function(__width, __min_left, __offset) {
  return this.each(function() {
    var context = this;

    var slider = jQuery("div.slides", this);

    var count = jQuery("div.feat_photos", this).length;

    var width = count * __width;
    var min_left = -width + __min_left;

    var max_left = 0;

    var l_arrow = jQuery("div.arrow_left", this);
    var r_arrow = jQuery("div.arrow_right", this);

    slider
      .css("width", width+"px")
      .css("left", "0px")
    ;

    function __scroll(mod) {
      if (slider.is(":animated")) return;

      var left = parseInt(slider.css("left"));

      left += mod;

      if (left <= min_left) {
        left = min_left;
        r_arrow.removeClass("bw").removeClass("rbw").addClass("rbw");
      } else {
        r_arrow.removeClass("bw").removeClass("rbw").addClass("bw");
      }
      if (left >= max_left) {
        left = max_left;
        l_arrow.removeClass("bw").removeClass("lbw").addClass("lbw");
      } else {
        l_arrow.removeClass("bw").removeClass("lbw").addClass("bw");
      }

      slider.animate({ left: left }, 500, "swing");
    }

    if (count < 4) {
      l_arrow.hide();
      r_arrow.hide();
    } else {
      l_arrow.bind("click", function() { __scroll(__offset); });
      r_arrow.bind("click", function() { __scroll(-__offset); });
    }
  });
}

jQuery(document).ready(function() {
  jQuery("div.info div.featured").applySlider(301, 903, 903);
  jQuery("div.info div.b_photos").applySlider(293, 879, 879);
});
//*/

// -- TEXT AREA EXPANDABLE ---------------------------------------------------------------
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);
});
//*/

// -- COMMENTS ---------------------------------------------------------------------------
function ui_comment_delete(comment_id) {
  if (ui_init() && confirm("Вы действительно хотите удалить этот комментарий?")) {
    function __callback(data, status) {
      if (ui_status("ui_comment_delete", data, status)) {
        jQuery("#div_comment_"+comment_id).remove();
      }
    }
    ui_wrap.AjaxCommentDelete(comment_id, __callback);
  }
}
//*/

// -- NAVIGATION MENU --------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery("div.header .nav > table > tbody > tr > td").hover(
    function() {
      jQuery(this).find(".menu_hide_div_abs").show();
    },
    function() {
      jQuery(this).find(".menu_hide_div_abs").hide();
    }
  )
});
//*/

/* -- FORM -------------------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery("#form a.search_abs").click(function() {
    jQuery("#form .main_form").hide();
    jQuery("#form .search_form").show();
    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, answer_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.AjaxVotePoll(poll_id, answer_id, __callback);
  }
  return false;
}
//*/

// -- HOT --------------------------------------------------------------------------------
jQuery(document).ready(function() {
  jQuery("#div_hot .bar_tabs a").bind("click", function() {
    var j_a = jQuery(this);
    jQuery("#div_hot .bar_tabs .item").removeClass("active");
    j_a.closest(".item").addClass("active");
    jQuery("#div_hot .hot_content .item").hide();
    jQuery("#"+j_a.attr("rel")).show();
  });
  jQuery("#div_hot_office, #div_hot_trade, #div_hot_store")
    .find(".content_text .content .n_item:last")
    .css("border-bottom", "0px none");
});
//*/

// -- SEARCH -----------------------------------------------------------------------------
jQuery(document).ready(function () {
  var j_base = jQuery("base");
  var j_query = jQuery("#div_search_box .query");
  var j_button = jQuery("#div_search_box .button");
  function __do_watermark() {
    var q = j_query.attr("value");
    if (q == "") j_query.addClass("watermark");
  }
  function __do_search() {
    var q = j_query.attr("value");
    if (q != "") document.location.href = j_base.attr("href") + "yandex/?searchid=146366&text="+encodeURIComponent(q);
    return false;
  }
  j_query.each(__do_watermark);
  j_query.bind("keypress", function(e) {
    jQuery(this).removeClass("watermark"); 

    var evt = e || window.event;
    if (evt.keyCode == 13) {
      return __do_search();
    }
  });
  j_query.bind("focus", function() { 
    jQuery(this).removeClass("watermark"); 
  });
  j_query.bind("blur", __do_watermark);
  j_button.bind("click", __do_search);

  jQuery("input.noautocomplete").attr("autocomplete", "off");

  function __decode_params(j) {
    var h = {};
    var pairs = j.split("&");
    for (i = 0; i < pairs.length; i++) {
      var g = pairs[i];
      var f = g.indexOf("=");
      if (f > 0) {
        var e = g.substring(0, f).toLowerCase();
        var d = g.substring(f + 1, g.length);
        h[e] = d;
      }
    }
    return h;
  }
  jQuery("#yandex-results-outer").each(function () {
    var params = __decode_params(document.location.search.substr(1));
    if (params.text != undefined && params.searchid != undefined) {
      if (params.web == undefined) params.web = "0";
      if (params.text == undefined) params.text = "";
      
      var href = document.location.href;
      var index = href.indexOf("#");
      if (index > -1) {
         href = href.substring(0, index);
      }

      var src = "http://yandex.ru/sitesearch?text=" + params.text + "&searchid=" + params.searchid + "&web=" + params.web + "&frame=1&topdoc=" + encodeURIComponent(href);

      var html = '<iframe scrolling="no" frameborder="0" style="border-width: 0px; margin: 0px; width: 100%; height: 1200px;" src="'+ src +'"/>';

      jQuery(this).html(html);
    }
  });
});
//*/