var snum = 3+2+1;
window.addEvent('domready', function() {  
    
    // prvotni vytvoreni
    // vyhledani vychoziho typu1,2 podle vybrane reference
    if (window.reference!==undefined) {
        tp1 = 0;
        tp2 = 0;
        if (refinfo.id_selected==0) {
            reference.some(function(item, index) {
            tp1 = index;
            ref  = new Hash(item);
            ref.some(function(item, index) {
                tp2 = index;
                return true;
                });
                return true;
            });
        } else {
            reference.some(function(item, index) {
            tp1 = index;
            ref  = new Hash(item);
            return ref.some(function(item, index) {
                tp2 = index;
                return index==refinfo.id_selected;
                });
            });
        }
        createRef(tp1,tp2);
    }
    
    // pridani udalosti na odeslani formulare
    $('detailForm').addEvent('submit', function(e) {
        e.stop();
        
        var ajax = new Request({
                method: 'post', 
                url: this.action,
                data: {'quantity': this.quantity.value,
                       'id_reference': this.id_reference.value,
                       'addCart_x': 'xx'},
                onComplete: function(data) {
                    // nastav obsah vracenych elementu
                    updateElements(JSON.decode(data));
                    //alert(data);
                    }
        }).send();
        
    });
}); 

function createRef(tp1, tp2) {
    text = '';
    text += '<label for="type1">' + refinfo.type1 + ':</label>\n';
    // pokud je jen jeden typ1
    if (reference.getKeys().length==1) {
        reference.each(function(item, index) {
            text += '<input type="hidden" name="type1" id="type1" value="'+index+'" />\n'+
                    '<strong>'+index+'</strong><br />\n';
        });
    } else {
        text += '<select name="type1" id="type1" onChange="changeRef();">\n';
        reference.each(function(item, index) {
            text += '<option value="'+index+'"'; 
             if (index==tp1) {
                text += ' selected="selected"';
            }
            text += '/>'+index+'</option>\n';
        });
        text += '</select><br />\n';
    }
    // typ2
    ref = new Hash(reference[tp1]);
    // pokud je jedna
    if (refinfo.type2){
         text += '<label for="id_reference">' + refinfo.type2 + ':</label>\n';
    }
    if (ref.getKeys().length==1) {
        
        ref.each(function(item, index) {
            text += '<input type="hidden" name="id_reference" id="id_reference" value="'+index+'" />\n';
            if (refinfo.type2){
                    text += '<strong>'+item.txt+'</strong><br />\n';
            }
            cena = item;
        });
    } else {
        text += '<select name="id_reference" id="id_reference" onChange="changeRef();">\n';
        issettp2=0;
        ref.each(function(item, index) {
            if (!ref.has(tp2) && issettp2==0) {
                tp2 = index;
                issettp2=1;
            }
            text += '<option value="'+index+'"';
            if (index==tp2) {
                text += ' selected="selected"';
                cena = item;
            }
            text += '/>'+item.txt+'</option>\n';
        });
        text += '</select><br />\n';
    }

    text += '<div class="price">cena:</div>\n'+
            '<strong';
    if (refinfo.discount!=0) {
        text += ' class="prices"';
    }
    text += '><span><strong>' + cena.price + ' ('+ cena.pricewo+')</span>\n';
    if (refinfo.discount!=0) {
        text += '<br /><strong>'+cena.dprice+' ('+cena.dpricewo+')\n';
    }
    text += '</strong><br />';
    
    $('refbox').set('html', text);
    
    
}

function changeRef() {
    createRef($('type1').get('value'),$('id_reference').get('value'));
}

function destroyConfirm() {
    if ($('goods-in-cart')) {
        $('goods-in-cart').destroy();
    }
}