var PREFIX = "";

$(document).ready(function(e) {
  $("#sLocalitate").change(function(){
    getZone($("#sLocalitate").val());    
  });
   
  $(".filterGo").click(function(event){$("#frmFilter").submit(); event.preventDefault();});
  
  $('#homeImg').innerfade({ speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '220px' });   
  
  $("a.imagineLink").fancybox({'overlayOpacity': 0.5}); 
    
  if($("#sLocalitate").attr("selectedIndex") != '')
  {
    getZone($("#sLocalitate").val(), sZona);
  }
  
  $('a.favLink').click(function(event){event.preventDefault();});
  $('a.printPage').click(function(event){window.print(); event.preventDefault();});
  
  $("#recperpage").change(function(){changeRec(this.value)});
  
  $("img#imgSec").click(function(){this.src="include/script_generate_code.php?rnd="+Math.random()});
  
  $("#frmContact").validationEngine();
	$("#frmFav").validationEngine();
});

function getZone(idLoc, selected)
{
  if(idLoc != '')
  {
    var options = '';
    $.getJSON(PREFIX + "/ajax.server.php?act=getZone&id=" + idLoc,
            function(data)
            {   
               $('#sZona').find('option').remove();
               $.each(data.zone, function(i, item){
                //$('#sZona').append(new Option(item.denumire, item.id));
               options += "<option value=\"" + item.id + "\">" + item.denumire + "</option>";
              $("#sZona").html(options);
            });
            if (selected != '') 
            {
               try 
               {
                  $("#sZona").val(parseInt(selected)); 
               }
               catch(ex) {
                  setTimeout("$('#sZona').val('" + selected + "')",1); 
               }
            }                                       

      });            
  }
  else
  {
    $('#sZona').find('option').remove();
    $('#sZona').append(new Option('- toate -', ''));
  }
}

function addToFav(id){
  $.getJSON(PREFIX + "/ajax.server.php?act=addToFav&id=" + id, function(data)
  {
    $("a.oferteFav").html("oferte favorite (" + data.count + ")");
    if($("#favTotal"))
    {
      $("#favTotal").html("Total oferte favorite: " + data.count);
    }
    if(data.act == 'add')
    {
      $("#fav_"+id).html("sterge din favorite");
    }
    else
    {
      $("#fav_"+id).html("adauga la favorite");
      if($("#ofertaFav_"+id))
      {
        $("#ofertaFav_"+id).fadeOut("slow");        
      }
      if(data.count == 0)
      {
        $("#favoriteContainer").fadeOut("slow");
        $("#oferteFav0").fadeIn("slow");        
      }
    }
  });
}

function changeRec(val)
{
  //window.location.href = ""
}

