document.observe("dom:loaded", function() {
    var showProductNav = false;
    function showSubnav() {
        $('subnav').show();
        $$('.main-container, .home-container, .footer-container').each(function(e) {
            e.addClassName('opacity30');
        });
        showProductNav = true;
    }

    function hideSubnav() {
        window.setTimeout(function() {
            if(!showProductNav) {
                $('subnav').hide();
                $$('.main-container, .home-container, .footer-container').each(function(e) {
                    e.removeClassName('opacity30');
                });
            }
        }, 300);
        showProductNav = false;
    }

    // Show/Hide product subnavigation
    Event.observe('products', 'mouseover',function(){showSubnav();});
    Event.observe('subnav', 'mouseover',function(){showSubnav();});
    Event.observe('products', 'mouseout',function(){hideSubnav();});
    Event.observe('subnav', 'mouseout',function(){hideSubnav();});

    var nav_li = $$('#nav li');
    nav_li.each(function(element){
        Event.observe(element, 'mouseover', function() {
            var subnav = element.down('ul');
            if (subnav) {
                subnav.show();
            }
        });
        Event.observe(element, 'mouseout', function() {
            var subnav = element.down('ul');
            if (subnav) {
                subnav.hide();
            }
        });
    });

    $$('.in-stock').each(Element.hide);
    var settings        = $$('.super-attribute-select');
    var radioButtons    = $$('ul.radiobuttons input[type="radio"]');
    var url = '/improove-catalog/product_configurable/available/';
    var stock_data;

    function loadStockStatus() {
            $$('.in-stock').each(Element.hide);
            $$('#stock-qty tr').each(Element.remove);
            new Ajax.Request(url, {
                method: 'get',
                parameters: $('product_addtocart_form').serialize(true),
                onSuccess: function(transport) {
                    var mediumSrc = '';

                    if (transport.responseText != '') {
                        var json = transport.responseText.evalJSON(true);
                        var row_class = 'odd';
                        $$('.in-stock').each(Element.hide);
                        $$('#stock-qty tr').each(Element.remove);
                        for (var key in json.stock) {
                            if (json.stock.hasOwnProperty(key)) {
                                $$('#stock-qty tbody')[0].insert({'bottom': '<tr class="'+row_class+'"><th>'+key+'</th><td>'+json.stock[key]+'</td></tr>'});
                                if (row_class == 'even') {
                                    row_class = 'odd';
                                } else {
                                    row_class = 'even';
                                }
                            }
                        }
                        if ('qty_per_sqm' in json) {

                            $$('.products-per-unit').first().innerHTML = $$('.products-per-unit').first().innerHTML.sub(/\d+(\.\d+)?/, (Math.round(json.qty_per_sqm * 100)/100).toFixed(2));
                            $$('.products-per-unit').first().title = json.qty_per_sqm;
                            spConfig.reloadPrice();
                            // element.fire("widget:frobbed", { widgetNumber: 19 });

                        }
                        $$('.in-stock').each(Element.show);
                        if (json.flickr_id.length) {
                            mediumSrc += $('flickr-' + json.flickr_id).down('a').href;
                            titletext = $('flickr-' + json.flickr_id).down('a').title;
                            $('product-image').down('img').src = mediumSrc;
                            $('product-image').down('a').title = titletext;
                            $('product-image').down('a').href = mediumSrc;
                        }
                    }
                }
            });
    }

    radioButtons.each(function(element){
        Event.observe(element, 'click', function() {
            var select = $(element.name.substr(1));
            if(select.select('option')) {
                select.select('option').each(function(o) {
                    if(o.readAttribute('value') == element.value) { // note, this compares strings
                        o.setAttribute('selected', 'selected');
                        throw $break;
                    }
                    else {
                        o.removeAttribute('selected');
                    }
                });
            }
            loadStockStatus();
            spConfig.reloadPrice();
        });
    });

    settings.each(function(element){
        Event.observe(element, 'change', function() {
            loadStockStatus();
        });
    });
});

function fireEvent(element,event){
    if (document.createEventObject){
    // dispatch for IE
    var evt = document.createEventObject();
    return element.fireEvent('on'+event,evt)
    }
    else{
    // dispatch for firefox + others
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true ); // event type,bubbling,cancelable
    return !element.dispatchEvent(evt);
    }
}

// Switch the product image source, alt and link
function switch_product_image(mediumSrc, alttext) {
    $('product-image').down('img').src = mediumSrc;
    $('product-image').down('img').alt = alttext;
    $('product-image').down('a').title = alttext;
    $('product-image').down('a').href = mediumSrc;

    // Update Lightbox
    Lightbox.prototype.updateImageList();
}
