// JavaScript Document

function ge(id){
	return document.getElementById(id);
}


$(document).ready(function(){

	productsScrollsInitialize();

    if ($('#moreprodshere').position())  setInterval('scrollgoods();', 250);

    $("#goup").click(function(){
        $("html, body").animate({scrollTop: 0},1000);
    })

    $("#godown").click(function(){
        bodyheight = $(document).height()-$(window).height()-300;
        $("html, body").animate({scrollTop: bodyheight},1000);
//        $(window).scrollTop($(document).height(),'slow');
    })


});

var ngoodsoffset=0; var goupdisp = false; var godowndisp = false;
function scrollgoods() {

    scrollPosition = $(window).scrollTop();
    bheight = $(window).height();
    bodyheight = $(document).height();

    //BUTTON UP
    if (scrollPosition>300 && goupdisp==false) {
        $('#goup').fadeIn();
        goupdisp = true;
    } else if (scrollPosition<300 && goupdisp==true) {
        $('#goup').fadeOut();
        goupdisp = false;
    }

    //BUTTON DOWN
    downdiff = bodyheight - scrollPosition - bheight;
//    document.title = bodyheight + ' ' + scrollPosition + ' ' + bheight + ' ' + downdiff;
    if (downdiff>300 && godowndisp==false && scrollPosition>20) {
        $('#godown').fadeIn();
        godowndisp = true;
    } else if ((downdiff<300 || scrollPosition<20) && godowndisp==true) {
        $('#godown').fadeOut();
        godowndisp = false;
    }

    if (ngoodsoffset<0) return false;

    var pos = $('#moreprodshere').position();
    if (!$('#moreprodshere').position()) return false;
    //document.title = scrollPosition+'px '+bheight+' '+pos.top;

    var total = scrollPosition + bheight - 600;
    var val = $("#moreprodshere").attr('val');
    var thetype = $("#moreprodshere").attr('type');

    if (1==2 && pos.top<total) {
        //$("#moreprodshere").text('yes there');
        //$("#moreprodshere").css({'height':hgt});
        //$("#moreprodshere").before("<br><br><br><br><br><br><br><br><br><br>");

        $.post("/search_catalog/filter.htm", {ids: val, offset: ngoodsoffset, thetype: thetype}, function(data) {
            //alert (data);
            if (data) {
                $("#moreprodshere").before(data);
                $(".searchResults a[id^='productAddToBasket']").unbind();
                $(".compare a").unbind();
                $(".compare2 a").unbind();
                initall();
            } else ngoodsoffset = -1;
        });
        ngoodsoffset += 20;

    }

}


function productsScrollsInitialize() {
	$(".products-scroll").each(function(){

		var skey = $(this).find(".products-scroll-arrow").attr('key');
		var productScrollList = $("ul[key="+skey+"]");
        var sheight = productScrollList.height();
        var ilasth = productScrollList.children("li:first").height();

        var ilast = productScrollList.children("li:last").html();

        $("ul[key="+skey+"]").prepend('<li>'+ilast+'</li>');

        productScrollList.children("li:last").remove();

        $("ul[key="+skey+"]").css({'top':-ilasth-30});


        /// SCROLL BUTTONS FUNCTIONS
        $(this).find(".products-scroll-down-arrow .enabled").click(function(){
        	productScrollList.animate({"top": "-=" + (productScrollList.children("li").height() + 30) + "px"},"normal",function(){
        		 	$("ul[key="+skey+"]").append('<li>'+productScrollList.children("li:first").html()+'</li>');
        		 	productScrollList.children("li:first").remove();
        		 	$("ul[key="+skey+"]").css({'top':-ilasth-30});
        	});
        });

        $(this).find(".products-scroll-up-arrow .enabled").click(function(){
        	productScrollList.animate({"top": "+=" + (productScrollList.children("li").height() + 30) + "px"},"normal",function(){
        		 	$("ul[key="+skey+"]").prepend('<li>'+productScrollList.children("li:last").html()+'</li>');
        		 	$("ul[key="+skey+"]").css({'top':-ilasth-30});
        		 	productScrollList.children("li:last").remove();
        	});
        });



 		});
 };

function productsScrollsInitialize_old() {

	$(".products-scroll").each(
		function() {

			productsScrollsCount++;

			var productScrollName = "productScroll" + productsScrollsCount;
			var productScrollList = $(this).find(".products-scroll-list");

			$(this).attr("id", productScrollName);
			productsScrollsInformation[productScrollName] = {

				count: productScrollList.find("ul li").size(), // количество
				seeCount: Math.floor(productScrollList.height() / productScrollList.find("ul li").height()), // умещаются в блок
				currentPosition: 0

			};
//			alert()
//			productsScrollArrowsCheck(productScrollName);



		}
	);
}
