function p$(targetid)
{
  return $('#' + targetid).get(0);
}
function kosar_req(pars)
{
//  alert("kreq");
  $.getJSON("kosar.php",  pars,
  function(data) {
//      alert("ajaxfun");
      update_classes(data,{kosar_empty: function(hs)
{
  
  switch(hs)
  {
    case "hide": {
        if(window.location.pathname=="/order.php")
	window.location.reload();
	else
      $(".tohide_orderlink").hide(); break;
    }
    case "show": $(".tohide_orderlink").show();break;
    
  }
},
kosaron: function(id){
  //alert('on'+id);
  $('.myaxu_kosarbag_'+id).show();
  $('.myaxu_kosarg_'+id).hide();
  
},
kosaroff: function(id){
  //alert('off'+id);
  $('.myaxu_kosarg_'+id).show();
  $('.myaxu_kosarbag_'+id).hide();
  
  
  
},free_van: function(vane){
  if(vane) $('.fcvan').show();
  else $('.fcvan').hide();
},preord_van: function(vane){
  if(vane) $('.myaxu_preord_van').show();
  else $('.myaxu_preord_van').hide();
},isfreeship: function(vane)
{
  if(vane) $('.myaxu_isfreeship').addClass("isfreeship");
  else $('.myaxu_isfreeship').removeClass("isfreeship");
}
/*,nonpreord_kosar_db: function(kdb) {
  var atl=$('.atvet_radio_1,.payment_radio_1').parent("label");
  var antiatl=$('.atvet_radio_4').parent("label");
  var uzenet='';
  if(kdb>0) {atl.show();antiatl.hide();
    if($('.atvet_radio_4').is(':checked')){
    $('.atvet_radio_4').attr("checked","");
    $('.atvet_radio_3').attr("checked","checked");
  }
  }
  else {
    atl.hide();
    antiatl.show();
  if($('.atvet_radio_1').is(':checked')){
    $('.atvet_radio_1').attr("checked","");
    $('.atvet_radio_4').attr("checked","checked");
    uzenet=uzenet+"A futár általi szállítás ilyen rendeléseknél nem választható. A szelvényeket akár ki is kinyomtathatja,  vagy kérésére díjmentesen postázzuk őket Önnek - kérjük válasszon a felkínált lehetőségek közül!\n\n";
  }
        if($('.payment_radio_1').is(':checked')){
    $('.payment_radio_1').attr("checked","");
    $('.payment_radio_2').attr("checked","checked");
    uzenet=uzenet+"Ezeknél a rendeléseknél nem ajánljuk az utánvétes fizetést!";
  }
if(uzenet.length) alert("Rendelése kizárólag szolgáltatásokat és/vagy utalványokat tartalmaz.\n\n"+uzenet);
  };

}*//*,preord_problem_van: function(vane){
  if(vane) $('.myaxu_preord_problem_van').show();
  else $('.myaxu_preord_problem_van').hide();
},paytotals: function(ptots){
  //if(console!=undefined) console.log(ptots.valaszt,ptots.utanvet);
  $("#orderprodcont").attr("class",ptots.prodcont_classes);
  if(ptots.valaszt>0) $(".myaxu_valaszt_cont").show();
  else {
    $(".myaxu_valaszt_cont").hide();
    $('.myaxu_valaszt_cont input').attr("checked","");
    $('.myaxu_valaszt_cont input:radio:nth(0)').attr("checked","checked");
    
  }
    if(ptots.utanvet>0) $(".myaxu_utanvet_cont").show();
  else $(".myaxu_utanvet_cont").hide();
  update_classes(ptots,{});
}*/

}
);
  }
  
  );



}



function testfn(data)
{
//  alert("testfn");
  
}
function update_classes(resp,nstdh)
{
var prefix="myaxu_", thecoll, ns, func;
//iterating through json vars
$.each(resp, function(pkey,pvalue){

//getting the collection
thecoll=$("."+prefix+pkey);
//getting the func to invoke on all the matched elements
func=nstdh[pkey];
//by default update the contents of the matched elements with the value
if(!$.isFunction(func))
{
  thecoll.html(pvalue);
  ajax_forms("."+prefix+pkey+" .ajax_kosar_form");
} else
{
//invoke the function if it exists with the value as parameter
  func(pvalue);
}


});
  
}

function ajax_forms(selector) //generalize with callbacks?
{
  //handling the click element of the remove button
  var thecoll;
  thecoll=$(selector).filter(":not(.ajax_forms_active)");
  thecoll.find(" .remv").click(function(ev)
    {
      var el=ev.target;
      $(this).parents("form").find(".input_qty").get(0).value=0;
     });
  //handling the submit element, stopping the event and submitting via ajax
  thecoll.submit(function(ev){
  ev.preventDefault();
  var serd= $(this).formSerialize();
  kosar_req(serd);
  return false;
  });
  thecoll.addClass("ajax_forms_active");
}

function showkosar()
{
		
		$.get("kosar.php",{k_view: 'popup'},function(data)
		      {
			$("#cart_container").html(data);
			ajax_forms('.ajax_kosar_form');
		      }
		      );					  
					  
		$("#panel").slideToggle("slow");
		$(".btn-slide").toggleClass("active");
		
	

/*
	$.nyroModalManual({  minWidth: 700,
  minHeight: 420,
ajax:{url: 'kosar.php',data:{k_view: 'popup'}},
endShowContent: function() {
   ajax_forms('.ajax_kosar_form');
      $("#axsel input").change(function()
      {
	var akc=$(this).val();
	kosar_req({type: 'setdata', akcio_choice: akc});
       });
      
}, endRemove: function() {
  if(window.location.pathname=="/order2.php" || window.location.pathname=="/order.php")
  window.location.reload();
}



});*/
    //$('#contenttoupdate').load('kosar.php',{k_view: 'popup'},function(resp) {
      //$('#kosar_box').load('kosar.php',{k_view: 'popup'},function(resp) {
        //  ajax_forms('#kosar_box .ajax_kosar_form');

      //});
    
//ajax_forms('#contenttoupdate .ajax_kosar_form');
//$(".ajax_kosar_form").removeClass('ajax_kosar_form');
  
  
} 

$(function() {

  $(".fav").live("click",function() {
  $(this).removeClass("fav").addClass("notfav").html("Kedevencekhez ad");
  $.get("setfaves.php",{fn: "unset", id: $(this).attr('rel')});
  });
$(".notfav").live("click",function() {
  $(this).removeClass("notfav").addClass("fav").html("Kedvencekből eltávolít");;
  $.get("setfaves.php",{fn: "set", id: $(this).attr('rel')});
  });

  
  
  $('#close').click(function(ev) {
    kosar_req({type: "refresh", k_id: page_prodid});
  });

  $("a.lightbox").nyroModal();
  $('.kosar_plus').live("click",function() {
    var qtyinp=$(this).parents("form").find("input.input_qty");

    qtyinp.val(parseInt(qtyinp.val())+1);
    });
    $('.kosar_minus').live("click",function() {
    var qtyinp=$(this).parents("form").find("input.input_qty");
    qtyinp.val(parseInt(qtyinp.val())-1);
    });
  
  $('.megrendeles').click(showkosar);
  ajax_forms(".ajax_kosar_form");
  //$(".ajax_kosar_form").removeClass('ajax_kosar_form');
}
);






