var oferta = '';
var cat = '';
var tip = '';
var judet = '';
var url = 'vanzari.php';
function getDataTo(elem, label) {
  $.post(url, {of: oferta, ca: cat, ti: tip, ju: judet, el: elem, la: label}, function(data) {
    $('#c'+elem).html(data.select);
    $('#cbunuri').html(data.bunuri);
  }, 'json');
}
function getData() {
  $.post('vanzari.php', {of: oferta, ca: cat, ti: tip, ju: judet}, function(data) {
    $('#cbunuri').html(data);
    pagi();
  }, 'html');
}
function getMeniu() {
  $.post('meniu.php', {ju: judet}, function(data) {
    $('#cmeniu').html(data);
  }, 'html');
}
function getJudete() {
  $.post('judete.php', {of: oferta, ca: cat, ti: tip, ju: judet}, function(data) {
    $('#cseljud').html(data);
  }, 'html');
}
$(window).load(function() {
  var menuo = false;
  //getJudete();
  getMeniu();
  $('#seljud').live('change', function() {
    judet = $(this).val();
    getMeniu();
    getData();
    $('#cbunuri').html('');
  });
  $('#meniu>li>ul>li>ul>li>a').live('click', function() {
    if($(this).parent().hasClass('acj')) {
      $('#meniu li.acj ul').slideToggle(100);
    } else {
      $('#meniu li.acj ul').slideUp(100);
      $('#meniu li.acj').removeClass('acj');
      $(this).parent().attr('class', 'acj');
      $('#meniu li.acj ul').slideDown(100);
    }
    menuo = true;
  });
  $('#meniu>li>ul>li>ul>li>ul>li>a').live('click', function() {
    menuo = true;
  });
  $('#meniu a').live('click', function() {
    var qry = $(this).attr('href').split('#');
    if(qry.length == 1) {
      oferta = qry[0];
      cat = '';
      tip = '';
      judet = '';
    } else if(qry.length == 2) {
      oferta = qry[0];
      cat = qry[1];
      tip = '';
      judet = '';
    } else if(qry.length == 3) {
      oferta = qry[0];
      cat = qry[1];
      tip = qry[2];
      judet = '';
    } else if(qry.length == 4) {
      oferta = qry[0];
      cat = qry[1];
      tip = qry[2];
      judet = qry[3];
    }
    if(menuo) {
      menuo = false;
    } else {
      $('#meniu li.acj ul').slideUp(100);
      $('#meniu li.acj').removeClass('acj');
    }
    $('#meniu a').removeClass('activ');
    $(this).addClass('activ');
    //getJudete();
    getData();
  });
});






function pagi() {
  var show_per_page = 10;
  var number_of_items = $('#cbunuri .item').size();
  var number_of_pages = Math.ceil(number_of_items/show_per_page);
  if(number_of_pages > 1) {
    $('#current_page').val(0);
    $('#show_per_page').val(show_per_page);

    var navigation_html = '<a class="previous_link" href="javascript:previous();">Înapoi</a>';
    var current_link = 0;
    while(number_of_pages > current_link) {
      navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link + ')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
      current_link++;
    }
    navigation_html += '<a class="next_link" href="javascript:next();">Înainte</a>';
    $('#page_navigation').html(navigation_html);
    $('#page_navigation .page_link:first').addClass('active_page');
    $('#cbunuri .item').css('display', 'none');
    $('#cbunuri .item').slice(0, show_per_page).css('display', 'block');
  }
}

function previous(){
  new_page = parseInt($('#current_page').val()) - 1;
  if($('.active_page').prev('.page_link').length==true){
    go_to_page(new_page);
  }
}

function next(){
  new_page = parseInt($('#current_page').val()) + 1;
  if($('.active_page').next('.page_link').length==true){
    go_to_page(new_page);
  }
}
function go_to_page(page_num){
  var show_per_page = parseInt($('#show_per_page').val());
  start_from = page_num * show_per_page;
  end_on = start_from + show_per_page;
  $('#cbunuri .item').css('display', 'none').slice(start_from, end_on).css('display', 'block');
  $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
  $('#current_page').val(page_num);
}

