 function parseaffinfo(){

	aff=$("#affinfo").val();
	prids="";
	$("span.ids").each(function () { 
	  if(prids!="")prids+=",";
	  prids+=$(this).attr("id");
	  });
	 
	if (prids!="")
		$.ajax({
			url: "http://www.kupizoloto.com/ajpricespr.php",
			data: {affinfo: aff, ids: prids},
			dataType: 'jsonp',
			jsonp: 'callback',
			jsonpCallback: 'jsonpCallbackIDS',
			success: function(){ }
		});
	
	if($(".variants").length>0){
		$.ajax({
			url: "http://www.kupizoloto.com/ajpricespr.php",
			data: {affinfo: aff, id: $(".variants").attr("id")},
			dataType: 'jsonp',
			jsonp: 'callback',
			jsonpCallback: 'jsonpCallbackID',
			success: function(){ }
		});
	  
	  }
	 
 }


function jsonpCallbackID(arr){
	sel=$(".variants select");
	
	for (var i in arr) {
		var obj = arr[i];
		sel.append( $('<option value="'+obj.value+'" price="'+obj.cena+'" pricesk="'+obj.cenask+'">'+obj.value+'</option>') );
		if (i==0) $('#price_value').val(obj.cena); 
	  }
  }

function jsonpCallbackIDS(arr){
	$.ajax({
        type: 'post',
        url: 'index.php?route=product/product/formatpricearr',
        dataType: 'html',
        data:  {data: JSON.stringify(arr)}, 
        success: function (data) {
			res=eval('('+data+')');

			for (var pid in res) {
				var obj = res[pid];
				if(obj.cena!=0){
				   
				   $("*[id="+pid+"] .product_price").html(obj.cena);
				   if(obj.cenask!=0)$("*[id="+pid+"]  .product_pricesk").html(obj.cenask);
				   
				   //$("td#"+pid).css("display","");
				   $("tr#"+pid).css("display","");
				   
				  }
			}
			
			$('.list').each(function(index) {
			  var stack=[];
			  
			  list=$(this).find("td");
			  list.each(function(index) {
					el=list.eq(index);
					pid=parseInt(el.attr("id"))
					
					if (pid>0){
					   var obj = res[pid];
					   if(obj.cena!=0)stack.push(index);
					   }
			    });
			  
			  i=0;
			  while(stack.length>0){
			    ind=stack.shift();
				list.eq(i).html(list.eq(ind).html());
				list.eq(i).css("display","");
				i++;
				}
				
			});
			
			
        }
    });
}	 


 $(document).ready(function() {
    parseaffinfo();  
 });

