//get label of the specified select element

jQuery.fn.getLabel = function() {
    var a;
    this.each(function() {
    
    if (this.tagName == 'SELECT') {

        var selectElement = this;
        a = selectElement.options[selectElement.options.selectedIndex].text;
        //alert(a);
        }
    });
    
    return a;
}


jQuery.fn.manipulateConfigProds = function(data)
{
              //loop through so we can get the key => values from data
              jQuery.each(data.attributes, function(i,attribute){

                  //Assign the correct default label to the dropdowns depending on their attribute id during page load
                  jQuery("select[name=super_attribute["+ attribute.id+"]] option[value='']").text(attribute.label);

                  //on dropdown/combobox change, do the magic!
                  jQuery("select[name=super_attribute["+ attribute.id+"]]").bind("change", function(){
                    
                     //Re-Assign the correct default label to the dropdowns depending on their attribute id
                     jQuery("select[name=super_attribute["+ attribute.id+"]] option[value='']").text(attribute.label);

                       /*
                        * Get the label of the selected option
                        * - we will use the label to compare to the "JSON" label if that particular label is in stock or not
                        */
                       
                       var label = jQuery("select#attribute"+ attribute.id +" :selected ").text();
                    
                       var out_of_stock = false;
                       var product_id = 0;

                       jQuery.each(attribute.options, function(i,option){

                           //compare the selected label from the "JSON" label
                           if(label == option.label)
                               {
                                     /*
                                      * in_stock values are :
                                      * 1 if it is in stock, 0 if out of stock
                                      */
                                     
                                     if(option.in_stock == 0)
                                    {
                                         out_of_stock = true;
                                         
                                         //get the product id, we'll use this when notifying customer if this product is back in stock
                                         product_id = option.products;

                                         return false;
                                    }
                               }

                       });
                       
                        //alert("Out of stock? - " + out_of_stock + "   Product ID - " + product_id);

                        if(out_of_stock)
                        {
                            jQuery("button.btn-cart").hide();
                            jQuery("button.btn-notify-when-in-stock").show();

                            /*
                             * change the value of hidden input "product_id"
                             * - to be used for the "Notify Me When In Stock" functionality
                             */
                            
                            jQuery("input[name=product_id]").val(product_id);
                        }
                        else
                        {
                            jQuery("button.btn-cart").show();
                            jQuery("button.btn-notify-when-in-stock").hide();
                            /*
                             * reset product_id's value to 0
                             * - not really needed to do this
                             */

                            jQuery("input[name=product_id]").val(0);
                        }

                  }) ;

              });
}
