$(document).ready(function(){	
	$( 'body' ).removeClass( 'nojs' );
	
	var opts={
		navTitle:'h2 a',
		slide:'.slide',
		openSlide:'.open',
		openPadding:90,
		slideContents:'.slideContents',
		hslidersContainer:'.horizontalSliders',
		speed:750
	}
	
	var slideCount, slideWidth;
	$currentSlide=$(opts.slide + ':first');
	
	function getDimensions(){
		closedWidth=$(opts.slide).width() + 
						parseInt($(opts.slide).css('margin-right')) + 
						parseInt($(opts.slide).css('margin-left')) +
						parseInt($(opts.slide).css('padding-right')) +
						parseInt($(opts.slide).css('padding-left')
		);
		slideCount=$(opts.slide).length;
		hslidersWidth=$(opts.hslidersContainer).width();
		slideWidth=hslidersWidth - (closedWidth * slideCount) + closedWidth - opts.openPadding;
	}
	
	getDimensions();
	
	$slideParent=$(opts.slideContents+':first').parents('div.slide');
	$slideParent.addClass('open');
	$(opts.slideContents+':first').animate({ width:slideWidth }, opts.speed, 'swing');
	
	function changeSlide($newSlide){
		$currentSlide.find(opts.slideContents).animate({width:0}, opts.speed/2, 'swing', function(){
			$currentSlide.removeClass('open');
		});
		
		$newSlide.addClass('open');
		$newSlide.find(opts.slideContents).animate({width:slideWidth}, opts.speed, 'swing', function(){
			$currentSlide=$newSlide;
		});
	}
	
	$(opts.slide+' '+opts.navTitle).click(function(){
		$parent=$(this).parents('.slide');
		
		if( !$parent.hasClass('open') ){
			$parent.addClass('open');
			changeSlide($parent);
		}
		return false;
	});
	
	$(opts.slide+' '+opts.navTitle).hover(function(){
		$(this).parents('h2').addClass('on');
	},function(){
		$(this).parents('h2').removeClass('on');
	});
	
	if( $('#imgLoader').length){
		$('#imgLoader').append( '<img src="#" />' );
	}
	
	
	/* website portfolio */
	if($('.websites .portfolioCycle').length){
		$('.portfolioCycle').gencycle({
			simulFunction: function(){ 
					if( typeof( genopts.newItem != 'undefined' ) && typeof( genopts.slides != 'undefined' ) ){
						$(genopts.slides[genopts.newItem]).imageCycler();
					}
				} 
		});
	}
	
	
	/* logo portfolio ( no thumbnails ) */
	if($('.logos .portfolioCycle').length){
		$('.portfolioCycle').gencycle();
	}
	
	
	
	if( $( '.expandable' ).length ){
		$( '.expandable li' ).expandable();
	}
	

	/* Anchor Tag Recognization */
	//On load, highlight the correct link
	firstHash = document.location.hash;
	$( '#subnav a[href*="#"]' ).each( function(i){
		$link = $( this );
		href = $link.attr( 'href' );
		if( href.split( "#", 1 ) != ""){
			href = href.split( "#", 1 );
		}
		if( href == firstHash ){
			$( '#subnav a.current' ).removeClass( 'current' );
			$link.addClass( 'current' );
		}
	});
	$('#subnav a[href*="#"]').click(function(){
		$link = $( this );
		href = $link.attr( 'href' );
		if( href.split( "#", 1 ) == ""){
			href = href.split( "#", 1 );
		}
		
		if( !$link.hasClass( 'current' ) ){
			$( '#subnav a.current' ).removeClass( 'current' );
			$link.addClass( 'current' );
		}
		
		//nextOffset = $( href ).offset();
		
		// TODO: figure out scrolling
		//$(document).animate( { top: 400 }, '4000' );
		
		//return false;
	});
	
	/* Smart Scrolling Menu */
	var subnav = "#subnav";
	var $subnav = $("#subnav");
	
	if( $(subnav).length ){	
		var menuYloc = null;
		menuYloc = parseInt( $(subnav).css("top").substring( 0, $(subnav).css("top").indexOf("px")));
		topVal = $(subnav).css('top');
		origOffset = $subnav.offset();
		
		$(window).scroll( function () {
			scrollNav();
		});	
		
	}
	
	function scrollNav(){
			distanceTop = $(window).scrollTop();
		footerOffset = $('#footerOuterWrapper').offset();
			maxOffset = parseInt( footerOffset.top -  $subnav.height() ); // placed here to accommodate expandable content
			if( distanceTop <= origOffset.top ){
				var offset = origOffset.top;
			}else if( distanceTop >= maxOffset ){
				var offset = maxOffset;
			}else{
				var offset =  parseInt( 50 + distanceTop ) + "px";
			}
			
			$(subnav).animate({top:offset},{duration:125,queue:false}); 
			
	}
	
	if( $(subnav).length ){	
		scrollNav();  // position on load also
	}
	
	
	
	$( 'a.loadInline' ).click( function(){
		
		if( $( this ).attr( 'href' ) ){
			$link = $( this );
			$targ = $link.attr( 'href' );
		}else{
			return false; // don't bother without target
		}
		
		$link.html( '<img src="/display/images/ajax-loader.gif" alt="loading" />' );
		
		$link.after( '<br /><img src="' + $targ + '"/>' );
		
		$link.remove();
		return false;
		
	});
	
	$( 'a[href^="http://"]' ).addClass('elsewhere');
	
});
