$(document).ready(function() {

    $("div#tech-specs table tr").find("td:first").css("padding-right", "1em");



    // Handle in-page tabs

    //When page loads...
    $.history.init(pageload);
    function pageload(hash) {
        var hash = window.location.hash;
        $(".tab").hide(); //Hide all content

        if (hash.length == 0 || hash == "#") {
            $("ul#tabs li").removeClass("selected");
            $("ul#tabs li:first").addClass("selected").show(); //Activate first tab
            $(".tab:first").show(); //Show first tab content
        }
        else {
            hash = hash.replace("#", "");
            var anchor = "a." + hash;
            $("ul#tabs li").removeClass("selected");
            $(anchor).parent('li').addClass('selected').show(); //Activate tab with correct hash
            //Show correct tab content	
            $("#" + hash + '-content').show();
        }
    }


    // set onlick event for tabs
    $("a[rel='history']").click(function() {
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        location.hash = hash;
        // moves to a new page. 
        // pageload is called at once. 
        $.history.load(hash);
        return false;
    });

    $('#carousel').jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    $('#title-carousel').jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    $('#carousel-video').jcarousel({
        scroll: 3
    });


    function mycarousel_initCallback(carousel) {
        $('.jcarousel-control a').bind('click', function() {
            carousel.scroll($.jcarousel.intval($(this).text()));
            return false;
        });

        $('.jcarousel-scroll select').bind('change', function() {
            carousel.options.scroll = $.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        $('#mycarousel-next').bind('click', function() {
            carousel.next();
            return false;
        });

        $('#mycarousel-prev').bind('click', function() {
            carousel.prev();
            return false;
        });
    };


    // FancyBox image gallery
    $("a.gallery").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true, 'overlayOpacity': 0.4, 'imageScale': false });




    // Tooltips for video slider
    if ($('#tooltip-display').length < 1) {
        $('body').append('<div id="tooltip-display"> </div>')
    }



    $('#carousel-video li').hover(function() {
        var tooltip = $(this).find('#tooltip').html(); // Get html to use for tooltip
        var offset = '';
        var offset = $(this).offset();
        var leftvar = offset.left;  // Get the left offset for <li> item
        var topvar = offset.top;  // Get the top offset for <li> item
        $('#tooltip-display').css({ top: topvar - 140, left: leftvar, display: 'block' });  // Set the css for the tooltip
        $('#tooltip-display').html(tooltip);  // Add the html to the tooltip
    });

    $('#product-experience').hover(function() {
        //
    }, function() {
        var tooltip = '';
        $('#tooltip-display').html(tooltip);
        $('#tooltip-display').css({ display: 'none' });
    });

    $('.jcarousel-prev, .jcarousel-next').click(function() {
        $('#tooltip-display').css({ display: 'none' });
    });



    $("div#image-gallery2").slideViewerPro({
        thumbs: 6,
        autoslide: false,
        asTimer: 3500,
        typo: true,
        galBorderWidth: 0,
        thumbsBorderOpacity: 0,
        buttonsTextColor: "#707070",
        buttonsWidth: 40,
        thumbsActiveBorderOpacity: 0.8,
        thumbsActiveBorderColor: "aqua",
        shuffle: false
    });

    $('#product-experience * li:first').css("margin-left", "0");


    // Carousel count handling
    var carouselcount = $('ul#carousel-video li').size();
    var lastItem = 3;
    $('div.jcarousel-prev').hide();
    $('div.jcarousel-next').click(function() {
        lastItem += 3;
        videoCarousel();
    });
    $('div.jcarousel-prev').click(function() {
        lastItem -= 3;
        videoCarousel();
    });

    function videoCarousel() {
        if (lastItem > carouselcount)
            lastItem = carouselcount;
        else if (lastItem < 3)
            lastItem = 3;

        if (carouselcount == lastItem) {
            $('div.jcarousel-next').hide();
        } else
            $('div.jcarousel-next').show();
        if (lastItem == 3) {
            $('div.jcarousel-prev').hide();
        } else
            $('div.jcarousel-prev').show();

        var lower = Number(lastItem) - 2;
        var upper = lastItem;
        $('div#carousel-video-title span#carousel-video-count').text(lower + "-" + upper);
    }
});
