// переменная для хранения точки возврата
var bottom;
bottom = 0;

//переменная для хранения высоты div'а категории new
var height_div;
height_div = 0;

//переменная для хранения высоты div'а категории recomend
var height_div_recom;
height_div_recom = 0;

//переменная из которой видно, какая позиция видна вверху для new
var top_value;
top_value = 0;

//переменная из которой видно, какая позиция видна вверху для recomend
var top_value_recom;
top_value_recom = 0;

//переменная показывает, сколько всего элементов в div'е new
var num_elem = 0;

//переменная показывает, сколько всего элементов в div'е recomend
var num_elem_recom = 0;

//переменная, указывающая смещение для new
var shift;
shift = 0;

//переменная, указывающая смещение для recom
var shift_recom;
shift_recom = 0;

$(document).ready(function(){

    $("#up_new").mousedown(function(){
        $("#up_new").animate({opacity: "0.0"}, "fast");
    });
    
    $("#up_new").mouseup(function(){
        $("#up_new").animate({opacity: "1.0"}, "fast");
    });
    
    $("#down_new").mousedown(function(){
        $("#down_new").animate({opacity: "0.0"}, "fast");
    });
    
    $("#down_new").mouseup(function(){
        $("#down_new").animate({opacity: "1.0"}, "fast");
    });
    
    $("#up_new").click(function(){
        if ( top_value == ( num_elem - 5 ) )
        {
            $("#new").animate({"top": "-=10px"}, "fast");
            $("#new").animate({"top": "+=10px"}, "fast");
        }
        else
        {
            ind = 0;
            $(".ppp").each(function(){
                var height = $(this).height();
                if (ind == top_value)
                    shift = height;
                ind += 1;
            })
            $("#new").animate({"top": "-=" + shift + "px"}, "slow");
            top_value += 1;
            shift = 0;
            ind = 0;
            height_div = 0;
            $(".ppp").each(function(){
                var height = $(this).height();
                if ( ( ind > ( top_value - 1 ) ) && ( ind < ( top_value + 5 ) ) )
                {
                    height_div += height;
                }
                ind += 1;
            })
            $("#wrap").height(height_div);
        }
    })
    
    $("#down_new").click(function(){
        if ( top_value == 0 )
        {
            $("#new").animate({"top": "+=10px"}, "fast");
            $("#new").animate({"top": "-=10px"}, "fast");
        }
        else
        {
            ind = 0;
            $(".ppp").each(function(){
                var height = $(this).height();
                if ( ind == ( top_value - 1 ) )
                    shift = height;
                ind += 1;
            })
            $("#new").animate({"top": "+=" + shift + "px"}, "slow");
            top_value -= 1;
            shift = 0;
            ind = 0;
            height_div = 0;
            $(".ppp").each(function(){
                var height = $(this).height();
                if ( ( ind > ( top_value - 1 ) ) && ( ind < ( top_value + 5 ) ) )
                {
                    height_div += height;
                }
                ind += 1;
            })
            $("#wrap").height(height_div);
        }
    });
    
    /////////////////////////////////////////////////////////////
    $("#up_recom").mousedown(function(){
        $("#up_recom").animate({opacity: "0.0"}, "fast");
    });
    
    $("#up_recom").mouseup(function(){
        $("#up_recom").animate({opacity: "1.0"}, "fast");
    });
    
    $("#down_recom").mousedown(function(){
        $("#down_recom").animate({opacity: "0.0"}, "fast");
    });
    
    $("#down_recom").mouseup(function(){
        $("#down_recom").animate({opacity: "1.0"}, "fast");
    });
    
    $("#up_recom").click(function(){
        if ( top_value_recom == ( num_elem_recom - 2 ) )
        {
            $("#recom").animate({"top": "-=10px"}, "fast");
            $("#recom").animate({"top": "+=10px"}, "fast");
        }
        else
        {
            ind = 0;
            $(".ppr").each(function(){
                var height = $(this).height();
                if (ind == top_value_recom)
                    shift_recom = height;
                ind += 1;
            })
            $("#recom").animate({"top": "-=" + shift_recom + "px"}, "slow");
            top_value_recom += 1;
            shift_recom = 0;
            ind = 0;
            height_div_recom = 0;
            $(".ppr").each(function(){
                var height = $(this).height();
                if ( ( ind > ( top_value_recom - 1 ) ) && ( ind < ( top_value_recom + 2 ) ) )
                {
                    height_div_recom += height;
                }
                ind += 1;
            })
            $("#recomend").height(height_div_recom);
        }
    })
    
    $("#down_recom").click(function(){
        if ( top_value_recom == 0 )
        {
            $("#recom").animate({"top": "+=10px"}, "fast");
            $("#recom").animate({"top": "-=10px"}, "fast");
        }
        else
        {
            ind = 0;
            $(".ppr").each(function(){
                var height = $(this).height();
                if ( ind == ( top_value_recom - 1 ) )
                    shift_recom = height;
                ind += 1;
            })
            $("#recom").animate({"top": "+=" + shift_recom + "px"}, "slow");
            top_value_recom -= 1;
            shift_recom = 0;
            ind = 0;
            height_div_recom = 0;
            $(".ppr").each(function(){
                var height = $(this).height();
                if ( ( ind > ( top_value_recom - 1 ) ) && ( ind < ( top_value_recom + 2 ) ) )
                {
                    height_div_recom += height;
                }
                ind += 1;
            })
            $("#recomend").height(height_div_recom);
        }
    });
    /////////////////////////////////////////////////////////////
    
    // спрятать #back-top в начале
    $("#back-top").hide();
    
    // спрятать #back-bottom в начале
    $("#back-bottom").hide();
    
    // показать #back-top
    $(function () {
        $(window).scroll(function () {
            if ($(this).scrollTop() > 1500) { 
                $('#back-top').fadeIn();
                $('#back-bottom').fadeOut();
            } else {
                $('#back-top').fadeOut(); 
                if (bottom != 0)
                {
                    $('#back-bottom').fadeIn();
                }
            } 
        }); 

        // прокрутить плавно тело документа к 0px при нажатии 
        $('#back-top a').click(function () {
            bottom = $(window).scrollTop();
            $('body,html').animate({
                scrollTop: 0 
            }, 800);
            return false;
        });
        
        // прокрутить плавно тело документа вниз при нажатии 
        $('#back-bottom a').click(function () {
            $('body,html').animate({
                scrollTop: bottom 
            }, 800);
            bottom = 0;
            return false;
        });
    });
    
    var ind;
    var pp;
    pp = 0;
    $(".img_new").each(function(){
        $(this).load(function(){
            var height = $(this).height();
            if ( pp == 4 )
            {
                div_height();
            }
            pp += 1;
        });
    });
    pp = 0;
    $(".img_recom").each(function(){
        $(this).load(function(){
            var height = $(this).height();
            if ( pp == 1 )
            {
                recom_height();
            }
            pp += 1;
        });
    });
    
    div_height();
    
    recom_height();
    
    $(".loopcl").click(function(){
        $(this).parents(".list_product_elem_fon").css("z-index", "9999");
        var ins = $(this).parents(".list_product_elem_fon").children("div:first");
        var iwidth = ins.width();
        var iheight = ins.height();
        ins.css("width",""+iwidth+"px");
        ins.css("height",""+iheight+"px");
        var achild = ins.children('a');
        var newchild = '<img class="loopbg" style="position: absolute; top:0;left:0;width:'+iwidth+'px;height:'+iheight+'px;z-index:999; cursor: pointer; border: 2px solid #ffb525; background-color: #ffffff;" src='+$(this).attr("rel")+' />';
        //achild.css("display","none");
        ins.append(newchild);
        ins.children("img:last").animate({width:""+(iwidth*3)+"px",height:""+iheight*3+"px",left:"-"+(iwidth)+"px",top:"-"+(iheight)+"px"}, 400);
    });
    $('.loopbg').live( 'click', hidebigimg );
});
function hidebigimg()
{
    $(this).animate({width:""+($(this).width()/3)+"px",height:""+$(this).height()/3+"px",left:"-1px",top:"-1px"}, 400,"", hidbg);
    
}
function hidbg()
{
    $(this).parents(".list_product_elem_fon").css("z-index", "1");
    $(this).remove()
}
function div_height(){
    ind = 0;
    height_div = 0;
    $(".ppp").each(function(){
        var height = $(this).height();
        if (ind < 5)
        {
            height_div += height;
        }
        ind += 1;
    });

    num_elem = ind;

    $("#wrap").height(height_div);
}

function recom_height(){
    ind = 0;
    height_div_recom = 0;
    $(".ppr").each(function(){
        var height = $(this).height();
        if (ind < 2)
        {
            height_div_recom += height;
        }
        ind += 1;
    });

    num_elem_recom = ind;
    $("#recomend").height(height_div_recom);
}
