var headerOffset = 133;
var scrollSpeed = 500;
var animSpeed = 600;
var fadeSpeed = 250;

jQuery(document).ready(function($) {
		
	$(".footerContent .social li:first").css("margin-left", "0");
	
	//logic for 3 column row
	$(".threeColRow").each(function() {
		$(this).children("div").first().next().addClass("centerItem");
		$(this).children("div").last().children("img").addClass("floatRight");
	});
	
	//related projects slider
	if ($(".sliderTable div").length <= 5 ) {
		$(".moreLink").css("display", "none");	
	}
	
	//services grid logic
	$(".servicesGrid ul").each(function() {
		if( $(this).children("li").length > 12 ) {
			$(this).parent().css("width", "980px");
		}
		
		else if ( ($(this).children("li").length > 8) && ($(this).children("li").length <= 12) ) {
			$(this).parent().css("width", "735px");
		}

		else if ( $(this).children("li").length > 4 &&  $(this).children("li").length < 9 ) {
			$(this).parent().css("width", "490px");			
		}
			
		else if ( $(this).children("li").length <= 4 ) {
			$(this).parent().css("width", "245px");			
		}
		
	});
	
	//jobs listings
	$(".openPositions .col1x div:last").addClass("last");
	
	$(".closeBtn").click( function() {
	
		$(this).css("display", "none");
		$(".seeMore").css("display", "block");
		
	});
	
	
	
    var overlay = new Image().src = '/sites/all/themes/andco/images/overlay_316x168.png';
    
    if( navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/) ) { headerOffset = 0; }
    
    function initWorkContent()
    {
        
        $( '.sliderTable' ).easySlider( {
            count: 5, 
            controlsShow: false
        } );
        
        $(".closeBtn").show();
        
        $( ".closeProject" ).click( function(){
            
            $( ".workLink" ).removeClass( "overlay" );
             
            $( "html, body" ).scrollTop( $("#workContentHolder").prev().offset().top - headerOffset );
            
            closeWorkContent();
        });
        
        var s = document.createElement( 'script' ), s1 = document.getElementsByTagName( 'script' )[ 0 ];
        s.type = 'text/javascript';
        s.async = true;
        s.src = ( 'https:' == document.location.protocol ? 'https' : 'http' ) + '://connect.facebook.net/en_US/all.js#xfbml=1';
        s1.parentNode.insertBefore( s, s1 );
        
        if( !$.browser.msie )
        {
            gapi.plusone.go();
        }
        
        
        $( ".projectDetails .workLink").click(function(){
            
            var objIdx = $( '#workGridContent > div' ).index( $(this).parent( 'div#workContentHolder' ).prev() );
            var insIdx = Math.max( objIdx + ( 3 - ( ( objIdx ) % 3 ) ), 3 ) - 1;
            insIdx = Math.min( insIdx, $( '#workGridContent > div.workItem' ).length - 1 );
            
            var href = $(this).attr( 'href' );
            
            loadWorkContent( href, insIdx );
            
            return false;
            
         });
        
        var endTop = $( "#workContentHolder" ).offset().top - headerOffset;
        
        $( "html, body" ).scrollTop( endTop );
        $( ".closeProject" ).show();
        
        
        var x = Math.floor( $(".work").offset().left);
        
        var mod = $.browser.mozilla ? 0 : 1;
        
        $(".closeProject").css("right" , x+mod +"px" );
        
        var cbTop = $( "#workContentHolder" ).offset().top - 6;
        var cbBottom = $(".relatedProjects").offset().top;
        
        $( window ).scroll( function( data ) {
            
            if( $( ".projectDetails" ).offset() != null )
            {
                if( $( ".projectDetails" ).offset().top - $( window ).scrollTop()  > headerOffset + 1 )
                {
                    $( '.closeProject' ).css( { 'position': 'static', 'margin-right': '16px' } );
                } else if( $( ".relatedProjects").offset().top - $( window ).scrollTop() < headerOffset + 29 ) {
                    $( '.closeProject' ).css( { 'position': 'absolute', 'top': $( ".relatedProjects" ).offset().top - 27, "z-index": '-10000' } );
                } else {
                    $( '.closeProject' ).css( { 'position': 'fixed', 'margin-right': '0', "top": headerOffset - 2, 'z-index': '1000000' } );
                }    
            }
            
            
        });
        
    }
    
    function closeWorkContent( callback )
    {
        $( "#workContentHolder" ).remove();
        if( callback != null )
        {
            callback();
        }
    }
    
    function loadWorkContent( href, index )
    {       
            hash = href.substr( href.lastIndexOf( '/' ) + 1 );
            
            if( hash.indexOf( '#' ) != -1 )
            {
                hash = hash.substring( 0, hash.indexOf( '#' ) );
            }

            var st = $( document ).scrollTop();
            window.location.hash = hash; 
            $( document ).scrollTop( st );
            
            $.get( href + "?ajax=true", function( data ) {
            
                closeWorkContent();
                
                if( $( "#" + hash ) )
                {
                    var objIdx = $( "#workGridContent > div.workItem" ).index( $( "#" + hash ) );
                    var insIdx = Math.max( objIdx + ( 3 - ( ( objIdx ) % 3 ) ), 3 ) - 1;
                    index = Math.min( insIdx, $( '#workGridContent > div.workItem' ).length - 1 );
                    
                }
                
                $( '<div id="workContentHolder"></div>' ).insertAfter( $( '#workGridContent > div').get( index ) );
                
                $("#workContentHolder").html( data );
                
                _gaq.push( [ '_trackPageview', href ] );
                
                initWorkContent();
                
            } );
    }
    
    $(".workLink").click( function(){
        var obj = $( this );
        
        var objIdx = $( '#workGridContent > div' ).index( obj.parent( 'div' ) );
        var insIdx = Math.max( objIdx + ( 3 - ( ( objIdx ) % 3 ) ), 3 ) - 1;
        insIdx = Math.min( insIdx, $( '#workGridContent > div.workItem' ).length - 1 );

        $( ".workLink" ).removeClass( "overlay" );
        $( this ).addClass( "overlay" );
        
        loadWorkContent( obj.attr( 'href' ), insIdx );
        
        return false;
    });
    
    $(".jobLink").click( function(){
        $( "#jobHolder > .intro").hide( );
        $( "#jobHolderContent" ).html( '<div class="jobDesc"><img src="/sites/all/themes/andco/images/loading.gif" class="loadingJob" /></div>' );
        $( "#jobHolderContent" ).fadeIn( 200 );
        var href = $( this ).attr( 'href' );
        $.get( href + "?ajax=true", function( data ){
            
            _gaq.push( [ '_trackPageview', href ] );
            
            $( "#jobHolderContent" ).hide();
            $( "#jobHolderContent" ).html( data );
            $( "#jobHolderContent" ).fadeIn( 250 );
            $( ".closeJob" ).click( function(){
                $( "#jobHolderContent" ).fadeOut( 200, function(){
                    $( "#jobHolder > .intro" ).fadeIn( 150 );
                });
            });
        } );
        
        return false;
    });
    
    var hash = window.location.hash;
    if( hash && hash != undefined )
    {
        if( $( hash ) )
        {
            $( "html, body").scrollTop( 0 );
            var objIdx = $( '#workGridContent > div' ).index( $( hash ) );
            var insIdx = Math.max( objIdx + ( 3 - ( ( objIdx ) % 3 ) ), 3 ) - 1;
            insIdx = Math.min( insIdx, $( '#workGridContent > div.workItem' ).length - 1 );
            loadWorkContent( $( hash + " > a" ).attr( 'href' ), insIdx );
        }
        
    }
    
    //if we have js, make the featured work magical
    /*
    $( ".featuredWorkLink" ).each( function(){
        
        var href = $( this ).attr( 'href' );
        href = href.substr( href.lastIndexOf( '/' ) + 1 );
        
        $( this ).attr( 'href', '/work#' + href );
    });
    */
    
    $( ".moreClients" ).click( function(){
        $( ".clientGrid" ).fadeOut( 150, function(){
            $( ".clientGrid" ).height( 'auto' );
            $( ".moreClients" ).hide();
            $( ".closeClients" ).show();
            $( ".clientGrid" ).fadeIn( 200 );
            
        } );
        
        var x = Math.round( $(".contentWrap").offset().left + 11 );
        
        $(".closeClients").css("right" , x +"px" );
        
    });
    
    $( ".closeClients" ).click( function() {
        $( ".clientGrid" ).fadeOut( 200, function(){
            $( ".clientGrid" ).height( 285 );
            $( ".closeClients" ).hide();
            $( ".moreClients").show();
            
            $( ".clientGrid" ).fadeIn( 200 );
        });
    });
    
    $( ".clientGrid" ).height( 285 );
    $( ".clientGrid" ).css( { overflow: 'hidden' } );
    $( ".closeClients" ).hide();

});
