$(document).ready(function() {

    var s7viewer;

    // Lightbox for enlarged images script
    $('#prodDtl-imageArea img').click(function() {
		$('#lb-sceneSeven-otherViews #sceneSeven-sprayPatterns').css('display','none');	
		$('#lb-sceneSeven-otherViews #sceneSeven-altViews').css('display','block');
        openEnlargeImageLayer(0);
        return false;
    });

    $('.images-otherViews img').click(function() {
		$('#lb-sceneSeven-otherViews #sceneSeven-sprayPatterns').css('display','none');	
		$('#lb-sceneSeven-otherViews #sceneSeven-altViews').css('display','block');	
        var index = $(".images-otherViews img").index(this);
        openEnlargeImageLayer(index + 1);
        return false;
    });
	
    // Lightbox for spray patterns popup
    $('a.lightbox-sprayPatterns').click(
		function() {
			$('#lb-sceneSeven-otherViews #sceneSeven-altViews').css('display','none');
			$('#lb-sceneSeven-otherViews #sceneSeven-sprayPatterns').css('display','block');
			var sprayPatternDefault = $(this).attr('title');
			$("#lightbox-imageEnlarge").showLightbox(function() {
	            if (s7viewer == null) {
	                s7viewer = new SjZViewer('http://s7d4.scene7.com/is/image/Kohler/', sprayPatternDefault, 498, 498);
	                s7viewer.setBackground("0xffffff");
	                s7viewer.setTurnTime(1);
	                s7viewer.setMaxZoom(100); //sets the initial maxZoom for images
	                s7viewer.setZoomStep(2); //sets the initial maxZoom for images
	            }
				if (s7viewer != null) {
		            var imageUrl = '';
		            imageUrl = sprayPatternDefault + "?op_sharpen=1&qlt=75,1";
					s7viewer.setImage('http://s7d4.scene7.com/is/image/Kohler/' + imageUrl, true);
		        }
			});
	});	


    // Clicking on other views opens the enlarged images lightbox
    $("lb-sceneSeven-otherViews a").click(function() {
        var index = $("lb-sceneSeven-otherViews a").index(this);
        enlargeImage(index);
        return false;
    });

    $("#sceneSeven-tools .lightbox-zoomIn").click(function() {
        if (s7viewer != null) {
         s7viewer.zoomIn();
        }
        return false;
    });

    $("#sceneSeven-tools .lightbox-zoomOut").click(function() {
        if (s7viewer != null) {
            s7viewer.zoomOut();
        }
        return false;
    });

    $("#sceneSeven-tools .lightbox-reset").click(function() {
        if (s7viewer != null) {
            s7viewer.reset();
        }
        return false;
    });

    $(".alternateViews a").click(function() {
        var index = $(".alternateViews a").index(this);
        enlargeImage(index);
        return false;
    });

    function openEnlargeImageLayer(initalIndex) {
        var mainImage = $("#lb-sceneSeven .scene7img:first").text();
        $("#lightbox-imageEnlarge").showLightbox(function() {
            if (s7viewer == null) {
                s7viewer = new SjZViewer('http://s7d4.scene7.com/is/image/', mainImage, 498, 498);
                s7viewer.setBackground("0xffffff");
                s7viewer.setTurnTime(1);
                s7viewer.setMaxZoom(100); //sets the initial maxZoom for images
                s7viewer.setZoomStep(2); //sets the initial maxZoom for images
            }
            enlargeImage(initalIndex);
        });
    }

    function enlargeImage(index) {
        if (index < 0 || index > $("#lb-sceneSeven-otherViews img").size() - 1) {
            return;
        }
        if (s7viewer != null) {
            var imageUrl = $("#lb-sceneSeven-otherViews img").get(index).src;
            imageUrl = imageUrl.substring(0, imageUrl.lastIndexOf("?"));
            imageUrl = imageUrl + "?op_sharpen=1&qlt=75,1"
            s7viewer.setImage(imageUrl, true);
        }
    }

    // Lightbox for approved icons script
    $('.icon-approved').click(
        function() {
            $("#lightbox-scrollableArea-prodApprovals").showLightbox(function() {
                $(this).find('.lightbox-scrollMe-content').css('display','block');
                $(this).find('.lightbox-scrollMe').jScrollPane({showArrows:false, scrollbarWidth:10});
            });
            return false;
        }
    );

    // Lightbox for about shower trays popup
    $('.lightbox-aboutShowerTrays').click(
        function() {
            $("#lightbox-scrollableArea-aboutShowerTrays").showLightbox(function() {
                $(this).find('.lightbox-scrollMe-content').css('display','block');
                $(this).find('.lightbox-scrollMe').jScrollPane({showArrows:false, scrollbarWidth:10});
            });
            return false;
        }
    );


    // Populate the print friendly layer
    $("#prodDtl-print").click(function() {

        var print = $("#lightbox-printDetail");

        // Empty the layer
        $("#lightbox-printDetail").children()
                .not("div.closeLightbox")
                .not("div#printDetail-copyright")
                .remove();

        // Insert content
        var contentMarker = $("#lightbox-printDetail").children("#printDetail-copyright");
        contentMarker.before($("#prodDtl-imageArea").children().clone());
        contentMarker.before($("#prodDtl-info").children().clone());
        print.find(".prodDtl-info-left").prepend($(print.find(".prodDtl-name")).remove());
		$("#lightbox-printDetail .prodDtl-guaranteeLogos").prepend('<img width="35" height="35" border="0" alt="Print this page" onclick="window.print(); return false;" src="/common/images/btn-print-off.gif" class="btn-printMe" /><br />')
		$('.btn-printMe').bind("mouseover", function() {
			$(this).attr("src",$(this).attr("src").replace("-off","-on"));
		});
		$('.btn-printMe').bind("mouseout", function() {
			$(this).attr("src",$(this).attr("src").replace("-on","-off"));
		});
        
        // Remove non-print elements
        $(print).find(".prodDtl-mainImage+span+span, .prodDtl-name .btn-back, #prodDtl-pdfLinks, .prodDtl-info-right .prodDtl-tools, #prodDtl-prodLinks, .images-otherViews span").remove();

        // Adjust the image dimensions
        var width = 435;
        var height = 405;
        var mainImage = print.find(".prodDtl-mainImage");
        var src = mainImage.attr("src");
        src = src.replace(/wid=\d*?&/, "wid=" + width + "&");
        src = src.replace(/hei=\d*?&/, "hei=" + height + "&");
        mainImage.attr("src", src);
        mainImage.attr("width", width);
        mainImage.attr("height", height);
    });
});

function guaranteePanel() {
    $('#lightbox-scrollableArea-guarantee').showLightbox();
    return false;
}

function waterPanel(system) {
    $('#lightbox-scrollableArea-systems').showLightbox();
    $(".single-system").hide();
    var div = "#"+system;
    $(div).show('normal',function() {
		$('#lightbox-scrollableArea-systems').find('.lightbox-scrollMe').jScrollPane({showArrows:false, scrollbarWidth:10});
	});
    return false;
}

function showRiserKits(whichOne) {
	$("#aboutRiserKits-" + whichOne).css('display','block');
	$("#lightbox-scrollableArea-aboutRiserKits").showLightbox();
	return false;
	}