﻿/*Feature Module*/


require.config({
    "baseUrl": "js",
    "paths": {
        "MsOnePlayer": "//www.microsoft.com/videoplayer/js/oneplayer"
    }
});

require(["jquery", "document", "domReady!", "MsOnePlayer"], function ($, document) {

function HeightFeature() {
    setTimeout(function () {
        $('.feature_custom_class.f-align-left, .feature_custom_class.f-align-right').each(function () {
            var this_ = $(this);
            var replace_media;
            if ($(this).find('.c-logo .c-image').length) {
                var imagediv = $(this).find('.c-logo .c-image');
            }
            else {
                $(this).find('.c-logo .c-image').css('height', '1px');
            }
            
            var content_section_div = this_.find('.content_section_feature');
            var content_height;
                     

            var image_interv = setInterval(framed, 100);
            function framed() {
                if ($(this).find('.c-logo .c-image').length==0 || imagediv.innerHeight() > 1) {
                    //clearInterval(image_interv);
                    if (this_.find('.c-logo .c-image').length) {
                        var final_imgheighth = imagediv.innerHeight();
                    }
                    content_height = content_section_div.innerHeight();
                    //console.log(final_imgheighth + 'me');

                    var dummy_imag = this_.find('.c-logo .c-image').innerHeight();
                    //console.log(content_height);
                    //console.log(dummy_imag);

                    var image_height = this_.find('picture').innerHeight();
                    var video_height = this_.find('.c-video-player').innerHeight();
                    var lightbox_height = this_.find('.c-lightbox-video picture').innerHeight();
                    //var lightbox_height = $(this).find('.c-lightbox-video').innerHeight();
                    var ambient_height = this_.find('.m-ambient-video').innerHeight();
                    if (this_.find('.c-video-player').length) {
                        replace_media = video_height;
                    }
                    else if (this_.find('.c-lightbox-video').length) {
                        replace_media = lightbox_height;
                    }
                    else if (this_.find('.m-ambient-video').length) {
                        replace_media = ambient_height;
                    }


                    if (this_.find('.c-video-player').length || this_.find('.c-lightbox-video').length || this_.find('.m-ambient-video').length) {

                        if (content_height >= replace_media) {
                            this_.find('.c-video-player').addClass('relative_height');
                            this_.find('.c-lightbox-video').addClass('relative_height');
                            this_.find('.m-ambient-video').addClass('relative_height');
                            this_.css('height', content_height);
                            //if ($('body').hasClass('d_edge') && (window.matchMedia("(max-width: 1399px)").matches)) {
                            //    $(this).css('min-height', '460px');
                            //}

                        }
                        else {
                            this_.find('.c-video-player').removeClass('relative_height');
                            this_.find('.c-lightbox-video').removeClass('relative_height');
                            this_.find('.m-ambient-video').removeClass('relative_height');
                            this_.css('height', 'auto');
                        }
                    }
                    else {
                        if (content_height >= image_height) {
                            this_.find('picture').addClass('relative_height');
                            this_.css('height', content_height);
                        }
                        else {
                            this_.find('picture').removeClass('relative_height');
                            this_.css('height', 'auto');
                            if (this_.find("a").hasClass("img_click")) {
                                this_.css('height', image_height);
                            }
                        }
                    }
                } else {
                    var final_imgheighth = imagediv.innerHeight();
                    console.log(final_imgheighth  + 'new');
                }
            }




        });
    }, 300);
}


function playiconalignFeature() {
    setTimeout(function () {

        $('.sample_container .feature_custom_class.f-align-right').each(function () {
            if ($(this).find('.c-lightbox-video').length) {
                var image_width = $(this).find('picture img').width();
                $(this).find('div[data-js-dialog-show]').css('width', image_width);
            }
        });

        $('.sample_container .feature_custom_class.f-align-left').each(function () {
            if ($(this).find('.c-lightbox-video').length) {
                var image_width = $(this).find('picture img').width();
                $(this).find('div[data-js-dialog-show]').css('width', image_width);
            }
        });

        $('.feature_custom_class.f-feature-small.f-align-left').each(function () {
            if ($(this).find('.c-lightbox-video').length) {
                var image_width = $(this).find('picture img').width();
                var padding_value = $(this).find('picture').css('padding-right');
                $(this).find('div[data-js-dialog-show]').css('width', image_width);
                $(this).find('div[data-js-dialog-show]').css('right', padding_value);
            }

        });

        $('.feature_custom_class.f-feature-small.f-align-right').each(function () {
            if ($(this).find('.c-lightbox-video').length) {
                var image_width = $(this).find('picture img').width();
                var left_value = $(this).find('picture img').position().left;
                var margin_value = $(this).find('picture img').css('margin-left');
                var added_v = left_value + parseInt(margin_value);
                $(this).find('div[data-js-dialog-show]').css('width', image_width);
                $(this).find('div[data-js-dialog-show]').css('left', added_v);
            }

        });

    }, 400);
}

$(document).ready(function () {
    HeightFeature();
    playiconalignFeature();

    //$('.custom_whitebutton').each(function () {
    //    $(this).attr('id', 'custom_white');
    //});

    $('.onegdcweb-feature').each(function () {
        var hasgrid = $(this).parent().parent().attr('data-grid');
        if (hasgrid != "container") {
            $(this).addClass('sample_container');
        }

        var value = $(this).find(".content_section_feature .c-heading").text();
        $(this).find(".c-lightbox-video button.c-action-trigger span").append(" " + value);
        var value2 = $(this).find(".c-lightbox-video button.c-action-trigger span").text();
        $(this).find(".c-lightbox-video button.c-action-trigger span").text(value2);

        $(this).find('.flinks').each(function () {
            $(this).find('a').each(function () {
            if ($(this).hasClass('glyph-play')) {
                $(this).attr('href', 'javascript:void(0);');
            }
            });
        });
    });

    //$('.onegdcweb-feature a[data-js-dialog-show]').on('click', function () {

    //    var this_ = $(this);

    //    setTimeout(function () {

    //        var datajs_text = this_.attr('data-js-dialog-show');

    //        $('#' + datajs_text).find('.c-action-trigger.glyph-play').trigger('click');

    //    }, 100);

    //});

    //$(".onegdcweb-feature a[data-js-dialog-show]").on("keydown", function (e) {

    //    if (e.keyCode == 13) {

    //        var this_ = $(this);

    //        setTimeout(function () {

    //            var datajs_text = this_.attr('data-js-dialog-show');

    //            $('#' + datajs_text).find('.c-action-trigger.glyph-play').trigger('click');

    //        }, 100);

    //    }

    //});

    $('img').each(function () {
        if (typeof ($(this).attr('alt')) == "undefined" || $(this).attr('alt') == "") {
            $(this).attr("role", "presentation");
            $(this).attr("alt", "");
        }

    });

    $('.content_section_feature ul.c-list').each(function () {
        $(this).parent('.c-paragraph').addClass('cta_nopadding');

    });

    /*focus on close button safari iphone*/
    if (navigator.userAgent.toLowerCase().indexOf("iphone") > -1) {
        $(document).on("click", "a[data-js-dialog-show]", function (e) {
            var this_id = $(this).attr('data-js-dialog-show');
            setTimeout(function (e) {
                $('#' + this_id).find('button.glyph-cancel').addClass('pspldps');
                $('#' + this_id).find('button.glyph-cancel').focus();
                e.preventDefault();
                e.stopImmediatePropagation();
            }, 700);
        });
    }
    /*focus on close button safari iphone*/

    FeatureGroupTabletmode();
});

$(document).ready(function () {
    HeightFeature();
    playiconalignFeature();
    FeatureGroupTabletmode();
});

$(window).on('load', function () {
    HeightFeature();
    playiconalignFeature();


    $('.onegdcweb-feature').each(function () {
        if (($(this).parent().parent('div[data-grid=container]').length && $(this).find('.c-video-player').length) || ($(this).parent().parent('div[data-grid=container]').length && $(this).find('.m-ambient-video').length)) {
            $(this).addClass('high_resolutionvideline');        
        }
    });
    
    FeatureGroupTabletmode();


});

$(window).on('resize', function () {
    HeightFeature();
    playiconalignFeature();
    FeatureGroupTabletmode();
});

function FeatureGroupTabletmode() {   
    if ($("div").hasClass("m-feature-group")) {
        if ($(window).width() >= 768 && $(window).width() < 1084) {
            $('.onegdcweb-feature').each(function () {
                $(this).find(".content_section_feature").removeClass("content_section_feature").addClass("content_section_featuregroup");
            });
        }
        else {           
            $('.onegdcweb-feature').each(function () {
                $(this).find(".content_section_featuregroup").removeClass("content_section_featuregroup").addClass("content_section_feature");
            });
        }
    }
}

});


