﻿// JScript File - top image rotator
var nextImage = new Image();

        function RotateImage()
        {
            var currentImage = getObject("homeimage");
            if (currentImage && IsImageOk(currentImage)) {
                if (nextImage) {
                    if (IsImageOk(nextImage)){
                        ", nextImage.width:" + nextImage.width+
                        ", currentImage.width:" + currentImage.width+
                        ", nextImage.src:" + nextImage.src;
                        if (nextImage.src){
                            currentImage.src = nextImage.src;
                            nextImage = new Image();
                            nextImage.src = GetNextImageSrc(currentImage);
                        }
                    }
                    if (!nextImage.src){
                        nextImage.src = GetNextImageSrc(currentImage);
                    }
                }
            }
            setTimeout('RotateImage()',4000);
        }

        function GetNextImageSrc(existingImg)
        {
            var numImages = 18
            var currImageSrc = existingImg.src
            var newImageSrc;

            for (var i=0; i<100; i++){
                newImageNum = (Math.round(Math.random()*numImages+1))
                newImageSrc = "files/featured/main" + newImageNum + ".jpg";
                if (newImageSrc != currImageSrc){
                    return newImageSrc;
                }
            } 
            return null;
        }
        
        
        
        
/* Following script is for the banner scroller on home - myer style*/
        $(
            function () 
            {
                // Product scroller
                var baseWidth = 904;
                var scrollerDuration = 10000;
                // Gets the amount of products within scroller
                var itemCount = $(".scroller-inner").children(".item").length;
                // Works out how many sets of products are there and rounds value off
                var itemSets = Math.ceil(itemCount/3);
                // Works out width based on itemCount
                var containerWidth = Math.round(baseWidth * (itemCount/3));
                // Works out max position scroller can move based on itemCount
                var maxOffset = (-containerWidth/2)+1;
                var currentPosition  = parseInt($(".scroller-inner").css("left"));
        		
                $(".scroller-inner").css({width: containerWidth});
        		
                if(itemCount > 3)
                {
                    $(".scroller-column01 a").click(
                        function(evt)
                        {
                            if(currentPosition != 0)
                            {
                                window.clearInterval(autoScroll);
                                $(".scroller-column02 a").css({ visibility: "visible" });
        						
                                $(".scroller-inner").animate({
                                    left: (currentPosition+903)
                                }, "slow",
                                    function()
                                    {
                                        autoScroll = window.setInterval(
	                                        function() 
	                                        {
		                                        if(currentPosition != maxOffset) 
		                                        {
			                                        $(".scroller-column01 a").css({ visibility: "visible" });
        											
			                                        $(".scroller-inner").animate({
				                                        left: (currentPosition-903)
			                                        }, "slow",
				                                        function()
				                                        {
					                                        currentPosition = parseInt($(".scroller-inner").css("left"));
					                                        if(currentPosition != 0)
					                                        {
						                                        $(".scroller-column02 a").css({ visibility: "hidden" });
					                                        }
				                                        }
			                                        );
		                                        } 
		                                        if(currentPosition == maxOffset)
		                                        {
			                                        $(".scroller-column02 a").css({ visibility: "visible" });
        											
			                                        $(".scroller-inner").animate({
				                                        left: (currentPosition+903)
			                                        }, "slow",
				                                        function()
				                                        {
					                                        currentPosition = parseInt($(".scroller-inner").css("left"));
					                                        $(".scroller-column01 a").css({ visibility: "hidden" });
				                                        }
			                                        );
		                                        }
	                                        }, scrollerDuration
                                        );

                                        currentPosition = parseInt($(".scroller-inner").css("left"));
                                        if(currentPosition == 0)
                                        {
	                                        $(".scroller-column01 a").css({ visibility: "hidden" });
                                        }
                                    }
                                );
                            }
                            evt.preventDefault();
                        }
                    );
                    $(".scroller-column02 a").click(
                        function(evt)
                        {
                            if(currentPosition != maxOffset)
                            {
                                window.clearInterval(autoScroll);
                                $(".scroller-column01 a").css({ visibility: "visible" });
        						
                                $(".scroller-inner").animate({
                                    left: (currentPosition-903)
                                }, "slow",
                                    function()
                                    {
                                        autoScroll = window.setInterval(
	                                        function() 
	                                        {
		                                        if(currentPosition != maxOffset) 
		                                        {
			                                        $(".scroller-column01 a").css({ visibility: "visible" });
        											
			                                        $(".scroller-inner").animate({
				                                        left: (currentPosition-903)
			                                        }, "slow",
				                                        function()
				                                        {
					                                        currentPosition = parseInt($(".scroller-inner").css("left"));
					                                        if(currentPosition != 0)
					                                        {
						                                        $(".scroller-column02 a").css({ visibility: "hidden" });
					                                        }
				                                        }
			                                        );
		                                        } 
		                                        if(currentPosition == maxOffset)
		                                        {
			                                        $(".scroller-column02 a").css({ visibility: "visible" });
        											
			                                        $(".scroller-inner").animate({
				                                        left: (currentPosition+903)
			                                        }, "slow",
				                                        function()
				                                        {
					                                        currentPosition = parseInt($(".scroller-inner").css("left"));
					                                        $(".scroller-column01 a").css({ visibility: "hidden" });
				                                        }
			                                        );
		                                        }
	                                        }, scrollerDuration
                                        );
                                        currentPosition = parseInt($(".scroller-inner").css("left"));
                                        if(currentPosition == maxOffset)
                                        {
	                                        $(".scroller-column02 a").css({ visibility: "hidden" });
                                        }
                                    }
                                );
                            }
                            evt.preventDefault();
                        }
                    );
                }
        		
                autoScroll = window.setInterval(
                    function() 
                    {
                        if(currentPosition != maxOffset) 
                        {
                            $(".scroller-column01 a").css({ visibility: "visible" });
        					
                            $(".scroller-inner").animate({
                                left: (currentPosition-903)
                            }, "slow",
                                function()
                                {
                                    currentPosition = parseInt($(".scroller-inner").css("left"));
                                    if(currentPosition != 0)
                                    {
                                        $(".scroller-column02 a").css({ visibility: "hidden" });
                                    }
                                }
                            );
                        } 
                        if(currentPosition == maxOffset)
                        {
                            $(".scroller-column02 a").css({ visibility: "visible" });
        					
                            $(".scroller-inner").animate({
                                left: (currentPosition+903)
                            }, "slow",
                                function()
                                {
                                    currentPosition = parseInt($(".scroller-inner").css("left"));
                                    $(".scroller-column01 a").css({ visibility: "hidden" });
                                }
                            );
                        }
                    }, scrollerDuration
                );
        		
                $('.tandc').each(
                    function () 
                    {
                        // options
                        var distance = 6;
                        var time = 300;
                        var hideDelay = 200;

                        var hideDelayTimer = null;

                        // tracker
                        var beingShown = false;
                        var shown = false;

                        var trigger = $('.trigger', this);
                        var popup = $('.box01', this).css('opacity', 0);

                        var currentProduct = "";

                        // set the mouseover and mouseout on both element
                        $([trigger.get(0), popup.get(0)]).mouseover(function () {
        				  
                          // stops the hide event if we move from the trigger to the popup element
                          if (hideDelayTimer) clearTimeout(hideDelayTimer);

                          // don't trigger the animation again if we're being shown, or already visible
                          if (beingShown || shown) {
                            return;
                          } else {
                            beingShown = true;

                            // reset position of popup box
                            popup.css({
                              //top: -100,
                              //left: -33,
                              left: 0,
                              top: 6,
                              display: 'block' // brings the popup back in to view
                            })

                            // (we're using chaining on the popup) now animate it's opacity and position
                            .animate({
                              top: '-=' + distance + 'px',
                              opacity: 1
                            }, time, 'swing', function() {
                              // once the animation is complete, set the tracker variables
                              beingShown = false;
                              shown = true;
                            });
                          }
                        }).mouseout(function () {
                          // reset the timer if we get fired again - avoids double animations
                          if (hideDelayTimer) clearTimeout(hideDelayTimer);
        				  
                          // store the timer so that it can be cleared in the mouseover if required
                          hideDelayTimer = setTimeout(function () {
                            hideDelayTimer = null;
                            popup.animate({
                              top: '-=' + distance + 'px',
                              opacity: 0
                            }, time, 'swing', function () {
                              // once the animate is complete, set the tracker variables
                              shown = false;
                              // hide the popup entirely after the effect (opacity alone doesn't do the job)
                              popup.css('display', 'none');
                            });
                          }, hideDelay);
                        });
                    }
                );
            }
        );


        $(function(){
            $(".scroller-container .item-padding a.mainlink")
                .parents(".item-padding")
                    .css("cursor","pointer")
                    .click(function(){
                        var mainlink = $(this).find("a.mainlink:first");
                        var mainhref = mainlink.attr("href");
                        if (mainlink.attr("target") == "_blank") {
                            window.open(mainhref);
                        } else {
                            document.location = mainhref;
                        }
                    })
                .find("*")
                    .click(function(){;
                        $(this).parents(".item-padding").click();
                        return false;
                    });
        });

