var ob;

function callJS(jsStr) { 
  return eval(jsStr)
}

function open_gtshow(page, id)
{
 window.open('/mcat/'+page+'?id='+id+'','gtview',
 'width=760,height=700,scrollbars=yes,resizable=yes');
}

function open_gtselect(page)
{
 var gt=document.all('gt').options[document.all('gt').selectedIndex].value;
 window.open('/mcat/'+page+'?id='+gt+'','gtview',
 'width=760,height=700,scrollbars=yes,resizable=yes');
}

function open_lamselect(page, id)
{
 window.open('/mcat/'+page+'?id='+id+'','gtview',
 'width=760,height=400,scrollbars=yes,resizable=yes');
}

function reset_images(index)
{
    document.all('k_t').value='';
    document.all('k_t_img').src='/images/choose_companion.gif';
    document.all('m_t').value='';
    document.all('m_t_img').src='/images/choose_obivka.gif';
    $('#hp').val( $('#gt option:selected').attr('mebelus:price') );
    if (index > 0)
       {
    document.getElementById('cloth1').style.display = 'block';
    document.getElementById('cloth2').style.display = 'block';
       }
      else
       {
    document.getElementById('cloth1').style.display = 'none';
    document.getElementById('cloth2').style.display = 'none';
       }
}

function updateHandlers() {
    $('#window-select .txt3').click(function(){
        return false;
    });

    $('.btn-select').click(function(){
        if ( ob.is('.cl-main') ) {
            $('#m_t').val( $(this).attr('mebelus:tid') );
            $('#m_t_img').attr('src', $(this).attr('mebelus:link'));
        }
        if ( ob.is('.cl-second') ) {
            $('#k_t').val( $(this).attr('mebelus:tid') );
            $('#k_t_img').attr('src', $(this).attr('mebelus:link'));
        }
        return false;
    });
}

$(document).ready(function(){
    $('.table-cell').hover(function(){
        $(this).addClass('active-cell');
    }, function(){
        $(this).removeClass('active-cell');
    });
    
    $('.gt-radio').click(function(){
        $('.cl-main img').attr('src', '/images/choose_obivka.gif');
        $('.cl-second img').attr('src', '/images/choose_companion.gif');
        $('#hp').val( $('.gt-radio:checked').attr('mebelus:price') );
        $('#k_t').val('');
        $('#m_t').val('');
        $('#window-select').html('');
        $('#window-select').hide();
        $('.cl').removeClass('cl-active');
    });
    
    $('.cl').click(function(){
        ob = $(this);
        
        $('.cl').removeClass('cl-active');
        ob.addClass('cl-active');
        
        if ( $('#window-select').is(':visible') ) {
            updateHandlers();
            return true;
        }

        $('#window-select').html('<p align="center">Загрузка тканей...</p>');
        $('#window-select').show();

        var gt = $('.gt-radio:checked');
        if ( !gt.is(':checked') ) {
            $('#window-select').html( '<p align="center">Категория ткани не выбрана</p>' );
        }
        else {
            $.get( 
                '/cgi-bin/mebelus/mebelus.pl',
                { action: 'show_ts', id: gt.val(), inline: '4' },
                function( data ) {
                    $('#window-select').html( data );
                    updateHandlers();
                    initLytebox();
                }
            );
        }
        
    });
    
}); 