﻿var noOfPages = 0;
var currentPage = 1;
var imageArray = new Array();
var imagesLoaded = 0;
var imageHolderOpen1 = "<table id=\""
var imageHolderOpen2 = "\" class=\"galTable\"> \
        <tr><td class=\"galNW\"></td><td class=\"galN\"></td><td class=\"galNE\"></td></tr> \
        <tr><td class=\"galW\"></td><td class=\"overlayImageHolder\"><img src=\""  
var imageHolderClose = "\" /></td><td class=\"galE\"></td></tr> \
        <tr><td class=\"galSW\"></td><td class=\"galS\"></td><td class=\"galSE\"></td></tr> \
        </table>"
var imageHolderOpenAlt = "\" class=\"galTable\"> \
        <tr><td class=\"galNW\"></td><td class=\"galN\"></td><td class=\"galNE\"></td></tr> \
        <tr><td class=\"galW\"></td><td class=\"overlayImageHolder\">"
var imageHolderCloseAlt = "</td><td class=\"galE\"></td></tr> \
        <tr><td class=\"galSW\"></td><td class=\"galS\"></td><td class=\"galSE\"></td></tr> \
        </table>"
var hasBeenLoaded = false;
isAnimated = false;
var scrollAmount = new Array();
var noOfLifestyles = 0;
var notScrolledYet = true;
var currentLifestyle = 0;
var lifestylesTotalWidth = 0;
var currentTab = -1;
var inThisRangePage = 1;
var galleryTopOffset = 0;


function addHotSpotHovers() {
    $(".galleryHotSpot").hover(
  function() {

      //Empty infoDiv
      $(".galleryInfoSubDiv").empty();
      $(".galleryInfoDiv").css("display", "block");

      //From hotspot get siblings and add to new div
      $.each($(this).parent().siblings(), function(i, v) {
          $(v).clone().appendTo(".galleryInfoSubDiv");
      });

      //Position infoDiv
      if ($(this).offset().left < (document.documentElement.clientWidth / 2)) {
          $('.hotSpotOpen').attr("src","http://www.neptune.com/App_Themes/Default/Images/Master/HotSpotHoverRightOpen.png");
          $('.hotSpotClose').attr("src","http://www.neptune.com/App_Themes/Default/Images/Master/HotSpotHoverRightClose.png");
          $(".galleryInfoDiv").css("left", ($(this).offset().left + 25));
      } else {
          $('.hotSpotOpen').attr("src", "http://www.neptune.com/App_Themes/Default/Images/Master/HotSpotHoverLeftClose.png");
          $('.hotSpotClose').attr("src", "http://www.neptune.com/App_Themes/Default/Images/Master/HotSpotHoverLeftOpen.png");
          $(".galleryInfoDiv").css("left", ($(this).offset().left - $(".galleryInfoDiv").width()+2));
      }

      $(".galleryInfoDiv").css("top", ($(this).offset().top) - 3);

  },
  function() {

      //Hide info div
      $(".galleryInfoDiv").css("display", "none");

  }
 );
 
}

function loadDynamicGallery(n, t, r, c, m) {

    //Reset previous gallery if present
    $(".overlayImages").empty();
    noOfPages = 0;
    $('.overlayBackground').css('opacity', '0');
    $('.overlay').css('opacity', '1');
    showElement(".overlayLoader");
    
    //Stop slideshow if on home page
    if ($('#slideshow').length > 0) {
        $('#slideshow').cycle('pause');
    }
    galleryTopOffset = -17;

        initOverlay();
        $(".dynamicGalleryTemp").load("/Gallery/Gallery.aspx", { isNew: n, byType: t, byRange: r, byCategory: c, byMarket: m }, function() {
            //With contents of gallery temp load images (for each td in #dynamicGalleryTemp get image and related product data)

            //For each galleryQueryImage
            var counter = 0;
            $.each($(".galleryQueryImage"), function(i, v) {

                //Make galleryQueryImageProduct same width as image
                var img = $(v).children($(".GalleryImageUrl"));
                var imgWidth = $(img).attr("width");
                var parent = $(img).parent();
                $(img).siblings("div.galleryQueryImageProduct").css("width", imgWidth);

                $.each($(img).siblings("div.galleryQueryImageProduct"), function(i, v) {

                });

                $('.overlayImages').append(imageHolderOpen1 + "galT" + counter + imageHolderOpenAlt + $(v).html() + imageHolderCloseAlt);
                $(v).attr("id", counter);
                positionElementOffscreen(v);
                $(v).css('display', 'block');
                $(v).addClass('galleryImage');


                counter += 1;
                noOfPages += 1;
            });

            //Slide in .overlayImages
            $.each($(".galTable"), function(i, v) {
                var theElement = $(v);
                var left = ($(v).offset().left - document.documentElement.clientWidth);
                $(v).animate({ left: left }, 500, function() {
                    // Animation complete.
                    //Show right navigation if more than 1 image
                    if ($('.galleryImage').length > 1) {
                        showElement(".overlayGalleryRight");
                    }


                    //Clear out unneccessary temp data...
                    $(".dynamicGalleryTemp").empty();

                });
                //Hide animated loader gif
                hideElement(".overlayLoader");
            });

            addHotSpotHovers()

        });
}

function swapForMainImage(ctr) {
    //pre-load image
    var imageUrl = $(ctr).attr('rel');
    //adjust rel(needed in development mode) if starts with  a tilde
    if (imageUrl.substring(0, 1) == '~') {
        //strip of tilde and add'../'
        imageUrl.replace("~", "");
    }
    
    var img = new Image();
    img.src = imageUrl;
    $(img).load(function() {
        //image has loaded, fade out current
        $(".img").fadeOut('slow', function() {
            // Animation complete, swap image
            $(".img").attr('src', imageUrl);
            $(".img").fadeIn('slow');
        });
    });
}

//Recursive load function
function loadAllImages(images,callback, navigation, copy){
      //Create an temp image and load the url
    var img = new Image();
    img.src = images[imagesLoaded];

    $(img).load(function() {
        //Increment the images loaded variable
        imagesLoaded += 1;
        img.id = (imagesLoaded);
        $('.overlayImages').append(imageHolderOpen1 + "galT" + img.id + imageHolderOpen2 + img.src + imageHolderClose);
        positionElementOffscreen(img);
        $(img).css('display', 'block');
        $(img).addClass('galleryImage');
        //Check if all images are loaded
        if (imagesLoaded == images.length) {
            
            //If all images loaded
            //Slide in .overlayImages
                $.each($(".galTable"), function(i, v) {
                var theElement = $(v);
                var left = ($(v).offset().left - (document.documentElement.clientWidth*2));
                $(v).animate({ left: left }, 500, function() {
                    // Animation complete.
                    //Show right navigation if more than 1 image
                    if (images.length > 1) {
                        showElement(".overlayGalleryRight");
                    }
                });
            });
            //Hide animated loader gif
            hideElement(".overlayLoader");
            
        } else {
            //If not all images are loaded call own function again
            loadAllImages(images, "");
        }
    }); 
}

function showCutout() {

    if (hasBeenLoaded == false) {
        initOverlay();
        imageArray = "";
        imagesLoaded = 0;
        //Get urls of images into array
        var imageArray = new Array();
        $.each($(".thumbImage"), function(i, v) {
            var theElement = $(v);
            imageArray.push(theElement.attr("longdesc"));
            noOfPages += 1;
        });
        if (imageArray.length == 0) {
            var url = $('#ctl00_ProductCutoutImage').attr("rel").replace("~", "");
            imageArray.push(url);
            noOfPages = 1
        }
        $.each($(".productLifestyleImage"), function(i, v) {
            var theElement = $(v);
            var url = theElement.attr("longdesc").replace("~", "");
            imageArray.push(url);
            noOfPages += 1;
        });
        loadAllImages(imageArray, "");
        hasBeenLoaded = true;
    } else {
        $('.overlay').css("display", "block");
        $('.MenuBar').css("z-index", "0");
        $('.CGFooterMenu').css("z-index", "0");
        $('.overlay').stop().animate({
            opacity: 1.0
        }, 250, function() {
        // Animation complete.
        $('.MenuBar').css("z-index", "1000");
        $('.CGFooterMenu').css("z-index", "1000");
        });
        positionOverlayElements();
        //For each image (table) re-position
        $.each($(".galTable"), function(i, v) {
        var element = $(v);
        var imageNo = v.id;
        imageNo = imageNo.replace("galT", "");
        var left = (imageNo * document.documentElement.clientWidth) + (document.documentElement.clientWidth / 2) - ($(element).width() / 2);
        left = left - (currentPage * document.documentElement.clientWidth);
        $(element).css("left", left);
    });
    }
    
}

function initOverlay() {

    currentPage = 1;
    positionOverlayElements();

    $('.overlay').css("display", "block");
    $('.MenuBar').css("z-index", "0");
    $('.CGFooterMenu').css("z-index", "0");
    $('.overlayImages').css('display', 'block');
    $('.overlayImages').css('opacity', '1.0');
    $('.overlayBackground').css('display', 'block');
    $('.overlayBackground').stop().animate({
        opacity: 0.8
    }, 250, function() {
        // Animation complete.
    });
}

function closeOverlay() {

    $('.MenuBar').css("z-index", "0");
    $('.CGFooterMenu').css("z-index", "0");
    $('.overlay').stop().animate({
        opacity: 0.0
    }, 250, function() {
        // Animation complete.
        $('.overlay').css("display", "none");
        $('.MenuBar').css("z-index", "1000");
        $('.CGFooterMenu').css("z-index", "1000");
    });
    //Stop slideshow if on home page
    if ($('#slideshow').length > 0) {
        $('#slideshow').cycle('resume');
    }
}

function positionOverlayElements() {
    
    //Set overlay height
    var windowHeight = document.documentElement.clientHeight;
    var windowWidth = document.documentElement.clientWidth;
    var top = $('.HeadLogo').offset().top;
    var left = $('.HeadLogo').offset().left;

    //Set Overlay Height
    $(".overlayBackground").css({ "height": windowHeight });

    //Set Overlay Image holder height, width & position
    //$(".overlayImages").css({ "height": windowHeight - ((windowHeight / 2) - 275) });
    $(".overlayImages").css({ "width": windowWidth });
    $(".overlayImages").css({ "top": top + 69 + galleryTopOffset });
    
    //Position Logo
    $('.overlayLogo').css('top', top);
    $('.overlayLogo').css('left', left);
    
    //Position Close Button
    $('.overlayClose').css('left', (left + 940));
    $('.overlayClose').css('top', (top + 14));

    //Position Loader
    $(".overlayLoader").css('top', ((windowHeight / 2) - 12));
    $(".overlayLoader").css('left', ((windowWidth / 2) - 12));

    //Position Navigation
    $(".overlayGalleryLeft").css('top', ((windowHeight / 2)-34));
    $(".overlayGalleryRight").css('top', ((windowHeight / 2)-34));
    $(".overlayGalleryLeft").css('left', left-6);
    $(".overlayGalleryRight").css('left', left+934);

}


function showElement(target) {
    $(target).css('display', 'block');
    $(target).stop().animate({
            opacity: 0.8
    }, 550, function() {
    // Animation complete.
    });
}

function hideElement(target) {
    $(target).stop().animate({
        opacity: 0.0
    }, 550, function() {
    // Animation complete.
    $(target).css('display', 'none');
    });
}

function positionElementOffscreen(element) {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;

    //from element find the parent table and make this the element
    var imageNo = element.id;
    var idRef = "#galT" + element.id;
    element = $(idRef);

    var top = (275 - ($(element).height() / 2));
    var left;
    if ((".galleryImages").length > 0) {
    left = (windowWidth) + ((parseInt(imageNo) * windowWidth) + ((windowWidth / 2) - ($(element).width() / 2)));
    } else {
    left = (parseInt(imageNo) * windowWidth) + ((windowWidth / 2) - ($(element).width() / 2));
    }
    $(element).css('left', left);
    $(element).css('top', top);
    $(element).css('position', 'absolute');
    
}

function navigateImages(element) {

    $(".galleryHotSpot").css("display", "none");
    
    var noOfElementsToAnimate;
    if (imageArray.length > 0) {
        noOfElementsToAnimate = imageArray.length;
    } else {
        noOfElementsToAnimate = $(".galleryImage").length - 1;
    }
    var counter = 0
    //hide element
    $(".overlayGalleryRight").css('display', 'none');
    $(".overlayGalleryLeft").css('display', 'none');

        //Slide in .overlayImages from left or right
        if (element.className == "overlayGalleryRight") {
            currentPage += 1;
            $.each($(".galTable"), function(i, v) {
                counter += 1;
                var theElement = $(v);
                var left = ($(v).offset().left - document.documentElement.clientWidth);
                $(v).animate({ left: left }, 500, function() {
                    // Animation complete.
                    //Show left navigation and hide right navigation if last page
                    showElement(".overlayGalleryLeft");
                    if (parseInt(currentPage) != parseInt(noOfPages)) {
                        $(element).css("display", "block");
                    }
                    if (counter == noOfElementsToAnimate) {
                        $(".overlayGalleryRight").css('display', 'block');
                    }
                    $(".galleryHotSpot").css("display", "block");

                });
                if (parseInt(currentPage) == parseInt(noOfPages)) {
                    hideElement(".overlayGalleryRight");
                }
            });
        } else {
            currentPage -= 1;
            $.each($(".galTable"), function(i, v) {
                counter += 1;
                var theElement = $(v);
                var left = ($(v).offset().left + document.documentElement.clientWidth);
                $(v).animate({ left: left }, 500, function() {
                    // Animation complete.
                    //Show right navigation if more than 1 image
                    showElement(".overlayGalleryRight");
                    $(element).css("display", "block");

                    $(".galleryHotSpot").css("display", "block");

                });
                if (currentPage == 1) {
                    hideElement(".overlayGalleryLeft");
                }
                if (counter == noOfElementsToAnimate) {
                    $(".overlayGalleryRight").css('display', 'block');
                }
            });
        }

}

$(window).resize(function() {
    positionOverlayElements();
    //For each image (table) re-position
    $.each($(".galTable"), function(i, v) {
        var element = $(v);
        var imageNo = v.id;
        imageNo = imageNo.replace("galT", "");
        var left = (imageNo * document.documentElement.clientWidth) + (document.documentElement.clientWidth / 2) - ($(element).width() / 2);
        left = left - (currentPage * document.documentElement.clientWidth);
        $(element).css("left", left);
    });

})

//IF this resize is problematic then use smartResize http://paulirish.com/demo/resize


function productImageNav(element) {
    
    if (noOfLifestyles > 1) {

        var counter = 0
        //hide element
        $(".productImageLeft").css('display', 'none');
        $(".productImageRight").css('display', 'none');

        //Slide in lifestyles from left or right
        if (element.className == "productImageRight") {

            var theElement = $("#makeMeScrollable");
            var currentOffset = theElement.scrollLeft();
            var leftAmount = (parseInt(currentOffset) + 1) + parseInt(scrollAmount[currentLifestyle]);

            if (leftAmount > (lifestylesTotalWidth-695)) {
                leftAmount = lifestylesTotalWidth - 695;
            }

            $("#makeMeScrollable").stop().animate({ scrollLeft: leftAmount }, 300, function() {
                // Animation complete.
                //Show left navigation and hide right navigation if last page
                showElement(".productImageLeft");
                if (parseInt(currentLifestyle) != parseInt(noOfLifestyles)) {
                    $(element).css("display", "block");
                }
                counter += 1;
                if (counter == noOfLifestyles) {
                    $(".productImageRight").css('display', 'block');
                }
            });

            if ((parseInt(currentLifestyle) + 2) == parseInt(noOfLifestyles) || leftAmount == (lifestylesTotalWidth-695)) {
                hideElement(".productImageRight");
            }
            currentLifestyle += 1;

        } else {
            var theElement = $("#makeMeScrollable");
            var currentOffset = theElement.scrollLeft();
            var leftAmount = (parseInt(currentOffset) + 1) - parseInt(scrollAmount[(currentLifestyle - 1)]);
            $("#makeMeScrollable").stop().animate({ scrollLeft: leftAmount }, 300, function() {
                // Animation complete.
                //Show left navigation and hide right navigation if last page
                showElement(".productImageRight");
                if (parseInt(currentLifestyle) != parseInt(noOfLifestyles)) {
                    $(element).css("display", "block");
                }
                counter += 1;
                if (counter == noOfLifestyles) {
                    $(".productImageLeft").css('display', 'block');
                }
            });
            if ((parseInt(currentLifestyle)) == 1) {
                hideElement(".productImageLeft");
            }
            currentLifestyle -= 1;
        }

    }
}

function minimiseTab() {
    $(".tabUp").css("display", "block");
    $(".tabDown").css("display", "none");
    $(".tab0 img").attr("src", "/App_Themes/Default/Images/Product/TabWhereToBuy.png");
    $(".tab1 img").attr("src", "/App_Themes/Default/Images/Product/TabLooksGreatWith.png");
    $(".tab2 img").attr("src", "/App_Themes/Default/Images/Product/TabInThisRange.png");
    $(".tab3 img").attr("src", "/App_Themes/Default/Images/Product/TabPaintOptions.png");
    $(".productTabHolder").stop().animate({ bottom: 0 }, 300, function() {
    currentTab = -1;
    $(".productWhereToBuy").css("display", "none");
    $(".productLooksGreatWith").css("display", "none");
    $(".productInThisRange").css("display", "none");
    $(".productPaintOptions").css("display", "none");
    $(".productWhereToBuy").css("opacity", 0.95);
    $(".productLooksGreatWith").css("opacity", 0.95);
    $(".productInThisRange").css("opacity", 0.95);
    $(".productPaintOptions").css("opacity", 0.95);
    });
}

function productTabSelect(e) {
    
    $(".tabUp").css("display", "none");
    $(".tabDown").css("display", "block");
    if (e.className == "tab0" || e == "noTab") {
        $(".tab0").css("opacity", 0.95);
        $(".tab0 img").attr("src", "/App_Themes/Default/Images/Product/TabWhereToBuyOn.png");
        $(".tab1 img").attr("src", "/App_Themes/Default/Images/Product/TabLooksGreatWith.png");
        $(".tab2 img").attr("src", "/App_Themes/Default/Images/Product/TabInThisRange.png");
        $(".tab3 img").attr("src", "/App_Themes/Default/Images/Product/TabPaintOptions.png");
        $(".productWhereToBuy").css("display", "block");
        $(".productLooksGreatWith").css("display", "none");
        $(".productInThisRange").css("display", "none");
        $(".productPaintOptions").css("display", "none");
        $(".productTabHolder").stop().animate({ bottom: 120 }, 300, function() {
            currentTab = 0;
        });
    } else if (e.className == "tab1") {
        $(e).css("opacity", 0.95);
        $(".tab0 img").attr("src", "/App_Themes/Default/Images/Product/TabWhereToBuy.png");
        $(".tab1 img").attr("src", "/App_Themes/Default/Images/Product/TabLooksGreatWithOn.png");
        $(".tab2 img").attr("src", "/App_Themes/Default/Images/Product/TabInThisRange.png");
        $(".tab3 img").attr("src", "/App_Themes/Default/Images/Product/TabPaintOptions.png");
        $(".productLooksGreatWith").css("display", "block");
        $(".productWhereToBuy").css("display", "none");
        $(".productInThisRange").css("display", "none");
        $(".productPaintOptions").css("display", "none");
        $(".productTabHolder").stop().animate({ bottom: 120 }, 300, function() {
            currentTab = 1;
        });
    } else if (e.className == "tab2") {
        $(e).css("opacity", 0.95);
        $(".tab0 img").attr("src", "/App_Themes/Default/Images/Product/TabWhereToBuy.png");
        $(".tab1 img").attr("src", "/App_Themes/Default/Images/Product/TabLooksGreatWith.png");
        $(".tab2 img").attr("src", "/App_Themes/Default/Images/Product/TabInThisRangeOn.png");
        $(".tab3 img").attr("src", "/App_Themes/Default/Images/Product/TabPaintOptions.png");
        $(".productInThisRange").css("display", "block");
        $(".productWhereToBuy").css("display", "none");
        $(".productLooksGreatWith").css("display", "none");
        $(".productPaintOptions").css("display", "none");
        $(".productTabHolder").stop().animate({ bottom: 120 }, 300, function() {
            currentTab = 2;
        });
    } else if (e.className == "tab3") {
        $(e).css("opacity", 0.95);
        $(".tab0 img").attr("src", "/App_Themes/Default/Images/Product/TabWhereToBuy.png");
        $(".tab1 img").attr("src", "/App_Themes/Default/Images/Product/TabLooksGreatWith.png");
        $(".tab2 img").attr("src", "/App_Themes/Default/Images/Product/TabInThisRange.png");
        $(".tab3 img").attr("src", "/App_Themes/Default/Images/Product/TabPaintOptionsOn.png");
        $(".productPaintOptions").css("display", "block");
        $(".productInThisRange").css("display", "none");
        $(".productWhereToBuy").css("display", "none");
        $(".productLooksGreatWith").css("display", "none");
        $(".productTabHolder").stop().animate({ bottom: 120 }, 300, function() {
            currentTab = 3;
        });
    }

}

function paintNav(e) {
    jQuery.easing.def = "easeOutExpo";
    $(".paintLeft").css("display", "none");
    $(".paintRight").css("display", "none");
    var left = $(".productPaintOptions").position().left;
    left = left.toString();
    left = parseInt(left.replace("px", ""));
    if (e.className == "paintLeft") {
        $(".productPaintOptions").stop().animate({ left: (left + 680) }, 450, function() {
            $(".paintLeft").fadeIn();
            $(".paintRight").fadeIn();
            var l = parseInt($(".productPaintOptions").css("left").toString().replace("-", ""));
            $(".productPaintIntro").css("left", l);

        });
    } else {
    $(".productPaintOptions").stop().animate({ left: (left - 680) }, 450, function() {
        $(".paintLeft").fadeIn();
        $(".paintRight").fadeIn();
        var l = parseInt($(".productPaintOptions").css("left").toString().replace("-", ""));
        $(".productPaintIntro").css("left", l);
    });
    }
}

function backToCol(e) {

    if (e.className == "paintNext") {
        $(".productPaintIntro").fadeOut();
        $(".productPaintListGroup").fadeIn();
    } else {
        $(".productPaintIntro").fadeIn();
        $(".productPaintListGroup").fadeOut();
    }
}

function initTabs() {

    if ($(".productInThisRangeItemDiv").length > 5) {
        $(".inThisRangeRight").css("display", "block");
    } else {
        $(".inThisRangeRight").css("display", "none");
    }
    if ($(".productLooksGreatWithItemDiv").length > 5) {
        $(".looksGreatWithRight").css("display", "block");
    } else {
        $(".looksGreatWithRight").css("display", "none");
    }
    //IN THIS RANGE
    //For each item div if item index is divisiable by 5 then add the larger left margin
    index = 0;
    $.each($(".productInThisRangeItemDiv"), function(i, v) {
        var theElement = $(v);
        if (index == 5 || index == 10 || index == 15) {
            theElement.css("margin-left", 60);
        }
        index += 1;
    });

    //LOOKS GREAT WITH
    //For each item div if item index is divisiable by 5 then add the larger left margin
    index = 0;
    $.each($(".productLooksGreatWithItemDiv"), function(i, v) {
        var theElement = $(v);
        if (index == 5 || index == 10 || index == 15) {
            theElement.css("margin-left", 60);
        }
        index += 1;
    });

    //INIT LIFESTYLE IMAGES
    if (notScrolledYet) {
        //Set up array of image widths
        $.each($(".productLifestyleImage"), function(i, v) {
            var theElement = $(v);
            scrollAmount.push(theElement.width());
            lifestylesTotalWidth += parseInt(theElement.width());
            noOfLifestyles += 1;
        });
        notScrolledYet = false;
    }
    if (noOfLifestyles < 2) {
        $(".productImageRight").css("display", "none");
    }

    setImgageDims();

    $('.productInThisRange').scrollLeft(0);
    $('.productLooksGreatWith').scrollLeft(0);
}


function rangeNav(e,v,c) {

    v = "." + v;
    t = v + "ItemDiv";
    cR = "." + c + "Right";
    cL = "." + c + "Left";
    var n = Math.ceil((($(t).length * 136) /680));
    jQuery.easing.def = "easeOutExpo";
    $(cL).css("display", "block");
    $(cR).css("display", "block");
    var leftScroll = $(v).scrollLeft();
    var leftLeft = $(cL).css("left");
    var leftRight = $(cR).css("left");
    leftLeft = leftLeft.replace("px", "");
    leftRight = leftRight.replace("px", "");
    $(cL).css("display", "none");
    $(cR).css("display", "none");
    if (e.className == cL.replace(".","") && inThisRangePage != 1) {
        inThisRangePage -= 1;
        $(v).stop().animate({ scrollLeft: (parseInt(leftScroll) - parseInt(680)) }, 650, function() {
        $(cL).css('left', parseInt(leftLeft) - 680);
        $(cR).css('left', parseInt(leftRight) - 680);
        $(cR).fadeIn();
        if (inThisRangePage != 1) {
            $(cL).fadeIn();
        }
        });
    }
    if (e.className == cR.replace(".", "")) {
        inThisRangePage += 1;
        $(v).stop().animate({ scrollLeft: (leftScroll + 680) }, 650, function() {
        $(cL).css('left', (parseInt(leftLeft)+680));
        $(cR).css('left', (parseInt(leftRight)+680));
        $(cL).fadeIn();
        if (inThisRangePage != n) {
            $(cR).fadeIn();
        };
        });
    }
}

//Set widths and heights of images. Required for chrome,safari for the image scrolling
function setImgageDims() {
    $.each($("img"), function(i, v) {
        var theElement = $(v);
        theElement.width(theElement.offset().width);
        theElement.height(theElement.offset().height);
    });
}
