	$(document).ready(function()	{
	
		window.studio_slider = $('.studio_slider').outerHTML();
		window.slide = $('#slider').outerHTML();
			
		// Anything Slider
		
		function applySliders () {

			var slide_width = $('.col.eight').width() < 960 ?  $('.col.eight').width() : 960,
				slide_height = $('.col.eight').width() < 740 ? $('.col.eight').width() / 1.7297297297 : 560; 
		
			if ( $( window ).width() > 450 ) {

				$('body.project .col.twelve.slideshow').css({'height' : slide_height });
			}
			
			$('#slider').anythingSlider({
				startStopped    : true, // If autoPlay is on, this can force it to start stopped
				width           : slide_width,  // Override the default CSS width
				height			: slide_height,
				forwardText : "&rarr;",
				backText : "&larr;",
				resizeContents : false,
				expand: true,
				buildNavigation : true,
				toggleControls  : false, // if true, then slide in controls on hover and slider change, hide @ other times
				onInitialized : function(e, slider) {
					build_slider();
					paginate( slider );
				},
				onSlideComplete : function(slider){
					paginate( slider );
				}
			});

			if ($('.studio_slider'))  {
			
				/* Redo Calc to Reflect Studio Section */
				slide_width = $('.col.eight>div').width() < 960 ?  $('.col.eight>div').width() : 960;
				
				setTimeout( function() {
				
					if ( $(document).width() < 463 ) {
						$('.studio_slider').find('img').css({ 'width' : slide_width }).end()
					}

					$('.studio_slider').each( function() {
					
						$(this).anythingSlider({
							startStopped    : true, // If autoPlay is on, this can force it to start stopped
							forwardText : "&rarr;",
							backText : "&larr;",
							expand: true,
							resizeContents : false,
							buildNavigation : true,
							toggleControls  : false, // if true, then slide in controls on hover and slider change, hide @ other times
							onInitialized : function( e, slider ) {
								build_slider();
								paginate( slider );
								$('.studio_slider_wrapper').css({ 'opacity' : 1 });
							},
							onSlideComplete : function(slider){
								paginate( slider );
							}
						}); // anythingSlider()								
					});	

				
				
				}, 1000);
			
			}
						
			function paginate( slider ) {
				
				if ( slider.$controls.find('span.total_slides').length === 0 )  {
					
					slider.$controls.append('<span class="total_slides"></span>');
				} 
				
				var $span = slider.$controls.find('span.total_slides');
				$span.html( slider.currentPage + '/' + slider.pages);
				
				if ( $span.hasClass('hidden')) {
					$span.fadeIn();
				}				
			} // paginate
	
			// Meta Data
			setTimeout( function() {
			
				var $span = $('span.total_slides'),
					$slider = $span.parents('.anythingSlider');
			
//				$("span.total_slides").html("1/" + $slider.find('li').length).fadeIn();
			}, 500);
			
			function build_slider() {
				if ( 0 === $('div.anythingControls').find('span.total_slides').length ) {
					
					var html = '<span class="total_slides hidden"></span>';
					$('div.anythingControls').append(html);
				}
			}
			
		} // applySliders()
		applySliders();
		
		
		$( function ()	{
		
			var theWindow = $(window),
				$bg = $('#bg'),
				aspectRatio = $bg.width() / $bg.height();
			
			function resizeBg()    {
				if ((theWindow.width() / theWindow.height()) < aspectRatio)    {
					$bg.removeClass()
					   .addClass('bgheight');
				} else    {
					$bg.removeClass()
					   .addClass('bgwidth');
				}				
			}
			
			theWindow.resize(function()	{
				resizeBg();
			}).trigger('resize');
		})
		
		$('ul.sub li a').click(function(e) {
		
			e.preventDefault();
			
			
			var $self, href, $target;
		
				$self = $(this),
				href = $self.attr('href'),
				$target = $(href);
				
			if ($('.col.eight div.active').length || $('li.active-tab').length) {
				$('.col.eight div.active').removeClass('active');
				$('li.active-tab').removeClass('active-tab');
			}
			
			window.location.hash = href.replace('#', '#!/');
			
			$target.addClass('active');
			$self.parents('li').addClass('active-tab');
	
			return false;
		});
		
		
		if (window.location.hash.match('#!/')) {
			
			$('a[href=#' + window.location.hash.substr(3) + ']').trigger('click');
		
		}
		
		$(window).hashchange(function(e) {
		
			e.preventDefault();
			return false;
		});
		
		/* LADEBUG */
		
		if (window.location.hash === "#debug" ) {

			html = '<div class="rule"></div><div class="rule"></div><div class="rule"></div>';
			
			$('html').addClass('debug').find('body').append(html);
		}
		
	}); // End jQuery
