/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 1.4 $
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

function checkBeforeComparison()
{
	var id_list = '';
        var i = 0;
	$('.compare_checkbox:checked').each(
		function()
		{
                    i++;
                    id_list += $(this).val() + '|';
		}
	);
        return i;
//	$('.compare_product_list').val(id_list);
//
//	if ($('.comparator:checked').length == 0)
//	{
//		alert(min_item);
//		return false;
//	}

	return true;
}


function reloadBlock(data){
    $('#compare_block_ajax').html(data);
}


function delProductInPage(product_id){
    var url = baseDir + 'modules/productcompareblock/product-compare-ajax.php';
    var href= $('#compare_href').val();
     $.post(url,
    {
        product_id : product_id,
        mod : "delete"
    },function(data){
        document.location.href=href;
    },"json");
    
}

function reloadDiv(data,prod_id){
    if(prod_id)
        $('#comparator_div_'+prod_id).html(data);
   
}

function deleteCompareProduct(data){
    var url = baseDir + 'modules/productcompareblock/product-compare-ajax.php';
    var product_id = $(data).find(".hid_compare").attr('value');
    $('#comparator_item_'+product_id).attr({
        checked:false
    });
    $.post(url,
    {
        product_id : product_id,
        mod : "delete"
    },function(data){
        
        reloadDiv(data.link,product_id);
        reloadBlock(data.block);
    },"json");
}



function deleteAllCompare(){
    $('.compare_checkbox').attr({
        checked:false
    });
     var url = baseDir + 'modules/productcompareblock/product-compare-ajax.php';
     $.post(url,
    {
        product_id : false,
        mod : "delete_all"
    },function(data){
        window.location.reload(true);
    },"json");

}


function checkForComparison(product_id)
{
    var mod = $('#comparator_item_'+product_id).attr('checked') == true ? 'add' : 'delete';
    var url = baseDir + 'modules/productcompareblock/product-compare-ajax.php';
    var i = checkBeforeComparison();
    if(i > 20){
         alert(max_item);
    }else{
         $.post(url,
            {
                product_id:product_id,
                mod:mod

            },function(data){
               if(data.block){
                    reloadDiv(data.link,product_id)
                    reloadBlock(data.block);
               }else{
                   if(data == 2){
                       alert(max_item);
                   }else{
                       alert(category_err);
                   }
                    $('#comparator_item_'+product_id).attr({
                        checked:false
                    });
               }
            },"json");
    }
    
}

function checkForComparisonCategory(product_id){
      var mod = $('#comparator_item_category_'+product_id).attr('checked') == true ? 'add' : 'delete';
     var url = baseDir + 'modules/productcompareblock/product-compare-ajax.php';
     $.post(url,
            {
                product_id:product_id,
                mod:mod

            },function(data){
               if(data.block){
                    reloadDivCategory(data.link,product_id)
                    reloadBlock(data.block);
               }else{
                   if(data == 2){
                       alert(max_item);
                   }else{
                       alert(category_err);
                   }
                    $('#comparator_item_category'+product_id).attr({
                        checked:false
                    });
               }
            },"json");
}

function reloadDivCategory(data,prod_id){
      if(prod_id)
        $('#comparator_div_category'+prod_id).html(data);
}


