jQuery.query = function() {
    var r = {};
    var q = location.search;
    q = q.replace(/^\?/,''); // remove the leading ?
    q = q.replace(/\&$/,''); // remove the trailing &
    jQuery.each(q.split('&'), function(){
        var key = this.split('=')[0];
        var val = this.split('=')[1];
                // convert floats
        if(/^[0-9.]+$/.test(val))
            val = parseFloat(val);
                // ingnore empty values
        if(val)
            r[key] = val;
    });
    return r;
};

function openBrightcoveWindow(brightcoveUrl) {
    var url = "http://link.brightcove.com/services/link/bcpid1078541307/bclid1896835944/bctid1896776658";
    if (brightcoveUrl) {
        url = brightcoveUrl;
    }
    window.open(url, 'videolibrary', 'width=1000,height=660,resizable=yes');
}

function submitOver() {
    $('.submit').hover(
            function() {
                $(this).attr("src",$(this).attr("src").replace("-off","-on"));
            },
            function() {
                $(this).attr("src",$(this).attr("src").replace("-on","-off"));
            }
            );
}

function spiffOver() {
    $('.spiff').hover(
		function() {
			$(this).css("background-image", "url('/common/images/bg-leftNav-spiff-on.jpg')");
		}, function() {
			$(this).css("background-image", "url('/common/images/bg-leftNav-spiff-off.jpg')");
		}
	);

	$('.spiff-subzero').hover(
		function() {
			$(this).css("background-image", "url('/common/images/bg-leftNav-spiff-subzero-on.jpg')");
		}, function() {
			$(this).css("background-image", "url('/common/images/bg-leftNav-spiff-subzero-off.jpg')");
		}
	);
}

function downloadOver() {
    $('li.download a').hover(
            function() {
                $(this).parent().css("background-image", "url('/common/images/bullet-upDown-over.gif')");
            },
            function() {
                $(this).parent().css("background-image", "url('/common/images/bullet-downloadIcon.gif')");
            }
            );
}

function popupOver() {
    $('li.quicklook a').hover(
            function() {
                $(this).parent().css("background-image", "url('/common/images/bullet-upDown-over.gif')");
            },
            function() {
                $(this).parent().css("background-image", "url('/common/images/bullet-quicklookIcon.gif')");
            }
            );
}


function newProductIconOver() {
    var myTimeout;
    var removeFunction = function() { $("#newProduct-hover").remove() };
    var hover = $('<div id="newProduct-hover">' +
                              '<div class="newProduct-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                              '<div class="icon-text">New Arrival</div>' +
                              '<div class="newProduct-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                              '</div>');
    $('img.newproduct-results').hover(
            function() {
                clearTimeout(myTimeout);
                $("#saleProduct-hover").remove();
                $(hover).css('left', this.offsetLeft - 60);
                $(hover).hover(
                        function() { clearTimeout(myTimeout); },
                        function() { myTimeout = setTimeout(removeFunction, 150); }
                );
                $(this).parent('.productIcon-container').append(hover);
            },
            function() {
                myTimeout = setTimeout(removeFunction, 150);
            }
    );
    $('img.newproduct').hover(
            function() {
                clearTimeout(myTimeout);
                $("#saleProduct-hover").remove();
                $(hover).css('left', $(this).parent()[0].offsetLeft + 20);
                $(hover).hover(
                        function() { clearTimeout(myTimeout); },
                        function() { myTimeout = setTimeout(removeFunction, 150); }
                );
                $('#prodDtl-tools').before(hover);
            },
            function() {
                myTimeout = setTimeout(removeFunction, 150);
            }
    );
}

function saleProductIconOver() {
    var myTimeout;
    var removeFunction = function() { $("#saleProduct-hover").remove() };
    var hover = $('<div id="saleProduct-hover">' +
                              '<div class="saleProduct-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                              '<div class="icon-text">Sale</div>' +
                              '<div class="saleProduct-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                              '</div>');
    $('img.saleproduct-results').hover(
            function() {
                clearTimeout(myTimeout);
                $("#newProduct-hover").remove();
                $(hover).css('left', this.offsetLeft - 30);
                $(hover).hover(
                        function() { clearTimeout(myTimeout); },
                        function() { myTimeout = setTimeout(removeFunction, 150); }
                );
                $(this).parent('.productIcon-container').append(hover);
            },
            function() {
                myTimeout = setTimeout(removeFunction, 150);
            }
    );
    $('img.saleproduct').hover(
            function() {
                clearTimeout(myTimeout);
                $("#newProduct-hover").remove();
                $(hover).css('left', $(this).parent()[0].offsetLeft + 20);
                $(hover).hover(
                        function() { clearTimeout(myTimeout); },
                        function() { myTimeout = setTimeout(removeFunction, 150); }
                );
                $('#prodDtl-tools').before(hover);
            },
            function() {
                myTimeout = setTimeout(removeFunction, 150);
            }
    );
}

   function iconOver(whichOne,imgname) {
       var imgsrc="/common/images/" + imgname;
       var term="";
    $(whichOne).attr("src",imgsrc);
       if(imgname=='sale_on.gif'){
         term='<b>Sale</b> - This item is currently on sale.' ;
       }else if (imgname=='new_on.gif'){
            term= '<b>New</b> - This is a new item.';
       } else if (imgname=='classic_on.gif'){
          term='<b>Classic "Made-To-Order"</b> - This item is not stocked  at<br>'+'McGuire, rather it is manufactured per customer order and may<br> have up to a 16-20 week lead time. ';
       }else if(imgname=='finish_on.gif'){
           term= '<b>Limited Finishes</b> - Finishes for this item do not follow the <br> standard McGuire finish convention. See "Finishes" tab for<br>options.';
       }else if (imgname='availability_on.gif'){
               term= '<b>Limited Availability</b> - Item is currently in-stock at McGuire<br>but will not be replenished once sold. Please check with<br>McGuire customer service at ? for availability before placing an<br>order.';
       }
   $('#iconText').html(term);

}

 function iconOut(whichOne,imgname) {
       var imgsrc="/common/images/" + imgname;
       $(whichOne).attr("src",imgsrc);
       $('#iconText').html('(roll over icon for info)');

}

 

function newProduct(obj) {
    $(obj).each(function(i){
        var thisLink = $(this).children();
        thisLink.after('<div style="position:absolute; top:3px; right:10px;"><img src="/common/images/icon-new.png" width="25" height="8" alt="New" border="0" /></div>');
    });
};

function roomScene(obj) {
    $(obj).each(function(i){
        $(this).before('<div class="relative"><img src="/common/images/icon-roomScene.gif" width="10" height="10" alt="View Room Scene" border="0" class="roomScene-icon" /></div>');
    });
};

function roomSceneKeyVisual(objs) {
    $(objs).each(function(j){
        $(this).after('<div class="relative"><img src="/common/images/icon-roomScene.gif" width="10" height="10" alt="View Room Scene" border="0" class="roomScene-keyVisual-icon" /></div>');
    });
};

function enlargeKeyVisual(objs) {
    $(objs).each(function(j){
        $(this).after('<div class="relative"><a href="#" onclick="showEnlargedImages(\'0\'); return false;"><img src="/common/images/icon-roomScene.gif" width="10" height="10" alt="View Room Scene" border="0" class="roomScene-keyVisual-icon" /></a></div>');
    });
};

function launchVideo(whichVideo) {
    var videoCode =
            '<div id="videoPlayer">' +
            '<div class="lightbox-canvasClose"><img src="/common/images/btn-closeLightbox.gif" width="9" height="9" alt="Close" border="0" onClick="killLightbox();" /></div>' +
            '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="508" height="327" name="flashvideo" id="flashvideo">' +
            '<param name="movie" value="/common/swf/' + whichVideo + '.swf">' +
            '<param name="quality" value="best">' +
            '<param name="wmode" value="opaque">' +
            '<param name="allowscriptaccess" value="always">' +
            '<param name="scale" value="noscale">' +
            '<embed src="/common/swf/' + whichVideo + '.swf" quality="best" scale="noscale" allowscriptaccess="always" id="flashvideo" name="flashvideo" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="508" height="327"></embed>' +
            '</object>';
    '</div>';
    createLightbox();
    $('#lightbox-copy').html(videoCode);
    ieWidthFix();
    fireOmnitureVideoEvent(whichVideo);
}

function fireOmnitureVideoEvent(video) {
    var s=s_gi('kohlerinteriorsmcguire,kohlerglobal');
    s.linkTrackVars='channel,prop1,prop2,pageName,prop9,events';
    s.linkTrackEvents='event13';
    s.prop1='MCGUIRE';
    s.channel='MCGUIRE:VIDEO';
    s.prop2='MCGUIRE:VIDEO:VIDEO';
    s.pageName='MCGUIRE:VIDEO:VIDEO:' + video.toUpperCase();
    s.prop9='VIDEO';
    s.events='event13';
    void(s.t());
}

function createLightbox() {
    $('#lightbox').css("display","block");
    $('#lightbox-canvas').css("display","block");
    if((document.documentElement.scrollTop > -1) && (!$.browser.safari)) {
        $('#lightbox-canvas').css("top", (document.documentElement.scrollTop + 10) + "px");
    } else {
        $('#lightbox-canvas').css("top", (self.pageYOffset + 10) + "px");
    }
}

function killLightbox() {
	$('#glasstopdata').hide();
    $('#lightbox').css("display","none");
    $('#lightbox-canvas').css("display","none");
    if($.browser.msie) {
        $('.lightbox-canvas-3').css("width",'1px');
        $('.lightbox-canvas-2').css("width",'1px');
        $('.lightbox-canvas-1').css("width",'1px');
    }
}

function populateEnlarge() {
    $('#lightbox-copy').html(enlargeImageHtml);
}

function getNavQueryString() {
    var sectionNumber = $.query()['sectionNumber'];
    var subsectionNumber = $.query()['subsectionNumber'];
    var tertiarySectionNumber = $.query()['tertiarySectionNumber'];
    var navQueryString = ""
    if (sectionNumber != null) navQueryString += "&sectionNumber=" + sectionNumber;
    if (subsectionNumber != null) navQueryString += "&subsectionNumber=" + subsectionNumber;
    if (tertiarySectionNumber != null) navQueryString += "&tertiarySectionNumber=" + tertiarySectionNumber;

    return navQueryString;
}

function populateQuicklookProduct(prodId) {
    var navQueryString = getNavQueryString();
    $.get('/ajax/quicklook.mcg?productNumber=' + prodId + navQueryString, function(transport) {
        $('#lightbox-copy').html(transport);
        createLightbox();
        ieWidthFix();
    });
}

function populateQuicklookTextile(prodId) {
    var navQueryString = getNavQueryString();
    $.get('/ajax/textileQuicklook.mcg?productNumber=' + prodId + navQueryString, function(transport) {
        $('#lightbox-copy').html(transport);
        createLightbox();
        ieWidthFix();
    });
}

function swapQlImg(whichImg,shownIn,qlObj) {
    if(qlObj != undefined) {
        $('#ql-otherImages img').removeClass("active");
        $(qlObj).addClass("active");
    }
    $('#ql-image img').attr({src:'/onlinecatalog/resources/images/jpg375x375/' + whichImg});
    $('#shownIn').html(shownIn);
    if(shownIn=="") {
        $('#shownIn-copy').hide();
    } else {
        $('#shownIn-copy').show();
    }
}

function populateEnlargeImage(hasScene7) {
    if (hasScene7) {
		$('#lightbox-copy').append($("#lb-otherViews-content"));
    } else {
        createEnlargedCode();
        $('#lightbox-copy').html(enlargedImage);
    }
}

function populateGlassTop() {
	$('#glasstopdata').show();
    $('#lightbox-copy').html($('#glasstopdata').html());
		   
}

function populateEnlargeTextile() {
    createEnlargedTxtlCode();
    $('#lightbox-copy').html(enlargedTxtl);
}

function ieWidthFix() {
    if($.browser.msie) {
        var setLineWidth = $('#lightbox-copy').width();
        $('.lightbox-canvas-3').css("width",setLineWidth + 5 + 'px');
        $('.lightbox-canvas-2').css("width",setLineWidth + 7 + 'px');
        $('.lightbox-canvas-1').css("width",setLineWidth + 9 + 'px');
    }
}

function wtbDefinition(term, define, whichOne) {
    $(whichOne).after('<div id="definition-canvas">' +
                      '<div class="definition-top"><img src="/common/images/spacer.gif" width="184" height="11" alt="" border="0" /></div>' +
                      '<div class="definition-copy"><span id="definition-term"></span><br /><span id="definition-define"></span></div>' +
                      '<div class="definition-bottom"><img src="/common/images/spacer.gif" width="184" height="15" alt="" border="0" /></div>' +
                      '</div>');
    $('#definition-term').html(term);
    $('#definition-define').html(define)
}



function dimensionDefinition(whichOne) {
    $(whichOne).after('<div id="definition-canvas">' +
                      '<div class="definition-top"><img src="/common/images/spacer.gif" width="184" height="11" alt="" border="0" /></div>' +
                      '<div class="definition-copy"><span id="definition-term">COM</span><br /><span id="definition-define">Customer\'s Own Material based on 54\" plain goods</span><br /><span id="definition-term">COL</span><br /><span id="definition-define">Customer\'s Own Leather</span></div>' +
                      '<div class="definition-bottom"><img src="/common/images/spacer.gif" width="184" height="15" alt="" border="0" /></div>' +
                      '</div>');
}

function dimensionDefinitionMetric(whichOne) {
    $(whichOne).after('<div id="definition-canvas">' +
                      '<div class="definition-top"><img src="/common/images/spacer.gif" width="184" height="11" alt="" border="0" /></div>' +
                      '<div class="definition-copy"><span id="definition-term">COM</span><br /><span id="definition-define">Customer\'s Own Material based on 137cm plain goods</span><br /><span id="definition-term">COL</span><br /><span id="definition-define">Customer\'s Own Leather</span></div>' +
                      '<div class="definition-bottom"><img src="/common/images/spacer.gif" width="184" height="15" alt="" border="0" /></div>' +
                      '</div>');
}


$(function() {
    newProduct('li.new');
    roomScene('img.roomScene');
    roomSceneKeyVisual('img.roomScene-keyVisual');
    enlargeKeyVisual('img.enlarge-keyVisual');
    submitOver();
    spiffOver();
    downloadOver();
    popupOver();
    newProductIconOver();
    saleProductIconOver();
   
});

(function($){
    $(function() {
        var pageHeight;
        if (document.documentElement && document.documentElement.scrollHeight) {
            pageHeight = document.documentElement.scrollHeight;
        } else if (document.body) {
            pageHeight = document.body.scrollHeight;
        }

        $('#footer').after(
                '<div id="lightbox">&nbsp;</div>' +
                '<div id="lightbox-canvas">' +
                '<div class="lightbox-canvas-3"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '<div class="lightbox-canvas-2"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '<div class="lightbox-canvas-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '<div class="rightShadow">' +
                '<div id="lightbox-copy">&nbsp;</div>' +
                '<div class="lightbox-canvas-1"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '<div class="lightbox-canvas-2"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '<div class="lightbox-canvas-3"><img src="/common/images/spacer.gif" width="1" height="1" border="0" alt="" /></div>' +
                '</div>' +
                '<div class="bottomShadow"><div><img src="/common/images/spacer.gif" width="1" height="11" border="0" alt="" /></div></div>' +
                '</div>'
                );

        $("div.roundedCorners").corner("round 3px");

        $('#lightbox').css('height', pageHeight)


        var searchBox = $("#search-box input");
        var defaultSearchText = searchBox.val();
        searchBox.focus(function() {
            if ($(this).val() == defaultSearchText) {
                $(this).val("");
            }
        });
        searchBox.blur(function() {
            if ($.trim($(this).val()) == "") {
                $(this).val(defaultSearchText);
            }
        });
        $("#search-box a").click(function() {
            if ($.trim(searchBox.val()) == "" || $.trim(searchBox.val()) == $.trim(defaultSearchText)) {
                return false;
            }
            $(this).parent().get(0).submit();
            return false;
        });

        $('a.print').click(
                function() {
                    createLightbox();
                    populatePrint();
                    ieWidthFix();
                }
                );

	  $('#glasstoplink').click(
		  
                function() {
					createLightbox();
					populateGlassTop();
                    ieWidthFix();
                }
                );

        $('a.enlarge').click(
                function() {
                    var hasScene7 = false;
                    if ($(this).hasClass('has_scene7')) {
                        hasScene7 = true;
                    }
                    createLightbox();
                    populateEnlargeImage(hasScene7);
                    ieWidthFix();
                }
                );

        $('a.enlargeTxtl').click(
                function() {
                    createLightbox();
                    populateEnlargeTextile();
                    ieWidthFix();
                }
                );

        $('a.unsubscribe').click(
                function() {
                    createLightbox();
                    populateUnsubscribe();
                    submitOver();
                    ieWidthFix();
                }
                );

        $('a.quickLook-product').click(
                function() {
                    populateQuicklookProduct($(this).attr('id').substring(3,$(this).attr('id').length));
                }
                );

        $('a.quickLook-textile').click(
                function() {
                    populateQuicklookTextile($(this).attr('id').substring(3,$(this).attr('id').length));
                }
                );



      /*  $('.limitedIcon').hover(
                function() {
                    $(this).next('.limitedCopyHolder').css("display", "block");
                },
                function() {
                    $(this).next('.limitedCopyHolder').css("display", "none");
                }

                );    */

        $('img.definition').mouseout(
                function() {
                    $('#definition-canvas').remove();
                }
                );

        $('#lightbox').add('.lightbox-canvasClose img').click(
                function() {
                    killLightbox();
                }
                );
    })
})(jQuery);
