$(document).ready(function () {

    // If menu has open class, ie landed on a subcategory page
    // make sure menu is open
    $('.s_menucategoryopen').next('ul').show();

    // Apply Selectbox replace to downloads dropdown
    $('.downloadsDropDown').selectbox();

    // Set height of replaced select boxes
    $('#content .jquery-selectbox').each(function () {
        if ($(this).find('.jquery-selectbox-list').height() <= '166') {
            return;
        } else {
            $(this).find('.jquery-selectbox-list').css({ 'height': '167px', 'overflow': 'auto' });
        }
    });

    // Expand/Collapse FAQ Answers
    // Hide all answers initially
    $('.faqAnswer').hide();
    $('.faqs li').each(function () {
        var self = $(this),
        selfAnswer = $(this).children('.faqAnswer');

        self.children('a').bind('click', function (e) {
            e.preventDefault();
            if (selfAnswer.hasClass('open')) {
                selfAnswer.slideUp().removeClass('open');
            } else {
                if (self.closest('div').find('.open')) {
                    if (self.closest('div').attr('class').indexOf('questionwrapper') != -1) {
                        self.closest('.questionwrapper').find('.open').slideUp().removeClass('open');
                    } else {
                        self.closest('.questioncolumn').find('.open').slideUp().removeClass('open');
                    }
                    selfAnswer.slideDown().addClass('open');
                } else {
                    selfAnswer.slideDown().addClass('open');
                }
            }
        });
    });

    // Support Search Functions
    if (document.documentMode && document.documentMode == 7 && $('#sidebar').hasClass('supportSearch')) {
        $('#page-container').addClass('ie7');
    }

    $('.AppliedProduct .jquery-selectbox').hide();

    familySlider();
    supportNavigation();
    //videoModal();

    /*$('.supportSearch .searchResultsBar').each(function () {
    $(this).find('label:eq(0)').attr('class','').addClass('sortBy');
    });*/

});

function supportNavigation() {
    /// Expand/Collapse product navigation
    $('.productcatalog > li > a').bind('click', function (e) {
        var self = $(this);
        self.toggleClass('s_menucategoryopen');
        self.next('ul').slideToggle('fast');
        e.preventDefault();
    });
}

//function videoModal() {
//    $('.video-link').each(function () {
//        $(this).bind('click', function (e) {
//            // Do something to pass link to modal box
//            var iframeSrc = $(this).attr('href');
//            // Create iframe with src and show model once loaded
//            //$('#videoFrame').attr('src', iframeSrc);
//            $('#iframeWrap').html('<iframe id="videoFrame" src="' + iframeSrc + '" width="400" height="300" frameborder="0" scrolling="no"></iframe>');
//            $('#videoFrame').load(function () {
//                loadModal();
//            });
//            e.preventDefault();
//        });
//    });
//}

function loadModal() {


    // Set up Mask
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    var maskMarginLeft = -(maskWidth / 2);
    $('#videoOverlay').css({ 'z-index': 999, 'height': maskHeight, 'width': maskWidth, 'left': '50%', 'margin-left': maskMarginLeft });

    // Set up modal
    var windowH = $(window).height();
    var modalH = $('#videoModal').outerHeight();
    var posTop = (windowH - modalH) / 2;
    $('#videoModal').css({ 'top': posTop + 'px', 'z-index': 1001 });

    //Set up modal border
    var borderH = $('#videoModalBorder').outerHeight();
    var borderTop = (windowH - borderH) / 2;
    $('#videoModalBorder').css({ 'top': borderTop + 'px', 'z-index': 1000 });

    // Centre advanced search in window
    $(window).resize(function () {
        maskHeight = $(document).height();
        maskWidth = $(window).width();
        maskMarginLeft = -(maskWidth / 2);

        var windowH = $(window).height();
        var modalH = $('#videoModal').outerHeight();
        var posTop = (windowH - modalH) / 2;
        var borderH = $('#videoModalBorder').outerHeight();
        var borderTop = (windowH - borderH) / 2;
        $('#videoModal').css({ 'top': posTop });
        $('#videoModalBorder').css({ 'top': borderTop });
        $('#videoOverlay').css({ 'height': maskHeight, 'width': maskWidth, 'left': '50%', 'margin-left': maskMarginLeft });
    });

    if ($.browser.msie && parseFloat($.browser.version) < 7) {
        applyMask('video');
        $('#videoModal').fadeIn(500);
    } else {
        applyMask('video');
        $('#videoModal').fadeIn(500);
    }

    $('#videoModal #closeVideo').click(function () {
        $('#iframeWrap').empty();
        removeMask('video');
        $('#videoModal').fadeOut(500);
        return false;
    });

    /*$(document).keyup(function (e) {
        if (e.which == 27) {
            $('#iframeWrap').empty();
            removeMask('video');
            $('#videoModal').fadeOut(500);
        }
    });*/

    $('#videoOverlay').click(function () {
        $('#iframeWrap').empty();
        removeMask('video');
        $('#videoModal').fadeOut(500);
    });
}

function applyMask(elem) {
    $('#' + elem + 'Overlay').css({ 'display': 'block', opacity: 0 });
    $('#' + elem + 'Overlay').fadeTo(1000, 0.7);
    $('#' + elem + 'ModalBorder').css({ 'display': 'block', opacity: 0 });
    $('#' + elem + 'ModalBorder').fadeTo(1000, 0.5);
}

function removeMask(elem) {
    $('#' + elem + 'Overlay').fadeOut(1000);
    $('#' + elem + 'ModalBorder').fadeOut(1000);
}

function familySlider() {
    var maxLeft = 0,
    hidePrev = false,
	hideNext = false,
	liWidth = $('#productFamilySlider ul li:eq(0)').outerWidth(true),
	viewWidth = parseInt($('#productFamilySliderView').css('width')),
	ulWidth = 0;
    $('#productFamilySlider ul li').each(function (i) {
        var thisWidth = $(this).outerWidth(true);
        ulWidth += thisWidth;
    });
    $('#productFamilySlider ul').css({ 'width': ulWidth });

    // Create next/prev buttons
    if (parseInt($('#productFamilySlider ul').css('width')) > viewWidth) {
        $('<a href="#" id="pfSlidePrev" class="disabled"><span>Previous</span>&laquo;</a>').insertBefore('#productFamilySliderView');
        $('<a href="#" id="pfSlideNext"><span>Next</span>&raquo;</a>').insertAfter('#productFamilySliderView');
    }

    if (parseInt($('#productFamilySliderView ul').css('left')) != 0) {
        $('#pfSlidePrev').removeClass('disabled');
    }

    maxLeft = ulWidth - viewWidth;
    // Move it
    $('#pfSlideNext').bind('click', function (e) {
        var self = $(this),
        state = self.data('animation_state');
        if (state == 'busy') {
            return false;
        }
        self.data('animation_state', 'busy');
        $('#productFamilySlider ul').animate({ 'left': '-=' + liWidth + 'px' }, 500, function () {
            if (parseInt($(this).css('left')) == -maxLeft) {
                $('#pfSlideNext').addClass('disabled');
            }
            if (parseInt($(this).css('left')) != 0) {
                $('#pfSlidePrev').removeClass('disabled');
            }
            self.data('animation_state', 'complete');
        });
        e.preventDefault();
    });
    $('#pfSlidePrev').bind('click', function (e) {
        var self = $(this),
        state = self.data('animation_state');
        if (state == 'busy') {
            return false;
        }
        self.data('animation_state', 'busy');
        $('#productFamilySlider ul').animate({ 'left': '+=' + liWidth + 'px' }, 500, function () {
            if (parseInt($(this).css('left')) == 0) {
                $('#pfSlidePrev').addClass('disabled');
            }
            if (parseInt($(this).css('left')) != -maxLeft) {
                $('#pfSlideNext').removeClass('disabled');
            }
            self.data('animation_state', 'complete');
        });
        e.preventDefault();
    });

    // Find position and make sure it's visible on child page
    $('#productFamilySlider ul li').each(function (i) {
        if ($(this).hasClass('active')) {
            var thisIndex = i,
            position = $(this).position(),
            thisPosition = position.left,
            parentSize = $(this).closest('ul').children('li').size();
            positionInView(thisPosition, thisIndex, parentSize);
        }
    });

    function positionInView(offset, index, size) {
        var index = index + 1,
        size = size,
        viewSize = 6,
        doScroll = index - viewSize,
        scrollWidth = liWidth * doScroll;

        if (index > viewSize) {
            $('#pfSlideNext, #pfSlidePrev').removeClass('disabled');
            $('#productFamilySlider ul').animate({ 'left': '-' + scrollWidth + 'px' }, 500, function () {
                if (parseInt($(this).css('left')) == 0) {
                    $('#pfSlidePrev').addClass('disabled');
                }
                if (parseInt($(this).css('left')) == -maxLeft) {
                    $('#pfSlideNext').addClass('disabled');
                }
            });
        }
    }

}
