function showbig(picname) {
    window.open('/includes/foto.php5?pic=' + picname, '_blank', 'scrollbars=yes,taskbar=no,resizeable=no,status=no,menubar=no,top=30,left=30,width=30,height=30');
}

function PreLoad(source) {
    var NewImage = new Image();
    NewImage.src = source;
}

function prepareSubmit(frm) {
    var len = frm.all("editbox").length;
    if (!len) len = 1;
    for (var i = 0; i < len; i++)
        frm.all("codebox", i).innerText = frm.all("codebox", i).style.display ? borderOn(frm.all("codebox", i).innerText) : borderOn(frm.all("editbox", i).innerHTML);
}

function menuProcess(menuId) {
    if (menuId.style.display == 'none') {
        menuId.style.display = 'block';
    } else {
        menuId.style.display = 'none';
    }
}

function btShowFunctionArguments(name) {
    btChangeFunctionArgumentsBlock(name, 'none', 'block', 'block');
}

function btHideFunctionArguments(name) {
    btChangeFunctionArgumentsBlock(name, 'block', 'none', 'none');
}

function btChangeFunctionArgumentsBlock(name, plus, minus, arguments) {
    document.getElementById('backtrace_img_plus_' + name).style.display = plus;
    document.getElementById('backtrace_img_minus_' + name).style.display = minus;
    document.getElementById('backtrace_arguments_' + name).style.display = arguments;
}

function processTree(elementId, isTop) {
    tbl = document.getElementById('dir_' + elementId);
    img = document.getElementById('img_' + elementId);

    if (tbl.style.display != 'none') {
        tbl.style.display = 'none';
        img.alt = 'Раскрыть';
        if (isTop)
            img.src = '/images/design/tree_plus.gif';
        else
            img.src = '/images/design/tree_sub_plus.gif';
    } else {
        tbl.style.display = 'block';
        img.alt = 'Закрыть';
        if (isTop)
            img.src = '/images/design/tree_minus.gif';
        else
            img.src = '/images/design/tree_sub_minus.gif';
    }
}

/**
 * Преобразует многомерный обьект данных в строковое представление
 */
function Dump(d, l) {
    if (l == null) l = 1;
    var s = '';
    if (typeof(d) == "object") {
        s += typeof(d) + " {\n";
        for (var k in d) {
            for (var i = 0; i < l; i++) s += "    ";
            s += k + ": " + Dump(d[k], l + 1);
        }
        for (var i = 0; i < l - 1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
}
function print_r(arr) {
    alert(Dump(arr));
}

/**
 * Создаёт копию обьекта (для передачи не по ссылки)
 */
function clone(d) {
    var s = new Array();
    if (typeof(d) == "object") {
        for (var k in d)
            s[k] = clone(d[k]);
    } else
        s = d;
    return s;
}

/** v 1.1
 * Выдаёт окончание
 *
 * @param int $count - Количество элементов
 * @param str $word - название элемента в ед. числе
 * @return str - название в указанном количестве
 */
function rus_case(count, word) {
    var sp_count = (1 * count % 100 - 1 * count % 10) == 10;
    var count = 1 * count % 10;
    var mod = word.substr(word.length - 1, 1);
    if (count == 1 && !sp_count)
        return word;
    switch (mod) {
        case 'а' :
            if ((count == 2 || count == 3 || count == 4) && !sp_count)
                return word.substr(0, word.length - 1) + 'ы';
            else
                return word.substr(0, word.length - 1);
        case 'ь' :
            if ((count == 2 || count == 3 || count == 4) && !sp_count)
                return word.substr(0, word.length - 1) + 'я';
            else
                return word.substr(0, word.length - 1) + 'ей';
        case 'е' :
            if ((count == 2 || count == 3 || count == 4) && !sp_count)
                return word.substr(0, word.length - 1) + 'я';
            else
                return word.substr(0, word.length - 1) + 'й';
        default:
            if ((count == 2 || count == 3 || count == 4) && !sp_count)
                return word + 'а';
            else
                return word + 'ов';
    }
}
function hideClass(id) {
    (function($) {
        $('#' + id).hide();
    })(jQuery)
}
function number_format(number, decimals, dec_point, thousands_sep) {  // Format a number with grouped thousands

    var i, j, kw, kd, km;

    // input sanitation & defaults
    if (isNaN(decimals = Math.abs(decimals))) {
        decimals = 2;
    }
    if (dec_point == undefined) {
        dec_point = ",";
    }
    if (thousands_sep == undefined) {
        thousands_sep = ".";
    }

    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

    if ((j = i.length) > 3) {
        j = j % 3;
    } else {
        j = 0;
    }

    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


    return km + kw + kd;
}
(function($) {


    function calcCount() {
        var walk;
        var stone;
        var border;
        walk = $('#walk').html();
        walk = parseFloat(walk.split(' ').join('')) + 154;
        $('#walk').html(number_format(walk, ' ', '.', ' '));

        stone = $('#stone').html();
        stone = parseFloat(stone.split(' ').join('')) + 154;
        $('#stone').html(number_format(stone, ' ', '.', ' '));

        border = $('#border').html();
        border = parseFloat(border.split(' ').join('')) + 154;
        $('#border').html(number_format(border, ' ', '.', ' '));


        window.setTimeout(calcCount, 15000);
    }
function getElementPosition(elemId) {
    var elem = document.getElementById(elemId);
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
    while (elem) {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {"left":l, "top":t, "width": w, "height":h};
}
    $(document).ready(function() {
                        dotts = new Array(
                        [20,20],
                        [19,20],
                        [18,20],
                        [20,19],
                        [20,18]);
        $('#slideshow').mouseenter(function() {
            $('.top-picture-pages').show();
        }).mouseleave(function() {
            $('.top-picture-pages').hide();
        });

        if ($('a.auth').length){
            $('a.auth').click(function(){
                bodyWidth = $('#all-wrapper').width();
                bodyHeight = $('#all-wrapper').width();
                pos = getElementPosition('user-block');
                bottomHeight = bodyHeight - pos.top - $('#user-block').height();

                $('body').prepend('<div class="topdark darks darker" style="width:'+bodyWidth+'px;height:'+(pos.top-20)+'px"></div>');
                $('body').prepend('<div class="bottomdark darks darker" style="width:'+bodyWidth+'px;height:'+bottomHeight+'px;top:'+(pos.top+20 + $('#user-block').height())+'px"></div>');
                $('body').prepend('<div class="leftdark darks darker" style="width:'+(pos.left-20)+'px;left:0;top:'+(pos.top-20)+'px;height:'+($('#user-block').height()+40)+'px"></div>');
                $('body').prepend('<div class="rightdark darks darker" style="width:'+(bodyWidth-(pos.left+$('#user-block').width()+20))+'px;left:'+(pos.left+$('#user-block').width()+20)+'px;top:'+(pos.top-20)+'px;height:'+($('#user-block').height()+40)+'px"></div>');
//                alert(dotts.length);return;
                for (i in dotts){
                    if (dotts[i] instanceof Array){
                        y1 = (pos.top-dotts[i][0]);
                        x1 = (pos.left-dotts[i][1]);
                        y2 = (pos.top+dotts[i][0]-1+$('#user-block').height());
                        x2 = (pos.left+$('#user-block').width()-1+dotts[i][1]);
                        $('body').prepend('<div class="dot darks darker" style="top:'+y1+'px;left:'+x1+'px">');
                        $('body').prepend('<div class="dot darks darker" rel="now" style="top:'+y2+'px;left:'+x1+'px">');
                        $('body').prepend('<div class="dot darks darker" style="top:'+y1+'px;left:'+x2+'px">');
                        $('body').prepend('<div class="dot darks darker" rel="now" style="top:'+y2+'px;left:'+x2+'px">');
                    }
                }
                $('.darker').fadeOut(5000, function(){
                    $(this).remove();
                }).click(function(){
                    $('.darker').remove();
                })
                return false;
            });
        }

        window.setTimeout(calcCount, 15000);
        $(document).bind('keypress', function(e) {
            if (e.keyCode == 27) {
                hideClass('faq-question-form');
            }
        });

        $('.single-product').mouseover(function() {
            $(this).children('.border-product').each(function() {
                $(this).show();
            });
        }).mouseout(function() {
            $(this).children('.border-product').each(function() {
                $(this).hide();
            });
        });

        $('a.show-product').click(function() {
            path = $(this).attr('href') + '?ajax';
            $.get(path, function(result) {
                $('#info-product').html(result);
                $('#info-product').hide();
                $('#info').show();
                $('#info-product').fadeIn('slow');
                var api = $("#infinite").data("scrollable");
                api.begin();

            })
            return false;
        });

        

        $('a.colors').click(function() {
            path = $(this).attr('href') + '?ajax';
            $.get(path, function(result) {
                $('#info-product').html(result);
                $('#info-product').hide();
                $('#info').show();
                $('#info-product').fadeIn('slow');
            })
            return false;
        })

        var issip = false;
        $('#slideshow .backward, #slideshow .forward').click(function() {
            if (issip) {
                return;
            }
            p = $('#slideshow .pictures');
            c = p.find('li:visible');
            next = $(this).hasClass('backward') ? c.prev('li') : c.next('li');
            if (!next.length) {
                next = $(this).hasClass('backward') ? p.find('li:last-child') : p.find('li:first-child');
            }
            c.removeClass('current');
            next.addClass('current');

            issip = true;
            next.show();
            c.fadeOut('normal', function() {
                issip = false;
            });
        });
        $('a.pic-num').click(function() {
            p = $('#slideshow .pictures');
            c = p.find('li:visible');
            next = $(p.find('li[id="' + $(this).attr('rel') + '"]'));
            if (c.attr('id') != next.attr('id')) {
                c.removeClass('current');
                next.addClass('current');

                issip = true;
                next.show();
                c.fadeOut('normal', function() {
                    issip = false;
                });
            }
        });
        $('#top-links').mouseover(function() {
            $(this).show();
        }).mouseout(function() {
            $(this).hide();
        });
        $('#top-popup').mouseover(function() {
            $('#top-links').show();
        }).mouseout(function() {
            $('#top-links').hide();
        });

        $('.question-text').click(function() {
            id = $(this).attr('id');
            $('.faq-answer-block').each(function() {
                $(this).fadeOut('normal');
            })
            $('#answer-' + id).fadeIn('normal');
        });

        (function slideshow(f) {
            !f && !issip ? $('#slideshow .forward').click() : null;
            window.setTimeout(slideshow, 7000);
        })(1);

        $('#faq-question-content form').ajaxForm({
            url:  '/materials/faq/?ajax=1&noMainIndex',
            dataType:  'json'
            ,success: function(text) {
                
                if(text.error == undefined){
                    message = 'Ваш вопрос сохранен. Спасибо.';
                }else{
                    message = text.error;    
                }
                $('#faq-question-content').html(message);
                setTimeout("hideClass('faq-question-form')", 3000);
            }
        });

        $('#set-question').click(function() {
            $('#faq-question-form').show();
        });

        $('#close-faq').click(function() {
            hideClass('faq-question-form');
        });

        $('#for-specialists').mouseover(function() {
            $('#for-specialists-info').css('opacity', '0').css('display', 'block').stop().fadeTo(500, 0.95);
        })

        $('#for-specialists').mouseout(function() {
            $('#for-specialists-info').stop().fadeTo(500, 0, function() {
                $(this).css('display', 'none');
            });
            //setTimeout("hideClass('for-specialists-info')", 3000);
        })

    })
})(jQuery);
