﻿$(document).ready(function() {

    /* carousel for categories */
    var productCarouselUlWidth = 0;
    var productCarouselArray = [];
    $('.HomePageProductCarouselItem').each(function(index) { //Loop through every item in the carousel
        var element = $(this).attr('id'); //Retrive ID of current Element
        var link = $("#" + element + " span a");
        var linkWidth = link.width() + 20; //Retrive the img width
        link.css("left", productCarouselUlWidth + "px");
        productCarouselArray.push(link);
        productCarouselUlWidth += linkWidth; //Calculate the total width of the UL
    });
    var activeLink = productCarouselArray[0];
    var activeLinkNumber = 0;

    $('#HomePageProductCarouselUL').css('width', productCarouselUlWidth + 'px'); //Apply width to the UL so it elements don't wrap
    var maxLeftPosition = 550 - productCarouselUlWidth; //Amount of room we will allow the carousel to move to the left

    /*Previous Button*/
    $("#homePageProductCarouselPrev").click(function() {
        if (!$('#HomePageProductCarouselUL').is(':animated')) {
            if ($('#HomePageProductCarouselUL').css("left").replace("px", "") < 0) {
                $("#homePageProductCarouselNext").removeClass("inactive");
                activeLinkNumber--;
                activeLink = productCarouselArray[activeLinkNumber];
                activeLinkCSS = activeLink.css("left").replace("px", "");
                $('#HomePageProductCarouselUL').animate({
                    left: activeLinkCSS * -1
                }, 300, function() {
                });
                if (activeLinkCSS == 0) {
                    $("#homePageProductCarouselPrev").addClass("inactive");
                }
				else {
                    $("#homePageProductCarouselNext").addClass("active");
				}

            }
        }
    });


    /*Next Button*/
    $("#homePageProductCarouselNext").click(function() {
        if (!$('#HomePageProductCarouselUL').is(':animated')) {
            activeLinkNumber++;
            activeLink = productCarouselArray[activeLinkNumber];
            if ($('#HomePageProductCarouselUL').css("left").replace("px", "") > maxLeftPosition) {
                $("#homePageProductCarouselPrev").removeClass("inactive");
                activeLinkCSS = activeLink.css("left").replace("px", "");
                $('#HomePageProductCarouselUL').animate({
                    left: activeLinkCSS * -1
                }, 300, function() {
                });
                if ((activeLinkCSS * -1) <= maxLeftPosition) {
                    $("#homePageProductCarouselNext").addClass("inactive");
                }
            } else {
                activeLinkNumber--;
                activeLink = productCarouselArray[activeLinkNumber];
            }
        }
    });
	
    //The following code has been changed to work with the home page NEW PRODUCTS carousel
	var newProductWidth = $('#NewProductsContainer ul li').outerWidth();
	//console.log(newProductWidth);
	var newProductCount = $('#NewProductsContainer ul li').size();
	//console.log(newProductCount);
    var newProductCarouselWidth = newProductWidth * newProductCount - (newProductWidth * 3);
	//console.log(newProductCarouselWidth);
	$("#newProductsCounter").html("(" + newProductCount + ")");
    $('#NewProductsContainer ul').css('width', newProductCarouselWidth + 'px');
    if ($("#NewProductsContainer ul li").size() > 0) {
        var newProductCarouselLeftPositon = 0;
        var newProductCarouselArray = [];
		$("#homePageNewProductCarouselPrev").css({opacity: .3});
        $('#NewProductsContainer ul li').each(function(index) {
            var element = $(this);
            element.css("position", "absolute");
            element.css("left", newProductCarouselLeftPositon + "px");
            newProductCarouselLeftPositon += newProductWidth;
            newProductCarouselArray.push(element);
        });

        var maxnewProductCarouselPosition = (228 - newProductCarouselWidth);
        var newProductCarouselActive = newProductCarouselArray[0];
        var newProductCarouselActiveNumber = 0;

        /*Previous Button*/
        $("#homePageNewProductCarouselPrev").click(function() {
            if (!$('#NewProductsContainer ul').is(':animated')) {
                if ($('#NewProductsContainer ul').css("left").replace("px", "") < 0) {
                    $("#homePageNewProductCarouselNext").removeClass("inactive");
                    newProductCarouselActiveNumber--;
                    newProductCarouselActive = newProductCarouselArray[newProductCarouselActiveNumber];
                    newProductCarouselActiveCSS = newProductCarouselActive.css("left").replace("px", "");
					$('#NewProductsContainer ul').animate({
                        left: newProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if (newProductCarouselActiveCSS == 0) {
                        $("#homePageNewProductCarouselPrev").css({opacity: .3});
                        $("#homePageNewProductCarouselNext").css({opacity: 1});
                    }
					else {
                        $("#homePageNewProductCarouselPrev").css({opacity: 1});
                        $("#homePageNewProductCarouselNext").css({opacity: 1});
					}
                }
            }
        });

        /*Next Button*/
        $("#homePageNewProductCarouselNext").click(function() {
            if (!$('#NewProductsContainer ul').is(':animated')) {
                newProductCarouselActiveNumber++;
                newProductCarouselActive = newProductCarouselArray[newProductCarouselActiveNumber];
                if ($('#NewProductsContainer ul').css("left").replace("px", "") > maxnewProductCarouselPosition) {
                    $("#homePageNewProductCarouselPrev").removeClass("inactive");
                    $("#homePageNewProductCarouselPrev").addClass("active");
                    newProductCarouselActiveCSS = newProductCarouselActive.css("left").replace("px", "");
                    $('#NewProductsContainer ul').animate({
                        left: newProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if ((newProductCarouselActiveCSS * -1) > maxnewProductCarouselPosition) {
                        $("#homePageNewProductCarouselPrev").css({opacity: 1});
                        $("#homePageNewProductCarouselnext").css({opacity: 1});
                    }
						else if ((newProductCarouselActiveCSS * -1) == maxnewProductCarouselPosition) {
                        	$("#homePageNewProductCarouselPrev").css({opacity: 1});
                        	$("#homePageNewProductCarouselNext").css({opacity: .3});
						}
					else {
                        $("#homePageNewProductCarouselPrev").css({opacity: 1});
					}
						
                } else {
                    newProductCarouselActiveNumber--;
                    newProductCarouselActive = newProductCarouselArray[newProductCarouselActiveNumber];
                }
            }
        });
    } if ($("#NewProductsContainer ul li").size() <= 4) {
		$("#homePageNewProductCarouselPrev, #homePageNewProductCarouselNext").css({opacity: .3});
	};

    //The following code has been changed to work with the home page TOP PRODUCTS carousel
	var topProductWidth = $('#topRatedContainer ul li').outerWidth();
	//console.log('product width: ' + topProductWidth);
	var topProductCount = $('#topRatedContainer ul li').size();
	//console.log('product count: ' + topProductCount);
    var topProductCarouselWidth = topProductWidth * topProductCount - (topProductWidth * 3);
	//console.log('carousel width: ' + topProductCarouselWidth);
	$("#topRatedProductsCounter").html("(" + topProductCount + ")");
    $('#topRatedContainer ul').css('width', topProductCarouselWidth + 'px');
	//$("#homePageTopProductCarouselPrev").css({opacity : .3});
    if ($("#topRatedContainer ul li").size() > 0) {
        var topProductCarouselLeftPositon = 0;
        var topProductCarouselArray = [];
		$("#homePageTopProductCarouselPrev").css({opacity: .3});
        $('#topRatedContainer ul li').each(function(index) {
            var element = $(this);
            element.css("position", "absolute");
            element.css("left", topProductCarouselLeftPositon + "px");
            topProductCarouselLeftPositon += topProductWidth;
            topProductCarouselArray.push(element);
        });

        var maxtopProductCarouselPosition = (228 - topProductCarouselWidth);
        var topProductCarouselActive = topProductCarouselArray[0];
        var topProductCarouselActiveNumber = 0;

        /*Previous Button*/
        $("#homePageTopProductCarouselPrev").click(function() {
            if (!$('#topRatedContainer ul').is(':animated')) {
                if ($('#topRatedContainer ul').css("left").replace("px", "") < 0) {
                    //$("#homePageTopProductCarouselNext").removeClass("inactive");
                    topProductCarouselActiveNumber--;
                    topProductCarouselActive = topProductCarouselArray[topProductCarouselActiveNumber];
                    topProductCarouselActiveCSS = topProductCarouselActive.css("left").replace("px", "");
                    $('#topRatedContainer ul').animate({
                        left: topProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if (topProductCarouselActiveCSS == 0) {
                        $("#homePageTopProductCarouselPrev").css({opacity: .3});
                        $("#homePageTopProductCarouselNext").css({opacity: 1});
                    }
					else {
                        $("#homePageTopProductCarouselPrev").css({opacity: 1});
                        $("#homePageTopProductCarouselNext").css({opacity: 1});
					}
                }
            }
        });

        /*Next Button*/
        $("#homePageTopProductCarouselNext").click(function() {
            if (!$('#topRatedContainer ul').is(':animated')) {
                topProductCarouselActiveNumber++;
                topProductCarouselActive = topProductCarouselArray[topProductCarouselActiveNumber];
                if ($('#topRatedContainer ul').css("left").replace("px", "") > maxtopProductCarouselPosition) {
					//console.log('new left position: ' + maxtopProductCarouselPosition);
                    //$("#homePageTopProductCarouselPrev").removeClass("inactive");
                    topProductCarouselActiveCSS = topProductCarouselActive.css("left").replace("px", "");
                    $('#topRatedContainer ul').animate({
                        left: topProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if ((topProductCarouselActiveCSS * -1) > maxtopProductCarouselPosition) {
                        $("#homePageTopProductCarouselPrev").css({opacity: 1});
                        $("#homePageTopProductCarouselNext").css({opacity: 1});
                    }
						else if ((topProductCarouselActiveCSS * -1) == maxtopProductCarouselPosition) {
                        	$("#homePageTopProductCarouselPrev").css({opacity: 1});
                        	$("#homePageTopProductCarouselNext").css({opacity: .3});
						}
					else {
                        $("#homePageTopProductCarouselPrev").css({opacity: 1});
					}
                } else {
                    topProductCarouselActiveNumber--;
                    topProductCarouselActive = topProductCarouselArray[topProductCarouselActiveNumber];
                }
            }
        });
    } if ($("#topRatedContainer ul li").size() <= 4) {
		$("#homePageTopProductCarouselPrev, #homePageTopProductCarouselNext").css({opacity: .3});
	};

    //The following code has been changed to work with the home page RECENTLY REVIEWED PRODUCTS carousel
	var recProductWidth = $('#recentlyReviewedContainer ul li').outerWidth();
	//console.log(recProductWidth);
	var recProductCount = $('#recentlyReviewedContainer ul li').size();
	//console.log(recProductCount);
    var recProductCarouselWidth = recProductWidth * recProductCount - (recProductWidth * 3);
	//console.log(recProductCarouselWidth);
	$("#recentlyReviewedProductsCounter").html("(" + recProductCount + ")");
    $('#recentlyReviewedContainer ul').css('width', recProductCarouselWidth + 'px');
    if ($("#recentlyReviewedContainer ul li").size() > 0) {
        var recProductCarouselLeftPositon = 0;
        var recProductCarouselArray = [];
        $('#recentlyReviewedContainer ul li').each(function(index) {
            var element = $(this);
            element.css("position", "absolute");
            element.css("left", recProductCarouselLeftPositon + "px");
            recProductCarouselLeftPositon += recProductWidth;
            recProductCarouselArray.push(element);
        });

        var maxrecProductCarouselPosition = (300 - recProductCarouselWidth);
        var recProductCarouselActive = recProductCarouselArray[0];
        var recProductCarouselActiveNumber = 0;

        /*Previous Button*/
        $("#homePageRecentProductCarouselPrev").click(function() {
            if (!$('#recentlyReviewedContainer ul').is(':animated')) {
                if ($('#recentlyReviewedContainer ul').css("left").replace("px", "") < 0) {
                    recProductCarouselActiveNumber--;
                    recProductCarouselActive = recProductCarouselArray[recProductCarouselActiveNumber];
                    recProductCarouselActiveCSS = recProductCarouselActive.css("left").replace("px", "");
                    $('#recentlyReviewedContainer ul').animate({
                        left: recProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if (recProductCarouselActiveCSS == 0) {
                        $("#homePageRecProductCarouselPrev").css({opacity: .3});
                        $("#homePageRecProductCarouselNext").css({opacity: 1});
                    }
					else {
                        $("#homePageRecProductCarouselPrev").css({opacity: 1});
                        $("#homePageRecProductCarouselNext").css({opacity: 1});
					}
                }
            }
        });

        /*Next Button*/
        $("#homePageRecentProductCarouselNext").click(function() {
            if (!$('#recentlyReviewedContainer ul').is(':animated')) {
                recProductCarouselActiveNumber++;
                recProductCarouselActive = recProductCarouselArray[recProductCarouselActiveNumber];
                if ($('#recentlyReviewedContainer ul').css("left").replace("px", "") > maxrecProductCarouselPosition) {
                    recProductCarouselActiveCSS = recProductCarouselActive.css("left").replace("px", "");
                    $('#recentlyReviewedContainer ul').animate({
                        left: recProductCarouselActiveCSS * -1
                    }, 300, function() {
                        // Animation complete.
                    });
                    if ((recProductCarouselActiveCSS * -1) > maxrecProductCarouselPosition) {
                        $("#homePageRecentProductCarouselPrev").css({opacity: 1});
                        $("#homePageRecentProductCarouselNext").css({opacity: 1});
                    }
						else if ((recProductCarouselActiveCSS * -1) == maxrecProductCarouselPosition) {
                        	$("#homePageRecentProductCarouselPrev").css({opacity: 1});
                        	$("#homePageRecentProductCarouselNext").css({opacity: .3});
						}
					else {
                        $("#homePageRecentProductCarouselPrev").css({opacity: 1});
					}
                } else {
                    recProductCarouselActiveNumber--;
                    recProductCarouselActive = recProductCarouselArray[recProductCarouselActiveNumber];
                }
            }
        });
    } if ($("#recentlyReviewedContainer ul li").size() <= 4) {
		$("#homePageRecentProductCarouselPrev, #homePageRecentProductCarouselNext").css({opacity: .3});
	};


    var $tabs = $('#HomePageTabbedCarouselContainer').tabs();

    /*Sliders
    //Figure out the number of slides in the slider
    var newProductNumberOfSlides = $('#NewProductsContainer ul').children("li").length;
    $("#newProductsCounter").html("(" + newProductNumberOfSlides + ")");
    var newProductSliderULWidth = 323 * newProductNumberOfSlides;
    $('#NewProductsContainer ul').css('width', newProductSliderULWidth + 'px');
    var newProductSliderContainer = 950;
    //As more products are added into the slider you will need to adjust the width of the UL - also adjust the variable sliderULWidth appropriatly
    $(function() {
    $("#newProductsSlider").slider({
    range: false,
    min: 0,
    max: 100,
    values: [0],
    slide: function(event, ui) {
    $('#NewProductsContainer ul').animate({
    left: (ui.value * .01) * -(newProductSliderULWidth - newProductSliderContainer) + 1
    }, 1, function() {
    // Animation complete.
    });
    }
    });
    });

    var topRatedNumberOfSlides = $('#topRatedContainer ul').children("li").length;
    $("#topRatedProductsCounter").html("(" + topRatedNumberOfSlides + ")");
    var topRatedSliderULWidth = 323 * topRatedNumberOfSlides;
    $('#topRatedContainer ul').css('width', topRatedSliderULWidth + 'px');
    var topRatedSliderContainer = 950;
    //As more products are added into the slider you will need to adjust the width of the UL - also adjust the variable sliderULWidth appropriatly
    $(function() {
    $("#topRatedSlider").slider({
    range: false,
    min: 0,
    max: 100,
    values: [0],
    slide: function(event, ui) {
    $('#topRatedContainer ul').animate({
    left: (ui.value * .01) * -(topRatedSliderULWidth - topRatedSliderContainer) + 1
    }, 1, function() {
    // Animation complete.
    });
    }
    });
    });

    var recentlyReviewedNumberOfSlides = $('#recentlyReviewedContainer ul').children("li").length;
    $("#recentlyReviewedProductsCounter").html("(" + recentlyReviewedNumberOfSlides + ")");
    var recentlyReviewedSliderULWidth = 323 * recentlyReviewedNumberOfSlides;
    $('#recentlyReviewedContainer ul').css('width', recentlyReviewedSliderULWidth + 'px');
    var recentlyReviwedSliderContainer = 950;
    //As more products are added into the slider you will need to adjust the width of the UL - also adjust the variable sliderULWidth appropriatly
    $(function() {
    $("#recentlyReviewedSlider").slider({
    range: false,
    min: 0,
    max: 100,
    values: [0],
    slide: function(event, ui) {
    $('#recentlyReviewedContainer ul').animate({
    left: (ui.value * .01) * -(recentlyReviewedSliderULWidth - recentlyReviwedSliderContainer) + 1
    }, 1, function() {
    // Animation complete.
    });
    }
    });
    });
    */

    //Change Project and Advice Tabs
    $("input[name='HomePageProjectsFilerBtns']").click(function() {
        var displayProjectDiv = $("input[@name='rdio']:checked").val();
        $(".homeProjectsAdvicepanel").hide();
        $("#" + displayProjectDiv).show();
    });
});

    





