var panelState = {
  show: false
};

$(document).ready(function() {

    $(".details-link").click(function (e) {
		if ($("#add_" + $(this).attr('id')).css('display') == 'none') {
            $("#add_" + $(this).attr('id')).show();
			$("#hellip_" + $(this).attr('id')).hide();
			$(this).html("Скрыть&nbsp;&laquo;");
		} else {
			$("#add_" + $(this).attr('id')).hide();
			$("#hellip_" + $(this).attr('id')).show();
			$(this).html("&nbsp;Подробней&nbsp;&raquo;");
		}
		e.preventDefault();
    });

  //$.datepicker.setDefaults($.datepicker.regional['ru']);

  // все ссылки с данным классом будут жить с кликом, при котором будет делаться аджакс запрос
  $('.ajax-link, #ajax-dialog-form-containter a').live('click', function() {

    var http_type = 'get';
    var data = {};

    if ($(this).hasClass('confirm-action') && !confirm('Вы уверены?')) return false;

    if ($(this).hasClass('http-method-delete')) {
      http_type = 'post';
      data.sf_method = 'delete';
    }

    $.ajax({
      async: true,
      data: data,
      cache: false,
      url: $(this).attr('href'),
      timeout: 4000,
      type: http_type,
      error: function() {
        console.log('Ошибка запроса данных: ' + $(this).attr('href'));
      },
      success: function(data) {
      }
    });
    return false;
  });


  $('<p class="ajax-loading"><img src="/images/design/journal/wait_ax.gif" align="middle" width="33" height="33" />' + ' Загрузка...' + '</p>')
    .ajaxStart(function() {
    $(this).show();
  })
    .ajaxStop(function() {
    $(this).hide();
  })
    .appendTo('body');


  $("#panel-switcher").click(function(e) {
    var panel = $('#ppanel');
    var switcher = $("#panel-switcher", panel);
    if (panelState.show) {
      panel.animate({'left' : "-=300"}, 400);
      switcher.text("→");
    } else {
      panel.animate({'left' : "+=300"}, 400);
      switcher.text("←");
    }
    panelState.show ^= true;
  });

  $('.content-box').click(function (e) {
    if (panelState.show) {
      var panel = $('#ppanel');
      var switcher = $("#panel-switcher", panel);
      panel.animate({'left' : "-=300"}, 400);
      switcher.text("→");
      panelState.show ^= true;
    }
  })
  ;
});


$.fn.showModalDialog = function(newContentElements) {
  $( "#dialog" ).dialog( "destroy" );
  $( "#dialog-message" ).dialog( "destroy" );


  return this.each(function() {
    console.log('==============='+$(this).attr('id'));
    var el = $(this).append(newContentElements);
      el.find('#dialog-message').dialog({
      modal: true,
      width: 800,
      buttons: {
        Ok: function() {
          $(this).dialog("close");
        }
      }
    });
  });
};

$.fn.messageBox = function(msg) {
  alert(msg);
};

$.fn.extend({
insertAtCaret: function(myValue){
  return this.each(function(i) {
    if (document.selection) {
      this.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
      this.focus();
    }
    else if (this.selectionStart || this.selectionStart == '0') {
      var startPos = this.selectionStart;
      var endPos = this.selectionEnd;
      var scrollTop = this.scrollTop;
      this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
      this.focus();
      this.selectionStart = startPos + myValue.length;
      this.selectionEnd = startPos + myValue.length;
      this.scrollTop = scrollTop;
    } else {
      this.value += myValue;
      this.focus();
    }
  })
}
});
