﻿var oldStartHeight = 0;
var newStartHeight = 0;

$(document).ready(function() {
    //MAIN NAVIGATION
    $('#Navigation img').mouseenter(function() {
        var targetID = $(this).attr("id");
        $('#' + targetID).attr('src', 'images/' + targetID + 'Hover.gif');
    });

    $('#Navigation img').mouseleave(function() {
        var targetID = $(this).attr('id');
        var LinkTarget = $('#' + targetID).attr('id').replace('Nav', '');

        if (!$('#' + LinkTarget).hasClass('Active')) {
            $('#' + targetID).attr('src', 'images/' + targetID + '.gif');
        }
    });

    //MAIN NAVIGATION ACTIVE STATE
    $('#Navigation a').filter(function() {
        //Take the current URL and split it into chunks at each slash
        var currentURL = window.location.toString().split("/");

        if ($(this).attr('href') == currentURL[currentURL.length - 1]) {
            return true;
        }

        var navURL = $('meta[name=NavigationURL]').attr("content");

        return $(this).attr("href") == navURL;

    }).addClass('Active');

    $('#Navigation a.Active').filter(function() {
        var targetID = $(this).attr('id');
        $('#' + targetID + 'Nav').attr('src', 'images/' + targetID + 'NavHover.gif');
    });

    //OPP PAGE EXPAND/COLLAPSE
    $('.Trigger').click(function() {
        if ($(this).attr('class') == 'InlineImage Trigger ExpandCollapse') {
            var expandID = $(this).attr('id').replace('Expand', 'JobText');
            var expandHeaderID = $(this).attr('id').replace('Expand', 'TextExpand');
            var expandTeaserID = $(this).attr('id').replace('Expand', 'JobTextTeaser');
            var expandCollapseID = $(this).attr('id');
        }
        else if ($(this).attr('class') == 'Contracts Trigger Text') {
            var expandID = $(this).attr('id').replace('TextExpand', 'JobText');
            var expandHeaderID = $(this).attr('id');
            var expandTeaserID = $(this).attr('id').replace('TextExpand', 'JobTextTeaser');
            var expandCollapseID = $(this).attr('id').replace('TextExpand', 'Expand');
        }
        else {
            var expandID = $(this).attr('id').replace('ReadMoreExpand', 'JobText');
            var expandHeaderID = $(this).attr('id').replace('ReadMoreExpand', 'TextExpand');
            var expandTeaserID = $(this).attr('id').replace('ReadMoreExpand', 'JobTextTeaser');
            var expandCollapseID = $(this).attr('id').replace('ReadMoreExpand', 'Expand');
        }

        if ($('.JobTextActive').attr('id') == undefined) {
            var collapseID = 'expand';
            var collapseTextID = 'expand';
            newStartHeight = $('#' + expandHeaderID).height() + 60;
        }
        else {
            var collapseID = $('.JobTextActive').attr('id').replace('JobText', 'Expand');
            var collapseTeaserID = $('.JobTextActive').attr('id').replace('JobText', 'JobTextTeaser');
            var collapseTextID = $('.JobTextActive').attr('id');
        }

        oldStartHeight = newStartHeight;
        newStartHeight = $('#' + expandHeaderID).height() + 60;

        var height = $('#' + expandCollapseID).parent().height();

        //First check to see if we are collapsing or expanding
        if (expandID == collapseTextID) {
            //COLLAPSING
            $('#' + collapseID).attr('src', 'images/Expand.gif'); //Flip icon from - to +
            $('#' + expandID).parent().animate({ height: oldStartHeight }, function() {
                $('#' + collapseTeaserID).removeClass('Hidden'); //Show Teaser Text
                $('#' + collapseTextID).removeClass('JobTextActive'); //Update class from Active
                $('#' + collapseTextID).addClass('JobText'); //Update class from Active            
            }); //Set the new height for the animation                        
        }
        else if (collapseTextID == 'expand') {
            //EXPANDING
            $('#' + expandCollapseID).attr('src', 'images/Collapse.gif'); //Flip icon from + to -
            $('#' + expandCollapseID).parent().height(height); //Set the height so it doesn't jump
            $('#' + expandTeaserID).addClass('Hidden'); //Hide Teaser Text
            $('#' + expandID).removeClass('JobText'); //Update class to Active
            $('#' + expandID).addClass('JobTextActive'); //Update class to Active            
            var textHeight = $('#' + expandID).height(); //Get the height of the main text
            $('#' + expandID).parent().animate({
                height: height + textHeight //Set the new height for the animation
            });
        }
        else {
            //COLLAPSING & EXPANDING
            //COLLAPSING
            $('#' + collapseID).attr('src', 'images/Expand.gif'); //Flip icon from - to +
            $('#' + collapseTextID).parent().animate({ height: oldStartHeight }, function() {
                $('#' + collapseTeaserID).removeClass('Hidden'); //Show Teaser Text
                $('#' + collapseTextID).removeClass('JobTextActive'); //Update class from Active
                $('#' + collapseTextID).addClass('JobText'); //Update class from Active            
            }); //Set the new height for the animation              

            //EXPANDING
            $('#' + expandCollapseID).attr('src', 'images/Collapse.gif'); //Flip icon from + to -
            $('#' + expandCollapseID).parent().height(height); //Set the height so it doesn't jump
            $('#' + expandTeaserID).addClass('Hidden'); //Hide Teaser Text
            $('#' + expandID).removeClass('JobText'); //Update class to Active
            $('#' + expandID).addClass('JobTextActive'); //Update class to Active            
            var textHeight = $('#' + expandID).height(); //Get the height of the main text
            $('#' + expandID).parent().animate({
                height: height + textHeight //Set the new height for the animation
            });
        }
    });

    //FAQ PAGE EXPAND/COLLAPSE
    $('.TriggerFAQ').click(function() {
        if ($(this).attr('class') == 'InlineImage TriggerFAQ ExpandCollapse') {
            var expandID = $(this).attr('id').replace('Expand', 'JobText');
            var expandHeaderID = $(this).attr('id').replace('Expand', 'TextExpand');
            var expandCollapseID = $(this).attr('id');
        }
        else {
            var expandID = $(this).attr('id').replace('TextExpand', 'JobText');
            var expandHeaderID = $(this).attr('id');
            var expandCollapseID = $(this).attr('id').replace('TextExpand', 'Expand');
        }

        if ($('.JobTextActive').attr('id') == undefined) {
            var collapseID = 'expand';
            var collapseTextID = 'expand';
            newStartHeight = $('#' + expandHeaderID).height() + 28;
        }
        else {
            var collapseID = $('.JobTextActive').attr('id').replace('JobText', 'Expand');
            var collapseHeaderID = $('.JobTextActive').attr('id').replace('JobText', 'TextExpand');
            var collapseTextID = $('.JobTextActive').attr('id');
        }

        oldStartHeight = newStartHeight;
        newStartHeight = $('#' + expandHeaderID).height() + 28;

        var height = $('#' + expandCollapseID).parent().height();

        //First check to see if we are collapsing or expanding
        if (expandID == collapseTextID) {
            //COLLAPSING
            $('#' + collapseID).attr('src', 'images/Expand.gif'); //Flip icon from - to +
            $('#' + collapseHeaderID).removeClass('ContractsActive'); //Update class from Active
            $('#' + collapseHeaderID).addClass('Contracts'); //Update class from Active
            $('#' + expandID).parent().animate({ height: oldStartHeight }, function() {
                $('#' + collapseTextID).removeClass('JobTextActive'); //Update class from Active
                $('#' + collapseTextID).addClass('JobText'); //Update class from Active
            }); //Set the new height for the animation                        
        }
        else if (collapseTextID == 'expand') {
            //EXPANDING
            $('#' + expandCollapseID).attr('src', 'images/Collapse.gif'); //Flip icon from + to -
            $('#' + expandCollapseID).parent().height(height); //Set the height so it doesn't jump
            $('#' + expandHeaderID).removeClass('Contracts'); //Update class to Active
            $('#' + expandHeaderID).addClass('ContractsActive'); //Update class to Active
            $('#' + expandID).removeClass('JobText'); //Update class to Active
            $('#' + expandID).addClass('JobTextActive'); //Update class to Active            
            var textHeight = $('#' + expandID).height(); //Get the height of the main text
            $('#' + expandID).parent().animate({
                height: height + textHeight //Set the new height for the animation
            });
        }
        else {
            //COLLAPSING & EXPANDING
            //COLLAPSING
            $('#' + collapseID).attr('src', 'images/Expand.gif'); //Flip icon from - to +
            $('#' + collapseHeaderID).removeClass('ContractsActive'); //Update class from Active
            $('#' + collapseHeaderID).addClass('Contracts'); //Update class from Active
            $('#' + collapseTextID).parent().animate({ height: oldStartHeight }, function() {
                $('#' + collapseTextID).removeClass('JobTextActive'); //Update class from Active
                $('#' + collapseTextID).addClass('JobText'); //Update class from Active            
            }); //Set the new height for the animation              

            //EXPANDING
            $('#' + expandCollapseID).attr('src', 'images/Collapse.gif'); //Flip icon from + to -
            $('#' + expandCollapseID).parent().height(height); //Set the height so it doesn't jump
            $('#' + expandHeaderID).removeClass('Contracts'); //Update class to Active
            $('#' + expandHeaderID).addClass('ContractsActive'); //Update class to Active            
            $('#' + expandID).removeClass('JobText'); //Update class to Active
            $('#' + expandID).addClass('JobTextActive'); //Update class to Active            
            var textHeight = $('#' + expandID).height(); //Get the height of the main text
            $('#' + expandID).parent().animate({
                height: height + textHeight //Set the new height for the animation
            });
        }
    });

    $('.Bottom').children('div').each(function(index, value) {
        if (index % 2 == 0) {
            var Col1 = $(value);
            var Col2 = $($('.Bottom').children().get(index + 1));

            if (Col1.height() < Col2.height()) {
                  Col1.height(Col2.height());
            }
            else {
                Col2.height(Col1.height());
            }
        }
    });
});
