

/*
* Author:           Marco Kuiper (http://www.marcofolio.net/)
* Automated By:     Stephen Bond (http://www.silvertech.com)
*/

var slideshowSpeed = 8000;

var photos = [
        {"image" : "/uploads/images/homepage-backgrounds/NS_Awards_FrontDoor.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/ultimate-soft-shell.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/short-cut.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/one-more-time.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/just-warming-up.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/one-tired-dog.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/snow-days.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/escape.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/rock-hang.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/another-busy-day.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/great-escapes.jpg"},
	{"image" : "/uploads/images/homepage-backgrounds/smores.jpg"}
];


    $(document).ready(function () {

        // Backwards navigation
        $("#back").click(function () {
            stopAnimation();
            navigate("back");
        });

        // Forward navigation
        $("#next").click(function () {
            stopAnimation();
            navigate("next");
        });

        var interval;
        $("#control").toggle(function () {
            stopAnimation();
        }, function () {
            // Change the background image to "pause"
            //$(this).css({ "background-image": "url(/library/images/common/btn_pause.png)" });

            // Show the next image
            navigate("next");

            // Start playing the animation
            interval = setInterval(function () {
                navigate("next");
            }, slideshowSpeed);
        });


        var activeContainer = 1;
        var currentImg = 0;
        var animating = false;
        var navigate = function (direction) {
            // Check if no animation is running. If it is, prevent the action
            if (animating) {
                return;
            }

            // Check which current image we need to show
            if (direction == "next") {
                currentImg++;
                if (currentImg == photos.length + 1) {
                    currentImg = 1;
                }
            } else {
                currentImg--;
                if (currentImg == 0) {
                    currentImg = photos.length;
                }
            }

            // Check which container we need to use
            var currentContainer = activeContainer;
            if (activeContainer == 1) {
                activeContainer = 2;
            } else {
                activeContainer = 1;
            }

            showImage(photos[currentImg - 1], currentContainer, activeContainer);

        };

        var currentZindex = -1;
        var showImage = function (photoObject, currentContainer, activeContainer) {
            animating = true;

            // Make sure the new container is always on the background
            currentZindex--;

            // Set the background image of the new active container
            $("#headerimg" + activeContainer).css({
                "background-image": "url(" + photoObject.image + ")",
                "display": "block",
                "z-index": currentZindex
            });


            $('#popupDescriptions').remove('.triangle');

            var popupDescriptionsHtml = '';

            var popupTableStart = '<table cellpadding="0" cellspacing="0"><tr><td style="background:url(/library/images/common/home-popup-bubble-top.png) no-repeat; width:236px; height:12px;"></td></tr><tr><td style="background:url(/library/images/common/home-popup-bubble-bg.png) repeat-y; width:206px; height:12px;padding:0 15px;" class="bubbleContent">';
            var popupTableEnd = '</td></tr><tr><td style="background:url(/library/images/common/home-popup-bubble-bottom.png) no-repeat; width:236px; height:12px;"></td></tr></table>';
            var createPopup = function (top, left, content) {
                var html = '<div class="triangle" style="cursor:pointer; position:absolute; top:' + top + 'px; left:' + left + 'px; width:30px; height:30px;">';
                html += '<div style="display:none; position:absolute; top:-35px; left:-236px; width:236px;">' + popupTableStart + content + popupTableEnd + '</div>';
                html += '</div>';
                return html;
            }

            switch (photoObject.image) {
		case "/uploads/images/homepage-backgrounds/NS_Awards_FrontDoor.jpg":
			break;
		case "/uploads/images/homepage-backgrounds/ultimate-soft-shell.jpg":
			popupDescriptionsHtml += createPopup(340, 635, polartecPowerShieldPro);
			break;
		case "/uploads/images/homepage-backgrounds/short-cut.jpg":
			popupDescriptionsHtml += createPopup(82, 162, polartecPowerShield);
			popupDescriptionsHtml += createPopup(57, 200, polartecPowerShield);
			break;
		case "/uploads/images/homepage-backgrounds/one-more-time.jpg":
			popupDescriptionsHtml += createPopup(416, 190, polartecClassic);
			popupDescriptionsHtml += createPopup(315, 710, polartecPowerDry);
			popupDescriptionsHtml += createPopup(227, 572, polartecThermalPro);
			popupDescriptionsHtml += createPopup(127, 602, polartecPowerDry);
			popupDescriptionsHtml += createPopup(120, 760, polartecPowerShield);
			popupDescriptionsHtml += createPopup(194, 862, polartecPowerStretch);
			break;
		case "/uploads/images/homepage-backgrounds/just-warming-up.jpg":
			popupDescriptionsHtml += createPopup(108, 504, polartecWindBloc);
			popupDescriptionsHtml += createPopup(242, 691, polartecPowerShield);
			break;
		case "/uploads/images/homepage-backgrounds/one-tired-dog.jpg":
			popupDescriptionsHtml += createPopup(156, 703, polartecWindBloc);
			popupDescriptionsHtml += createPopup(274, 696, polartecPowerShield);
			break;
		case "/uploads/images/homepage-backgrounds/snow-days.jpg":
			popupDescriptionsHtml += createPopup(456, 48, polartecPowerStretch);
			popupDescriptionsHtml += createPopup(400, 92, polartecThermalPro);
			popupDescriptionsHtml += createPopup(447, 200, polartecPowerShield);
			popupDescriptionsHtml += createPopup(415, 245, polartecClassic);
			popupDescriptionsHtml += createPopup(447, 388, polartecClassic);
			popupDescriptionsHtml += createPopup(388, 386, polartecWindPro);
			popupDescriptionsHtml += createPopup(285, 95, polartecClassic);
			break;
		case "/uploads/images/homepage-backgrounds/escape.jpg":
			popupDescriptionsHtml += createPopup(277, 511, polartecPowerShield);
			popupDescriptionsHtml += createPopup(231, 640, polartecPowerDry);
			popupDescriptionsHtml += createPopup(337, 920, polartecWindPro);
			break;
		case "/uploads/images/homepage-backgrounds/rock-hang.jpg":
			popupDescriptionsHtml += createPopup(350, 223, polartecPowerDry);
			break;
		case "/uploads/images/homepage-backgrounds/another-busy-day.jpg":
			popupDescriptionsHtml += createPopup(103, 690, polartecPowerDry);
			popupDescriptionsHtml += createPopup(341, 713, polartecWindPro);
			break;
		case "/uploads/images/homepage-backgrounds/great-escapes.jpg":
			popupDescriptionsHtml += createPopup(343, 584, polartecPowerDry);
			break;
		case "/uploads/images/homepage-backgrounds/smores.jpg":
			popupDescriptionsHtml += createPopup(128, 905, polartecClassic);
			popupDescriptionsHtml += createPopup(390, 777, polartecWindPro);
			break;

                default:
                        break;
            }

            $("#popupDescriptions").html(popupDescriptionsHtml);

            $(".triangle").mouseover(function () {
                $(this).children("div").css("display", "block");
            }).mouseout(function () {
                $(this).children("div").css("display", "none");
            });

            // Fade out the current container
            // and display the header text when animation is complete
            //alert("#headerimg" + currentContainer);
            $("#headerimg" + currentContainer).fadeOut(600, function () {
                setTimeout(function () {
                    animating = false;
                }, 100);
            });
        };

        var stopAnimation = function () {
            // Change the background image to "play"
            //$("#control").css({ "background-image": "url(/library/images/common/btn_play.png)" });

            // Clear the interval
            clearInterval(interval);
        };

        // We should statically set the first image
        navigate("next");

        // Start playing the animation
        interval = setInterval(function () {
            navigate("next");
        }, slideshowSpeed);

    });
