/**
 * Funkcja sprawdzajaca czy wybrano mocowanie 
 * przy dodawaniu do koszyka
 * 
 * @return boolean
 */
function checkBindings(force_unchecked) {

//	alert('to sie w ogole dzieje?');
  /* jesli produkt nie jest grupa nie sprawdzaj mocowan  */
 
  if(product_type != Store_Product_TYPE_GROUP && !force_unchecked) {
    return true;
  }
  
  checked = 0;
//    $(".bindings_form").find("input").each(function(i) {
//        checked = 0;
//    });

    $(".bindings_form").find("input:checked").each(function(i) {
        checked = 1;
    });
    
    if( force_unchecked )
    {
    	checked = 0;
    }

    if (checked == 0) {
        /* Wywolaj warstwe ajaksowa z komunikatem*/
//        showAjaxLayer ('add_to_cart_warning');
    	showAjaxLayer("select_binding");
        return false;
    }
    else {
        return true;
    }
}
/**
 * Sprawdzanie ybranego mocwania
 * przy dodawani zestawu
 */
function checkBindingsForSet()
{
    if(product_type != Store_Product_TYPE_GROUP) return true;
    checked = 0;

    $(".bindings_form").find("input:checked").each(function(i) {
        checked = 1;
    });

    if (checked == 0) {
        tb_show('', '#TB_inline?height=230&width=380&inlineId=layer_id_select_binding_for_sets&modal=false');
        return false;
    }
    else {
        return true;
    }
}

/**
 * Dodajemy produkt do zestawu,
 * zestaw tworzony jest na podstawie glownego produktu
 * 
 * @param subproduct_id
 * @return
 */
function addProductToSet() {
	
    $('.add_to_set').click(function() {
    	if(checkBindingsForSet()) {
            var main_product_id = $("#binded_product_id").attr("value");
            var subproduct_id = $(this).attr("name");
            if ($(this).is(":checked")) {

                        $.post(
                                'default/store_set/ajax-add-product',
                                {
                                    subproduct_id: subproduct_id,
                                    main_product_id: main_product_id
                                },
                                function(response) {

                                    if(response.result) {
                                            $('#set_products_box').html(response.html);

                                            removeProductFromSet();
                                            removeSet();

                                        /* dodanie zestawu do koszyka*/
                                        $('#add_set_to_cart').click(function(){
                                            var result = checkBindingsForSet();
                                            return result;
                                          });

                                            $('#set_products_box').show();
                                    }

                                },
                                'json'
                        );
            }
            else {
                    // odznaczenie czyli usuniecie z zestawu
                $.post(
                        'default/store_set/ajax-remove-product',
                        {
                            subproduct_id: subproduct_id,
                            main_product_id: main_product_id
                        },
                        function(response) {

                            $('#set_products_box').html(response.html);
                            if (response.items_in_set == 0) {
                                    $('#set_products_box').hide();
                            }

                            /* dodanie zestawu do koszyka*/
                            $('#add_set_to_cart').click(function(){
                                var result = checkBindingsForSet();
                                return result;
                              });

                            removeProductFromSet();
                            removeSet();
                        },
                        'json'
                );
            }
        }
        else {
            return false;
        }
    });
    

}

/**
 * Usuniecie produktu z zestawu
 * 
 * @param subproduct_id
 * @return
 */
function removeProductFromSet(){
	
	$('.remove_product_from_set').click(function(){	
		
		var main_product_id = $("#binded_product_id").attr("value");
		var subproduct_id = $(this).attr("name");
		
		$('.add_to_set[name='+subproduct_id+']').removeAttr('checked');
		
        $.post(
                'default/store_set/ajax-remove-product',
	            {
	            	subproduct_id: subproduct_id,
	            	main_product_id: main_product_id
	            },
                function(response) {
                	
	            	$('#msg_standard p').html(response.message); 
                	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');

                	$('#set_products_box').html(response.html);
                	
                	if (response.items_in_set == 0) {
                		$('#set_products_box').hide();
                	}
                	
  	                /* dodanie zestawu do koszyka*/	
	                $('#add_set_to_cart').click(function(){
	                    var result = checkBindingsForSet();
	                    return result;
	                  });
	                
                	removeProductFromSet();
                	removeSet();
                	 
                },
                'json'
        );
        return false;      		   	
	});
}

function removeSet(){
	
	$('.remove_set').click(function(){	
		
		var main_product_id = $("#binded_product_id").attr("value");
		var subproduct_id = $(this).attr("name");
		
		$('.add_to_set').removeAttr('checked');
		
        $.post(
                'default/store_set/ajax-remove-product',
	            {
	            	subproduct_id: subproduct_id,
	            	main_product_id: main_product_id
	            },
                function(response) {
                	
	            	$('#msg_standard p').html(response.message); 
                	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');

                	$('#set_products_box').html(response.html);
                	removeProductFromSet();
                	$('#set_products_box').hide();

                },
                'json'
        );
        return false;      		   	
	});
}

function notAvailableSpecificProduct()
{
	$('#msg_standard p').html('Produkt chwilowo niedostępny i nie może zostać dodany do koszyka. Zapytaj nas o dostępność produktu <a href="'+ ask_vendor_url + '">zadaj pytanie</a>'); 
	tb_show('', '#TB_inline?height=130&width=380&inlineId=msg_standard&modal=false');
}

$(document).ready(function() {


        $('td.col_2 a[onclick!=window.open],td.col_1 a[onclick!=window.open],a.img_opis').live('click',function(){
           $(this).lightBox();
        });
        
	addProductToSet();
	removeProductFromSet();
	
    $('.technologies').click(function() {
        popup = window.open($(this).attr('href'), '', 'scrollbars = 1, status = 1, height = 600, width = 600, resizable = 1');
        return false;
    });   
    
    /* dodanie do koszyka*/	
    $('#add_to_cart').click(function(){
        var result = checkBindings();
        return result;
      });
    
    /* produkt niedostepny */
    $('#add_to_cart_not_available').click(function(){
    	notAvailableSpecificProduct();
        return false;
      }); 
    
    /* dodanie zestawu do koszyka*/	
    $('#add_set_to_cart').click(function(){
        var result = checkBindingsForSet();
        return result;
      });  
    
   
    $('#change_binding').click(function(){
    	
    	var result = checkBindings(true);
    	return false;
    });
    
    // wybranie mocowania na warstwie
    $('#cart_form input[type="radio"]').click(function(){
    	var val;
    	val = $(this).val();
    	
    	$('#add_to_cart_layer').removeAttr('disabled');
    	$('#change_binding_layer').removeAttr('disabled');
    	$('#cart_form input[value="'+val+'"]').each(function(){
    		$(this).attr("checked","checked");
    	});
    });

    /* wybor mocowania przy zmianie dla zestawow */
    $('#select_binding_for_sets').click(function(){
        
        $('#binding_for_sets input').each(function(){
            if($(this).is(':checked')) {
             val = $(this).val();
            }
        });

        $('#add_to_cart_layer').removeAttr('disabled');
        $('#change_binding_layer').removeAttr('disabled');
        $('#cart_form input[value="'+val+'"]').each(function(){
                $(this).attr("checked","checked");
        });
        finded = false;
        if(val) {
            $(".bindings_form").find("input:radio").each(function(i){
                if($(this).val() == val) {
                    finded = true;
                    $(this).click();
                    return false;
                }
            });
        }
        else {
            return false;
        }
    });

    $('#add_to_cart_layer').click(function(){
        var result = checkBindings();
        if(result){
        	 $('#cart_form').submit();
        }
      });       
    
    $('#change_binding_layer').click(function(){
    	var result = checkBindings();
        if(result){
        	tb_remove();
        }
       
        return true;
      });       
});

    $(function() {

      
      /* pobierz nowa cene dla wybrnaje gwarancji  */
      $('#warranty_select').change(function() {
          var product_id = $("#product_id").attr("value");
          var warranty = $(this).attr("value");
          $('#warranty_type').val(warranty);
          
          $.post(
        		  'default/store_products/ajax-get-warranty-info',
                  {
        			  product_id: product_id,
        			  warranty: warranty  
                  },
                  function(response) {
                	  	/* ustawienie nowej ceny */
                	  	$('#price_label').html('Cena:');
						$('#product_promotional_price').html(parseInt(response['promotional_price']));
						$('.product_price').html(parseInt(response['price']));
                  },
                  'json'
          );
      });
;
      /* dodaj produkt do przechowalni     */
        $('#add_to_repository').click(function() {

            var product_id = $("#product_id").attr("value");
            $.get(
                    'default/store_repository/ajax-add-to-repository',
                    {product_id: product_id},
                    function(response) {
                    	showAjaxLayer ('product_added_to_repository');
                    },
                    'json'
            );
            return false;
        });

    /* pobrienie nowych informacji produktu po wybraniu mocowania       */
      $(".bindings_form").find("input:radio").each(function(i) {
          $(this).click(function(){
                showWaitingLayer();
            product_id = $(this).attr('value');
            // ustawienie id produktu z mocwaniem jako glownego produktu dla zestawu
            $("#binded_product_id").attr("value",product_id);
            $("#_binded_product_id").attr("value",product_id);
              $.post(
                      'default/store_products/ajax-get-product-info',
                      {product_id: product_id},
                      function(response) {
                        
						  /* ustawienie nowej ceny */
                    	  $('#price_label').html('Cena:');
                                if(parseInt(response.promotional_price)) {
			              $('.product_price').html(parseInt(response.promotional_price));
                                      $('.product_promotional_price').html(parseInt(response.price));

                                      $('.product_promotional_price_span_2').html('stara cena:');
                                      $('.product_promotional_price_span').html(' zł');
                                 } else {
			              $('.product_price').html(parseInt(response.price));
                                      $('.product_promotional_price').html('');
                                      $('.product_promotional_price_span_2').html('');
                                      $('.product_promotional_price_span').html('');
                                 }

			              $('#product_name').html(response.name);
			              
		              	  /* ustawienienowych danych specyfikacji */
			              $('#tabs_wrapper').html(response.html);
			              $('#warranty_box').html(response.html_w);
			              $('#images_box').html(response.html_i);
			              $('#technology_box').html(response.html_t);
			              $('#add_to_cart_box').html(response.html_c);
			              
                                        addProductToSet();
                                        removeProductFromSet();
                                        removeSet();
				        	
			              $('#add_to_cart').click(function(){
			                var result = checkBindings();
			                return result;
			              });
			              
			              /* uaktywnienie zmiany zamowienia */
			              $('#change_binding').click(function(){
			              	
			              	var result = checkBindings(true);
			              	return false;
			              });
			        	
			              /* uaktywnienie dodawania zestawu do koszyka*/	
			              $('#add_set_to_cart').click(function(){
			                  var result = checkBindingsForSet();
			                  return result;
			                });
			                
			              /* Restart paginacji */
  			              $("#paginacja").pagination();
			              
			              $('.technologies').click(function() {
			                  popup = window.open($(this).attr('href'), '', 'scrollbars = 1, status = 1, height = 600, width = 600, resizable = 1');
			                  return false;
			              }); 
			              tb_remove();

                                      $('td.col_2 a[onclick!=window.open],td.col_1 a[onclick!=window.open],a.img_opis').lightBox();
                                      tb_init('a.thickbox, area.thickbox, input.thickbox');

                                       $(".bindings_form").find("input:radio").each(function(){
                                            if($(this).val() == product_id) {
                                                $(this).attr('checked','checked');
                                                return false;
                                            }
                                        });
                      },
                      'json'
              );
          });
      });

    $(".bindings_form input[class='disabled']").attr('disabled','disabled');

  });
    
/**
 * Pokazuje warstwe z loaderm na czas pobierania infromacji 
 * wybranym mocowaniu
 * 
 * @return
 */    
function showWaitingLayer() 
{
        $('#TB_window,#TB_HideSelect').trigger("unload").unbind();
        $("#TB_load,#TB_window").fadeOut();
	tb_show('Ładowanie danych...','#TB_inline?height=20&width=800&inlineId=#loading_new_product_info&modal=true');
}

	
		

