var panel_open=false;
function p$(targetid)
{
  return $('#' + targetid).get(0);
}
function kosar_req(pars)
{
//  alert("kreq");
 // console.log(pars);
  if(typeof(pars)!='object')
  {
  var popen=(+panel_open);
  if(pars.length>0) pars+="&"; else pars+="?";
  pars+="need_page="+popen;
  //console.log(pars);
  }
  $.getJSON("kosar.php",  pars,
  function(data) {
//      alert("ajaxfun");
      update_classes(data,{kosar_empty: function(hs)
{
  
  switch(hs)
  {
    case "hide": $(".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();
}

}
);
  }
  
  );



}



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))
{
  //console.log("updating:",thecoll);
  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?
{
//alert("ajax_forms");
//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){
//alert("sm");
ev.preventDefault();
//alert("submit");
//var serd=ev.target.formSerialize();
var serd= $(this).formSerialize();

kosar_req(serd);


return false;
});
thecoll.addClass("ajax_forms_active");

}

function showkosar()
{
  		var panel=$("#panel");
		panel.slideToggle("slow");
		$.get('kosar.php',{k_view: 'popup'},function(data) {
		  panel.html(data);
		  ajax_forms('.ajax_kosar_form');
		  });
		panel_open=true;
   
  
}

$(function() {
  //Lightbox.init();
//$(document).append("<div id='kosar_box'>");	
$(".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')});
  });


    $(".scrollable").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		auto: 800,
    speed: 1000
    });

  
  
  
//$("div.scrollable").scrollable({
//interval: 1000,
//loop: true
//});
  $('#close').click(function(ev) {
    kosar_req({type: "refresh", k_id: page_prodid});
  });
  $("a.lightbox").nyroModal();
  /*click(function(ev)
    {
      
       event.preventDefault();
      
      el=Event.findElement(ev,'a');
      if (el != document)
      Lightbox.showBoxImage(el.readAttribute("href"));
      
    });*/
  
  //$('.megrendeles').click(showkosar);
    	$(".btn-slide").live("click",function(){
$(this).toggleClass("active");
showkosar();
return false;
	});
  
  
  ajax_forms(".ajax_kosar_form");
  //$(".ajax_kosar_form").removeClass('ajax_kosar_form');

  
  $('#slickbox').hide();
  $('a#slick-toggle').click(function() {
 $('#slickbox').toggle(400);
 return false;
  });

}

);






