﻿var script_subdirectory = ''; 

$(document).ready(function() {
  $('#image_upload input').after('&nbsp;&nbsp;&nbsp;<b style="font-size:14px;cursor:pointer;" onclick="add_input()">+</b>');
  $('a[rel=lightbox]').lightBox({overlayBgColor: '#000', overlayOpacity: 0.8, txtImage : 'Фото №', txtOf : 'из', containerBorderSize : 20, imageLoading: script_subdirectory+'/template/lb/loading.gif', imageBtnClose: script_subdirectory+'/template/lb/close.gif', imageBtnPrev: script_subdirectory+'/template/lb/prev.gif', imageBtnNext: script_subdirectory+'/template/lb/next.gif'});
  
});

function add_input()
{
  if ($('#image_upload img').size()+$('#image_upload input').size()<9)
    $('#image_upload').append('<div><input name="req_file[]" type="file">&nbsp;&nbsp;&nbsp;<b style="font-size:14px;cursor:pointer;" onclick="remove_input(this)">-</b></div>');
  else
    alert('Можно добавлять не более 9 изображений.');
}

function remove_input(node)
{
  $(node).parent().remove();
}

function set_image(normal, big)
{
  $('img', $('a[rel=lightbox]').attr('href', big)).css('background', 'url('+normal+') no-repeat center center');
}

function check_post_form()
{
  var error = '';

  if($('#city select:last').val()==-1)
    error += "Не выбран \"Город\" в который вы хотите подать объявление.\n";
  if($('#category select:last').val()==-1)
    error += "Не выбрана \"Рубрика\" в которую вы хотите подать объявление.\n";
  if($.trim($('input[name=ads_title]').val())=='')
    error += "Не заполнено поле \"Заголовок объявления\".\n";
  if($.trim($('textarea[name=ads_description]').val())=='')
    error += "Не заполнено поле \"Текст объявления\".\n";
  if(($.trim($('input[name=ads_phone]').val())=='') && ($.trim($('input[name=ads_email]').val())=='') && ($.trim($('input[name=ads_messenger]').val())=='') && ($.trim($('input[name=ads_url]').val())=='') && ($.trim($('input[name=ads_adress]').val())==''))
    error += "Не указано ни одного способа связи с вами.\n";
  if($.trim($('#pos').val())=='true')
    error += "Обязательно введите защитный код.\n";
  if (error!='')
  {
    alert(error);
    return false;
  }
  return true;
}

function select_category(n)
{
  var current_select = $('#category select:eq('+n+')');
  $('input[name=category_id]').val($('#category select:eq('+n+')').val());
  if (current_select.val()>-1)
  {
    $.get(script_subdirectory+'/command.php', {cmd: 'get_subcategories', id: $('#category select:eq('+n+')').val()},
     function(data){
       data = $.trim(data);
       if (!data)
       {
         $('#category select:gt('+n+')').remove();
         return;
       }
       data = data.split("\n");
       if (data.length)
       {
         var new_select = '<select style="display:block; width:350px;" onchange="select_category('+(n+1)+')">';
         new_select+='<option value="-1">Выберите</option>';
         $('input[name=category_id]').val(-1);
         for(var i=0; i<data.length; i++)
         {
           data[i] = data[i].split('|');
           new_select+='<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
         }
         new_select+= '</select>';
         if ($('#category select:eq('+(n+1)+')').size())
           $('#category select:eq('+(n+1)+')').replaceWith(new_select);
         else
           $('#category').append(new_select);
         select_category(n+1);
       }
       else
         $('#category select:gt('+n+')').remove();
     });
  }
  else
    $('#category select:gt('+n+')').remove();
  

}

function select_city(n)
{
  var current_select = $('#city select:eq('+n+')');
  $('input[name=sel_city_id]').val($('#city select:eq('+n+')').val());
  if (current_select.val()>-1)
  {
    $.get(script_subdirectory+'/command.php', {cmd: 'get_subcity', id: $('#city select:eq('+n+')').val()},
     function(datacity){
       datacity = $.trim(datacity);
       if (!datacity)
       {
         $('#city select:gt('+n+')').remove();
         return;
       }
       datacity = datacity.split("\n");
       if (datacity.length)
       {
         var new_select = '<select style="display:block; width:350px;" onchange="select_city('+(n+1)+')">';
         new_select+='<option value="-1">Выберите</option>';
         $('input[name=sel_city_id]').val(-1);
         for(var i=0; i<datacity.length; i++)
         {
           datacity[i] = datacity[i].split('|');
           new_select+='<option value="'+datacity[i][0]+'">'+datacity[i][1]+'</option>';
         }
         new_select+= '</select>';
         if ($('#city select:eq('+(n+1)+')').size())
           $('#city select:eq('+(n+1)+')').replaceWith(new_select);
         else
           $('#city').append(new_select);
         select_city(n+1);
       }
       else
         $('#city select:gt('+n+')').remove();
     });
  }
  else
    $('#city select:gt('+n+')').remove();
  

}
