function CorrectContentHeight() {
    var c = $('#content_in'),
        f = $('#footer'),
        ct = 0, fh = 0, ch = 0, wh = 0;

    if ((c.length > 0) && (f.length > 0)) {
        ct = c.offset().top;
        fh = f.height();
        ch = c.height();
        wh = $(window).height();

        if (ct + fh + ch < wh) {
            c.height(wh - (ct + fh));
        }
    }
}

function Tabs() {
    var detTabs = $('#detail_tabs');

    if (detTabs.length == 0) {
        return;
    }

    var tabs = detTabs.find('.tabs a'), i = 0, tab = null;

    for (i = 0; i < tabs.length; i++) {
        tab = $(tabs[i]);

        tab.click(function(_event){
            //_event.preventDefault();

            var th = $(this), detTabs = th.closest('#detail_tabs');

            detTabs.find('.tab_cont_item').hide();
            detTabs.find('#' + th.attr('id').replace(/tab_/, '')).show();
            th.closest('.tabs').find('a').removeClass('active');
            th.addClass('active');
        });

        if (i == 0) {  // first tab
            detTabs.find('#' + tab.attr('id').replace(/tab_/, '')).show();
        }
        //alert(tab.get(0).className);
    }
}

function STable() {
    var tables = $('.s_table'),
        percentWidth = 70;

    if (tables.length == 0) {
        return;
    }

    tables.each(function() {
        var t = $(this);

        t.find('tr').each(function(_idx){
            var tr = $(this),
                td = tr.find('td:last'),
                percent = 0,
                text = '',
                clas = 'rd';

            if ((_idx > 3) && (_idx % 2 == 1)) {
                tr.addClass('even');
            }

            if (td.length > 0) {
                text = td.text();
                percent = parseInt(text);

                if ((percent >= 0) && (percent <= 100)) {
                    if ((percent <= 70) && (percent >= 50)) {
                        clas = 'db';
                    } else if (percent < 50) {
                        clas = 'lb';
                    }

                    td.html('<div class="pr ' + clas + '" title="' + text
                        + '"><div class="prl"></div><div class="prm" style="width: '
                        + (percentWidth * (percent / 100)) + 'px;"></div><div class="prr"></div></div>');
                }
            }
        });
    });
}

function OnVariantChange() {
    var selectedOption = null,
        priceC = $('#price'),
        variantsC = $('#variants'),
        imgC = $('#primg'),
        variants = variantsC.find('select'),
        prCount = $('input[name=productCount]'),
        price = 0,
        count = 1,
        variantPrice = 0,
        variantId = 0,
        i = 0,
        variant = null;

    price = parseInt(priceC.get(0).className.replace(/price/, ''));

    if (price) {
        for (i = 0; i < variants.length; i++) {
            variant = $(variants[i]);
            selectedOption = variant.find('option[value=' + variant.val() + ']');
            if (selectedOption.length > 0) {
                variantId = parseInt(selectedOption.val());
                variantPrice = parseInt(selectedOption.get(0).className.replace(/pr/, ''));
                price += variantPrice;
            }
        }

        if (prCount.length > 0) {
            count = parseInt(prCount.val());
        }

        priceC.text(price * count);
    }

    if ((imgC.length > 0) && variantId) {
        var path = imgC.get(0).className.replace(/^.*path_/, ''),
            a = null, img = null;
        
        if (path) {
            path += '/imgv' + variantId;

            var pathBig = path + '_2.jpg',
                pathSmall = path + '_1.jpg';

            //a = imgC.find('a[href=' + pathBig + ']');
            a = imgC.find('a:first');
            img = imgC.find('img:first');

            if (img.length > 0) {
                img.attr('src', pathSmall);
            }

            if (a.length > 0) {
                a.attr('href', pathBig);
            }

            var bla = new Gallery2('photogallery', 'gimg', 'class', false);
            /*if (a.length == 0) {
                imgC.append('<a href="' + pathBig + '" class="gimg"><img src="'
                    + pathSmall + '" /></a>');
            }*/
        }
        //imgC.empty();
    }
}

function OnPrCountChange() {
    var selectedOption = null,
        priceC = $('#price'),
        variantsC = $('#variants'),
        variants = variantsC.find('select'),
        price = 0,
        variantPrice = 0,
        count = 0,
        i = 0,
        variant = null;

    price = parseInt(priceC.get(0).className.replace(/price/, ''));

    if (price) {
        for (i = 0; i < variants.length; i++) {
            variant = $(variants[i]);
            selectedOption = variant.find('option[value=' + variant.val() + ']');
            if (selectedOption.length > 0) {
                variantPrice = parseInt(selectedOption.get(0).className.replace(/pr/, ''));
                price += variantPrice;
            }
        }

        count = parseInt($(this).val());

        if (count > 0) {
            price *= count;
            priceC.text(price);
        }
    }
}

function ProductDetailPrice() {
    var priceC = $('#price'),
        prCount = $('input[name=productCount]'),
        variantsC = $('#variants');

    if ((prCount.length > 0) && (priceC.length > 0)) {
        prCount.bind('keyup', OnPrCountChange);
    }

    if ((priceC.length == 0) || (variantsC.length == 0)) {
        return;
    }

    var variants = variantsC.find('select'),
        variant = null,
        i = 0;

    for (i = 0; i < variants.length; i++) {
        variant = $(variants[i]);
        variant.bind('change', OnVariantChange);
    }

    OnVariantChange();
    //alert(price);
}

function SchemesWindow(_img, _name) {
    var overlay = $('<div></div>'),
        body = $('body'),
        window = null;

    function CloseW() {
        overlay.remove();
        window.remove();
    }

    overlay.css({
        position: 'fixed',
        zIndex: 5999,
        top: 0,
        left: 0,
        width: '100%',
        height: '100%',
        backgroundColor: '#000',
        opacity: 0.7
    });

    window = $('<div class="schmw"><div class="schmw_header">' + _name + '<div class="schmw_close"></div></div><div class="schmw_in"></div><div class="cleaner"></div>b</div>');
    window.find('.schmw_in').append(_img);
    window.css({
        position: 'absolute',
        top: 0,
        left: 0,
        zIndex: 6000
    });

    overlay.click(CloseW);
    window.find('.schmw_close').click(CloseW);

    body.append(overlay);
    body.append(window);

    var w = _img.width() + 8,
        h = _img.height(),
        ww = body.width(),
        t = $(document).scrollTop();

//alert(ww + ' ' + t);
    window.css({
        width: w,
        height: h,
        top: t + 30,
        left: (ww - w) / 2
    });
}

function OnSchemesChange(_select) {
    if (_select.length == 0) {
        return;
    }

    var sel = _select.find('option:selected'),
        id = 0,
        path = '/files/schemes/schm',
        img = null;

    if (sel.length > 0) {
        id = parseInt(sel.val());

        if (id > 0) {
            //alert(path + id + '.jpg');
            img = document.createElement('img');

            img.onload = function() {
                var th = $(this);

                SchemesWindow(th, sel.text());
            }

            _select.val(0);

            img.src = path + id + '.jpg';
        }
    }
}

function Schemes() {
    var schemes = $('#schemes select');

    if (schemes.length == 0) {
        return;
    }

    var i = 0,
        scheme = null;

    for (i = 0; i < schemes.length; i++) {
        scheme = $(schemes[i]);
        scheme.bind('change', function() {OnSchemesChange($(this));});
    }
}

function OnDeliveryChange(_price) {
    var prPrice = $('#pr_price'),
        delPrice = $('#del_price'),
        whlPrice = $('#whl_price');

    if ((prPrice.length > 0) && (delPrice.length > 0) && (whlPrice.length > 0)) {
        delPrice.text(_price);
        whlPrice.text(_price + parseInt(prPrice.text()));
    }
}

$(document).ready(function(){
    CorrectContentHeight();
    ProductDetailPrice();
    Tabs();
    Schemes();

    $('.not_clickable').click(function(_event){
        _event.preventDefault();
    });
});


