
DatasheetTileInit = function() {

    $(".show-adv-specs-button").click(function() {
        var section = $(this).attr("attr-section");
        $test = $(".adv-specs[attr-section=" + section + "]");
        $test.children(".th, .td").slideToggle(400, 'myEasing');

        if ($(this).text() === "More") {
            $(this).text("Less");
            SaleaeTrackEvent("Technical Specs", "show more " + section, "Show more Tech Specs", { 'section': section });
        } else {
            $(this).text("More");
        }
    });
}


;

FaqTileInit = function () {

    SetupTabbedPanels.call($('#FaqTile'));

    $(".faq-column").on("click", ".faq-question", function () {
        $(this).siblings('.faq-answer').slideToggle(50);
        var question_text = $(this).get(0).lastChild.nodeValue;
        SaleaeTrackEvent("FAQ", "clicked question", "clicked FAQ question", { 'question': question_text });
    });

    var faq_tab;
    if (faq_tab = $.urlParam('faq_tab')) {
        if (faq_tab === "faq") {
            $('#FaqTile .tp-tabs span').first().trigger('click');
        }
        else if (faq_tab === "service") {
            $('#FaqTile .tp-tabs span:nth-child(2)').trigger('click');
        }
        else if (faq_tab === "warranty") {
            $('#FaqTile .tp-tabs span:nth-child(3)').trigger('click');
        }
    }
}



;

HardwareTileInit = function () {
    $('#HardwareTile').on('click', '.point-of-interest', function () {
        $('#hardware-info p').html($(this).find('.point-detail-info').html());
        $('#hardware-info h4').html($(this).find('.point-hover-info p').html());
        $('.point-of-interest.selected').removeClass('selected');
        $(this).addClass('selected');

        if ($('#hardware-image').offset().top > $(window).scrollTop())
            HashJump('#HardwareTile', 66);

        SaleaeTrackEvent("hardware", "clicked details " + $(this).find('.point-hover-info p').html(), "clicked hardware details", { 'Section': $(this).find('.point-hover-info p').html() });
    });

    $('.logic-selector-button-red').on('click', function () {
        $(this).hide();
        $('.logic-spec-ed').hide();
        $('.logic-classic').show();
        $('.logic-selector-button-black').css("display", "inline-block");
        SaleaeTrackEvent("hardware", "changed color", "clicked hardware color");
    });
    $('.logic-selector-button-black').on('click', function () {
        $(this).hide();
        $('.logic-classic').hide();
        $('.logic-spec-ed').show();
        $('.logic-selector-button-red').css("display", "inline-block");
        SaleaeTrackEvent("hardware", "changed color", "clicked hardware color");
    });
}




;

HomepageTileInit = function() {

    /*****************************************/
    /*****************Index*******************/
    /*****************************************/
    if ($(window).height() > 900) {
        $('#HomepageTile').css("height", 800);
    }
    else {
        $('#HomepageTile').css("height", 493);
    }
    var show_laptop_video = true;
    if ($(window).height() <= 900)
        show_laptop_video = false;

    $(window).resize(function () {
        if ($(window).height() > 900) {
            $('#HomepageTile').css("height", 800);
            show_laptop_video = true;
        }
        else {
            $('#HomepageTile').css("height", 493);
            show_laptop_video = false;
        }
    });

    /**************************/
    /*******Laptop Video*******/
    /**************************/

    var video_index = 0;
    var video_locations = ["/Content/Images/Tiles/Homepage/LaptopVideo1.png", "/Content/Images/Tiles/Homepage/LaptopVideo2.png", "/Content/Images/Tiles/Homepage/LaptopVideo3.png",
        "/Content/Images/Tiles/Homepage/LaptopVideo4.png", "/Content/Images/Tiles/Homepage/LaptopVideo5.png", "/Content/Images/Tiles/Homepage/LaptopVideo6.png"
    , "/Content/Images/Tiles/Homepage/LaptopVideo7.png", "/Content/Images/Tiles/Homepage/LaptopVideo8.png", "/Content/Images/Tiles/Homepage/LaptopVideo9.png"
    , "/Content/Images/Tiles/Homepage/LaptopVideo10.png", "/Content/Images/Tiles/Homepage/LaptopVideo11.png", "/Content/Images/Tiles/Homepage/LaptopVideo12.png"
    , "/Content/Images/Tiles/Homepage/LaptopVideo13.png", "/Content/Images/Tiles/Homepage/LaptopVideo14.png"];
    var video_lengths = [28392, 25012, 14872, 14872, 20280, 25688, 13520, 20956, 17576, 22984, 14872, 20956, 27716, 25012];

    var interval_counter = 0;

    var frame_width = 676;
    var image_width = 28392;

    var num_frames = 28392 / 676;
    var num_frames_per_second = 15;


    var seconds_per_frame = parseInt(1000 / num_frames_per_second);

    //Video spacing interval
    var interval_counter = 0;
    var animation_time = num_frames * seconds_per_frame;
    var replay_timer = 4000;

    var offset = 0;

    video1 = setInterval(function () {

        if (show_laptop_video === false) {
            $('#laptop-video').css("display", "none");
            $('#laptop-video-preload').css("display", "none");
            return;
        }

        $('#laptop-video').css("display", "block");
        $('#laptop-video-preload').css("display", "block");

        if (offset == (-10 * frame_width)) {
            //preload next image
            if (video_index >= 13) {
                $('#laptop-video-preload').css("background-image", "url(//d2zk8zqyfisjls.cloudfront.net/" + video_locations[0] + ")");
            } else {
                $('#laptop-video-preload').css("background-image", "url(//d2zk8zqyfisjls.cloudfront.net/" + video_locations[video_index + 1] + ")");
            }
        }

        if (offset > ((-1 * image_width) + frame_width)) {
            offset -= frame_width;
            $('#laptop-video').css("background-position", offset + "px 0px");
        } else {
            $('#laptop-video').css("background-position-x", ((-1 * image_width) + frame_width) + "px");
        }

        interval_counter += seconds_per_frame;

        if (interval_counter >= replay_timer) {
            interval_counter = 0;
            video_index++;
            if (video_index >= 14) {
                video_index = 0;
            }

            $('#laptop-video').css("background-image", "url(//d2zk8zqyfisjls.cloudfront.net/" + video_locations[video_index] + ")");
            $('#laptop-video').css("background-position", "0px 0px");
            $('#laptop-video').css("background-size", video_lengths[video_index] + "px 100%");
            offset = 0;

            image_width = video_lengths[video_index];
        }
    }, seconds_per_frame);


    if ($(window).width() < 1550) {
        var h1_size = parseInt($('#homepage-title h1').css("font-size"), 10);
        $('#homepage-title h1').css("font-size", (h1_size * 0.80) + "px");

        var h2_size = parseInt($('#homepage-title h2').css("font-size"), 10);
        $('#homepage-title h2').css("font-size", (h2_size * 0.9) + "px");
    }
}


;

ReasonsTileInit = function () {

    SetupTabbedPanels.call($('#ReasonsTile'));  //Provided in Layout.js, always included

    return;
}


;

SoftwareTileInit = function () {
    $('#SoftwareTile').on('click', ' #software-features-list li a', function (event) {
        event.preventDefault();
        $ul = $(this).siblings('ul');
        if ($ul.size() > 0) {

            if ($ul.is(":hidden")) {
                $('#software-features-list ul').slideUp(200);
                $ul.delay(200).slideDown(200);
            } else {
                $ul.delay(100).slideUp(100);
            }
            $('#software-features-list a.selected').removeClass('selected');
            $(this).addClass('selected');
        }
        if ($(this).attr("href")) {
            $('#software-features-list a.selected').removeClass('selected');
            $(this).addClass('selected');

            //Set up new video
            $('#software-video-panel video').fadeOut(100);
            $('#software-video-panel source').attr("src", $(this).attr("href"));
            $('#software-video-panel video').get(0).load();
            $('#software-video-panel video').get(0).play();
            $('#software-video-panel video').fadeIn(100);

            SaleaeTrackEvent("Software Videos", "Play Software Video " + $(this).html(), "Played a Software Video", { 'Video': $(this).html() });

        }
        $p = $(this).siblings('p');
        if ($p.size() > 0) {
            $('#software-info p').html($p.html());
            $('#software-info h4').html($(this).html());
        }
    });

    /*Play on scroll over*/
    $(window).scroll(function () {
        var window_top = $(document).scrollTop();
        var elem_top = $('#software-video-panel').offset();
        var elem_height = $('#software-video-panel').height();
        var window_height = screen.height;
        if (window_top < elem_top.top && (window_top + window_height) > (elem_top.top + elem_height)) {
            $('#software-video-panel video').get(0).play();
        }

    });
}





;

TestimonialsTileInit = function () {
    $('.slide-bar-wrapper').each(function () {
        var $this = $(this);
        var $item_container = $this.find('.slidebar-item-container');
        //var item = $item_container.find('.slidebar-item').first();
        var item_width = 1060;//990;//item.show().innerWidth();
        var item_count = $item_container.children().length;

        $item_container.width(item_count * item_width);
        $item_container.css("left", 0);

        $item_container.children().each(function () {
            var index = $(this).index();
            $(this).css("left", index * item_width);
        });

        $.easing.easeOutExpo = function (x, t, b, c, d) {
            return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
        }

        var left = 0;
        event_in_progress = false;
        $this.on('click', '.left-button', function () {
            if (left < 0 && event_in_progress == false) {
                event_in_progress = true;
                $item_container.animate({ left: left + (item_width) }, 400, 'easeOutExpo', function () { left = parseInt($item_container.css("left"), 10); event_in_progress = false; });
                $this.find('.slidebar-item-button.selected').removeClass("selected").prev().addClass("selected");
            }

        });
        $this.on('click', '.right-button', function () {
            if ((left > (-1 * item_width * (item_count - 1))) && event_in_progress == false) {
                event_in_progress = true;
                $item_container.animate({ left: left - (item_width) }, 400, 'easeOutExpo', function () { left = parseInt($item_container.css("left"), 10); event_in_progress = false; })
                $this.find('.slidebar-item-button.selected').removeClass("selected").next().addClass("selected");
            }
        });

        //Set up Quick Search Buttons
        $this.find('.slidebar-item-buttons').width(($(this).find('.slidebar-item-buttons').children().length + 1) * $(this).find('.slidebar-item-buttons').children().first().outerWidth() + 2)

        $this.on('click', '.slidebar-item-button', function () {
            var index = $(this).index();
            $this.find('.slidebar-item-button.selected').removeClass("selected");
            $(this).addClass("selected");
            $item_container.animate({ left: (-1 * item_width * (index)) }, 400, 'easeOutExpo', function () { left = parseInt($item_container.css("left"), 10); });
        });
    });
}


;

//Tiles work like this:
//They are actions, in the Site controller
//They each have their own partial view
//They each have their own js file
//They share css

//All tile js is loaded by the master page -- scripts cannot be loaded from partial views anyway.
//All tiles have an init js script that must not be called until (1) documment load and (2) tile async load.
//Using @Html.AsyncLoadAction, jquery is used to 
//(1) create a placeholde div with the tile's name 
//(2) .get the html partial view 
//(3) async_html_array.push the jsXHR object returned from .get (so that later, we can .when(async_html_array).ready, so we can know when ALL the tiles are loaded)
//(3) when the .get calls back, replace the placeholder div with new partial view html
//(4) when the .get calls back, call the Init function for the particular tile that just loaded

//TileFinal is used to perform anything that required ALL the tiles to be loaded

//because the tiles load images, even when they are ready, it doesn't mean we can jump. so we'll use $(window).load...

//Todo: the proper way to do this is to create an Html helper to create images, which scans the size, cashes it, and sets the img height and width tags.

$(window).load(function () {

    var async_html_array = async_html_array || [];
    $.when.apply($, async_html_array).done(function () {  //.apply is used to turn an array into a list of items; .when only accepts a list.

        //standard useage of #ActionTile won't work because the page loads async; even checking after each tile
        //doesn't work because the other still-loading tiles will move the page after the jump
        //we must wait until ALL tiles are done loading.
    
        $.each(tile_fragment_array, function(index, value) {
            if (window.location.hash === value) {
                //var new_position = $(value).offset();
                //new_position.top += 1000;
                //window.scrollTo(new_position.left, new_position.top);

                $('html, body').animate({
                    scrollTop: $(value).offset().top-50
                }, 200);
            }
        });
    });
});
;
