// JavaScript Document
var navActive = false;
var animateNav = true;

$(document).ready(function () {
    //SITE OVERLAY
    $('#overlay').hide();

    //NAVIGATION
    $('#navDropDown').hide();
    
    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
        $('#nav ul').children('li:eq(0)').children('img').wrap("<a href='overview.aspx' />");
        $('#nav ul').children('li:eq(1)').children('img').wrap("<a href='whatwedo_overview.aspx' />");
        $('#nav ul').children('li:eq(2)').children('img').wrap("<a href='whoweserve_overview.aspx' />");
        $('#nav ul').children('li:eq(3)').children('img').wrap("<a href='whatwebuild.aspx' />");
        $('#nav ul').children('li:eq(4)').children('img').wrap("<a href='media_releases.aspx' />");
        $('#nav ul').children('li:eq(5)').children('img').wrap("<a href='recognition.aspx' />");
        /* $('#overlay').css({ position: 'absolute', backgroundImage: 'url(/Images/90_333_bg.png)' });
    
        $('#navHolder').hover(function () {
            dropAlpha = $('#navDropDown').css('opacity');
            //alert($('#navDropDown').css('display'));
            //$('#debugger').append(dropHeight + ', ');
            if ($('#navDropDown').css('display') == 'none') {
                $('#navHolder').css({ paddingBottom: '50px' });
                
                //$('#overlay').css({ display: 'block', opacity: 0 });
                $('#overlay').show(200);
                $('#navDropDown').stop(true, true).slideFadeToggle(200);
            }
            navActive = true;
            //alert(('#navDropDown').css('opacity'));
        }, function () {
            $('#navHolder').css({ paddingBottom: '0px' });
             $('#overlay').hide();
            //$('#overlay').stop(true, true).animate({ opacity: 'hide' }, 200);
            $('#navDropDown').stop(true, true).slideFadeToggle(200);
            navActive = false;
        });*/
    }else {
        $('#navDropDown').css({ 'opacity': 0 });
        $('#navHolder').hover(function () {
            dropAlpha = $('#navDropDown').css('opacity');
            //alert($('#navDropDown').css('display'));
            //$('#debugger').append(dropHeight + ', ');
            if ($('#navDropDown').css('display') == 'none') {
                $('#navHolder').css({ paddingBottom: '50px' }); 
                $('#overlay').css({ display: 'block', opacity: 0 });
                $('#overlay').stop(true, true).animate({ opacity: .6 }, 200);
                $('#navDropDown').stop(true, true).slideFadeToggle(200);
            }
            navActive = true;
        }, function () {
            $('#navHolder').css({ paddingBottom: '0px' });
            $('#overlay').stop(true, true).animate({ opacity: 'hide' }, 200);
            $('#navDropDown').stop(true, true).slideFadeToggle(200);
            navActive = false;
        });
    }


    $('.navSect').hover(function () {
        $('.navSect').css({ 'opacity': .50 });
        $(this).css({ 'opacity': 1 });
    }, function () {
        $('.navSect').css({ 'opacity': 1 });
    });


     $('#navHolder').css({ marginLeft: '0' });
    centerNav();
    keepHeight();
    
    //NAVIGATION POSITIONING
    $(window).bind('resize', function () {
        centerNav();
        keepHeight();
    });
    
    $(window).scroll(function() {
        keepHeight();
    });



    //SAMPLE
    //$('.cMid').hide();
    //$('.cRight').hide();
    //$('.cRightWide').hide();

    //$('.cMid').fadeIn(900);
    //$('.cRight').fadeIn(900);
    //$('.cRightWide').fadeIn(900);



    //SIDE NAVIGATION
    
    $('.disabled').css({ opacity: '.20' });
        
    //var t3Count = $('.s3Item').length;
    //if ($('.s3Item').length >= 1) {
    //    $('.sOn').next('.subSideNav').children('.s2Item').addClass('s2Avail');
    //} 
    
    
    //animateNav = false; 
    if (animateNav) {
       animateSideIn();
    } else {
        $('.sOn').next('.subSideNav').children('.s2Item').css({ marginLeft: '30px' });
        $('.sOn').next('.subSideNav').children('.s3Item').css({ marginLeft: '40px' });
        $('.sOn').next('.subSideNav').show();

        $('.nextTier').addClass('s2On');
        
        //if ($('.s3Item').length >= 1) {
            $('.sOn').next('.subSideNav').show();
            $('.s2On').next('.subSideNav').show();
        //}
    }

    $('.lItem').click(function () {
        window.location = $(this).children('a').attr('href').toString();
    });


//    $('.sDown').click(function () {
//        if ($(this).parent().next().css('display') == 'none') {
//            $(this).html('<img src="Images/mcsNavUp.gif" width="22" height="22" />');
//        } else {
//            $(this).html('<img src="Images/mcsNavDown.gif" width="22" height="22" />');
//        }
//        var subMenu = $(this).parent().next('.subSideNav');

//        if (subMenu.css('display') == 'none') {
//            $(this).parent().addClass('sOpen');
//            subMenu.slideDown();
//            subMenu.children('.s2Item').css({ marginLeft: '250px' });
//            var delayFor;
//            $(subMenu).children('.s2Item').each(function (intIndex) {
//                gotoNum = 30;
//                $(this).pause(intIndex * 100).animate({ marginLeft: gotoNum + 'px' });
//               
//                delayFor = intIndex * 100 + 400;
//            });
//            
//             $(subMenu).children('.s2On').next('.subSideNav').pause(delayFor).slideDown();
//             $(subMenu).children('.s2On').next('.subSideNav').children('.s3Item').css({ marginLeft: '250px' });
//            
//            $(subMenu).children('.s2On').next('.subSideNav').children('.s3Item').each(function (intIndex) {
//                gotoNum = 40;
//                $(this).pause(intIndex * 100 + 400).animate({ marginLeft: gotoNum + 'px' });

//            });

//            //subMenu.animate({ marginLeft: '20px' },300);
//        } else {
//            $(this).parent().removeClass('sOpen');
//            subMenu.hide();
//            subMenu.children('.s2Item').css({ marginLeft: '250px' });

//            //Hide Level 3
//            var sub3Menu = subMenu.children('.s2Item').next('.subSideNav');
//            //subMenu.children('.s2Item').removeClass('s2On');
//            subMenu.children('.s2Item').removeClass('overState');
//            sub3Menu.hide();
//            sub3Menu.children('.s3Item').css({ marginLeft: '250px' });
//        }
//    });

    $('.sItem').hover(function () {
        //FLOW LIST
        if (!$(this).hasClass('sOn')) {
            //$(this).css({ marginLeft: '15px', width: '213px' });
            $(this).addClass('over1State');
        }
    }, function () {
        if (!$(this).hasClass('sOn')) {
            if (!$(this).hasClass('sOpen')) {
                //$(this).css({ marginLeft: '20px', width: '208px' });
                $(this).removeClass('over1State');
            }
        }
    });

    $('.s2Item').hover(function () {
        $(this).addClass('over2State');
    }, function () {
        if (!$(this).hasClass('s2On')) {
            $(this).removeClass('over2State');
        }
    });

    $('.s3Item').hover(function () {
        $(this).addClass('over3State');
    }, function () {
        if (!$(this).hasClass('s3On')) {
            $(this).removeClass('over3State');
        }
    });

    $('.nextTier').click(function () {
        var subMenu = $(this).next('.subSideNav');

        if (subMenu.css('display') == 'none') {
            $(this).addClass('s2On');
            subMenu.slideDown();
            subMenu.children('.s3Item').css({ marginLeft: '250px' });

            $(subMenu).children('.s3Item').each(function (intIndex) {
                gotoNum = 30;
                $(this).pause(intIndex * 100).animate({ marginLeft: gotoNum + 'px' });

            });


            //subMenu.animate({ marginLeft: '20px' },300);
        } else {
            $(this).removeClass('s2On');
            subMenu.hide();
            subMenu.children('.s3Item').css({ marginLeft: '250px' });
        }
    });
});



//FUNCTIONS
function keepHeight() {
    scrollAmount = $(window).scrollTop();
    browserHeight = getHeight() + scrollAmount;
    
    $('#sheath').css({ height: browserHeight+'px' });
}

function centerNav() {
	browserWidth = getWidth();
	if (browserWidth > 1000) {
		inSet = (browserWidth - 1000)/2 + 248;
	}else {
		inSet = 248;
	}
	$('#navHolder').css({ left: inSet+'px' });
}

function animateSideIn() {
	$('.sItem').css({ marginLeft: '300px' });
	//$('.sItem').pause(500).animate({ marginLeft: '20px' });
	
	var totalItems = $('.sItem').length;
	
	$('.sItem').each(function( intIndex ){
		//$('#debugger').html(intIndex);
		if (!$(this).hasClass('sOn')) {
			gotoNum = 20;	
		}else {
			gotoNum = 20;	
		}
		$(this).pause(intIndex*100).animate({ marginLeft: gotoNum+'px'  });
		
 		if (intIndex == totalItems-1) {
			delayPlus = intIndex*100 + 400;
			$('.sOn').next('.subSideNav').pause(delayPlus).slideDown(500);
			$('.sOn').next('.subSideNav').children('.s2Item').each(function( int2Index ){
				$(this).pause((int2Index*100)+delayPlus).animate({ marginLeft: '30px'  });
			
			    var total2Items = $('.sOn').next('.subSideNav').children('.s2Item').length;
			    delay2Plus = int2Index*100 + delayPlus;
		        if (int2Index == total2Items-1) {	
			        subMenu = $('.sOn').next('.subSideNav').children('.s2On').next('.subSideNav');
        		
		            subMenu.pause(delay2Plus).show();
                    subMenu.children('.s3Item').css({ marginLeft: '250px' });

                    $(subMenu).children('.s3Item').each(function (int3Index) {
                        gotoNum = 40;
                        $(this).pause((int3Index*100)+delay2Plus).animate({ marginLeft: gotoNum + 'px' });

                    });
                }
    			
			});	
		}
	});
	
}

function getWidth() {
    var y = 0;
    if (self.innerWidth) {
            y = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
            y = document.documentElement.clientWidth;
    } else if (document.body) {
            y = document.body.clientWidth;
    }
    return y;
}

function getHeight() {
        var x = 0;
        if (self.innerHeight) {
                x = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
                x = document.documentElement.clientHeight;
        } else if (document.body) {
                x = document.body.clientHeight;
        }
        return x;
}

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
		if (!navActive) {
        	return this.animate({opacity: '1', height: 'show'}, speed, easing, callback);
		}else {
			return this.animate({opacity: '0', height: 'hide'}, speed, easing, callback);
		}
}; 
