product_detail = {   
    
    with_sizes : false,
    
    sizes_groups : {},


    add_options_to : function(node, children) {
        for(var i = 0; i < children.length; i++) {
            children[i].selected = false;
            node.appendChild(children[i]);
        }
    },

    remove_all_options_of : function(node) {
        while(node.childNodes.length >= 1 ) {   
            node.removeChild(node.firstChild);
        }
    },    

    /*
     * Changes the image set being displayed on screen. The set to be displayed      
     */
    show_set : function(set, update_finish_select) {
        
        var sets = $('images_wrapper').getElementsBySelector('div.product_image_set')  
        
        if (sets.length == 0) {
            return; // No images at all
        }
        
        var show_set = null;
        if (set == null) {
            show_set = sets[0];
        } else {
            show_set = set;
        }
        
        for(var i = 0; i < sets.length; i++) {           
            if (sets[i] == show_set) {
                product_detail.swap_main_image_with(sets[i].down('a'));
                Element.show(sets[i]);
            } else {
                Element.hide(sets[i]);
            }
        }
        
        var finish_select =  $('finish_select');
        if(finish_select && update_finish_select && show_set.id != 'images_0') {            
            var option_id = show_set.id.gsub('images_', 'finish_option_');
            var finish_options = finish_select.options;
            
            var i;
            for(i=0; i < finish_options.length; i++){
              if (finish_options[i].id != option_id) {
                finish_select.selectedIndex = i;
              }
            }

            product_detail.change_finish(finish_select);
        }
    },
    
    /*
     * Adds 
     */
    add_block_navigation_links : function(container) {        
        
        var sets = $('images_wrapper').getElementsBySelector('div.product_image_set')                
        // Only add navigation if there are at least two sets
        if (sets.length < 2) {
            if (sets.length == 0) {
                $('main_image').down('a').onclick = function() { return false; }
            }
            return;
        }
        
        // Append an HTML block of links
        sb = new StringBuffer();                        
        sb.append('<ul id="image_set_selector">');        
        sb.append('<h4>');
        sb.append(js_messages.more_images);
        sb.append(': ');
        sb.append('</h4>');        
        
        for (var i = 0; i < sets.length; i++) {
            var set = sets[i];
            id = set.id;
            name = set.down('h3').innerHTML;            
            
            sb.append('<li><a href="#" rel="');
            sb.append(id);
            sb.append('">');
            sb.append(set.down('h3').innerHTML);
            sb.append('</a></li>');            
        }                
        sb.append('</ul>');
        
        new Insertion.Bottom(container, sb.to_s());
        
        // Add new event handlers to the newly added links
        links = $$('#image_set_selector a');
        for (var i = 0; i < links.length; i++) {
            Event.observe(links[i], 'click', 
                product_detail.event_handlers.set_selector_click.
                    bindAsEventListener(links[i]));
        }
        
    },
        
    swap_main_image_with : function(link) {
        var image_div = $('main_image');
        var a = image_div.down('a');        
        a.href = link.href;
        var img1 = a.down('img');
        var img2 = link.down('img');
        img1.src = img2.src.gsub('mini', 'medium');
    },
    
    change_finish : function(select) {
        var selected_option = select.options[select.selectedIndex];
        var finish_id = selected_option.id.gsub('finish_option_','');
            
        with(product_detail) {
            show_set($('images_' + finish_id), false);
            if (with_sizes) {
                show_size_for_finish(finish_id);
                show_size_values(null);
            } else {  
                var selected_option = select.options[select.selectedIndex];                            
                show_finish_values(selected_option);
            }
        }
    },  
    
    set_finishes_options : function() {            
        $$('#finish_select option').each(function(option) {          
            var finish_id = option.id.gsub('finish_option_','');
            eval('var finish_data = finishes.finish_' + finish_id);
            option.selected = false;
            option.setAttribute('price', finish_data[i][1]);                
            option.setAttribute('reference', finish_data[i][2]);            
            option.setAttribute('price_before_taxes_and_tariffs', finish_data[i][3]);
        })
    },
    
    show_size_for_finish : function(finish_id) {
        var select = $('size_select');
        var size_unavailable = $('size_unavailable');
        var cart_controls = $('add_to_cart_controls');
                
        options = null;
        eval('var options = product_detail.sizes_groups.finish_' + finish_id);
        
        with(product_detail) {            
            // Never tried to create the options!, create options and store them
            if(options == null) {
                options = [];
                eval('var sizes = sizes_by_finish.finish_' + finish_id);                
                for (var i=0; i < sizes.length; i ++) {
                    var option = document.createElement('option');
                    option.innerHTML = sizes[i][1];
                    option.value = sizes[i][0];
                    option.setAttribute('price', sizes[i][2]);                
                    option.setAttribute('reference', sizes[i][3]);
                    option.setAttribute('price_before_taxes_and_tariffs', sizes[i][4]);
                    options.push(option);
                }                
                eval('product_detail.sizes_groups.finish_' + 
                    finish_id + ' = options')
            } 
          
            // TODO : ADD hide amounts form          
            if (options.length == 0) {
                remove_all_options_of(select);
                Element.hide(select);
                Element.hide(cart_controls);
                size_unavailable.removeClassName('hide');
            } else {               
                remove_all_options_of(select);
                add_options_to(select, options);
                Element.show(select);
                Element.show(cart_controls);
            }
       }              
    },

    show_finish_values :function(finish_option) {
    
        if (finish_option == null) {
            finish_option = $('finish_select').down('option');
        }
        
        product_detail.set_ui_values_using_option_attributes(finish_option);
    },

    show_size_values : function(size_option) {     
        
        if (size_option == null) {
            size_option = $('size_select').down('option');           
        }
       
        // Still null!!!
        if (size_option == null) {
            $('selected_price').innerHTML = js_messages.product_size_not_available;
            $('selected_reference').innerHTML = js_messages.product_size_not_available;
        } else {
            product_detail.set_ui_values_using_option_attributes(size_option);
        }
    },
    
    set_ui_values_using_option_attributes: function(option) {
        if (option.id) { $(option.id) }; // Wrap option with prototype magic
        $('selected_price').innerHTML = option.readAttribute('price');
        $('selected_reference').innerHTML = option.readAttribute('reference');
        
        var price_before_taxes_and_tariffs = 
            $('price_before_taxes_and_tariffs');
            
        if (price_before_taxes_and_tariffs) {
          price_before_taxes_and_tariffs.innerHTML = 
              option.readAttribute('price_before_taxes_and_tariffs');
        }
    },

    /*
     * EVENT_HANDLERS
     */
    event_handlers : {
        image_click : function(ev) {
            Event.stop(ev);
            product_detail.swap_main_image_with(this);          
        },
        
        set_selector_click : function(ev) {
            Event.stop(ev);
            product_detail.show_set($(this.rel), true);
        },
        
        finish_change : function() {
            product_detail.change_finish(this);            
        }, 
        
        size_change : function() {
            var selected_option = this.options[this.selectedIndex];            
            product_detail.show_size_values(selected_option);
        }
    },

    
    /*
     * BEHAVIOURS
     */
    behaviours : {        
        'body' : function() {
            product_detail.with_sizes = (typeof sizes_by_finish != 'undefined');
            var finish_select = $('finish_select');
            if (finish_select) {
              product_detail.change_finish(finish_select);
            }
        },
                 
        '#images_wrapper a' : function(a) {
            Event.observe(a, 'click', product_detail.event_handlers.image_click.bindAsEventListener(a));
        },
                  
        '#images_wrapper' : function(div) {      
            with(product_detail) {
                add_block_navigation_links(div);
                show_set(null, true);
                Element.show(div);
            }
        },
        
        '#finish_select' : function(select) {            
            
            with(product_detail) {
                if(!with_sizes) {                    
                    set_finishes_options();                    
                    show_finish_values();
                }
            
                Event.observe(select, 'change', event_handlers.finish_change.bind(select));                       
            }
        },
        
        '#size_select' : function(select) {
            
            if (!product_detail.with_sizes) { 
                return;
            }
            
            with(product_detail) {
                Event.observe(select, 'change', event_handlers.size_change.bind(select));
                show_size_values(null);
            }
        }      
    }
};

// Register common rules
Behaviour.register(product_detail.behaviours);
