(function($){
$.fn.extend({
  placeholder: function(text) {
    return this.each(function(){
      var obj = $(this);
      if (obj.val().length > 0 && obj.val() != text) {
        obj.attr('typed', '1');
      } else {
        obj.val(text).css('color', '#777');
      }
      obj.focus(function(){
        this.style.color = '#000';
        this.value =  this.getAttribute('typed') ? this.value : ''; 
      }).blur(function(){
        if ($.trim(this.value) == '') {
          this.value = text;
          this.setAttribute('typed', '');
          this.style.color = '#777';
        } else {
          this.setAttribute('typed', '1');
        }
      });
    });
  }
});
})(jQuery);



function submitItemForm(form) {
  $.post('market.php?act=save', form.serialize(), function(data, textStatus) {
    if (data['error']) {
      $('#errorMessage').html('<div class="alertmsg">' + data['error'] + '</div>').slideDown('fast');
    } else {
      var photosForm = $('#photosForm').get(0);
      if (photosForm.file1.value) {
        photosForm.hash.value = data.hash;
        photosForm.rhash.value = data.rhash;
        photosForm.aid.value = data.album_id;
        photosForm.mid.value = data.mid;
        photosForm.gid.value = data.item_id;
        photosForm.submit();
      } else {
        location.replace('market.php?act=view&id='+data.item_id+'&show=my&m=1');
      }
    }
  }, 'json');
  return true;
}


function getCat(obj, cat) {
   if (obj.className.indexOf('_active') != -1) {
     return false;
   };
   $('#categories a').each(function(){
     if (this.className.indexOf('_active') != -1) {
      this.className = this.className.substr(0, this.className.indexOf('_active'));
     }
   });
   obj.className += '_active';
   $('#itemsList').html('<div id="progressBar"></div>');
   
   if ($('#searchForm').length) {
     $('#category').val(cat);
     if (!$('#searchQuery').val().length) {
       $('#searchField').attr('disabled', 'disabled');
     }
     var data = $('#searchForm').serializeArray();
     $('#searchField').attr('disabled', '');
   } else {
     var data = {show: 'my'}
   }
   $('#itemsList').load('market.php?act=get_items', data);
   
   
   return false;
}

function getPage(offset) {
  $('#pageProgressTop').show();
  $('#pageProgressBottom').show();
  
  if ($('#searchForm').length) {
    var data = {
      q: $('#searchQuery').val(),
      cat: $('#category').val(),
      section: $('#selectSection').val(),
      show: $('#show').val(),
      st: offset
    };
  } else {
    var data = {
      show: 'my', 
      cat:  $('#category').val(),
      st: offset
    }
  }
  
  $('#itemsList').load('market.php?act=get_items', data, function(){
    $('#pageProgressTop').hide();
    $('#pageProgressBottom').hide();
    $("html,body").animate({ 
      scrollTop: 0//$('#itemsList').position().top 
    }, "normal");
    $("body").css('scrollTop', 0);
  });
  return false;
}

var deleteBox;
function deleteItem(id) {
  if (!deleteBox) deleteBox = new FadeBox({title: 'Подтверждение'});
  deleteBox.removeButtons().addButton({
    label:'Да',
    onClick: function(){
      location.replace('market.php?act=delete&id=' + id);
    }
  }).addButton({
    label: 'Нет',
    style: 'buttonNo',
    onClick: function(){
      deleteBox.hide();
    }
  }).content('Вы уверены, что хотите удалить это объявление?').show();
  return false;
}
var photoBox;
function showPhoto(url) {
  
  if (!photoBox) photoBox = new FadeBox({type: 'POPUP', width: 'auto'});
  photoBox.content('<img src="' + url + '" />').show();
  return false;
}