﻿(function($)
{
    jQuery.fn.putCursorAtEnd = function()
    {
        return this.each(function()
        {
            $(this).focus()
            if (this.setSelectionRange)
            {
                var len = $(this).val().length * 2;
                this.setSelectionRange(len, len);
            }
            else
            {
                $(this).val($(this).val());
            }
            this.scrollTop = 999999;
        });
    };
})(jQuery);



$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++){
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    },
    getBaseURL: function(){
        var url = location.href;  // entire url including querystring - also: window.location.href;
        var baseURL = url.substring(0, url.indexOf('/', 14));


        if (baseURL.indexOf('http://localhost') != -1) {
            // Base Url for localhost
            var url = location.href;  // window.location.href;
            var pathname = location.pathname;  // window.location.pathname;
            var index1 = url.indexOf(pathname);
            var index2 = url.indexOf("/", index1 + 1);
            var baseLocalUrl = url.substr(0, index2);

            return baseLocalUrl;
        }
        else {
            // Root Url for domain name
            return baseURL;
        }

    }
});


function AddWebPartClass(target, ClassName)
{
    
    $(target + ' > div table, ' + target + ' > table').addClass(ClassName);
}

function RotateAlerts(){
    if($('#Alerts').length>0){
        $('#Alerts').cycle({slideExpr:'.Alert',fx: 'fade', pause:1, timeout: 4000, speed: 800});
    }
    
    if($('.Alert').length == 1)
    {
        $('.Alert').css('display','block');
    }
}

function WebPartTabsInit(target, removeHeader)
{
    //$('.LeftLink1 table table').addClass('WebPartTable');
    $(target + ' td[id^=MSOZoneCell_WebPartWPQ] > table').addClass('WebPartTable');

    
    if($(target + " :header:contains('tab - ')").length > 0)
    {
        var TabContainer = '<div class="TabContainer"><ul class="TabStrip">';
        $(target + " :header:contains('tab - ')").each(function(i){
            var ele = $(this);
            var newTitle = ele.text().replace('tab - ', '');
            ele.text(newTitle);
            
            TabContainer += ('<li><a index="' + i + '" href="#">' + newTitle + '</a><li>');
            ele.parents('.WebPartTable').addClass('TabItem').css('display','none');
        });
        
        TabContainer += "</ul><div class='TabItems'></div></div>";
        
 
        $(target).prepend(TabContainer);
        
        $(target + ' table.TabItem').each(function(i){
            var item = $(this).detach();
            item.appendTo(target + ' .TabItems');
        });
        
        $(target + ' .TabItem:first').css('display', 'block');
        
        if(removeHeader)
        {
            $('.WebPartTable.TabItem .ms-WPHeader').html('');
        }

        $(target + ' .TabStrip li a').click(function() {
            $(target + ' .TabItem').css('display', 'none');
            var itemIndex = parseInt($(this).attr('index')) + 1;
            $(target + ' .TabItem:nth-child(' + itemIndex + ')').css('display', 'block');

            $(target + ' .TabStrip li').removeClass('Active');
            $(this).parent().addClass('Active');
            return false;
        });
        
        $(target + ' .TabStrip li:first').addClass('Active');
    }
}

function wrapItems(target, IdOrClass, wrapperID)
{
    $(target).wrapAll('<div '+ IdOrClass + '="' + wrapperID +'" />');    
}

function RecentDocAnimationsInit(){    

 if($('.LibraryDocuments').length >0)
    {

        $('div.Document .Expander').toggle(function(){
            var target = $(this);
            target.blur();
			target.addClass('Expanded').removeClass('Collapsed');
            target.siblings('div.DocDetails').slideDown(150);
        }, function(){
            var target = $(this);
            target.blur();
			target.addClass('Collapsed').removeClass('Expanded');
            target.siblings('div.DocDetails').slideUp(150);
        });
    }

}

function FeaturedNewsInit()
{
    var newsItemCount = $('.ListRotatorItem').length;
    var newsDeptItemCount = $('.ListRotatorItemDept').length;
    if(newsItemCount > 0)
    {
        $('.ListRotatorItem').wrapAll('<div id="ListRotatorContainer" />');        
        
        if(newsItemCount > 1)
            $('#ListRotatorContainer').append('<a id="SlideShowPlayPause" class="Pause" href="#"><span>Pause</span></a>');
        
        $('#SlideShowPlayPause').css('right', (newsItemCount * 16) + 30);
        
        $('#ListRotatorContainer').append("<ul class='ImageIndicator' />");

        // redefine Cycle's updateActivePagerLink function 
        $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
            $(pager).find('li').removeClass('activeLI') 
                .filter('li:eq('+currSlideIndex+')').addClass('activeLI'); 
        }; 

    

        $('#ListRotatorContainer').cycle({slideExpr:'.ListRotatorItem',pagerEvent: 'click', fx: 'scrollLeft',  timeout: 7000, speed: 1000, pager:'.ImageIndicator', activePagerClass:'Active', pagerAnchorBuilder: function(idx, slide) { 
            return '<li><a href="#">' + (idx + 1) +'</a></li>'; 
        }});


          $('#SlideShowPlayPause').click(function() {
            if($('#SlideShowPlayPause').hasClass('Pause')){
                $('#SlideShowPlayPause').addClass('Play').removeClass('Pause').text('Pause');
                $('#ListRotatorContainer').cycle('pause');
            }
            else
            {
                $('#SlideShowPlayPause').addClass('Pause').removeClass('Play').text('Play');
                $('#ListRotatorContainer').cycle('resume', true);
            }
        });


    }

    if (newsDeptItemCount > 0) {
        $('.ListRotatorItemDept').wrapAll('<div id="ListRotatorContainer" />');

        if (newsDeptItemCount > 1)
            $('#ListRotatorContainer').append('<a id="SlideShowPlayPause" class="Pause" href="#"><span>Pause</span></a>');

        $('#SlideShowPlayPause').css('right', (newsDeptItemCount * 16) + 30);

        $('#ListRotatorContainer').append("<ul class='ImageIndicator' />");

        // redefine Cycle's updateActivePagerLink function 
        $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
            $(pager).find('li').removeClass('activeLI')
                .filter('li:eq(' + currSlideIndex + ')').addClass('activeLI');
        };




        $('#ListRotatorContainer').cycle({ slideExpr: '.ListRotatorItemDept', pagerEvent: 'click', fx: 'scrollLeft', timeout: 7000, speed: 1000, pager: '.ImageIndicator', activePagerClass: 'Active', pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + (idx + 1) + '</a></li>';
        }
        });


        $('#SlideShowPlayPause').click(function() {
            if ($('#SlideShowPlayPause').hasClass('Pause')) {
                $('#SlideShowPlayPause').addClass('Play').removeClass('Pause').text('Pause');
                $('#ListRotatorContainer').cycle('pause');
            }
            else {
                $('#SlideShowPlayPause').addClass('Pause').removeClass('Play').text('Play');
                $('#ListRotatorContainer').cycle('resume', true);
            }
        });


    }
   
    if(newsItemCount == 1)
    {
        $('.ListRotatorItem').removeClass('Closed').css('display','block');
    }
    if (newsDeptItemCount == 1) {
        $('.ListRotatorItemDept').removeClass('Closed').css('display', 'block');
    }
}

///Summary
/// Attach animations to global navigation items.
function GlobalNavInit(){

    $('a.CloseButtonOne, a.CloseButtonTwo').css('display', 'none');

    //Wire Up the close button to hide all the items and perform slide animation.
    $('a.CloseButtonOne, a.CloseButtonTwo').click(function(){
    
        $('#GlobalToolsContainer').slideUp('fast', function(){
            $('#UtilityOne').css('display', 'none');
            $('#UtilityTwo').css('display', 'none');
            $('a.CloseButtonOne, a.CloseButtonTwo').css('display', 'none');
            $('ul.PrimaryTools a').removeClass('Selected');
        });
        
        //Ensure that the anchor tag does not cause a page refresh
        return false;
    });

    //Attach slide animation to links
    AttachGlobalNavAnimation('ul.PrimaryTools li.Departments a', '#UtilityOne', '#UtilityTwo', 'a.CloseButtonOne', 'a.CloseButtonTwo');
    AttachGlobalNavAnimation('ul.PrimaryTools li.eTools a', '#UtilityTwo', '#UtilityOne', 'a.CloseButtonTwo', 'a.CloseButtonOne');

}

/// Summary
/// Attach a slide animation to a link item
/// targetElement - The CSS Selector for the link item that trigers the animation.
/// parentContainer - The container of items to show.
/// sibliingContainer - The Sibling container that should be closed when the animation starts.
function AttachGlobalNavAnimation(targetElement, parentContainer, sibliingContainer, parentContainerClose, siblingContainerClose)
{
    //Attach the onclick event
    $(targetElement).click(function(){
        var clickTarget = $(this);
        this.blur();
        
        $(parentContainerClose).css('visibility', 'visible');
        
        //Check to see if the sliding control is already dropped down.
        if($('#GlobalToolsContainer').css('display') == 'block'){
        
            
            //If the container is already shown slide the container up and hide the current items
            //Then slide the container back down into view with the requested elements shown.
            if($(sibliingContainer).css('display') == 'block'){
                
                $(parentContainerClose).css('display', 'none');
                $(siblingContainerClose).css('display', 'none');
                
                $('#GlobalToolsContainer').slideUp('fast', function(){
                    $(sibliingContainer).css('display', 'none');
                    $(parentContainer).css('display', 'block');
                    
                    $('ul.PrimaryTools a').removeClass('Selected');
                    AttachGlobalNavArrows(clickTarget)
                    $('#GlobalToolsContainer').slideDown('fast', function() {
                        $(parentContainerClose).fadeIn('slow');
                        $(siblingContainerClose).css('display', 'none');
                        if(parentContainer == '#UtilityOne')
                            FixGlovalNavHeights();
                    });
                });
            } else {
            
                $('ul.PrimaryTools a').removeClass('Selected');
                //Slide the container up so it is no longer visible
                $(parentContainer).css('display', 'block'); //Ensure that the parent element will have a height.
                $(siblingContainerClose).css('display', 'none');
                $('#GlobalToolsContainer').slideUp('fast', function(){
                    $(parentContainer).css('display', 'none');
                    $(parentContainerClose).css('display', 'none');
                    $(siblingContainerClose).css('display', 'none');
                    $(siblingContainerClose).css('visibility', 'hidden');
                });
            }
        
        }else{
        
            //Slide the control down
            $(parentContainer).css('display', 'block'); //Ensure that the parent element will have a height.
            $(siblingContainerClose).css('display', 'none');
            $(parentContainerClose).css('display', 'none');
            $(sibliingContainer).css('display', 'none');
            $(siblingContainerClose).css('visibility', 'hidden');
            
            $('ul.PrimaryTools a').removeClass('Selected');
            AttachGlobalNavArrows(clickTarget)
            $('#GlobalToolsContainer').slideDown('fast', function(){
                $(parentContainerClose).fadeIn('slow');
                $(siblingContainerClose).css('display', 'none');
                
                if(parentContainer == '#UtilityOne')
                    FixGlovalNavHeights();
            });
        
        }
        
        //Ensure that the anchor tag does not cause a page refresh
        return false;
    }); 
}

function AttachGlobalNavArrows(target)
{
    target.addClass('Selected');
}

function FixGlovalNavHeights()
{
   var maxheight = 0;
   $.each($('#UtilityOneWrapper .UtilityList LI UL.ListGroup'), function()
   {
        var itemHeight = $(this).height();
        
        if(maxheight < itemHeight){
            maxheight = itemHeight;
        }
   });
   
   $.each($('#UtilityOneWrapper .UtilityList LI UL.ListGroup'), function()
   {
        $(this).css('height', maxheight);
   });
    
}

