$(document).ready(function() {

	// currentCompareItemIds icinde olanlari secili yap
	$('.cmpChk')
		.attr('checked', false)
		.filter(function() {
			return $.inArray( parseInt($(this).metadata().itemId), currentCompareItemIds) >= 0;
		}).attr('checked', true);

	// karsilastirma sepetinden cikar
	$(".ldelfromcmp").live("click" , function(e) {
		var itemId = $(this).metadata().itemId;
		$.ajax({
			url: "/resources/visitors/compareitems/" + itemId,
			
			type: "DELETE"
		});
		
		var parent = $(this).parents(".spt_item");
		var topParent = $(parent).parents(".bo_yorum");
		$(parent).next(".clear").remove();
		$(parent).next(".border_dashed").remove();
		$(parent).next(".clear10").remove();
		$(parent).remove();

		$(".bo_yorum:not(:has(.spt_item))").remove();

		return false;
	});
	var currentCompareItemIds = [];
	// Karsilastirma icin sec/cikar
	$(".cmpChk").click(function(e) {
	
		var _itemId = $(this).metadata().itemId;
		var categoryId = parseInt($(this).metadata().categoryId);
		// farkli bir kategoriye gectiyse degerleri degistir, checkedleri yenile
		if(currentCompareCategoryId != categoryId){

			currentCompareCategoryId = categoryId;

			$('.cmpChk').filter(function() {
					return $.inArray( parseInt($(this).metadata().itemId), currentCompareItemIds) >= 0;
				}).attr('checked', false);

			$.each(currentCompareItemIds, function(index, id) {
				$.post("/banaozel/delFromCurrentCompareItems.do", {itemId: id});
			});

			
		}

		if ($(this).attr('checked')){
			$.post("/banaozel/addToCurrentCompareItems.do", {itemId: _itemId});
			currentCompareItemIds.push(_itemId);
		}	else {
			$.post("/banaozel/delFromCurrentCompareItems.do", {itemId: _itemId});
			currentCompareItemIds = _.without(currentCompareItemIds, _itemId);
		}
		
	});

	// Urunleri karsilastir
	$(".cmpBtn").click(function(e) {
		var categoryId = parseInt($(this).metadata().categoryId);
		if(currentCompareCategoryId != categoryId){
			currentCompareCategoryId = categoryId;
		}
		if(currentCompareItemIds.length < 2){
			alert('En az 2 ürün karşılaştırabilirsiniz ! ');
			return false;
		}
	});
});

