function dFilterOpen(elem) {
    var re = /^f_([a-z0-9_]+)_opener$/i;

    $('.f_popup_lev_1').hide();
    $('.f_popup_lev_2').hide();

    if (elem && (typeof(elem) != 'undefined') && (re.test(elem.id))) {
        var id = RegExp.$1;

        $('#f_' + id + '_popup').show();
    }
}

function dFilterSelect(elem, value) {
    var re = /^f_([a-z0-9_]+)_popup$/i;

    if (elem && (typeof(elem) != 'undefined')) {
        var txt = elem.innerHTML;

        if (re.test(elem.id)) {
            dFilterSetValue(RegExp.$1, value, txt);
        } else {
            while (elem.parentNode && (typeof(elem.parentNode) != 'undefined')) {
                if (re.test(elem.parentNode.id)) {
                    dFilterSetValue(RegExp.$1, value, txt);
                    return;
                }

                elem = elem.parentNode;
            }
        }
    }
}

function dFilterSetValue(id, value, txt) {
    //alert(id + ', ' + value + ', ' + txt);

    $('#f_' + id + '_val').val(value);
    $('#f_' + id + '_opener').html(txt);
    $('#f_' + id + '_popup').hide();

    $('.f_popup_opener_lev_2').hide();
    $('#f_cat_' + $('#f_cat_id_val').val() + '_subcat_id_opener').show();
}

/* ========================================================================== */

function changePhoto(imgId, src) {
    var imgNew = $('#' + imgId + '_new');
    var imgOld = $('#' + imgId + '_old');

    if ((imgNew.length > 0) && (imgOld.length > 0) && (typeof(src) != 'undefined') && (src != '')) {
        imgNew.attr('src', src);
        imgOld.fadeOut(250, function() {imgOld.attr('src', src); imgOld.show();});
    }
}

function changePage(elem, direction) {
    if (elem && (typeof(elem) != 'undefined') && direction && (typeof(direction) != 'undefined')) {
        var dx = 0;
        var page = $(elem.parentNode).find('.previews_list_inner');
        var holder = $(elem.parentNode).find('.previews_list');

        if ((page.length > 0) && (holder.length > 0)) {
            if (page.attr('offsetHeight') > holder.attr('offsetHeight')) {
                if (direction == 'up') {
                    dx = (Math.abs(page.attr('offsetTop')) > holder.attr('offsetHeight')) ? holder.attr('offsetHeight') : Math.abs(page.attr('offsetTop'));
                } else if (direction == 'down') {
                    dx = page.attr('offsetHeight') - Math.abs(page.attr('offsetTop')) - holder.attr('offsetHeight');
                    dx = (dx > holder.attr('offsetHeight')) ? -holder.attr('offsetHeight') : -dx;
                }

                if (dx != 0) {
                    page.animate({"top": page.attr('offsetTop') + dx}, 750);
                }
            }
        }
    }
}

/* ========================================================================== */

function hideInputLabel(inp, label) {
    if (inp && (typeof(inp) != 'undefined') && label && (typeof(label) != 'undefined')) {
        if (inp.value == label) {
            inp.value = '';
        }
    }
}

function showInputLabel(inp, label) {
    if (inp && (typeof(inp) != 'undefined') && label && (typeof(label) != 'undefined')) {
        if (inp.value == '') {
            inp.value = label;
        }
    }
}

/* ========================================================================== */

function priceCheckAllChackboxes() {
    $('.cats_list input[type=checkbox]').attr('checked', true);
}

function priceUncheckAllChackboxes() {
    $('.cats_list input[type=checkbox]').attr('checked', false);
}

function priceCheckChilds(chckbx) {
    if (chckbx && (typeof(chckbx) != 'undefined')) {
        $(chckbx.parentNode).find('input[type=checkbox]').attr('checked', chckbx.checked);
    }
}

function priceCheckParent(chckbx) {
    if (chckbx && (typeof(chckbx) != 'undefined')) {
        try {
            if (chckbx.checked) {
                $(chckbx.parentNode.parentNode.parentNode).find('input[type=checkbox]')[0].checked = true;
            } else {
                var anyOne = false;

                $(chckbx.parentNode.parentNode).find('input[type=checkbox]').each(function() {
                    if (this.checked) {
                        anyOne = true;
                    }
                });

                if (!anyOne) {
                    $(chckbx.parentNode.parentNode.parentNode).find('input[type=checkbox]')[0].checked = false;
                }
            }
        } catch (e) {}
    }
}

function openPriceList(frm) {
    if (frm && (typeof(frm) != 'undefined')) {
        var params = '';

        $(frm).find('input').each(function() {
            try {
                if ((((this.type == 'checkbox') && this.checked) || (this.type == 'text')) && (this.value != '')) {
                    params += '&' + this.name + '=' + this.value;
                }
            } catch (e) {}
        });

        window.open(frm.action + params, null, 'width=800,height=600,scrollbars=1');
    }
}

/* ========================================================================== */

function dCartDel(a) {
    try {
        $(a.parentNode.parentNode).find('input').val(0);
    } catch (e) {}
}

function setDeliveryTxt(txt) {
    if (!txt || (typeof(txt) == 'undefined')) {
        txt = '&nbsp;';
    }

    $('#delivery_descr_frm').html(txt);
}

/* ========================================================================== */

$(document).ready(function() {
    swfobject.embedSWF(
        ((location.href.indexOf('devmode=1') != -1) ? '/i/shell.new.swf' : '/i/shell.swf'),
        'shell_on_top',
        '199',
        '256',
        '9.0.0',
        '/i/expressInstall.swf',
        {},
        {"menu": false, "wmode": 'transparent'},
        {},
        function(callbackObj) {
            if (callbackObj.success) {
                $('#shell_on_top').css('backgroundImage', 'none');
            }
        }
    );
});
