/* dodanie do koszyka*/	
function addToCart(product_id, type) {
	  
	  $('#product_id').val(product_id);
	  $('#cart_form').submit();
}
//dodanie grupy produktow do koszyka
$(function() {
	$('.add_group_to_cart').click(function() {
		var product_id = $(this).attr("name");
		showAjaxLayer ('add_to_cart_from_list');
	});
});

/**
 * Informacja o niedostepnosci produktu w
 * momencie proby dodadania do koszyka
 * @return
 */
function notAvailable(){
	$('#msg_standard p').html('Produkt jest niedostępny i nie może zostać dodany do koszyka.'); 
	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');
}

//  czyszczenia porownywarki
function cleanCompare() {
    $('#clean_compare').click(function() {
        
        $.post(
                'default/store_compare/ajax-clean-compare',	
                {},
                function(response) {   
                	$('#msg_standard p').html(response.message); 
                	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');
                		$('#compare_products_box').html(response.html);                	
                },
                'json'
        );
        return false;        
    }); 
}     

// usuniecie produktu z porownywarki
function removeProduct(){
	$('.remove_product_from_compare').click(function(){
		
        var product_id = $(this).attr("name");
        $.post(
                'default/store_compare/ajax-remove-product',
                {id: product_id},
                function(response) {
                	$('#msg_standard p').html(response.message); 
                	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');

                	$('#compare_products_box').html(response.html);

                	// uaktywnia uswanie boxow
                	removeProduct();
                	cleanCompare();
                },
                'json'
        );
        return false;      		   	
	});
}
    
// dodanie produktu do porownywarki 
function addProductToCompare() {
    $('.add_to_compare').click(function() {
    	
	    var product_id = $(this).attr("name");
	    $.post(
	            'default/store_compare/ajax-add-product',
	            {id: product_id},
	            function(response) {
	            	$('#msg_add_to_compare p').html(response.message);
	            	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_add_to_compare&modal=false');
	
	            	if(response.result) {
	            		$('#compare_products_box').html(response.html);
		            	// uaktywnia uswanie boxow
		            	removeProduct();
		            	cleanCompare();
	            	} 

	            },
	            'json' 
	    );	
	    return false;        
    });
}

$(document).ready(function(){
	
	/*
	 * Te 3 funkcje powinny byc uruchamiane po kazdej zmianie struktory html
	 * tak by jquery moglo sobie znalezc odpowienie klasy i przypisac im
	 * odpowiednie akcje
	 */   
	
    // uaktywnienie dodawania do porownywarki
    addProductToCompare();
   
    // uaktywnienie usuwania
    removeProduct();
    
    // uaktywanienie czysczenia porownywarki
    cleanCompare();
});