var W,H, sW;
if (window.innerWidth) {
    W = window.innerWidth;
    H = window.innerHeight;
} else if (document.documentElement.offsetWidth) {
    W = document.documentElement.offsetWidth;
    H = document.documentElement.offsetHeight;
} else if (d.body) {
    W = document.body.offsetWidth;
    H = document.body.offsetHeight;
}

sW = screen.width;    

function onBefore() {
   // window.status = 'before';
}

function onAfter() {
    //window.status = 'after';
}
var imagePosition = 0;


$(document).ready(function(){
    $('html').addClass('js');
    
    var thisPage = $('body').attr('id');
    var thisMode = $('body').attr('class');
    if(W < 1000) {
        $('.container').addClass('smallerScreen');
    }
    
    if($('body').attr('class') != 'about') {
        $('#aboutMenu').hide();
    }
    
    $('#menuMainDiv > ul > li:first').hover(function(){
         $('#aboutMenu').show('medium');
    },function(){
        if(thisMode != 'about') {
            $('#aboutMenu').fadeTo(1,1000, function() {
                $(this).hide('fast');
            });
        }
    });
    
    $(window).resize(function() {
		//window.location.href=window.location.href;
    });


    if (thisPage == "home") {
    
        $('#panelDiv').addClass('intro');
        
        $('#copyDiv').fadeTo(4000,1, function() {
        
            $(this).fadeOut('slow', function() {
                $('#panelDiv').removeClass('intro');

                 $('#titleDiv > img:first').css('margin-top', '58px').show().animate({'marginTop':0}, 1000, 'easeInOutQuad', function() {
                 
                    $('#titleDiv').cycle({
                        fx: 'scrollUp',
                        easing: 'easeInQuad',
                        speedIn: 1000,
                        speedOut: 500,
                        continuous: 1,
                        delay: -1000,
                        sync: 1
                    });
                });
                       
            });
        });
        
        $('#galleryDiv > img:first').fadeIn(2000);   
        $('#galleryDiv').cycle({
            fx: 'fade',
            speed: 2000
        });
        // end home
        
        
        
        
    } else if (thisMode == 'about') {

        $('#aboutMenu > li > a').each(function() {
            if (thisPage == $(this).attr('href')) {
                $(this).addClass('active');
            }
        });
        
        if (thisPage == 'practice') {
            $('#galleryDiv').hide();
            $('#titleDiv').hide().fadeIn('fast', function() {
                //$('#copyDiv').animate({top:0},500 , 'easeInCirc', function() {
                $('#copyDiv').animate({top:0},1000 , 'easeOutExpo', function() {
                    $('#galleryDiv').fadeIn(2000);
                    $('#titleDiv').fadeOut(2000);
                });
            });
            // end practice
            
            
            
            
        } else if (thisPage == 'social-responsibility') {
    
            $('#titleDiv').hide().fadeIn('fast',  function() {
                //$('#copyDiv').animate({top:0},500 , 'easeInCirc', function() {
                $('#copyDiv').animate({top:0},1000 , 'easeOutExpo', function() {
                    $('#bottomDiv').slideDown();
                    $('body').css('overflow', 'auto');
                    $('#titleDiv').fadeOut(2000);
                 });
            });
            // end social-responsibility
            
            
            
            
        } else if (thisPage == 'team') {
            $('#galleryDiv,#titleDiv').hide().fadeIn(2000);
            $('#titleDiv').fadeTo(1000,1,function() {
                $('#titleDiv').fadeOut(2000);
            });

            $('.staffThumb').hover(function() {
                var category = $(this).parent().prev().text();
                if(category != 'Senior Associates: ') {
                    if(category != 'Associates: ') {
                
                        $('#copyDiv').html('<div class="holder">' + $(this).find('.staffBio').html() + '<\/div>').animate({top: 0},'slow', function() {
                        });
                        $('.staffThumb').removeClass('activeThumb');
                        $(this).addClass('activeThumb');
                    }
                }
            }, function() {
                //
            });
            // end team
            
            
            
            
        } else if (thisPage == 'recognition') {
            $('#galleryDiv').hide();
             $('#titleDiv').hide().fadeIn('fast', function() {
                $('#copyDiv').animate({top:'0'},1000 , 'easeOutExpo', function() {
                    $('#titleDiv').fadeOut(2000);
                    $('#galleryDiv').fadeIn(2000);
                });
            });
           
            // end recognition
            
            
            
            
        } else if (thisPage == 'news') {
            $('#galleryDiv,#titleDiv').hide();
    
             $('#galleryDiv,#titleDiv').fadeIn(2000, function() {
                $('#titleDiv').fadeOut(2000);
            });
    
    
            $('.newsThumb').hover(function() {
                $('.photoCaption').remove();
                var photoCaption = $(this).find('img').attr('alt');
                $(this).find('h3').prepend('<p class="photoCaption" style="display: none">' + photoCaption + '<\/p>');
                $(this).find('.photoCaption').fadeIn('slow');
                
                $('#copyDiv').html('<div class="holder">' + $(this).find('.bodyTextDiv').html() + '<\/div>').animate({top: 0},'slow', function() {
                    $('#titleDiv').fadeOut(2000);
                });
                
                var offset =  $(this).offset().left - $('#galleryDiv').offset().left;
                if(offset < 600) {
                    $('#copyDiv').css({left: offset});
                } else if (offset == 600) {
                    $('#copyDiv').css({left: '400px'});
                }
                
            }, function() {

            });
    
            // end news
            
            
            
            
        }
        // end about
        
        
        
    } else if (thisPage == 'projects') {
        $('#galleryDiv').hide().fadeIn(2000);

        $('#menuMainDiv > ul > li:eq(1) > a').addClass('active');

        if (thisMode == 'list') {

            //$('.list').find('#titleDiv').animate({top:'-3.2em'},2000,'easeInOutQuad');
            
            
            
            $('.projectThumb').each(function() {
                $(this).hover(function() {
                    $(this).addClass('active').find('.holder').animate({
                        height: '216px',
                        marginTop: 0
                    },300,'jswing').find('img').animate({
                        marginTop: 0
                    },300,'jswing');
                    
                }, function() {
                    $(this).removeClass('active').find('.holder').animate({
                        height: '108px',
                        marginTop: '108px'
                    },500,'jswing').find('img').animate({
                        marginTop: '-54px'
                    },500,'jswing');
                });
            });
            
            
            
            var scrollListRight = '<a id="scrollListRight" href="#" title="more"></a>'; 
            $('.container').append(scrollListRight);
            $('#scrollListRight').click(function() {
                if(imagePosition < $('#galleryDiv li').length/4 -1) {
                    imagePosition = (parseInt(imagePosition)+1);
                    
                    if (imagePosition >= $('#galleryDiv li').length/4 - 1) {
                        $(this).addClass('switch');
                    }
                    
                } else {
                    imagePosition = 0;
                    $(this).removeClass('switch');
                }
                var scrollPos = -(imagePosition * 800);
            
                $('#galleryDiv > ul').animate({left: scrollPos},500,'easeInOutCirc');
               
                return false;
            });
            
            
        
        } else if (thisMode == 'detail') {
        
            $('#menuMainDiv').find('.active').html('other projects');
            
            // images listing
            var scroll = '<a id="scroll" href="#" title="more"></a>';
            var unScroll = '<a id="unScroll" href="#" title="back"></a>';
            $('.container').append(scroll + unScroll);
            toggleScrolls();
            $('#scroll').click(function() {
                $('#galleryDiv > ul').animate({marginLeft: -800},1500,'easeInOutCirc');
                $(this).hide().next().show();
                return false;
            });
            
            $('#unScroll').click(function() {
                $('#galleryDiv > ul').animate({marginLeft: 0},1500,'easeInOutCirc');
                $(this).hide().prev().show();
                return false;
            });
            
            // images listing
            $('#galleryDiv a').each(function(i) {
            
                $(this).attr('id', 'position' + i);
                
                $(this).click(function() {
                    imagePosition = $(this).attr('id').substring(8);
                    $('#maskDiv,#overlayDiv,#scrollRight,#scrollLeft,#close').fadeIn('fast');
                    toggleScrolls();
                    $('#overlayDiv').html('<img src="' + $(this).attr('href') + '" />');
                
                    return false;
                }); // end click


                $(this).hover(function() {
                    $(this).addClass('active');
                    
                }, function() {
                    $(this).removeClass('active');
                });


               
            });// end each
            
            
            // image detail
            var maskDiv = '<div id="maskDiv">&nbsp;</div>'; 
            var overlay = '<div id="overlayDiv"></div>'; 
            var scrollRight = '<a id="scrollRight" href="#" title="next">&nbsp;</a>'; 
            var scrollLeft = '<a id="scrollLeft" href="#" title="previous">&nbsp;</a>'; 
            var close = '<a id="close" title="return to thumbnail view">&nbsp;</a>'; 

            $('.container').append(overlay + maskDiv + scrollRight + scrollLeft + close);

            $('#scrollRight').click(function() {
                if(imagePosition <= $('#galleryDiv a').length - 2) {
                    imagePosition = (parseInt(imagePosition)+1);
                } else {
                    imagePosition = 0;
                }
                var nextImage = $('#galleryDiv a:eq('+imagePosition+')').attr('href');
                $('#overlayDiv').fadeOut('fast', function() {
                    $(this).html('<img src="' + nextImage + '" />').fadeIn('slow');
                });
                return false;
            });
            
            $('#scrollLeft').click(function() {
               
                if(imagePosition == 0) {
                    imagePosition = $('#galleryDiv a').length-1;
                } else {
                    imagePosition = (parseInt(imagePosition)-1);
                }
                var nextImage = $('#galleryDiv a:eq('+imagePosition+')').attr('href');
                $('#overlayDiv').fadeOut('fast', function() {
                    $(this).html('<img src="' + nextImage + '" />').fadeIn('slow');
                });
                return false;
            });
            
            $('#close').click(function() {
                $('#maskDiv,#overlayDiv,#scrollRight,#scrollLeft,#close').fadeOut('fast');
                imagePosition = '';
                toggleScrolls();
                return false;
            });
            
            
        }
        // end projects
        
        
        
    } else if (thisPage == 'careers') {
    
        $('#menuMainDiv > ul > li:eq(2) > a').addClass('active');
    
        if (thisMode == 'list') {
            $('.careersThumb .holder').hide();
            $('#galleryDiv, #titleDiv').hide().fadeIn(2000);
            
             $('#copyDiv').animate({top:0},1000 , 'easeOutExpo', function() {
                $('#bottomDiv').slideDown();
                $('#titleDiv').fadeOut(2000);
             });
           
            
            $('.container').append('<div id="careerFloater"></div>');
            $('#careersMenu > li:eq(0) > a').addClass('active');
           
            $('.positionListing').hover(function() {
                $('#copyDiv,#titleDiv').fadeOut('slow');
                $('#careerFloater').html('<div class="holder">' + $(this).find('.holder').html() + '<\/div>').animate({top: 0},'slow', function() {
                    //$('#titleDiv').fadeOut('fast');
                });
                $(this).addClass('active');
                
            }, function() {
                $(this).removeClass('active');
                
            });
            
            $('.positionListing > h3 > a').click(function() {
                return false;
            });
            
            var scrollRight = '<div id="scrollRight" title="more..."></div>'; 
            $('.container').append(scrollRight);
            $('#scrollRight').click(function() {
                if(imagePosition < $('#galleryDiv li').length/4 -1) {
                    imagePosition = (parseInt(imagePosition)+1);
                } else {
                    imagePosition = 0;
                    $('#scrollLeft').hide();
                }
                var scrollPos = -(imagePosition * 800);
            
                $('#galleryDiv > ul').animate({left: scrollPos},500,'easeInOutCirc');
               
                return false;
            });
            
            
            
        } else if (thisMode == 'detail') {
//             if($('#scholarship').length) {
//                 $('body').addClass('scholarship');
//                 $('#careersMenu > li:eq(1) > a').addClass('active');
//             } else {
                 $('#galleryDiv').hide().fadeIn(2000);
                 
                 $('#copyDiv').animate({top:0},1000 , 'easeOutExpo', function() {
                    $('#bottomDiv').slideDown('fast');
                    $('#titleDiv').fadeOut(2000);
                     $('#maskDiv').hide();
                 });
    

                $('#careersMenu > li:eq(1) > a').addClass('active');
//             }
        }
    } else if (thisPage == 'contact') {
        $('#menuMainDiv > ul > li:eq(3) > a').addClass('active');
        $('#galleryDiv').hide();

        $('#titleDiv').hide().fadeIn('fast', function() {
            $('#copyDiv').animate({top:'1em'},1000 , 'easeOutExpo', function() {
                $('#titleDiv').fadeOut('fast');
                $('#galleryDiv').fadeIn(2000);
            });
        });


    }

});

function toggleScrolls() {

    if($('#galleryDiv a').length > 4) {
        if(imagePosition == '') {
            $('#scroll').fadeIn('slow');
        } else {
            $('#scroll,#unScroll').hide();
        }
    } else {
        $('#scroll').hide();
    }
}

//             $.preloadImages("image1.gif", "/path/to/image2.png","some/image3.jpg");

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}