﻿
function OnClientItemClickingHandler(sender, e) {

    var item = e.get_item();
    var navigateUrl = item.get_navigateUrl();
    //follow link:
    window.location = navigateUrl;
    //...but don't expand branch
    //when  we arrive at our destination the page will be expanded by default
    //cancel any other default actions 
    e.set_cancel(true);
}

function homePageCarousel() {

    $('.home-page-carousel-list .list-item').hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    );
    
    $('#home-page-carousel-detail').html($('.home-page-carousel-list .list-item:first div.hidden').html());
    if ($('.home-page-carousel-list .list-item:first div.hidden').hasClass('rm')) {
        $('.home-page-carousel-media').css('background-image', 'none').html($('.home-page-carousel-list .list-item:first div.hidden.rm-content').html());
    } 

    $('.home-page-carousel-list .list-item').click(function() {
        $('.home-page-carousel-list .list-item').removeClass('active');
        $(this).addClass('active');
        var newhtml = $(this).find('div.hidden').html();
        var strrichmedia = ($(this).find('div.hidden').hasClass('rm')) ? $(this).find('div.hidden.rm-content').html() : '';
        var newBackground = $(this).find('img.hidden').attr('src');
        
        var newLink = $(this).find('img.hidden').attr('title');

        $('.home-page-carousel-media').fadeOut(500, function() {
            if (strrichmedia != '') {
                $(this).css('background-image', 'none').html(strrichmedia);
            } else {
                $(this).empty().css('background-image', 'url(' + newBackground + ')');
            }
            $('#home-page-carousel-detail').html(newhtml);
            $(this).fadeIn(500);
        });
        
        if (strrichmedia == '') {
            if(newLink.indexOf('_blank') > 0){
                $('#home-page-carousel-link').attr('href',newLink.replace('_blank',''));
                $('#home-page-carousel-link').attr('target','_blank');
            }else{
                $('#home-page-carousel-link').attr('href',newLink);
                $('#home-page-carousel-link').attr('target','_self');
            }
        }else{
            $('#home-page-carousel-link').removeAttr('href');
        }

    });

    $('.home-page-carousel-list .list-item:first').addClass('active');

}

function landingPageCarousel() {

    var inithtml = $('.landing-page-carousel-list .list-item:first div.hidden').html();
    var blnrichmedia = ($('.landing-page-carousel-list .list-item:first div.hidden h1').length == 0);
    if (blnrichmedia) {
        $('#landing-page-carousel-detail').prepend('<div class="rich-media">' + inithtml + '</div>');
    } else {
        $('#landing-page-carousel-detail div.overlay').html(inithtml);
    }

    $('.landing-page-carousel-list .list-item').hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    );

    $('.landing-page-carousel-list .list-item').click(function() {
        $('.landing-page-carousel-list .list-item').removeClass('active');
        $(this).addClass('active');
        var newhtml = $(this).find('div.hidden').html();
        var blnrichmedia = ($(this).find('div.hidden').hasClass('rm'));
        var newBackground = $(this).find('img.hidden').attr('src');

        if (($.browser.msie) && ($.browser.version == '8.0')) {
            //ie 8 so do additional fade
            $('#landing-page-carousel-detail div.overlay').fadeOut(500);
        }
        $('#landing-page-carousel-detail').fadeOut(500, function() {
            if (blnrichmedia) {
                $('#landing-page-carousel-detail').prepend('<div class="rich-media">'+newhtml+'</div>');
                $('#landing-page-carousel-detail').css('background-image', 'none');
                $('#landing-page-carousel-detail').fadeIn(500);
            } else {
                $('#landing-page-carousel-detail .rich-media').remove();
                $('#landing-page-carousel-detail div.overlay').html(newhtml);
                $('#landing-page-carousel-detail').css('background-image', 'url(' + newBackground + ')');
                if (($.browser.msie) && ($.browser.version == '8.0')) {
                    //ie 8 so do additional fade
                    $('#landing-page-carousel-detail div.overlay').fadeIn(500);
                }
                $('#landing-page-carousel-detail').fadeIn(500);
            }
        });

    });

    $('.landing-page-carousel-list .list-item:first').addClass('active');

}

function ProdServCarousel() {
    // variables
    var animRunning = false;
    var rightQueue = 0;
    var leftQueue = 0;

    function resetLayout() {

        var indicatorHTML = '';
        $('.prod-serv-items-group.active .prod-serv-items').each(function(x) {
            $(this).attr('id', 'prod-serv-items' + x);
            indicatorHTML += '<a id="prod-serv-indicator' + x + '" class="prod-serv-indicator"></a>\n';
        });

        $('#prod-serv-indicators').html(indicatorHTML);

        $('.prod-serv-indicator:first').addClass('prod-serv-indicator-active');
        
        $('.prod-serv-items-group.active .prod-serv-items:gt(0)')
	        .css('position', 'absolute')
	        .css('left', '730px');
	        
        $('.prod-serv-items-group.active .prod-serv-items:first')
        	.css('position', 'absolute')
	        .css('left', '0px');
	        
    };

    // add button click functions
    function slideLeft() {
        animSpeed = 650 / leftQueue;
        if (!animRunning) {
            animRunning = true;
            $('.prod-serv-items-group.active .prod-serv-items:last').css('left', '-730px');
            $('.prod-serv-items-group.active').prepend($('.prod-serv-items-group.active .prod-serv-items:last'));
            var currIndicatorID = '#' + $('.prod-serv-items-group.active .prod-serv-items:eq(1)').attr('id').replace('prod-serv-items', 'prod-serv-indicator');
            var nextIndicatorID = '#' + $('.prod-serv-items-group.active .prod-serv-items:first').attr('id').replace('prod-serv-items', 'prod-serv-indicator');
            $('.prod-serv-items-group.active .prod-serv-items:eq(1)').animate({
                left: 730
            }, animSpeed, 'easeInOutQuad');
            $('.prod-serv-items-group.active .prod-serv-items:first').animate({
                left: 0
            }, animSpeed, 'easeInOutQuad', function() {
                $(currIndicatorID).removeClass('prod-serv-indicator-active');
                $(nextIndicatorID).addClass('prod-serv-indicator-active');
                animRunning = false;
                leftQueue--;
                if (rightQueue > 0) {
                    leftQueue = 0;
                    slideRight();
                }
                if (leftQueue > 0) {
                    slideLeft();
                }
            });
        }
    };

    function slideRight() {
        var animSpeed = 650 / rightQueue;
        if (!animRunning) {
            animRunning = true;
            var currIndicatorID = '#' + $('.prod-serv-items-group.active .prod-serv-items:first').attr('id').replace('prod-serv-items', 'prod-serv-indicator');
            var nextIndicatorID = '#' + $('.prod-serv-items-group.active .prod-serv-items:eq(1)').attr('id').replace('prod-serv-items', 'prod-serv-indicator');
            $('.prod-serv-items-group.active .prod-serv-items:first').animate({
                left: -730
            }, animSpeed, 'easeInOutQuad');
            $('.prod-serv-items-group.active .prod-serv-items:eq(1)').animate({
                left: 0
            }, animSpeed, 'easeInOutQuad', function() {
                $(currIndicatorID).removeClass('prod-serv-indicator-active');
                $(nextIndicatorID).addClass('prod-serv-indicator-active');
                $('.prod-serv-items-group.active .prod-serv-items:first').css('left', '730px');
                $('.prod-serv-items-group.active').append($('.prod-serv-items-group.active .prod-serv-items:first'));
                animRunning = false;
                rightQueue--;
                if (leftQueue > 0) {
                    rightQueue = 0;
                    slideLeft();
                }
                if (rightQueue > 0) {
                    slideRight();
                }
            });
        }
    };

    $('.prod-serv-carousel-holder')
	.append('<a href="javascript:void(0);" id="prod-serv-left-arrow"><span>scroll left</span></a><a href="javascript:void(0);" id="prod-serv-right-arrow"><span>scroll right</span></a><div id="prod-serv-indicators"></div>');

    $('.prod-serv-items-group .prod-serv-items').find('.prod-serv-item:last').css('margin-right', 0);

    resetLayout();

    // bind buttons
    $('.prod-serv-category-tabs ul li a').each(function(i) {
        $(this).click(function() {
            $('.prod-serv-items-group').removeClass('active').addClass('hidden');
            $('.prod-serv-items-group:eq(' + i + ')').removeClass('hidden').addClass('active');
            $('.prod-serv-items-group.hidden .prod-serv-items').removeAttr('id');
            resetLayout();
        });
    });

    $('.prod-serv-item')
    .hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    )
    .click(function() {
        window.location = $(this).find('h5 a').attr('href');
    });
    
    $('#prod-serv-left-arrow').click(function() {
        leftQueue++;
        slideLeft();
    });

    $('#prod-serv-right-arrow').click(function() {
        rightQueue++;
        slideRight();
    });

    $('.prod-serv-items').css({
        'background-color': '#e2e2e2',
        'display': 'block'
    });

}

$(document).ready(function() {

    // may be required for pagetools if web part cannot be changed
    $('.pagetool li:eq(1) a').css('background-position', '0 -402px');
    $('.pagetool li:eq(2) a').css('background-position', '0 -424px');

    // solves tab navigation backgrounds
    $('.tab-navigation ul li').hover(function() {
        if (!$(this).hasClass('tab-selected')) {
            $(this).css('background-position', '0 -711px');
        }
    }, function() {
        if (!$(this).hasClass('tab-selected')) {
            $(this).css('background-position', '0 -234px');
        }
    });

    // sitemap functionality enhancement
    $('ul.sitemap-list li').hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    );

    // creates any accordions that may exist on the page
        //$(".accordion").accordion({ header: 'h3' });

        $('.accordion').addClass('ui-accordion');
        $('.accordion h3').next()
        .addClass('ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active')
        .slideToggle(0);

        $('.accordion h3')
        .prepend('<span class="ui-icon ui-icon-triangle-1-s"></span>')
        .addClass('ui-accordion-header ui-helper-reset ui-state-default ui-corner-all')
        .click(function() {
            $('.accordion h3')
            .removeClass('ui-state-active ui-corner-top')
            .addClass('ui-state-default ui-corner-all');

            $(this)
            .removeClass('ui-state-default ui-corner-all')
            .addClass('ui-state-active ui-corner-top')
            .next('div.accordion-content').slideToggle('fast').siblings('div.accordion-content').slideUp('fast');
        });

        $('.accordion h3:first')
        .removeClass('ui-state-default ui-corner-all')
        .addClass('ui-state-active ui-corner-top')
        .next().slideToggle('fast');

});
