var window;
var body;
var intro;
var main;
var nav;
var wrapper;
var clippings;
var gallery;
var sections;
var icons;
var product;
var testimonials;

$(document).ready(function(){
	
	window			= $(window);
	body			= $('body');
	intro			= $('body.intro')
	main			= $('#main');
	nav 			= $('#nav');
	wrapper			= $('.wrapper');
	clippings 		= $(".clipping");
	gallery			= $('#gallery');
	sections		= $('#section1, #section2, #section3');
	sectionLinks	= $('#galleryNav a');
	productLinks	= $('#productNav a');
	icons			= $('.postMeta .icon');
	product			= $('#product');
	testimonials	= $('#product ul.testimonials');
	
	
	// space out the nav links
	
	if (nav) {
	
		var itemsLength = 0;
	
		items = nav.find('a');
		
		items.each(function(){
		
			itemsLength += $(this).width();
		
		});
	
		spacing = (760 - itemsLength) / (items.length + 1);
	
		nav.find('a').each(function(){
			
			$this = $(this);
			
			$(this).css('margin-left', spacing);
		
		});
	
	}
	
	// create intro anim
	if (intro) {
		
		$('#whiteOut img').animate({ marginTop: 100, opacity: 1 }, { duration: 3000, queue: false });
		setTimeout(function(){ $('#whiteOut').fadeTo(5000, 0.01); }, 3000);
		setTimeout(function(){ $('#whiteOut').remove(); }, 8000);
		
	}
	
	// create product section links
	if (product) {
	
		productLinks.each(function(){
		
			$(this).click(function(event){
			
				event.preventDefault();
				target = $($(this).attr('href'));
				
				$('#product').scrollTo(target, { axis:'x', duration: 1000, queue: false });
				$('#product').animate({ 'height': target.height(), duration: 1000, queue: false });
			
			});
		
		});
	
	}
	
	// create gallery section links

	if (gallery) {
	
		// make them scroll!
	
		sectionLinks.each(function(){
		
			$(this).click(function(event){
				
				event.preventDefault();
				target = $($(this).attr('href'));
				
				$('#gallery').scrollTo(target, { axis:'x', duration: 1000, queue: false });
				$('#gallery').animate({ 'height': target.height(), duration: 1000, queue: false });
			
			});
		
		});
		
		// add rel tags to images
		
		gallery.find('.imagelisting').each(function(){
		
			$this 			= $(this);
			$this.rel		= $this.attr('rel');
			
			$this.find('a').each(function(){
			
				$(this).attr('rel', $this.rel);
			
			});
		
		});
		
		// add fancybox to images
		
		gallery.find('.imagelisting a').fancybox({
		
			'centerOnScroll':		true,
			'hideOnOverlayClick':	true,
			'overlayOpacity':		0.8,
			'overlayColor':			'#333',
			'speedIn':				1000,
			'speedOut':				1000
		
		});
		
		// add fancybox to videos
		
		gallery.find('.video a').fancybox({
		
			'centerOnScroll':		true,
			'hideOnOverlayClick':	false,
			'overlayOpacity':		0.8,
			'overlayColor':			'#333',
			'speedIn':				1000,
			'speedOut':				1000

		
		});
	
	}
	
	// create blog sharing fancy links
	
	if (icons) {
	
		icons.find('a').each(function(){
			
			$(this).click(function(event){
				
					event.preventDefault();
					window.open($(this).attr('href'),'PWillowLinkShare','height=600,width=600');
					if (window.focus) { newwindow.focus() }
					return false;
					
			});
			
		
		});
	
	}
	
	// create testimonial slider
	
	if (testimonials) {
	
		testimonials.each(function(){
			$(this).bxSlider({
		
			auto: 		true,
			controls: 	false,
			mode:		'fade',
			speed:		2000,
			pause:      5000,
			autoHover:	true
		
			});
		});
		
		testimonials.find('.testimonial').each(function(){
		
			height = ( 400 - $(this).height() ) / 2;
			
			$(this).css('margin-top', height );
		
		});
	
	}
	
	// create clipping fancybox links
	
	if (clippings) {
	
		clippings.each(function(){
		
			$this 			= $(this).find('a');
			$this.type		= $this.attr('class');
			
			if ( $this.hasClass('image') ) {
			
				$this.fancybox({
				
					'hideOnContentClick': 	($this.type == 'iframe') ? true : false,
					'width':				1000,
					'height':				600,
					'centerOnScroll':		true,
					'hideOnOverlayClick':	true,
					'overlayOpacity':		0.8,
					'overlayColor':			'#333',
					'speedIn':				1000,
					'speedOut':				1000
				
				});
			
			}
		
		});
	
	}

});

$(window).load(function(){

	// resize images larger than post width
	
	if ( $('.postBody img') ) {
	
		$('.postBody img').each(function(){
		
			width 		= $(this).outerWidth();
			height		= $(this).outerHeight();
			newWidth	= 400;
			newHeight	= (newWidth * height) / width ;
			
			$(this).css({
			
				'display' 			: 'block',
				'margin'			: '0 auto',
				'margin-bottom'		: '20px'
			
			});
			
			if ( width > 400 ) {
				$(this).width(newWidth);
				$(this).height(newHeight);
			}	
		
		});	
	
	}

	/* clippings functions */
	
	if (clippings) {
	
		clippings.each(function(index){
		
			// center images
		
			$this = $(this);
			$img  = $(this).find('img');
		
			$img.css('top', ( ( 175 - $img.height() ) / 2 ) );
		
		});
	
	}
	
	if (product) {
	
		product.height( $(sections[0]).height() );
	
	}
	
	// set gallery height for first section
	
	if (gallery) {
	
		gallery.height( $(sections[0]).height() );
	
	}

});
