(function($) {
	$.fn.equalHeight = function() {
		var group = this;
		var tallest = 0;
		group.each(function() {
						thisHeight = $(this).height();
						if(thisHeight > tallest) {
							tallest = thisHeight;
						}
					});
					
					
					group.height(tallest);
					
		
							  
							  
							  
	
	}
})(jQuery)


$().ready(function() {
				//alert('test');
				
				
				$.post("/classes/custom/ajax_bag.php", {action:'create'}, function(data){$("#count").html(data.count); $("#sum").html(data.sum);}, "json");
				
				
				$(".cg").equalHeight();
				
				
				
				
				
				$(".spinner").spinner({min:0});
				
				
				
				$().ajaxStart(function(){
				 $.blockUI({ message: '<p><img align="absmiddle" src="/schemas/acsf/images/bigrotation2.gif" /> Подождите...</p>', showOverlay: false, timeout:1000});
				 });

				
				$().ajaxStop(function(){
				  	//$.unblockUI();
				 });
			
				/*function equalHeight(group) {
					tallest = 0;
					//group.hide();
					group.each(function() {
						thisHeight = $(this).height();
						if(thisHeight > tallest) {
							tallest = thisHeight;
						}
					});
					
					alert(tallest+'|'+group.size());
					group.height(tallest);
					
					
					//group.show();

				}

				equalHeight($(".cg"));*/
				//$("#glist").css('visibility', 'visible');
				
				
				
				//setTimeout("$.spinnerStop();",2000); // Stop after 2 seconds
			});
			
			
			jQuery.fn.stripTags = function() {
         return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '')
 );
 };

			
			
function add(gid){
	
	var gcount_array = Array();	
	var i = 0;
	var blockHtml = '<table class="bag_block">'
	
	$('#count_cont_'+gid+' .spinner').each(function(){
								
								//alert($(this).attr("value"));
								//count[$(this).attr("colorid")] = Array($(this).attr("value");
								//blockHtml+=	$("#name_"+gid+"").html()+$("#"+gid+"_"+id).html()										  
								var q = $(this).val();
								var id = $(this).attr("colorid");
								$(this).val(0);
								if(q!=0){
								
									gcount_array[i] = id+'='+q+'';
									i++;
								}
								var name = $("#name_"+gid+"").html().replace(/<\/?[^>]+>/gi, '');
								var color_name = $("#"+gid+"_"+id).html();
								
								
								alert($("#"+gid+"_"+id).attr('price'));
								
								if($("#"+gid+"_"+id)==null){
									var price = $("#price_"+gid).html();
								}
								else{
									var price = $("#"+gid+"_"+id).attr('price');
								}
								
								
								
								
								
								if(q>0){
								blockHtml+='<tr><td class="name">'+name+'<br>'+(color_name==null?'':color_name)+'</td><td class="qu">'+q+' шт.</td><td class="qu">'+$("#"+gid+"_"+id).attr('price')+' руб.</td></tr>';
								}
								//blockHtml+=	name+$("#"+gid+"_"+id).html()+q+$("#"+gid+"_"+id).attr('price');
								
								
								}
					   
					   );
	
	blockHtml+="</table>";
	
	
	
	if (gcount_array.length > 0) {
	
	
	

	
	
		$.post("/classes/custom/ajax_bag.php", {
			action: 'add',
			gid: gid,
			q: gcount_array.join(':')
		}, function(data){
			$("#count").html(data.count);
			$("#sum").html(data.sum);
		}, "json");
	}
	
	 $.blockUI({ message: blockHtml,  centerY: 0, 
            css: { top: '10px', left: '', right: '10px'}, showOverlay: false, timeout: 2000});
	
	
}
