$(document).ready(function() {
	updateComparedList('comparebox');
});

var prodcount = 1;

function updateComparedList(tpl) {
	var checked = $('#comparelist .cbox :checked');
	var count = checked.length;
	var template = templates[tpl];
	
	if(count < 3 && count > 1) {
		$('#comparelist .cbox :checkbox').each(function() {
			$(this).removeAttr('disabled');
		});
	} else if(count == 3) {
		$('#comparelist .cbox :checkbox').each(function() {
			$(this).attr('disabled', 'true');
		});
		$('#comparelist .cbox :checked').each(function() {
			$(this).removeAttr('disabled');
		});
	}

	if(count > 0) {
		var i=0;
		$('#cproducts .cprod').each(function() {
			var current = $(this);
			$(current).html('');
			if( i < count ) {
				prodcount = 1;
				$('.attrcol').hide(); 
				var prodEAN = checked[i].name;
				$.post(base_uri + 'compare/getinfo/' + prodEAN, {}, 
				function(data){
					var html = '';
					html = template.replace(/\[NAME\]/g, data.name);
					html = html.replace(/\[PRICE\]/g, data.price);
					html = html.replace(/\[PATH\]/g, data.path);
					html = html.replace(/\[BRAND\]/g, data.brand);
					html = html.replace(/\[ID\]/g, data.id);
					html = html.replace(/\[EAN\]/g, data.ean);
					html = html.replace(/\[IMGURL\]/g, data.imgurl);
				
					var infotext = '';
					if(data.multiple == true) {
						infotext += '<td class="info" colspan="2"><span class="buy_nav"><a href="/store/category/product/'+data.path+'/'+data.brand+'/'+data.id+'">VAIHTOEHDOT</a></span></td>';
					} else {
						infotext += '<td class="info"><span class="buy_nav"><a href="/store/category/product/'+data.path+'/'+data.brand+'/'+data.id+'"><img src="/store/static/img/info_img.gif" alt=""/></a></span></td><td class="buy"><span class="buy_nav"><a href="javascript:void(0);" onmouseup="addToCart(\''+data.path+'/'+data.brand+'/'+data.id+'/'+data.ean+'\', this.parentNode, \'[NAME]\')"><img src="/store/static/img/buy_img.gif" alt=""/></a></span></td>';
					}
				
					html = html.replace(/\[INFOTEXTS\]/g, infotext);
				
					$(current).html(html);
					
					$('.prod-' + prodcount + '-name').html(data.name);
					$('.prod-' + prodcount + '-name').show();

					$('.compareinformation').find('TR').each(function() {
						var elid = $(this).attr('id');
						var tmp = elid.split('-');
						var attrid = parseInt(tmp[2]);
						
						$('#product-' + prodcount + '-attribute-' + attrid).html('&nbsp;');
						
						if( data.attributes[attrid] != '' )
							var attrdata = data.attributes[attrid];
						else
							var attrdata = '';
						
						$('#product-' + prodcount + '-attribute-' + attrid).html(attrdata);
					});
				
					$('.prod-' + prodcount).show(); 
					
					prodcount++;
				}, 'json');
				
				$(this).show();
			} else {
				$(this).hide();
			}
			i++;
		});
	}
}

function updateComparedProducts(productpath, add) {
	if(add) {
		$.post(base_uri + 'compare/add/' + productpath,
		function(data){
			updateCompareCount(parseInt(data));
		});

	} else {
		$.post(base_uri + 'compare/remove/' + productpath,
		function(data){
			updateCompareCount(parseInt(data));
		});
	}
}

function updateCompareCount(compareCount) {
	if(compareCount > 0) {
		$('#comparetable #comparecount').html(compareCount);
	} else {
		$('#comparetable #comparecount').html('0');
	}
}
