try{Typekit.load();}catch(e){}

function BVHandleSummaryResults(jsonData) {
    var $u = $('#UgcSummary_' + jsonData.subjectID);
    if ($u.length) {
        if (jsonData.totalReviews > 0 && jsonData.reviewsUrl.length > 0) {
            var averageRating = jsonData.averageRating.toFixed(2);
            var averageRatingSplit = averageRating.split('.');
            $u.append(
                "<span class='hotSpringSpaModelRating' itemprop='aggregateRating' itemscope itemprop='http://schema.org/AggregateRating'>" +
                "<span itemprop='ratingValue'><img src='http://hotspring.ugc.bazaarvoice.com/0526-en_us/"+ averageRatingSplit[0] + '_' + averageRatingSplit[1] +"/5/rating.gif' alt="+ averageRating +"  style='padding: 5px 0;' /></span>" +
                "<br/>" +
                "<a href=" + jsonData.reviewsUrl + " title='Read Hot Spring Reviews' target='_blank' style='display: block; clear: both; color: #4E7682; text-decoration: none;'>" +
                "Read <span itemprop='reviewCount'>" + jsonData.totalReviews + "</span> reviews</a>" +
                "</span>" +
                '<a href="http://www.hotspring.com" title="Hot Spring Spas" target="_blank" style="display: block; padding: 5px 0 0 0;"><img alt="Portable Hot tub by Hot Spring Spas" src="http://media.hotspring.com/logos/hss-video-logo.png" width="97" height="30" /></a>'
            );
        }
        if (jsonData.totalQuestions > 0 && jsonData.questionsUrl.length > 0) {
            var readQAStr = "Read " + jsonData.totalQuestions + " questions and " + jsonData.totalAnswers + " answers";
            $u.append("<br>" + readQAStr.link(jsonData.questionsUrl));
        }
    }
}

$(document).ready(function() {
	
		$(".about-sidelink:eq(0), .about-sidelink:eq(1)").children("h3").children("a").attr("target", "_blank");

    var oldIe = ($.browser.msie && parseInt($.browser.version) < 9);

    // emulate placeholder if browser doesn't support it
    var input = document.createElement('input');
    input = 'placeholder' in input;
    if (!input) {
        $('input[placeholder]').each(function() {
            var $this = $(this);
            var value = $this.attr('placeholder');
            $this.val(value).focus(function() {
                if ($this.val() == value) {
                    $this.val('');
                }
            }).blur(function() {
                if ($this.val() == '') {
                    $this.val(value);
                }
            });
        });
    }

    // subnav accordion
    $('#subnav').accordion({
        autoHeight: false,
        header: 'span.subnav',
        animated: 'slide',
        icons: {
            header: 'ui-icon-plus',
            headerSelected: 'ui-icon-minus'
        }
    });
    $('.subnav-links ul li:last-child').addClass('last');

    // homepage slideshow
    if ($('#slideshow').length) {
        var $wrapper = $('#wrapper');
        var $slideshow = $('#slideshow').css({opacity: 0});
        var slideshow = {
            imgPath: $wrapper.css('background-image').replace(/url\(|\)|"|'|[1-4]\.jpg/g, ''),
            nextSlide: 2,
            delay: 6000,
            speed: 2500,
            onload: function() {
                if (slideshow.nextSlide == 1) {
                    if (oldIe) {
                        setTimeout(function() {
                            $('div.slogan').slideDown(slideshow.speed / 2, 'easeInCubic');
                        }, slideshow.speed / 2);
                    }
                    else {
                        $('div.slogan').show().stop().animate({opacity: 1}, slideshow.speed, 'easeInCubic');
                    }
                }
                else if (slideshow.nextSlide == 2) {
                    if (oldIe) {
                        setTimeout(function() {
                            $('div.slogan').slideUp(slideshow.speed / 2, 'easeOutCubic');
                        }, slideshow.speed / 2);
                    }
                    else {
                        $('div.slogan').stop().animate({opacity: 0}, slideshow.speed, 'easeInCubic', function() {
                            $(this).hide();
                        });
                    }
                }
                $slideshow.css({
                    opacity: 0,
                    backgroundImage: 'url(' + slideshow.imgPath + slideshow.nextSlide + '.jpg)'
                }).stop().animate({opacity: 1}, slideshow.speed, 'easeInCubic', function() {
                    $wrapper.css('background-image', 'url(' + slideshow.imgPath + slideshow.nextSlide + '.jpg)');
                    $slideshow.css({opacity: 0});
                    if (++slideshow.nextSlide > 4) {
                        slideshow.nextSlide = 1;
                    }
                    setTimeout(slideshow.onload, slideshow.delay);
                });
            },
            init: function() {
                $('<img />').load(function() {
                    slideshow.onload();
                    $(this).remove();
                }).attr('src', slideshow.imgPath + slideshow.nextSlide + '.jpg');
            }
        };
        setTimeout(slideshow.init, slideshow.delay);
    }

    // homepage text slider
    if ($('#home-slider').length) {
        var slideWidth = $('#home-slider').innerWidth() + 2;
        var $slider = $('#home-slider-wrapper');
        /*
        if ($slider.children('div').length < 3) {
            $('#slide-right, #slide-left').css({opacity: 0, cursor: 'default'});
        }
        */
        var limit = slideWidth * Math.ceil($slider.children('div').length / 2) * -1;
        $('#slide-right').click(function() {
            if ($slider.is(':animated')) {
                return false;
            }
            current = parseInt($slider.css('margin-left'), 10);
            if (current - slideWidth <= limit) {
                return false;
            }
            $slider.stop().animate({
                marginLeft: current - slideWidth + "px"
            }, 500, 'easeOutCubic');
        });
        $('#slide-left').click(function() {
            if ($slider.is(':animated')) {
                return false;
            }
            current = parseInt($slider.css('margin-left'), 10);
            if (current + slideWidth > 0) {
                return false;
            }
            $slider.stop().animate({
                marginLeft: current + slideWidth + "px"
            }, 500, 'easeOutCubic');
        });
    }

    // sidebar tabs/blocks
    $('.about-sidelink').each(function() {
        if ($(this).find('a').length) {
            $(this).css('cursor', 'pointer').click(function() {
                $(this).find('a').trigger('click');
            });
        }
    });

    // spas tabs
    $('.tabimage > div').each(function() {
        $this = $(this);
        bottomOffset = -($this.height() - $this.find('strong').innerHeight());
        $this.data('bottom', bottomOffset).css('bottom', bottomOffset);
    });
    $('.tabimage').hover(function() {
        $(this).children('div').stop().animate({'bottom': 0}, 500, 'easeOutCubic');
    }, function() {
        $div = $(this).children('div');
        $div.stop().animate({bottom: $div.data('bottom')}, 300, 'easeOutCubic');
    });
    $('#vendor-tabs').tabs();

    // opera tabs hack
    if ($.browser.opera) {
        $('#vendor-tabs ul').css('margin-top', '-10px');
    }

    $('#commentform p label + span').each(function() {
        $(this).siblings('label').prepend($(this).clone());
        $(this).remove();
    });

    $('<span class="note">Email will not be published</span>').insertBefore('.form-submit');

    $('#post-19 .tabimage > div[id]').each(function() {
        var id = $(this).attr('id');
        var html = '<div itemscope itemtype="http://schema.org/Product">' +
            '<span id="UgcSummary_' + id + '" style="font-family: Arial, sans-serif; font-size: 12px; color: #5A5A5A;">' +
            '<span style="font-weight:bold;">Average <span itemprop="model">' + $(this).children('strong').html() + '</span> Rating:</span><br />' +
            '</span></div>';
        $(this).children('p').append('<br><br>' + html + '<script type="text/javascript" src="http://hotspring.ugc.bazaarvoice.com/0526-en_us/'+id+'/statisticssummary.js?format=embedded"></script>');
    });

});
